minecraft 0.0.4 → 0.0.5

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,20 +1,9 @@
1
1
  #!/usr/bin/env ruby
2
2
 
3
3
  require "slop"
4
- require "net/http"
5
4
  require "minecraft"
6
5
 
7
- def minecraft_exit
8
- if $opts.tempmobs?
9
- puts "[+] Restoring previous mob state."
10
- Minecraft::Tools.toggle_mobs
11
- end
12
- exit!
13
- end
14
-
15
- trap("SIGINT") { minecraft_exit }
16
-
17
- $opts = Slop.parse :help => true do
6
+ opts = Slop.parse :help => true do
18
7
  banner "Usage: minecraft [options]"
19
8
 
20
9
  on :n, :no_run, "Don't run the Minecraft server instance."
@@ -24,21 +13,8 @@ $opts = Slop.parse :help => true do
24
13
  on :no_auto_save, "Disable auto save."
25
14
  on :m, :tempmobs, "Temporarily toggles mobs."
26
15
  on :r, :rules, "Specify the server rules.", :optional => true
16
+ on :f, :savefreq, "Specify the frequency in seconds to save the server.", :optional => true
17
+ on :w, :welcome_message, "Specfiy a welcome message to connecting users.", :optional => true
27
18
  end
28
19
 
29
- if $opts.update?
30
- Minecraft::Tools.download_minecraft
31
- end
32
-
33
- if $opts.tempmobs?
34
- puts "[+] Temporarily toggling mobs."
35
- Minecraft::Tools.toggle_mobs
36
- end
37
-
38
- unless $opts.no_run?
39
- Minecraft::Tools.check_jarfile
40
- command = Minecraft::Tools.command($opts)
41
- puts "[+] #{command}"
42
- server = Minecraft::Server.new(command, $opts[:rules])
43
- server.sin.puts("save-on") unless $opts.no_auto_save?
44
- end
20
+ runtime = Minecraft::Runtime.new(opts)
@@ -1,8 +1,10 @@
1
+ require "json/pure"
1
2
  require "minecraft/tools"
2
3
  require "minecraft/data"
3
4
  require "minecraft/commands"
4
5
  require "minecraft/extensions"
5
6
  require "minecraft/server"
7
+ require "minecraft/runtime"
6
8
 
7
9
  module Minecraft
8
10
  end
@@ -71,7 +71,7 @@ module Minecraft
71
71
  item = resolve_item(item)
72
72
  @timers[user] ||= {}
73
73
  @timers[user][item] = duration
74
- @server.puts "say Timer added for #{user}. Giving #{item} every #{duration} seconds."
74
+ @server.puts "say Timer added for #{user}. Giving item id #{item} every #{duration} seconds."
75
75
  end
76
76
 
77
77
  def deltimer(user, *args)
@@ -81,9 +81,39 @@ module Minecraft
81
81
  end
82
82
 
83
83
  def printtimer(user)
84
+ unless @timers.has_key? user || @timers[user].length == 0
85
+ @server.puts "say No timers have been added for #{user}."
86
+ return
87
+ end
88
+ @timers[user].each do |item, frequency|
89
+ @server.puts "say #{item} every #{frequency} seconds."
90
+ end
91
+ end
92
+
93
+ def printtime(user)
84
94
  @server.puts "say Timer is at #{@counter}."
85
95
  end
86
96
 
97
+ def s(user, *args)
98
+ return @server.puts "say You need to specify a shortcut silly!" if args.length == 0
99
+
100
+ shortcut_name = args.slice! 0
101
+ if args.length == 0
102
+ @server.puts "say #{shortcut_name} is not a valid shortcut for #{user}." unless @shortcuts.has_key? user and @shortcuts[user].has_key? shortcut_name
103
+ return call_command(user, @shortcuts[user][shortcut_name].first, *@shortcuts[user][shortcut_name][1..-1]) if args.length == 0
104
+ end
105
+
106
+ command_string = args
107
+ @shortcuts[user] ||= {}
108
+ @shortcuts[user][shortcut_name] = command_string
109
+ @server.puts "say Shortcut labelled #{shortcut_name} for #{user} has been added."
110
+ end
111
+
112
+ def shortcuts(user, *args)
113
+ labels = @shortcuts[user].keys.join(", ") if @shortcuts.has_key? user
114
+ @server.puts "say Shortcuts for #{user}: #{labels}."
115
+ end
116
+
87
117
  def help(*args)
88
118
  @server.puts <<-eof
89
119
  say !tp target_user
@@ -135,9 +165,9 @@ say !deltimer item
135
165
 
136
166
  def resolve_key(key)
137
167
  bucket = key[0]
168
+ return no_key(key) unless ITEM_BUCKETS.include? bucket
138
169
  return key if ITEM_BUCKETS[bucket].include? key
139
170
 
140
- puts "Finding #{key} approximate in #{ITEM_BUCKETS[bucket]}"
141
171
  shortest_diff = nil
142
172
  shortest_key = nil
143
173
  ITEM_BUCKETS[bucket].each do |test_key|
@@ -154,10 +184,14 @@ say !deltimer item
154
184
  end
155
185
  end
156
186
 
157
- @server.puts "say No item #{key} found." if shortest_key.nil?
187
+ no_key(key) if shortest_key.nil?
158
188
  return shortest_key
159
189
  end
160
190
 
191
+ def no_key(key)
192
+ @server.puts "say No item #{key} found."
193
+ end
194
+
161
195
  def is_quantifier?(quantity)
162
196
  quantity.index(/[0-9]+[a-z]?/) == 0
163
197
  end
@@ -1,12 +1,13 @@
1
1
  module Minecraft
2
2
  module Data
3
3
  KITS = {
4
- :diamond => [276, 277, 278, 279, 293],
5
- :garmour => [314, 315, 316, 317],
6
- :armour => [310, 311, 312, 313],
7
- :ranged => [261, [262, 640]],
8
- :nether => [261, [262, 320], [348, 128], 278, 276],
9
- :portal => [[49, 10], 259]
4
+ :diamond => [276, 277, 278, 279, 293],
5
+ :goldarmour => [314, 315, 316, 317],
6
+ :armour => [310, 311, 312, 313],
7
+ :ranged => [261, [262, 320]],
8
+ :nether => [261, [262, 320], [89, 128], 278, 276],
9
+ :portal => [[49, 14], 259],
10
+ :redstone => [[331, 256], [356, 64], [69, 64], [77, 64], [70, 64]]
10
11
  }
11
12
 
12
13
  DATA_VALUE_HASH = {
@@ -1,20 +1,23 @@
1
- require "json/pure"
2
-
3
1
  module Minecraft
4
2
  class Extensions
5
3
  include Commands
6
4
 
7
- def initialize(server, rules = "No rules specified.")
5
+ def initialize(server, opts)
8
6
  @ops = File.readlines("ops.txt").map { |s| s.chomp }
9
- @userlog = get_user_log
7
+ @userlog = get_json("user.log")
10
8
  @users = []
11
- @timers = {}
9
+ @timers = get_json("user_timers.json")
10
+ @shortcuts = get_json("user_shortcuts.json")
12
11
  @counter = 0
13
12
  @logon_time = {}
14
13
  @server = server
15
- @rules = rules
16
14
  load_server_properties
17
15
 
16
+ opts = {
17
+ :rules => "No rules specified."
18
+ }.merge(opts)
19
+ opts.each { |k, v| instance_variable_set("@#{k}", v) }
20
+
18
21
  # Command set.
19
22
  @commands = {}
20
23
  add_command(:give, :ops => true, :all => true, :all_message => "is putting out.")
@@ -24,22 +27,34 @@ module Minecraft
24
27
  add_command(:rules, :ops => false, :all => false)
25
28
  add_command(:nom, :ops => true, :all => true, :all_message => "is providing noms to all.")
26
29
  add_command(:list, :ops => false, :all => false)
30
+ add_command(:s, :ops => false, :all => false)
27
31
  add_command(:uptime, :ops => false, :all => false)
28
32
  add_command(:addtimer, :ops => true, :all => false)
29
33
  add_command(:deltimer, :ops => true, :all => false)
30
34
  add_command(:printtimer, :ops => true, :all => false)
35
+ add_command(:printtime, :ops => true, :all => false)
36
+ add_command(:shortcuts, :ops => false, :all => false)
31
37
  add_command(:kitlist, :ops => false, :all => false)
32
38
  add_command(:property, :ops => true, :all => false)
33
39
  end
34
40
 
35
- def get_user_log
36
- if File.exists? "user.log"
37
- JSON.parse(File.read("user.log"))
41
+ def get_json(file)
42
+ if File.exists? file
43
+ JSON.parse(File.read(file))
38
44
  else
39
45
  {}
40
46
  end
41
47
  end
42
48
 
49
+ def save
50
+ save_file :timers
51
+ save_file :shortcuts
52
+ end
53
+
54
+ def save_file(var)
55
+ File.open("user_#{var}.json", "w") { |f| f.print instance_variable_get("@#{var}").to_json }
56
+ end
57
+
43
58
  def write_log
44
59
  File.open("user.log", "w") { |f| f.print @userlog.to_json }
45
60
  end
@@ -83,8 +98,20 @@ module Minecraft
83
98
  puts e.backtrace
84
99
  end
85
100
 
101
+ def check_save
102
+ if @savefreq.nil?
103
+ freq = 30
104
+ elsif @savefreq == 0
105
+ return
106
+ else
107
+ freq = @savefreq.to_i
108
+ end
109
+ @server.puts "save-all" if @counter % freq == 0
110
+ end
111
+
86
112
  def periodic
87
113
  @counter += 1
114
+ check_save
88
115
  @users.each do |user|
89
116
  next unless @timers.has_key? user
90
117
  @timers[user].each do |item, duration|
@@ -143,6 +170,7 @@ module Minecraft
143
170
  return remove_user(user)
144
171
  elsif line.index "logged in"
145
172
  @users << user
173
+ display_welcome_message(user)
146
174
  @logon_time[user] = Time.now
147
175
  return true
148
176
  end
@@ -193,5 +221,9 @@ module Minecraft
193
221
  @server_properties[key] = value
194
222
  end
195
223
  end
224
+
225
+ def display_welcome_message(user)
226
+ @server.puts "say #{@welcome_message.gsub('%', user)}" unless @welcome_message.nil?
227
+ end
196
228
  end
197
229
  end
@@ -0,0 +1,21 @@
1
+ module Minecraft
2
+ class Runtime
3
+ def initialize(opts)
4
+ @opts = opts
5
+ pre_checks
6
+
7
+ Minecraft::Tools.check_jarfile
8
+ unless @opts.no_run?
9
+ command = Minecraft::Tools.command(@opts)
10
+ puts "[+] #{command}"
11
+ server = Minecraft::Server.new(command, @opts)
12
+ server.sin.puts("save-on") unless @opts.no_auto_save?
13
+ end
14
+ end
15
+
16
+ def pre_checks
17
+ Minecraft::Tools.download_minecraft if @opts.update?
18
+ puts "[+] Temporarily toggling mobs. Setting to #{Minecraft::Tools.toggle_mobs}." if @opts.tempmobs?
19
+ end
20
+ end
21
+ end
@@ -4,15 +4,29 @@ module Minecraft
4
4
  class Server
5
5
  attr_accessor :sin
6
6
 
7
- def initialize(command, rules)
8
- @sin, @sout, @serr = Open3.popen3(command)
9
- @extensions = Extensions.new(@sin, rules)
10
- threads = []
11
- threads << Thread.new { loop { @extensions.process(@sout.gets) } }
12
- threads << Thread.new { loop { @extensions.process(@serr.gets) } }
13
- threads << Thread.new { loop { @extensions.periodic; sleep 1 } }
14
- threads << Thread.new { loop { @sin.puts gets } }
15
- threads.each(&:join)
7
+ def initialize(command, opts)
8
+ @sin, @sout, @serr, @thr = Open3.popen3(command)
9
+ @extensions = Extensions.new(@sin, opts)
10
+ @opts = opts
11
+
12
+ trap("SIGINT") { minecraft_exit }
13
+
14
+ @threads = []
15
+ @threads << Thread.new { loop { @extensions.process(@sout.gets) } }
16
+ @threads << Thread.new { loop { @extensions.process(@serr.gets) } }
17
+ @threads << Thread.new { loop { @extensions.periodic; sleep 1 } }
18
+ @threads << Thread.new { loop { @sin.puts $stdin.gets } }
19
+ @thr.value
20
+ exit!
21
+ end
22
+
23
+ def minecraft_exit
24
+ puts "[+] Restoring previous mob state to #{Minecraft::Tools.toggle_mobs}." if @opts.tempmobs?
25
+ puts "\n[+] Saving..."
26
+ @extensions.save
27
+ @threads.each(&:kill)
28
+ @sin.puts("save-all")
29
+ @sin.puts("stop")
16
30
  end
17
31
  end
18
32
  end
@@ -1,3 +1,5 @@
1
+ require "net/http"
2
+
1
3
  module Minecraft
2
4
  module Tools
3
5
  def self.check_jarfile
@@ -27,6 +29,7 @@ module Minecraft
27
29
  content.gsub! "spawn-monsters=#{state}", "spawn-monsters=#{new_state}"
28
30
 
29
31
  File.open("server.properties", "w") { |f| f.print content }
32
+ return new_state
30
33
  end
31
34
  end
32
35
  end
@@ -1,3 +1,3 @@
1
1
  module Minecraft
2
- VERSION = "0.0.4"
2
+ VERSION = "0.0.5"
3
3
  end
metadata CHANGED
@@ -2,7 +2,7 @@
2
2
  name: minecraft
3
3
  version: !ruby/object:Gem::Version
4
4
  prerelease:
5
- version: 0.0.4
5
+ version: 0.0.5
6
6
  platform: ruby
7
7
  authors:
8
8
  - Andrew Horsman
@@ -10,7 +10,7 @@ autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
12
 
13
- date: 2011-08-15 00:00:00 -04:00
13
+ date: 2011-08-18 00:00:00 -04:00
14
14
  default_executable:
15
15
  dependencies:
16
16
  - !ruby/object:Gem::Dependency
@@ -43,6 +43,7 @@ files:
43
43
  - lib/minecraft/commands.rb
44
44
  - lib/minecraft/data.rb
45
45
  - lib/minecraft/extensions.rb
46
+ - lib/minecraft/runtime.rb
46
47
  - lib/minecraft/server.rb
47
48
  - lib/minecraft/tools.rb
48
49
  - lib/minecraft/version.rb