Class: Mclaunch::Commands

Inherits:
Object
  • Object
show all
Defined in:
lib/mclaunch/commands.rb

Overview

Commands to issue on minecraft server

Instance Method Summary collapse

Constructor Details

#initialize(runtime = nil, options = nil, tools = nil) ⇒ Commands



5
6
7
8
9
10
11
# File 'lib/mclaunch/commands.rb', line 5

def initialize(runtime=nil, options=nil, tools=nil)
  @runtime = runtime
  @options = options
  @tools = tools
  @quiet = @options[:quiet]
  @debug = @options[:debug]
end

Instance Method Details

#ban_ip(ip, reason = nil) ⇒ Object

ban ip command



98
99
100
101
102
103
104
# File 'lib/mclaunch/commands.rb', line 98

def ban_ip(ip, reason=nil)
  raise "This is not an IP address!" unless ip =~/^[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}/
  puts "banning \"#{ip}\ from server" unless @quiet
  reason_string = reason if reason.is_a?(String)
  reason_string = @tools.array_to_string(reason) if reason.is_a?(Array)
  @runtime.send_command("ban-ip #{ip} #{reason_string}")
end

#ban_player(player, reason = nil) ⇒ Object

ban player command



90
91
92
93
94
95
# File 'lib/mclaunch/commands.rb', line 90

def ban_player(player, reason=nil)
  puts "banning #{player} from server" unless @quiet
  reason_string = reason if reason.is_a?(String)
  reason_string = @tools.array_to_string(reason) if reason.is_a?(Array)
  @runtime.send_command("ban #{player} #{reason_string}")
end

#change_difficulty(difficulty) ⇒ Object

difficulty command



56
57
58
59
60
61
62
# File 'lib/mclaunch/commands.rb', line 56

def change_difficulty(difficulty)
  raise "\n  Command only available with minecraft-server.jar" unless @options[:server_executable].eql? "minecraft-server.jar"
  raise "\n  False command: #{difficulty}\n\n  Use:\n    0 or peaceful\n    1 or easy\n    2 or normal\n    3 or hard" unless difficulty =~ /^([0-3]|(peaceful|easy|normal|hard))$/
  puts "changing difficulty to \"#{difficulty}\"" unless @quiet
  @runtime.send_command("difficulty #{difficulty.to_i}") if difficulty =~ /^[0-3]$/
  @runtime.send_command("difficulty #{difficulty.to_s}") if difficulty =~ /^(peaceful|easy|normal|hard)i$/
end

#change_gamemode(mode) ⇒ Object

defaultgamemode command



48
49
50
51
52
53
# File 'lib/mclaunch/commands.rb', line 48

def change_gamemode(mode)
  raise "\n  False command: #{mode}\n\n  Use:\n    0 or survival\n    1 or creative\n    2 or adventure" unless mode =~ /^([0-2]|(survival|creative|adventure))$/
  puts "changing gamemode to \"#{mode}\"" unless @quiet
  @runtime.send_command("defaultgamemode #{mode.to_i}") if mode =~ /^[0-2]$/
  @runtime.send_command("defaultgamemode #{mode.to_s}") if mode =~ /^(survival|creative|adventure)$/
end

#deop(player) ⇒ Object

deop command



78
79
80
81
# File 'lib/mclaunch/commands.rb', line 78

def deop(player)
  puts "removing #{player} from #{@options[:server_runtime_directory]}opts.txt" unless @quiet
  @runtime.send_command("deop #{player}") if @tools.is_op?(player)
end

#kick_player(player) ⇒ Object

kick player command



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

def kick_player(player)
  puts "kicking #{player} from server" unless @quiet
  @runtime.send_command("kick #{player}")
end

#op(player) ⇒ Object

op command



65
66
67
68
69
70
71
72
73
74
75
# File 'lib/mclaunch/commands.rb', line 65

def op(player)
  cmd = "op #{player}"
  online_mode = @tools.to_boolean(@runtime.get_server_properties['online-mode'])
  is_player = @tools.is_minecraft_player?(player)
  puts "adding \"#{player}\" to #{@options[:server_runtime_directory]}opts.txt" unless @quiet
  if online_mode
    @runtime.send_command(cmd) if is_player && @tools.is_op?(player)
  else
    @runtime.send_command(cmd)
  end
end

#pardon_ip(ip) ⇒ Object

pardon ip command



113
114
115
116
117
# File 'lib/mclaunch/commands.rb', line 113

def pardon_ip(ip)
  raise "This is not an IP address!" unless ip =~/^[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}/
  puts "removing ban for \"#{ip}\"." unless @quiet
  @runtime.send_command("pardon-ip #{ip}")
end

#pardon_player(player) ⇒ Object

pardon player command



107
108
109
110
# File 'lib/mclaunch/commands.rb', line 107

def pardon_player(player)
  puts "removing ban for \"#{player}\"." unless @quiet
  @runtime.send_command("pardon #{player}")
end

#reloadObject

Reload command



24
25
26
27
# File 'lib/mclaunch/commands.rb', line 24

def reload
  puts "sending reload command to server" unless @quiet
  @runtime.send_command("reload")
end

#save_allObject

save-all command



42
43
44
45
# File 'lib/mclaunch/commands.rb', line 42

def save_all
  puts "sending save-all command to server" unless @quiet
  @runtime.send_command("save-all")
end

#save_offObject

save-off command



36
37
38
39
# File 'lib/mclaunch/commands.rb', line 36

def save_off
  puts "sending save-off command to server" unless @quiet
  @runtime.send_command("save-off")
end

#save_onObject

save-on command



30
31
32
33
# File 'lib/mclaunch/commands.rb', line 30

def save_on
  puts "sending save-on command to server" unless @quiet
  @runtime.send_command("save-on")
end

#set_options(options) ⇒ Object

setter for @options



19
20
21
# File 'lib/mclaunch/commands.rb', line 19

def set_options(options)
  @options = options
end

#set_runtime(runtime) ⇒ Object

setter for @runtime



14
15
16
# File 'lib/mclaunch/commands.rb', line 14

def set_runtime(runtime)
  @runtime = runtime
end

#whitelist(cmd) ⇒ Object

whitelist <on|off|list> command



120
121
122
123
124
125
126
127
# File 'lib/mclaunch/commands.rb', line 120

def whitelist(cmd)
  raise "Available commands are: on, off, list" unless cmd =~ /^(on|off|list)$/
  unless @quiet
    puts "turning whitelist on." if cmd.eql?("on")
    puts "turning whitelist off." if cmd.eql?("off")
  end
  @runtime.send_command("whitelist #{cmd}")
end

#whitelist_add(player) ⇒ Object

whitelist add command



130
131
132
133
134
135
136
137
138
139
140
# File 'lib/mclaunch/commands.rb', line 130

def whitelist_add(player)
  cmd = "whitelist add #{player}"
  online_mode = @tools.to_boolean(@runtime.get_server_properties['online-mode'])
  is_player = @tools.is_minecraft_player?(player)
  puts "adding \"#{player}\" to whitelist" unless @quiet
  if online_mode
    @runtime.send_command(cmd) if is_player
  else
    @runtime.send_command(cmd)
  end
end

#whitelist_remove(player) ⇒ Object

whitelist remove command



143
144
145
146
147
148
149
150
151
152
153
# File 'lib/mclaunch/commands.rb', line 143

def whitelist_remove(player)
  cmd = "whitelist remove #{player}"
  online_mode = @tools.to_boolean(@runtime.get_server_properties['online-mode'])
  is_player = @tools.is_minecraft_player?(player)
  puts "removing \"#{player}\" to whitelist" unless @quiet
  if online_mode
    @runtime.send_command(cmd) if is_player && @tools.is_op?(player)
  else
    @runtime.send_command(cmd)
  end
end