Module: Minecraft::Commands
Overview
Contains all the methods for console commands executed by a connected player.
Constant Summary
Constants included from Data
Data::DATA_VALUE_HASH, Data::ITEM_BUCKETS, Data::KITS, Data::TIME, Data::TIME_QUOTES, Data::WOOL_COLOURS
Instance Method Summary collapse
-
#addtimer(user, *args) ⇒ Object
Adds a timer to the requesting users timers, the item and frequency in seconds of the timer should be specified.
-
#board(user, target_user = nil) ⇒ Object
Checks a users points or displays the leaderboard.
-
#cancelvote(user, target_user) ⇒ Object
Cancels a kickvote initiation for a specific user.
-
#dawn ⇒ Object
Changes to dawn.
-
#day ⇒ Object
Changes to day.
-
#dehop(user, target_user) ⇒ Object
De-half-ops the target user.
-
#deltimer(user, *args) ⇒ Object
Deletes a timer from the requesting user.
-
#disco(user) ⇒ Object
Toggles disco.
-
#disturb(user, target_user) ⇒ Object
Removes somebody from the DND list.
-
#dnd(user) ⇒ Object
Stops users from disturbing you.
-
#dusk ⇒ Object
Changes to dusk.
-
#evening ⇒ Object
Changes to evening.
-
#finished(user, *args) ⇒ Object
Removes an item from the todo list.
-
#give(user, *args) ⇒ Object
Give command takes an item name or numeric id and a quantifier.
-
#help(user, command = nil) ⇒ Object
Prints the available commands for the user.
-
#history(user) ⇒ Object
Prints the last three commands executed.
-
#hop(user, target_user) ⇒ Object
Gives half-op privileges to the target user.
-
#kickvote(user, target_user = nil) ⇒ Object
Initiates or votes for a specific user to be kicked, since half-ops and regular connected players cannot kick users they can initiate a vote instead.
-
#kickvotes(user) ⇒ Object
Displays all current kickvote initiations.
-
#kit(user, group) ⇒ Object
Kit command takes a group name and gives the contents of the kit to the target user.
-
#kitlist ⇒ Object
Prints the list of available kits to the connected players.
-
#last(user, history = 1) ⇒ Object
Executes a command from a users history.
-
#list(user) ⇒ Object
Lists the currently connecting players, noting which is the requesting user and which users are ops.
-
#memo(user, target_user, *args) ⇒ Object
Adds a memo for the specified user.
-
#morning ⇒ Object
Changes to morning.
-
#night ⇒ Object
Changes to night.
-
#nom(user) ⇒ Object
Gives a golden apple to the specified user.
-
#om(user, *args) ⇒ Object
Gives multiple golden apples to the specified user.
-
#points(user, target_user, num_points = 1) ⇒ Object
Gives a user a specific amount of points, the quantity is capped depending on the privileges of the user.
-
#printdnd ⇒ Object
Prints the users who do not wish to be disturbed.
-
#printtime ⇒ Object
Prints the current value of the counter (seconds since server initialized).
-
#printtimer(user) ⇒ Object
Prints the requesting users current timers.
-
#property(user, key = nil) ⇒ Object
Outputs the current value of a server property.
-
#roulette(user) ⇒ Object
Kicks a random person, the requesting user has a higher cance of being picked.
-
#rules ⇒ Object
Will print the server rules to all connected players.
-
#s(user, *args) ⇒ Object
Adds a shortcut for the user with a given label.
-
#shortcuts(user) ⇒ Object
Prints the requested users shortcuts.
-
#stop(user) ⇒ Object
Stops all timers for a user.
-
#todo(user, *args) ⇒ Object
Adds or prints the current todo list items.
-
#tp(user, target) ⇒ Object
Teleports the current user to the target user.
-
#tpall(user) ⇒ Object
Teleports all users to the target user.
-
#uptime(user, target_user = nil) ⇒ Object
Checks the current uptime of the current or target user.
-
#validate_kit(group = "") ⇒ Object
Validates a kit group, if the kit cannot be found it executes the !kitlist command.
-
#vote(user) ⇒ Object
Votes for the last initiated kickvote.
-
#warptime(user, time_change = nil) ⇒ Object
Allows a user to specify a periodic (once every ten seconds) time change.
-
#welcome(user, *args) ⇒ Object
Changes or appends to the welcome message.
Instance Method Details
#addtimer(user, *args) ⇒ Object
ops: hop
Adds a timer to the requesting users timers, the item and frequency in seconds of the timer should be specified. If the timer already exists for that item, the frequency is re-assigned. If the frequency is unspecified, it will default to 30.
529 530 531 532 533 534 535 536 |
# File 'lib/minecraft/commands.rb', line 529 def addtimer(user, *args) item, duration = items_arg(30, args) item = resolve_item(item) return @server.puts "say Timer was not added." if item.nil? @timers[user] ||= {} @timers[user][item] = duration say("Timer added for #{user}. Giving item id #{item} every #{duration} seconds.") end |
#board(user, target_user = nil) ⇒ Object
ops: none
Checks a users points or displays the leaderboard.
228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 |
# File 'lib/minecraft/commands.rb', line 228 def board(user, target_user = nil) if target_user.nil? leaderboard = {} @userpoints.each do |u, p| leaderboard[p] ||= [] leaderboard[p] << u end num_to_display = 5 leaderboard.keys.sort.reverse.each do |points| leaderboard[points].each do |u| return unless num_to_display >= 1 @server.puts "say #{u}: #{points}" num_to_display -= 1 end end else if @userpoints.has_key? target_user @server.puts "say #{u}: #{@userpoints[u]}" end end end |
#cancelvote(user, target_user) ⇒ Object
ops: op
Cancels a kickvote initiation for a specific user.
295 296 297 298 299 300 301 302 |
# File 'lib/minecraft/commands.rb', line 295 def cancelvote(user, target_user) if @userkickvotes.has_key? target_user @userkickvotes.delete(target_user) say("#{user} has cancelled the kickvote on #{target_user}.") else say("There is no kickvote against #{target_user} dummy.") end end |
#dawn ⇒ Object
ops: op
Changes to dawn.
85 86 87 |
# File 'lib/minecraft/commands.rb', line 85 def dawn() change_time(:dawn) end |
#day ⇒ Object
ops: op
Changes to day.
103 104 105 |
# File 'lib/minecraft/commands.rb', line 103 def day() change_time(:day) end |
#dehop(user, target_user) ⇒ Object
ops: op
De-half-ops the target user.
349 350 351 352 |
# File 'lib/minecraft/commands.rb', line 349 def dehop(user, target_user) @hops.reject! { |u| u == target_user.downcase } @server.puts "#{target_user} has been de-hoped, thanks #{user}!" end |
#deltimer(user, *args) ⇒ Object
ops: hop
Deletes a timer from the requesting user.
545 546 547 548 549 550 551 552 553 554 |
# File 'lib/minecraft/commands.rb', line 545 def deltimer(user, *args) item = args.join(" ") item = resolve_item(item) if @timers.has_key? user @timers[user].delete item @server.puts "say #{item} timer is deleted." else @server.puts "say #{item} timer did not exist." end end |
#disco(user) ⇒ Object
ops: op
Toggles disco.
140 141 142 143 144 145 146 147 148 149 |
# File 'lib/minecraft/commands.rb', line 140 def disco(user) @disco ||= false if @disco @server.puts "say Disco ends." @disco = false else say("#{user} has requested disco, s/he likely can't actually dance.") @disco = true end end |
#disturb(user, target_user) ⇒ Object
ops: op
Removes somebody from the DND list.
175 176 177 178 |
# File 'lib/minecraft/commands.rb', line 175 def disturb(user, target_user) say("#{target_user} is being disturbed by #{user}!") @userdnd.reject! { |u| u == target_user.downcase } end |
#dnd(user) ⇒ Object
ops: none
Stops users from disturbing you.
157 158 159 160 161 162 163 164 165 166 |
# File 'lib/minecraft/commands.rb', line 157 def dnd(user) user.downcase! if @userdnd.include? user say("#{user} is ready to be disturbed. *cough*") @userdnd.reject! { |u| u == user } else say("#{user} does not wish to be disturbed.") @userdnd << user end end |
#dusk ⇒ Object
ops: op
Changes to dusk.
94 95 96 |
# File 'lib/minecraft/commands.rb', line 94 def dusk() change_time(:dusk) end |
#evening ⇒ Object
ops: op
Changes to evening.
130 131 132 |
# File 'lib/minecraft/commands.rb', line 130 def evening() change_time(:evening) end |
#finished(user, *args) ⇒ Object
ops: none
Removes an item from the todo list.
684 685 686 687 688 689 690 691 692 693 694 |
# File 'lib/minecraft/commands.rb', line 684 def finished(user, *args) item = args.join(" ") if item.to_i.to_s == item index = item.to_i - 1 else index = @todo_items.find_index(item) end return @server.puts "say Item does not exist." if index.nil? or @todo_items[index].nil? @todo_items.slice! index @server.puts "say Hurray!" end |
#give(user, *args) ⇒ Object
ops: hop
all: is putting out.
Give command takes an item name or numeric id and a quantifier. If a quantifier is not specified then the quantity defaults to 1. Items will try to resolved if they are not an exact match.
366 367 368 369 370 371 372 373 374 375 376 377 |
# File 'lib/minecraft/commands.rb', line 366 def give(user, *args) item, quantity = items_arg(1, args) # For coloured wools/dyes. if WOOL_COLOURS.include? item (quantity / 64.0).ceil.times { kit(user, item) } item = 35 else item = resolve_item(item) end construct_give(user, item, quantity) end |
#help(user, command = nil) ⇒ Object
ops: none
Prints the available commands for the user.
625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 |
# File 'lib/minecraft/commands.rb', line 625 def help(user, command = nil) unless command.nil? return @server.puts "say #{command} does not exist." unless @commands.has_key? command.to_sym command_signature(command.to_sym) say(@commands[command.to_sym][:help]) return end commands = @commands.keys.inject([]) { |arr, key| priv = @commands[key][:ops] if is_op? user arr << key elsif is_hop? user priv == :op ? arr : arr << key else priv == :none ? arr << key : arr end }.map { |s| "!" + s.to_s } say(commands.join(", ")) end |
#history(user) ⇒ Object
ops: none
Prints the last three commands executed.
725 726 727 728 729 730 731 732 733 |
# File 'lib/minecraft/commands.rb', line 725 def history(user) user = user.downcase return say("No command history found.") if not @command_history.has_key? user or @command_history[user].length == 0 i = [@command_history[user].length, 3].min * -1 @command_history[user][i, 3].reverse.each_with_index do |command, index| say("#{index + 1}. #{command.join(" ")}") end end |
#hop(user, target_user) ⇒ Object
ops: op
Gives half-op privileges to the target user.
337 338 339 340 |
# File 'lib/minecraft/commands.rb', line 337 def hop(user, target_user) @hops << target_user.downcase unless @hops.include? target_user.downcase @server.puts "#{target_user} is now a hop, thanks #{user}!" end |
#kickvote(user, target_user = nil) ⇒ Object
ops: none
Initiates or votes for a specific user to be kicked, since half-ops and regular connected players cannot kick users they can initiate a vote instead.
succeeds.
261 262 263 264 265 266 267 268 269 270 271 272 273 274 |
# File 'lib/minecraft/commands.rb', line 261 def kickvote(user, target_user = nil) return @server.puts "say No user #{target_user} exists." unless @users.include? target_user return vote(user) if target_user.nil? unless submit_vote(user, target_user) @userkickvotes[target_user] = { :tally => kick_influence(user), :votes => [user], :start => Time.now } @last_kick_vote = target_user say("A kickvote has been initiated for #{target_user}.") say("To vote enter !kickvote #{target_user}.") end end |
#kickvotes(user) ⇒ Object
ops: op
Displays all current kickvote initiations.
310 311 312 313 314 |
# File 'lib/minecraft/commands.rb', line 310 def kickvotes(user) @userkickvotes.each do |target_user, data| say("#{target_user}: #{data[:tally]} #{data[:votes]}") end end |
#kit(user, group) ⇒ Object
ops: hop
all: is providing kits to all.
Kit command takes a group name and gives the contents of the kit to the target user.
388 389 390 391 392 393 394 395 396 |
# File 'lib/minecraft/commands.rb', line 388 def kit(user, group) KITS[group.to_sym].each do |item| if item.is_a? Array @server.puts construct_give(user, item.first, item.last) else @server.puts "give #{user} #{item} 1" end end end |
#kitlist ⇒ Object
ops: none
Prints the list of available kits to the connected players.
651 652 653 |
# File 'lib/minecraft/commands.rb', line 651 def kitlist() say("Kits: #{KITS.keys.join(", ")}") end |
#last(user, history = 1) ⇒ Object
ops: none
Executes a command from a users history.
704 705 706 707 708 709 710 711 712 713 714 715 716 717 |
# File 'lib/minecraft/commands.rb', line 704 def last(user, history = 1) user, history = user.downcase, history.to_i if not @command_history.has_key? user or @command_history[user].length < history return say("No command found.") end command = @command_history[user][-history] t = @command_history[user].length call_command(user, command.first, *command[1..-1]) # process_history_addition() will not add the same command twice in a # row, so only slice if the command history length has changed. @command_history[user].slice! -1 unless @command_history[user].length == t end |
#list(user) ⇒ Object
ops: none
Lists the currently connecting players, noting which is the requesting user and which users are ops.
503 504 505 506 507 508 509 510 511 512 513 514 515 516 |
# File 'lib/minecraft/commands.rb', line 503 def list(user) l = @users.inject("") do |s, u| pre, suf = "", "" if u == user pre = "[" suf = "]" end pre = pre + "@" if is_op? u pre = pre + "%" if is_hop? u s + "#{", " unless s.empty?}#{pre}#{u}#{suf}" end say(l) end |
#memo(user, target_user, *args) ⇒ Object
ops: none
Adds a memo for the specified user.
39 40 41 42 43 44 45 46 47 48 |
# File 'lib/minecraft/commands.rb', line 39 def memo(user, target_user, *args) target_user = target_user.downcase if @memos.has_key? target_user and @memos[target_user].length == 5 return @server.puts "say #{target_user} has too many memos already!" end @memos[target_user] ||= [] @memos[target_user] << [user, args.join(" ")] say "Memo for #{target_user} added. Will be printed next time s/he logs in." end |
#morning ⇒ Object
ops: op
Changes to morning.
121 122 123 |
# File 'lib/minecraft/commands.rb', line 121 def morning() change_time(:morning) end |
#night ⇒ Object
ops: op
Changes to night.
112 113 114 |
# File 'lib/minecraft/commands.rb', line 112 def night() change_time(:night) end |
#nom(user) ⇒ Object
ops: hop
all: is providing noms to all.
Gives a golden apple to the specified user.
427 428 429 |
# File 'lib/minecraft/commands.rb', line 427 def nom(user) @server.puts "give #{user} 322 1" end |
#om(user, *args) ⇒ Object
ops: hop
all: is noming everybody, gross.
Gives multiple golden apples to the specified user.
439 440 441 |
# File 'lib/minecraft/commands.rb', line 439 def om(user, *args) args.length.times { nom(user) } end |
#points(user, target_user, num_points = 1) ⇒ Object
ops: none
Gives a user a specific amount of points, the quantity is capped depending on the privileges of the user.
200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 |
# File 'lib/minecraft/commands.rb', line 200 def points(user, target_user, num_points = 1) target_user = target_user.downcase num_points = num_points.to_i if user.downcase == target_user say("Did you just try to give yourself points? Sure, minus twenty.") @userpoints[target_user] ||= 0 @userpoints[target_user] -= 20 return elsif num_points < 0 @server.puts "say Subtracting points? For shame." @userpoints[user] ||= 0 @userpoints[user] -= num_points return end num_points = [num_points, cap_points(user)].min @userpoints[target_user] ||= 0 @userpoints[target_user] += num_points say("#{user} has given #{target_user} #{num_points} points for a total of #{@userpoints[target_user]}.") end |
#printdnd ⇒ Object
ops: op
Prints the users who do not wish to be disturbed.
186 187 188 |
# File 'lib/minecraft/commands.rb', line 186 def printdnd() @server.puts "say #{@userdnd.join(", ")}" end |
#printtime ⇒ Object
ops: op
Prints the current value of the counter (seconds since server initialized).
578 579 580 |
# File 'lib/minecraft/commands.rb', line 578 def printtime() @server.puts "say Timer is at #{@counter}." end |
#printtimer(user) ⇒ Object
ops: hop
Prints the requesting users current timers.
562 563 564 565 566 567 568 569 570 |
# File 'lib/minecraft/commands.rb', line 562 def printtimer(user) unless @timers.has_key? user || @timers[user].length == 0 @server.puts "say No timers have been added for #{user}." return end @timers[user].each do |item, frequency| @server.puts "say #{item} every #{frequency} seconds." end end |
#property(user, key = nil) ⇒ Object
ops: op
Outputs the current value of a server property.
451 452 453 454 455 456 457 |
# File 'lib/minecraft/commands.rb', line 451 def property(user, key = nil) if key.nil? say(@server_properties.keys.join(", ")) else say ("#{key} is currently #{@server_properties[key]}") if @server_properties.include? key end end |
#roulette(user) ⇒ Object
ops: op
Kicks a random person, the requesting user has a higher cance of being picked.
323 324 325 326 327 328 |
# File 'lib/minecraft/commands.rb', line 323 def roulette(user) users = @users + [user] * 3 picked_user = users.sample say("#{user} has requested a roulette kick, s/he has a higher chance of being kicked.") @server.puts "kick #{picked_user}" end |
#rules ⇒ Object
ops: none
Will print the server rules to all connected players.
492 493 494 |
# File 'lib/minecraft/commands.rb', line 492 def rules() say(@rules) end |
#s(user, *args) ⇒ Object
ops: hop
Adds a shortcut for the user with a given label. Shortcuts can only be given for custom commands. If only a label is given, the shortcut is executed.
592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 |
# File 'lib/minecraft/commands.rb', line 592 def s(user, *args) shortcut_name = args.slice! 0 if args.length == 0 unless @usershortcuts.has_key? user and @usershortcuts[user].has_key? shortcut_name return kit(user, shortcut_name) if KITS.include? shortcut_name.to_sym return say("#{shortcut_name} is not a valid shortcut for #{user}.") end return call_command(user, @usershortcuts[user][shortcut_name].first, *@usershortcuts[user][shortcut_name][1..-1]) if args.length == 0 end command_string = args @usershortcuts[user] ||= {} @usershortcuts[user][shortcut_name] = command_string say("Shortcut labelled #{shortcut_name} for #{user} has been added.") end |
#shortcuts(user) ⇒ Object
ops: hop
Prints the requested users shortcuts.
614 615 616 617 |
# File 'lib/minecraft/commands.rb', line 614 def shortcuts(user) labels = @usershortcuts[user].keys.join(", ") if @usershortcuts.has_key? user say("Shortcuts for #{user}: #{labels}.") end |
#stop(user) ⇒ Object
ops: hop
Stops all timers for a user.
56 57 58 59 |
# File 'lib/minecraft/commands.rb', line 56 def stop(user) @timers.delete user @server.puts "say #{user} has stopped all his/her timers." end |
#todo(user, *args) ⇒ Object
ops: none
Adds or prints the current todo list items.
663 664 665 666 667 668 669 670 671 672 673 674 |
# File 'lib/minecraft/commands.rb', line 663 def todo(user, *args) if args.length == 0 @todo_items.each_with_index do |item, index| say("say #{index + 1}. #{item}") end return end item = args.join(" ") @todo_items << item @server.puts "say Added item." end |
#tp(user, target) ⇒ Object
ops: hop
all: is teleporting all users to their location.
Teleports the current user to the target user.
406 407 408 409 |
# File 'lib/minecraft/commands.rb', line 406 def tp(user, target) return if check_dnd(target) @server.puts "tp #{user} #{target}" end |
#tpall(user) ⇒ Object
Teleports all users to the target user. Overrides !tpall.
416 417 418 |
# File 'lib/minecraft/commands.rb', line 416 def tpall(user) @users.each { |u| tp(u, user) unless @userdnd.include? u.downcase } end |
#uptime(user, target_user = nil) ⇒ Object
ops: none
Checks the current uptime of the current or target user. Prints their connected uptime and their total uptime. If no target user is specified it will check the requesting user.
469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 |
# File 'lib/minecraft/commands.rb', line 469 def uptime(user, target_user = nil) target_user ||= user unless @users.include? target_user if @userlog.has_key? target_user say("#{target_user} has #{format_uptime(@userlog[target_user])} minutes of logged time.") else say("#{target_user} Does not exist.") end return end time_spent = calculate_uptime(target_user) if @userlog.has_key? target_user total = " Out of a total of #{format_uptime(@userlog[target_user] + time_spent)} minutes." end say("#{target_user} has been online for #{format_uptime(time_spent)} minutes.#{total}") end |
#validate_kit(group = "") ⇒ Object
Validates a kit group, if the kit cannot be found it executes the !kitlist command.
737 738 739 740 741 |
# File 'lib/minecraft/commands.rb', line 737 def validate_kit(group = "") return true if KITS.include? group.to_sym @server.puts "say #{group} is not a valid kit." kitlist end |
#vote(user) ⇒ Object
ops: none
Votes for the last initiated kickvote.
282 283 284 285 286 |
# File 'lib/minecraft/commands.rb', line 282 def vote(user) unless submit_vote(user, @last_kick_vote) @server.puts "say No kickvote was initiated, dummy." end end |
#warptime(user, time_change = nil) ⇒ Object
ops: op
Allows a user to specify a periodic (once every ten seconds) time change.
positive) every ten seconds.
16 17 18 19 20 21 22 23 24 25 26 27 28 29 |
# File 'lib/minecraft/commands.rb', line 16 def warptime(user, time_change = nil) if time_change.nil? return @server.puts "say Current rate: #{@time_change} every ten seconds." if @time_change return @server.puts "say No custom rate specified." end time_change = time_change.to_i if time_change < 0 @time_change = [-1000, time_change].max else @time_change = [1000, time_change].min end @server.puts "say New rate: #{@time_change} every ten seconds." end |
#welcome(user, *args) ⇒ Object
ops: op
Changes or appends to the welcome message. Use !welcome + foo to add foo.
69 70 71 72 73 74 75 76 77 78 |
# File 'lib/minecraft/commands.rb', line 69 def welcome(user, *args) if args.first == "+" @welcome_message += " " + args[1..-1].join(" ") @server.puts "say Appended to welcome message." else @welcome_message = args.join(" ") @server.puts "say Changed welcome message." end ("basicxman") end |