minecraft 0.3.1 → 0.3.2

Sign up to get free protection for your applications and to get access to all the features.
data/CHANGES.md CHANGED
@@ -1,3 +1,17 @@
1
+ 0.3.2 (2011-08-31)
2
+ ------------------
3
+
4
+ * Added dye kits with give support..
5
+ * Added a tool kit.
6
+ * Implemented command history.
7
+
8
+ 0.3.1 (2011-08-30)
9
+ ------------------
10
+
11
+ * Misc bug fixes.
12
+ * Revised README.
13
+ * Removed two time quotes.
14
+
1
15
  0.3.0 (2011-08-29)
2
16
  ------------------
3
17
 
data/README.md CHANGED
@@ -125,7 +125,7 @@ Read [https://github.com/basicxman/minecraft/wiki/Writing-tests](here) for how
125
125
  we develop code with test driven development.
126
126
 
127
127
  For any non-straightforward functionality (especially command naming and
128
- syntax) you are encouraged to open up an
128
+ syntax) you are encouraged to open up an
129
129
  [http://github.com/basicxman/minecraft/issues](issue) for discussion, issues
130
130
  will be read and commented on promptly. Remember to report any bugs as well,
131
131
  a bug can be something as simple as a command not returning feedback to the
@@ -365,7 +365,13 @@ module Minecraft
365
365
  # @note all: is putting out.
366
366
  def give(user, *args)
367
367
  item, quantity = items_arg(1, args)
368
- item = resolve_item(item)
368
+ # For coloured wools/dyes.
369
+ if WOOL_COLOURS.include? item
370
+ (quantity / 64.0).ceil.times { kit(user, item) }
371
+ item = 35
372
+ else
373
+ item = resolve_item(item)
374
+ end
369
375
 
370
376
  construct_give(user, item, quantity)
371
377
  end
@@ -645,7 +651,7 @@ module Minecraft
645
651
  # kitlist()
646
652
  # @note ops: none
647
653
  def kitlist()
648
- @server.puts "say Kits: #{KITS.keys.join(", ")}"
654
+ say("Kits: #{KITS.keys.join(", ")}")
649
655
  end
650
656
 
651
657
  # Adds or prints the current todo list items.
@@ -659,7 +665,7 @@ module Minecraft
659
665
  def todo(user, *args)
660
666
  if args.length == 0
661
667
  @todo_items.each_with_index do |item, index|
662
- @server.puts "say #{index + 1}. #{item}"
668
+ say("say #{index + 1}. #{item}")
663
669
  end
664
670
  return
665
671
  end
@@ -689,6 +695,45 @@ module Minecraft
689
695
  @server.puts "say Hurray!"
690
696
  end
691
697
 
698
+ # Executes a command from a users history.
699
+ #
700
+ # @param [String] user The requesting user.
701
+ # @param [Integer] history The number of commands to look back.
702
+ # @example
703
+ # last("basicxman")
704
+ # last("basicxman", "2")
705
+ # @note ops: none
706
+ def last(user, history = 1)
707
+ user, history = user.downcase, history.to_i
708
+ if not @command_history.has_key? user or @command_history[user].length < history
709
+ return say("No command found.")
710
+ end
711
+
712
+ command = @command_history[user][-history]
713
+ t = @command_history[user].length
714
+ call_command(user, command.first, *command[1..-1])
715
+
716
+ # process_history_addition() will not add the same command twice in a
717
+ # row, so only slice if the command history length has changed.
718
+ @command_history[user].slice! -1 unless @command_history[user].length == t
719
+ end
720
+
721
+ # Prints the last three commands executed.
722
+ #
723
+ # @param [String] user The requesting user.
724
+ # @example
725
+ # history("basicxman")
726
+ # @note ops: none
727
+ def history(user)
728
+ user = user.downcase
729
+ return say("No command history found.") if not @command_history.has_key? user or @command_history[user].length == 0
730
+
731
+ i = [@command_history[user].length, 3].min * -1
732
+ @command_history[user][i, 3].reverse.each_with_index do |command, index|
733
+ say("#{index + 1}. #{command.join(" ")}")
734
+ end
735
+ end
736
+
692
737
  private
693
738
 
694
739
  # Prints the command signature options for a specified command.
@@ -6,6 +6,7 @@ module Minecraft
6
6
  :diamond => [276, 277, 278, 279, 293],
7
7
  :goldarmour => [314, 315, 316, 317],
8
8
  :armour => [310, 311, 312, 313],
9
+ :tools => [277, 278],
9
10
  :ranged => [261, [262, 320]],
10
11
  :nether => [261, [262, 320], [89, 128], 278, 276],
11
12
  :portal => [[49, 14], 259],
@@ -15,9 +16,41 @@ module Minecraft
15
16
  :blacksmith => [61, [263, 16], [264, 3], [265, 32], [266, 16]],
16
17
  :miner => [278, [50, 64], 277, [65, 64]],
17
18
  :farmer => [293, [295, 128], 277],
18
- :chef => [[297, 3], [296, 192], [320, 3], 357, [353, 64], 354, [350, 2]]
19
+ :chef => [[297, 3], [296, 192], [320, 3], 357, [353, 64], 354, [350, 2]],
20
+ :lightgray => [[351, 64], [352, 64]],
21
+ :gray => [[351, 64], [352, 64]],
22
+ :black => [[351, 64]],
23
+ :red => [[38, 64]],
24
+ :orange => [[38, 64], [37, 64]],
25
+ :yellow => [[37, 64]],
26
+ :lime => [[352, 64], [81, 64]],
27
+ :green => [[81, 64]],
28
+ :lightblue => [[352, 64], [22, 64]],
29
+ :cyan => [[22, 64], [81, 64]],
30
+ :blue => [[22, 64]],
31
+ :purple => [[22, 64], [38, 64]],
32
+ :magenta => [[22, 64], [38, 64], [38, 64], [352, 64]],
33
+ :pink => [[352, 64], [38, 64]]
19
34
  }
20
35
 
36
+ # Possible wool colours.
37
+ WOOL_COLOURS = %w(
38
+ lightgray
39
+ gray
40
+ black
41
+ red
42
+ orange
43
+ yellow
44
+ lime
45
+ green
46
+ lightblue
47
+ cyan
48
+ blue
49
+ purple
50
+ magenta
51
+ pink
52
+ )
53
+
21
54
  # Values for time, 0 to 24000. 0 is dawn, 12000 is dusk.
22
55
  TIME = {
23
56
  :morning => 1000,
@@ -21,6 +21,7 @@ module Minecraft
21
21
  get_json :userdnd, []
22
22
  get_json :memos
23
23
  get_json :todo_items, []
24
+ get_json :command_history
24
25
  @users = []
25
26
  @counter = 0
26
27
  @logon_time = {}
@@ -110,6 +111,7 @@ module Minecraft
110
111
  save_file :userdnd
111
112
  save_file :memos
112
113
  save_file :todo_items
114
+ save_file :command_history
113
115
  end
114
116
 
115
117
  # Save an instance hash to it's associated data file.
@@ -121,6 +123,26 @@ module Minecraft
121
123
  File.open("#{var}.json", "w") { |f| f.print instance_variable_get("@#{var}").to_json }
122
124
  end
123
125
 
126
+ # Process command history addition.
127
+ #
128
+ # @param [String] user The user executing the command.
129
+ # @param [String] command The command to potetentially add to history.
130
+ # @param [Array] args Command arguments.
131
+ # @example
132
+ # process_history_addition("basicxman", "give", "cobblestone")
133
+ # process_history_addition("basicxman", "history")
134
+ def process_history_addition(user, command, args)
135
+ blacklist = %w( last history s )
136
+ return if blacklist.include? command.to_s
137
+
138
+ history = [command] + args
139
+ u = user.downcase
140
+
141
+ @command_history[u] ||= []
142
+ return if @command_history[u].last == history
143
+ @command_history[u] << history
144
+ end
145
+
124
146
  # Complicated method to decide the logic of calling a command. Checks
125
147
  # if the command requires op privileges and whether an `all` version is
126
148
  # available and has been requested.
@@ -138,6 +160,7 @@ module Minecraft
138
160
  # @example
139
161
  # call_command("basicxman", "give", "cobblestone", "64")
140
162
  def call_command(user, command, *args)
163
+ process_history_addition(user, command, args)
141
164
  is_all = command.to_s.end_with? "all"
142
165
  root = command.to_s.chomp("all").to_sym
143
166
  return send(root, user, *args) unless @commands.include? root
@@ -297,13 +320,20 @@ module Minecraft
297
320
  return if meta_check(line)
298
321
 
299
322
  # :foo should use the shortcut 'foo'.
300
- line.gsub!(/^(\<.*?\>\s+):/) { |m| "#{$1}!s " }
323
+ line.gsub!(/^(\<.*?\>\s+),/) { |m| "#{$1}!s " }
301
324
 
302
325
  match_data = line.match /^\<(.*?)\>\s+!(.*?)$/
303
326
  return if match_data.nil?
304
327
 
305
328
  user = match_data[1]
306
329
  args = match_data[2].split(" ")
330
+
331
+ if args.length == 0
332
+ return call_command(user, :last)
333
+ elsif args.first == "!" * args.first.length
334
+ return call_command(user, :last, args.first.length + 1)
335
+ end
336
+
307
337
  call_command(user, args.slice!(0).to_sym, *args)
308
338
  end
309
339
 
@@ -1,5 +1,5 @@
1
1
  # Global Minecraft module.
2
2
  module Minecraft
3
3
  # Yay, keep these versions coming!
4
- VERSION = "0.3.1"
4
+ VERSION = "0.3.2"
5
5
  end
@@ -81,6 +81,15 @@ eof
81
81
  assert_equal result, @server.string
82
82
  end
83
83
 
84
+ sandbox_test "give command should give kits for wools" do
85
+ @ext = Minecraft::Extensions.new(StringIO.new, {})
86
+ @ext.hops = ["basicxman"]
87
+ @ext.call_command("basicxman", "give", "lightgray", "2m")
88
+ t = @ext.server.string.gsub("\n", " ")
89
+ assert_match "give basicxman 35 64", t
90
+ assert_match "give basicxman 351", t
91
+ end
92
+
84
93
  # User points system testing.
85
94
  sandbox_test "should give a user points" do
86
95
  @ext = Minecraft::Extensions.new(StringIO.new, {})
@@ -420,6 +429,59 @@ eof
420
429
  assert_match "not exist", @ext.server.string
421
430
  end
422
431
 
432
+ # Command history.
433
+ sandbox_test "should keep track of command history and allow users to use previous commands" do
434
+ @ext = Minecraft::Extensions.new(StringIO.new, {})
435
+ @ext.ops = ["basicxman"]
436
+ @ext.command_history = {}
437
+ @ext.call_command("basicxman", "dusk")
438
+ @ext.call_command("basicxman", "dawn")
439
+ @ext.server.string = ""
440
+ @ext.call_command("basicxman", "last")
441
+ @ext.call_command("basicxman", "last", "2")
442
+ @ext.info_command("2011-08-30 15:52:55 [INFO] <basicxman> !")
443
+ @ext.info_command("2011-08-30 15:52:55 [INFO] <basicxman> !!")
444
+ t = @ext.server.string.split("\n")
445
+ assert_match "time set 0", t[0]
446
+ assert_match "time set 12000", t[1]
447
+ assert_match "time set 0", t[2]
448
+ assert_match "time set 12000", t[3]
449
+
450
+ @ext.server.string = ""
451
+ @ext.call_command("basicxman", "last", "3")
452
+ assert_match "No command found", @ext.server.string
453
+ end
454
+
455
+ sandbox_test "should print users command history" do
456
+ @ext = Minecraft::Extensions.new(StringIO.new, {})
457
+ @ext.ops = ["basicxman"]
458
+ @ext.call_command("basicxman", "dawn")
459
+ @ext.call_command("basicxman", "dusk")
460
+ @ext.call_command("basicxman", "help", "list")
461
+ @ext.call_command("basicxman", "give", "4", "64")
462
+ @ext.server.string = ""
463
+ @ext.call_command("basicxman", "history")
464
+ t = @ext.server.string.split("\n")
465
+ assert_match "1. give 4 64", t[0]
466
+ assert_match "2. help list", t[1]
467
+ assert_match "3. dusk", t[2]
468
+ end
469
+
470
+ sandbox_test "should not add the same command to history twice in a row" do
471
+ @ext = Minecraft::Extensions.new(StringIO.new, {})
472
+ @ext.ops = ["basicxman"]
473
+ @ext.call_command("basicxman", "day")
474
+ @ext.call_command("basicxman", "day")
475
+ assert_equal [["day"]], @ext.command_history["basicxman"]
476
+ end
477
+
478
+ sandbox_test "should not add command history from shortucts" do
479
+ @ext = Minecraft::Extensions.new(StringIO.new, {})
480
+ @ext.call_command("basicxman", "s", "foo", "day")
481
+ @ext.call_command("basicxman", "s", "foo")
482
+ assert_nil @ext.command_history["basicxman"]
483
+ end
484
+
423
485
  # Remaining commands testing (should test to ensure no errors are thrown in
424
486
  # the command execution).
425
487
  sandbox_test "should run commands without failure" do
data/test/helper.rb CHANGED
@@ -10,7 +10,7 @@ require "turn"
10
10
 
11
11
  module Minecraft
12
12
  class Extensions
13
- attr_accessor :commands, :users, :ops, :hops, :counter, :server, :kickvotes, :last_kick_vote, :uptime, :timers, :shortcuts, :userlog, :userpoints, :vote_threshold, :userdnd, :welcome_message, :memos, :todo_items
13
+ attr_accessor :commands, :users, :ops, :hops, :counter, :server, :kickvotes, :last_kick_vote, :uptime, :timers, :shortcuts, :userlog, :userpoints, :vote_threshold, :userdnd, :welcome_message, :memos, :todo_items, :command_history
14
14
  end
15
15
  end
16
16
 
@@ -28,6 +28,7 @@ class Test < MiniTest::Unit::TestCase
28
28
  FileUtils.cd("mc") do
29
29
  FileUtils.touch("ops.txt")
30
30
  FileUtils.touch("server.properties")
31
+ FileUtils.rm_f("command_history.json")
31
32
  instance_eval(&block)
32
33
  end
33
34
  end
metadata CHANGED
@@ -2,7 +2,7 @@
2
2
  name: minecraft
3
3
  version: !ruby/object:Gem::Version
4
4
  prerelease:
5
- version: 0.3.1
5
+ version: 0.3.2
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-30 00:00:00 -04:00
13
+ date: 2011-08-31 00:00:00 -04:00
14
14
  default_executable:
15
15
  dependencies:
16
16
  - !ruby/object:Gem::Dependency