Module: Minecraft::Commands

Includes:
Data
Included in:
Extensions
Defined in:
lib/minecraft/commands.rb

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

Instance Method Summary collapse

Instance Method Details

#addtimer(user, *args) ⇒ Object

Note:

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.

Examples:

addtimer("basicxman", "cobblestone")
addtimer("basicxman", "arrow", "10")

Parameters:

  • user (String)

    The requesting user.

  • args

    item, frequency



525
526
527
528
529
530
531
532
# File 'lib/minecraft/commands.rb', line 525

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
  @server.puts "say Timer added for #{user}.  Giving item id #{item} every #{duration} seconds."
end

#board(user, target_user = nil) ⇒ Object

Note:

ops: none

Checks a users points or displays the leaderboard.

Examples:

board("basicxman")
board("basicxman", "mike_n_7")

Parameters:

  • user (String)

    The requesting user.

  • target_user (String) (defaults to: nil)

    The user to check points of.



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

Note:

ops: op

Cancels a kickvote initiation for a specific user.

Examples:

cancelvote("basicxman", "blizzard4U")

Parameters:

  • user (String)

    The requesting user.

  • target_user (String)

    The user which currently has a kickvote.



295
296
297
298
299
300
301
302
# File 'lib/minecraft/commands.rb', line 295

def cancelvote(user, target_user)
  if @kickvotes.has_key? target_user
    @kickvotes.delete(target_user)
    @server.puts "say #{user} has cancelled the kickvote on #{target_user}."
  else
    @server.puts "say There is no kickvote against #{target_user} dummy."
  end
end

#dawnObject

Note:

ops: op

Changes to dawn.

Examples:

dawn()


85
86
87
# File 'lib/minecraft/commands.rb', line 85

def dawn()
  change_time(:dawn)
end

#dayObject

Note:

ops: op

Changes to day.

Examples:

day()


103
104
105
# File 'lib/minecraft/commands.rb', line 103

def day()
  change_time(:day)
end

#dehop(user, target_user) ⇒ Object

Note:

ops: op

De-half-ops the target user.

Examples:

dehop("basicxman", "blizzard4U")

Parameters:

  • user (String)

    The requesting user.

  • target_user (String)

    The target user to be de-hop’ed.



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

Note:

ops: hop

Deletes a timer from the requesting user.

Examples:

deltimer("basicxman", "cobblestone")

Parameters:

  • user (String)

    The requesting user.

  • args

    item



541
542
543
544
545
546
547
548
549
550
# File 'lib/minecraft/commands.rb', line 541

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

Note:

ops: op

Toggles disco.

Examples:

disco("basicxman")

Parameters:

  • user (String)

    The requesting user.



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
    @server.puts "say #{user} has requested disco, s/he likely can't actually dance."
    @disco = true
  end
end

#disturb(user, target_user) ⇒ Object

Note:

ops: op

Removes somebody from the DND list.

Examples:

disturb("basicxman", "mike_n_7")

Parameters:

  • user (String)

    The requesting user.

  • target_user (String)

    The target user.



175
176
177
178
# File 'lib/minecraft/commands.rb', line 175

def disturb(user, target_user)
  @server.puts "say #{target_user} is being disturbed by #{user}!"
  @userdnd.reject! { |u| u == target_user.downcase }
end

#dnd(user) ⇒ Object

Note:

ops: none

Stops users from disturbing you.

Examples:

dnd("basicxman")

Parameters:

  • user (String)

    The requesting user.



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
    @server.puts "say #{user} is ready to be disturbed. *cough*"
    @userdnd.reject! { |u| u == user }
  else
    @server.puts "say #{user} does not wish to be disturbed."
    @userdnd << user
  end
end

#duskObject

Note:

ops: op

Changes to dusk.

Examples:

dusk()


94
95
96
# File 'lib/minecraft/commands.rb', line 94

def dusk()
  change_time(:dusk)
end

#eveningObject

Note:

ops: op

Changes to evening.

Examples:

evening()


130
131
132
# File 'lib/minecraft/commands.rb', line 130

def evening()
  change_time(:evening)
end

#finished(user, *args) ⇒ Object

Note:

ops: none

Removes an item from the todo list.

Examples:

finished("basicxman", "foo")
finished("basicxman", "2")

Parameters:

  • user (String)

    The requesting user.

  • args

    The item.



680
681
682
683
684
685
686
687
688
689
690
# File 'lib/minecraft/commands.rb', line 680

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

Note:

ops: hop

Note:

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.

Examples:

give("basicxman", "cobblestone")
give("basicxman", "cobblestone", "9m")
give("basicxman", "flint", "and", "steel", "1")
give("basicxman", "4")

Parameters:

  • user (String)

    Target user of the command.



366
367
368
369
370
371
# File 'lib/minecraft/commands.rb', line 366

def give(user, *args)
  item, quantity = items_arg(1, args)
  item = resolve_item(item)

  construct_give(user, item, quantity)
end

#help(user, command = nil) ⇒ Object

Note:

ops: none

Prints the available commands for the user.

Examples:

help("basicxman")

Parameters:

  • user (String)

    The requesting user.



621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
# File 'lib/minecraft/commands.rb', line 621

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

#hop(user, target_user) ⇒ Object

Note:

ops: op

Gives half-op privileges to the target user.

Examples:

hop("basicxman", "blizzard4U")

Parameters:

  • user (String)

    The requesting user.

  • target_user (String)

    The target user to be hop’ed.



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

Note:

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.

Examples:

kickvote("basicxman", "blizzard4U")
kickvote("basicxman")

Parameters:

  • user (String)

    The requesting user.

  • target_user (String) (defaults to: nil)

    The target user to be kicked if the vote



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)
    @kickvotes[target_user] = {
      :tally => kick_influence(user),
      :votes => [user],
      :start => Time.now
    }
    @last_kick_vote = target_user
    @server.puts "say A kickvote has been initiated for #{target_user}."
    @server.puts "say To vote enter !kickvote #{target_user}."
  end
end

#kickvotes(user) ⇒ Object

Note:

ops: op

Displays all current kickvote initiations.

Examples:

kickvotes("basicxman")

Parameters:

  • user (String)

    The requesting user.



310
311
312
313
314
# File 'lib/minecraft/commands.rb', line 310

def kickvotes(user)
  @kickvotes.each do |target_user, data|
    @server.puts "say #{target_user}: #{data[:tally]} #{data[:votes].map { |u| u[0] + u[-1] }.join(", ")}"
  end
end

#kit(user, group) ⇒ Object

Note:

ops: hop

Note:

all: is providing kits to all.

Kit command takes a group name and gives the contents of the kit to the target user.

Examples:

give("basicxman", "diamond")

Parameters:

  • user (String)

    Target user of the command.

  • group (String)

    Label of the kit.



382
383
384
385
386
387
388
389
390
# File 'lib/minecraft/commands.rb', line 382

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

#kitlistObject

Note:

ops: none

Prints the list of available kits to the connected players.

Examples:

kitlist()


647
648
649
# File 'lib/minecraft/commands.rb', line 647

def kitlist()
  @server.puts "say Kits: #{KITS.keys.join(", ")}"
end

#list(user) ⇒ Object

Note:

ops: none

Lists the currently connecting players, noting which is the requesting user and which users are ops.

Examples:

list("basicxman")

Parameters:

  • user (String)

    The requesting user.



499
500
501
502
503
504
505
506
507
508
509
510
511
512
# File 'lib/minecraft/commands.rb', line 499

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

  @server.puts "say #{l}"
end

#memo(user, target_user, *args) ⇒ Object

Note:

ops: none

Adds a memo for the specified user.

Examples:

memo("basicxman", "mike_n_7", "Hi!")

Parameters:

  • user (String)

    The requesting user.

  • target_user (String)

    The target user.

  • args

    The memo.



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

#morningObject

Note:

ops: op

Changes to morning.

Examples:

morning()


121
122
123
# File 'lib/minecraft/commands.rb', line 121

def morning()
  change_time(:morning)
end

#nightObject

Note:

ops: op

Changes to night.

Examples:

night()


112
113
114
# File 'lib/minecraft/commands.rb', line 112

def night()
  change_time(:night)
end

#nom(user) ⇒ Object

Note:

ops: hop

Note:

all: is providing noms to all.

Gives a golden apple to the specified user.

Examples:

nom("basicxman")

Parameters:

  • user (String)

    Target user.



421
422
423
# File 'lib/minecraft/commands.rb', line 421

def nom(user)
  @server.puts "give #{user} 322 1"
end

#om(user, *args) ⇒ Object

Note:

ops: hop

Note:

all: is noming everybody, gross.

Gives multiple golden apples to the specified user.

Examples:

om("basicxman", "nom", "nom", "nom")

Parameters:

  • user (String)

    Target user.

  • args

    noms!



433
434
435
# File 'lib/minecraft/commands.rb', line 433

def om(user, *args)
  args.length.times { nom(user) }
end

#points(user, target_user, num_points = 1) ⇒ Object

Note:

ops: none

Gives a user a specific amount of points, the quantity is capped depending on the privileges of the user.

Examples:

points("basicxman", "mike_n_7")
points("basicxman", "mike_n_7", "50")

Parameters:

  • user (String)

    The requesting user.

  • target_user (String)

    The target user to give points to.

  • points (Integer)

    Quantity of points to give.



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
    @server.puts "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
  @server.puts "say #{user} has given #{target_user} #{num_points} points for a total of #{@userpoints[target_user]}."
end

#printdndObject

Prints the users who do not wish to be disturbed.

@note: ops: op

Examples:

printdnd()

Parameters:

  • user (String)

    The requesting user.



186
187
188
# File 'lib/minecraft/commands.rb', line 186

def printdnd()
  @server.puts "say #{@userdnd.join(", ")}"
end

#printtimeObject

Note:

ops: op

Prints the current value of the counter (seconds since server initialized).

Examples:

printtimer()


574
575
576
# File 'lib/minecraft/commands.rb', line 574

def printtime()
  @server.puts "say Timer is at #{@counter}."
end

#printtimer(user) ⇒ Object

Note:

ops: hop

Prints the requesting users current timers.

Examples:

printtimer("basicxman")

Parameters:

  • user (String)

    The requesting user.



558
559
560
561
562
563
564
565
566
# File 'lib/minecraft/commands.rb', line 558

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

Note:

ops: op

Outputs the current value of a server property.

Examples:

property("basicxman", "spawn-monsters")
property("basicxman")

Parameters:

  • user (String)

    The requesting user.

  • key (String) (defaults to: nil)

    The server property requested.



445
446
447
448
449
450
451
452
453
# File 'lib/minecraft/commands.rb', line 445

def property(user, key = nil)
  if key.nil?
    (@server_properties.length / 3.0).ceil.times do |n|
      @server.puts "say #{@server_properties.keys[n * 3, 3].join(", ")}"
    end
  else
    @server.puts "say #{key} is currently #{@server_properties[key]}" if @server_properties.include? key
  end
end

#roulette(user) ⇒ Object

Note:

ops: op

Kicks a random person, the requesting user has a higher cance of being picked.

Examples:

roulette("basicxman")

Parameters:

  • user (String)

    The requesting user.



323
324
325
326
327
328
# File 'lib/minecraft/commands.rb', line 323

def roulette(user)
  users = @users + [user] * 3
  picked_user = users.sample
  @server.puts "say #{user} has requested a roulette kick, s/he has a higher chance of being kicked."
  @server.puts "kick #{picked_user}"
end

#rulesObject

Note:

ops: none

Will print the server rules to all connected players.

Examples:

rules()


488
489
490
# File 'lib/minecraft/commands.rb', line 488

def rules()
  @server.puts "say #{@rules}"
end

#s(user, *args) ⇒ Object

Note:

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.

Examples:

s("basicxman", "cobble", "give", "cobblestone", "64")
s("basicxman", "mike", "tp", "mike_n_7")

Parameters:

  • user (String)

    The requesting user.

  • args

    label, command array



588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
# File 'lib/minecraft/commands.rb', line 588

def s(user, *args)
  shortcut_name = args.slice! 0
  if args.length == 0
    unless @shortcuts.has_key? user and @shortcuts[user].has_key? shortcut_name
      return kit(user, shortcut_name) if KITS.include? shortcut_name.to_sym
      @server.puts "say #{shortcut_name} is not a valid shortcut for #{user}."
    end
    return call_command(user, @shortcuts[user][shortcut_name].first, *@shortcuts[user][shortcut_name][1..-1]) if args.length == 0
  end

  command_string = args
  @shortcuts[user] ||= {}
  @shortcuts[user][shortcut_name] = command_string
  @server.puts "say Shortcut labelled #{shortcut_name} for #{user} has been added."
end

#shortcuts(user) ⇒ Object

Note:

ops: hop

Prints the requested users shortcuts.

Examples:

shortcuts("basicxman")

Parameters:

  • user (String)

    The requesting user.



610
611
612
613
# File 'lib/minecraft/commands.rb', line 610

def shortcuts(user)
  labels = @shortcuts[user].keys.join(", ") if @shortcuts.has_key? user
  @server.puts "say Shortcuts for #{user}: #{labels}."
end

#stop(user) ⇒ Object

Note:

ops: hop

Stops all timers for a user.

Examples:

stop("basicxman")

Parameters:

  • user (String)

    The requesting 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

Note:

ops: none

Adds or prints the current todo list items.

Examples:

todo("basicxman", "foo")
todo("basicxman")

Parameters:

  • user (String)

    The requesting user.

  • args

    The item.



659
660
661
662
663
664
665
666
667
668
669
670
# File 'lib/minecraft/commands.rb', line 659

def todo(user, *args)
  if args.length == 0
    @todo_items.each_with_index do |item, index|
      @server.puts "say #{index + 1}. #{item}"
    end
    return
  end

  item = args.join(" ")
  @todo_items << item
  @server.puts "say Added item."
end

#tp(user, target) ⇒ Object

Note:

ops: hop

Note:

all: is teleporting all users to their location.

Teleports the current user to the target user.

Examples:

tp("basicxman", "mike_n_7")

Parameters:

  • user (String)

    Current user.

  • target (String)

    User to teleport to.



400
401
402
403
# File 'lib/minecraft/commands.rb', line 400

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.

Examples:

tpall("basicxman")

Parameters:

  • user (String)

    Current (target) user.



410
411
412
# File 'lib/minecraft/commands.rb', line 410

def tpall(user)
  @users.each { |u| tp(u, user) unless @userdnd.include? u.downcase }
end

#uptime(user, target_user = nil) ⇒ Object

Note:

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.

Examples:

uptime("basicxman")
uptime("basicxman", "mike_n_7")

Parameters:

  • user (String)

    The requesting user.

  • target_user (String) (defaults to: nil)

    The user to check.



465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
# File 'lib/minecraft/commands.rb', line 465

def uptime(user, target_user = nil)
  target_user ||= user
  unless @users.include? target_user
    if @userlog.has_key? target_user
      @server.puts "say #{target_user} has #{format_uptime(@userlog[target_user])} minutes of logged time."
    else
      @server.puts "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
  @server.puts "say #{target_user} has been online for #{format_uptime(time_spent)} minutes.#{total}"
end

#vote(user) ⇒ Object

Note:

ops: none

Votes for the last initiated kickvote.

Examples:

vote("basicxman")

Parameters:

  • user (String)

    The requesting user.



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

Note:

ops: op

Allows a user to specify a periodic (once every ten seconds) time change.

positive) every ten seconds.

Examples:

warptime("basicxman", "5")
warptime("basicxman")

Parameters:

  • user (String)

    The requesting user.

  • time_change (Integer) (defaults to: nil)

    Amount of time to add (can be negative or



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

Note:

ops: op

Changes or appends to the welcome message. Use !welcome + foo to add foo.

Examples:

welcome("basicxman", "Welcome", "to", "the", "server")
welcome("basicxman", "+", "%")

Parameters:

  • user (String)

    The requesting user.

  • args

    The welcome message.



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
  display_welcome_message("basicxman")
end