minecraft 0.0.3 → 0.0.4

Sign up to get free protection for your applications and to get access to all the features.
@@ -23,6 +23,7 @@ $opts = Slop.parse :help => true do
23
23
  on :x, :max_memory, "Specify the maximum amount of memory to allocate.", :optional => true
24
24
  on :no_auto_save, "Disable auto save."
25
25
  on :m, :tempmobs, "Temporarily toggles mobs."
26
+ on :r, :rules, "Specify the server rules.", :optional => true
26
27
  end
27
28
 
28
29
  if $opts.update?
@@ -38,6 +39,6 @@ unless $opts.no_run?
38
39
  Minecraft::Tools.check_jarfile
39
40
  command = Minecraft::Tools.command($opts)
40
41
  puts "[+] #{command}"
41
- server = Minecraft::Server.new(command)
42
+ server = Minecraft::Server.new(command, $opts[:rules])
42
43
  server.sin.puts("save-on") unless $opts.no_auto_save?
43
44
  end
@@ -6,7 +6,7 @@ module Minecraft
6
6
  item, quantity = items_arg(1, args)
7
7
  item = resolve_item(item)
8
8
 
9
- quantify(user, item, quantity)
9
+ construct_give(user, item, quantity)
10
10
  end
11
11
 
12
12
  def validate_kit(group = "")
@@ -18,7 +18,7 @@ module Minecraft
18
18
  def kit(user, group)
19
19
  KITS[group.to_sym].each do |item|
20
20
  if item.is_a? Array
21
- @server.puts quantify(user, item.first, item.last)
21
+ @server.puts construct_give(user, item.first, item.last)
22
22
  else
23
23
  @server.puts "give #{user} #{item} 1"
24
24
  end
@@ -37,6 +37,23 @@ module Minecraft
37
37
  @server.puts "give #{user} 322 1"
38
38
  end
39
39
 
40
+ def property(user, key)
41
+ @server.puts "say #{key} is currently #{@server_properties[key]}" if @server_properties.include? key
42
+ end
43
+
44
+ def uptime(user, target_user = nil)
45
+ target_user ||= user
46
+ time_spent = calculate_uptime(target_user)
47
+ if @userlog.has_key? target_user
48
+ total = " Out of a total of #{format_uptime(@userlog[target_user] + time_spent)} minutes."
49
+ end
50
+ @server.puts "say #{target_user} has been online for #{format_uptime(time_spent)} minutes.#{total}"
51
+ end
52
+
53
+ def rules(*args)
54
+ @server.puts "say #{@rules}"
55
+ end
56
+
40
57
  def list(user)
41
58
  l = @users.inject("") do |s, u|
42
59
  if u == user
@@ -83,7 +100,7 @@ say !deltimer item
83
100
  @server.puts "say Kits: #{KITS.keys.join(", ")}"
84
101
  end
85
102
 
86
- def quantify(user, item, quantity)
103
+ def construct_give(user, item, quantity)
87
104
  if quantity <= 64
88
105
  @server.puts "give #{user} #{item} #{quantity}"
89
106
  return
@@ -101,8 +118,8 @@ say !deltimer item
101
118
  second = default
102
119
  first = args.first
103
120
  else
104
- if args.last.to_i.to_s == args.last # Last argument is an integer.
105
- second = args.last.to_i
121
+ if is_quantifier? args.last
122
+ second = quantify(args.last)
106
123
  first = args[0..-2].join(" ")
107
124
  else
108
125
  second = default
@@ -137,7 +154,27 @@ say !deltimer item
137
154
  end
138
155
  end
139
156
 
157
+ @server.puts "say No item #{key} found." if shortest_key.nil?
140
158
  return shortest_key
141
159
  end
160
+
161
+ def is_quantifier?(quantity)
162
+ quantity.index(/[0-9]+[a-z]?/) == 0
163
+ end
164
+
165
+ def quantify(value)
166
+ return value.scan(/[0-9]+[a-z]?/).inject(0) do |total, term|
167
+ quantity, flag = term.match(/([0-9]+)([a-z]?)/)[1..2]
168
+ quantity = quantity.to_i
169
+ return total + quantity if flag.nil?
170
+
171
+ total + case flag
172
+ when 'm' then [2560, quantity * 64].min
173
+ when 'd' then (64.0 / [1, quantity].max).round
174
+ when 's' then [1, 64 - quantity].max
175
+ else quantity
176
+ end
177
+ end
178
+ end
142
179
  end
143
180
  end
@@ -4,7 +4,7 @@ module Minecraft
4
4
  class Extensions
5
5
  include Commands
6
6
 
7
- def initialize(server)
7
+ def initialize(server, rules = "No rules specified.")
8
8
  @ops = File.readlines("ops.txt").map { |s| s.chomp }
9
9
  @userlog = get_user_log
10
10
  @users = []
@@ -12,19 +12,24 @@ module Minecraft
12
12
  @counter = 0
13
13
  @logon_time = {}
14
14
  @server = server
15
+ @rules = rules
16
+ load_server_properties
15
17
 
16
18
  # Command set.
17
19
  @commands = {}
18
- add_command(:give, :ops => true, :all => true, :all_message => "is putting out.")
19
- add_command(:tp, :ops => false, :all => true, :all_message => "is teleporting all users to their location.")
20
- add_command(:kit, :ops => true, :all => true, :all_message => "is providing kits to all.")
21
- add_command(:help, :ops => false, :all => false)
22
- add_command(:nom, :ops => true, :all => true, :all_message => "is providing noms to all.")
23
- add_command(:list, :ops => false, :all => false)
20
+ add_command(:give, :ops => true, :all => true, :all_message => "is putting out.")
21
+ add_command(:tp, :ops => false, :all => true, :all_message => "is teleporting all users to their location.")
22
+ add_command(:kit, :ops => true, :all => true, :all_message => "is providing kits to all.")
23
+ add_command(:help, :ops => false, :all => false)
24
+ add_command(:rules, :ops => false, :all => false)
25
+ add_command(:nom, :ops => true, :all => true, :all_message => "is providing noms to all.")
26
+ add_command(:list, :ops => false, :all => false)
27
+ add_command(:uptime, :ops => false, :all => false)
24
28
  add_command(:addtimer, :ops => true, :all => false)
25
29
  add_command(:deltimer, :ops => true, :all => false)
26
30
  add_command(:printtimer, :ops => true, :all => false)
27
31
  add_command(:kitlist, :ops => false, :all => false)
32
+ add_command(:property, :ops => true, :all => false)
28
33
  end
29
34
 
30
35
  def get_user_log
@@ -101,17 +106,32 @@ module Minecraft
101
106
  end
102
107
 
103
108
  def meta_check(line)
109
+ return true if check_kick_ban(line)
104
110
  return true if check_ops(line)
105
111
  return true if check_join_part(line)
106
112
  end
107
113
 
114
+ def remove_user(user)
115
+ @users.reject! { |u| u.downcase == user.downcase }
116
+ return true
117
+ end
118
+
119
+ def check_kick_ban(line)
120
+ user = line.split(" ").last
121
+ if line.index "Banning"
122
+ return remove_user(user)
123
+ elsif line.index "Kicking"
124
+ return remove_user(user)
125
+ end
126
+ end
127
+
108
128
  def check_ops(line)
109
129
  user = line.split(" ").last
110
130
  if line.index "De-opping"
111
- @ops.reject! { |u| u == user }
131
+ @ops.reject! { |u| u == user.downcase }
112
132
  return true
113
133
  elsif line.index "Opping"
114
- @ops << user
134
+ @ops << user.downcase
115
135
  return true
116
136
  end
117
137
  end
@@ -119,9 +139,8 @@ module Minecraft
119
139
  def check_join_part(line)
120
140
  user = line.split(" ").first
121
141
  if line.index "lost connection"
122
- @users.reject! { |u| u == user }
123
142
  log_time(user)
124
- return true
143
+ return remove_user(user)
125
144
  elsif line.index "logged in"
126
145
  @users << user
127
146
  @logon_time[user] = Time.now
@@ -138,17 +157,23 @@ module Minecraft
138
157
  end
139
158
 
140
159
  def log_time(user)
141
- logoff = Time.now
142
- logon = @logon_time[user]
143
- time_spent = logoff - logon
160
+ time_spent = calculate_uptime(user)
144
161
  @userlog[user] ||= 0
145
162
  @userlog[user] += time_spent
146
- @server.puts "say #{user} spent #{time_spent} seconds the server, totalling to #{@userlog[user]}."
163
+ @server.puts "say #{user} spent #{format_uptime(time_spent)} minutes in the server, totalling to #{format_uptime(@userlog[user])}."
147
164
  write_log
148
165
  end
149
166
 
167
+ def format_uptime(time)
168
+ (time / 60.0).round(2)
169
+ end
170
+
171
+ def calculate_uptime(user)
172
+ Time.now - @logon_time[user]
173
+ end
174
+
150
175
  def is_op?(user)
151
- @ops.include? user
176
+ @ops.include? user.downcase
152
177
  end
153
178
 
154
179
  def validate_ops(user, command)
@@ -159,5 +184,14 @@ module Minecraft
159
184
  def invalid_command(command)
160
185
  @server.puts "say #{command} is invalid."
161
186
  end
187
+
188
+ def load_server_properties
189
+ @server_properties = {}
190
+ File.readlines("server.properties").each do |line|
191
+ next if line[0] == "#"
192
+ key, value = line.split("=")
193
+ @server_properties[key] = value
194
+ end
195
+ end
162
196
  end
163
197
  end
@@ -4,9 +4,9 @@ module Minecraft
4
4
  class Server
5
5
  attr_accessor :sin
6
6
 
7
- def initialize(command)
7
+ def initialize(command, rules)
8
8
  @sin, @sout, @serr = Open3.popen3(command)
9
- @extensions = Extensions.new(@sin)
9
+ @extensions = Extensions.new(@sin, rules)
10
10
  threads = []
11
11
  threads << Thread.new { loop { @extensions.process(@sout.gets) } }
12
12
  threads << Thread.new { loop { @extensions.process(@serr.gets) } }
@@ -1,3 +1,3 @@
1
1
  module Minecraft
2
- VERSION = "0.0.3"
2
+ VERSION = "0.0.4"
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.3
5
+ version: 0.0.4
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-14 00:00:00 -04:00
13
+ date: 2011-08-15 00:00:00 -04:00
14
14
  default_executable:
15
15
  dependencies:
16
16
  - !ruby/object:Gem::Dependency