Module: Minecraft::Tools

Defined in:
lib/minecraft/tools.rb

Overview

Methods for external manipulation of the current deployment.

Class Method Summary collapse

Class Method Details

.check_jarfileObject

Checks if minecraft_server.jar and calls the download method if not.



7
8
9
# File 'lib/minecraft/tools.rb', line 7

def self.check_jarfile
  download_minecraft unless File.exists? "minecraft_server.jar"
end

.command(opts) ⇒ Object

Generates a command for running the server with default settings including memory.

Parameters:

  • opts (Slop)

    Command line options from Slop, used for memory specification.



28
29
30
# File 'lib/minecraft/tools.rb', line 28

def self.command(opts)
  "java -Xmx#{opts[:max_memory] || "1024M"} -Xms#{opts[:min_memory] || "1024M"} -jar minecraft_server.jar nogui"
end

.config_value(value) ⇒ String, Boolean

Parses a value (or set of) from a configuration file.

to be a boolean configuration flag).

Parameters:

  • An (String)

    array of values.

Returns:

  • (String)

    The string value joined together.

  • (Boolean)

    Will return a boolean true if the value is blank (meant



60
61
62
63
# File 'lib/minecraft/tools.rb', line 60

def self.config_value(value)
  return true if value.nil? or value.length == 0
  return value.join(" ")
end

.download_minecraftObject

Downloads the minecraft server jarfile into the current directory.



12
13
14
15
16
# File 'lib/minecraft/tools.rb', line 12

def self.download_minecraft
  url = get_minecraft_page
  puts "[+] Downloading Minecraft server..."
  `wget http://minecraft.net/#{url} -O minecraft_server.jar -q`
end

.get_configuration_fileHash

Grabs the extension configuration file and parses it.

Returns:

  • (Hash)

    Configuration hash.



47
48
49
50
51
52
# File 'lib/minecraft/tools.rb', line 47

def self.get_configuration_file
  return {} unless File.exists? "minecraft.properties"
  File.readlines("minecraft.properties").map { |l| l.split(" ") }.inject({}) do |hash, (key, *value)|
    hash.merge({ key.to_sym => config_value(value) })
  end
end

.get_minecraft_pageObject

Parses the miencraft.net download page for the current jarfile URL.



19
20
21
22
23
# File 'lib/minecraft/tools.rb', line 19

def self.get_minecraft_page
  page = Net::HTTP.get("www.minecraft.net", "/download.jsp")
  data = page.match /\"([0-9a-zA-Z_\/]*minecraft_server\.jar\?v=[0-9]+)/
  data[1]
end

.toggle_mobsObject

Toggles mobs in server.properties and returns the new state.



33
34
35
36
37
38
39
40
41
42
# File 'lib/minecraft/tools.rb', line 33

def self.toggle_mobs
  return unless File.exists? "server.properties"
  content = File.read("server.properties")
  state = content.match(/spawn\-monsters=(true|false)/)[1]
  new_state = state == "true" ? "false" : "true"
  content.gsub! "spawn-monsters=#{state}", "spawn-monsters=#{new_state}"

  File.open("server.properties", "w") { |f| f.print content }
  return new_state
end