Class: Bukkit::Server

Inherits:
Object
  • Object
show all
Defined in:
lib/bukkit.rb,
lib/bukkit/start.rb,
lib/bukkit/create.rb,
lib/bukkit/update.rb,
lib/bukkit/download.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name) ⇒ Server

Returns a new instance of Server.



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

def initialize(name)
  @name = name
end

Class Method Details

.download(uri, options = {}) ⇒ Object

Download a file from a URI.



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/bukkit/download.rb', line 7

def self.download(uri, options = {}) # Options: { :filename => "filename.ext" }
  # Get the filename. If it isn't defined, derive it from the URI.
  if options[:filename]
    filename = options[:filename]
  else
    filename = uri.split("\/").last
  end

  # Catch SIGINT if needed.
  trap("SIGINT") {
    puts "\nDownload failed.".red
    FileUtils.rm(filename) if File.exists? filename
    exit
  }

  # Give some friendly output.
  puts "Downloading: ".yellow + filename
  puts "       From: ".yellow + uri
  puts "(This may take a while depending on your internet connection.)".light_yellow

  # Download the file.
  data = RestClient.get(uri)
  File.open(filename, "wb") do |file|
    file.write(data)
  end
  # => filename.ext

  puts filename.light_green + " successfully downloaded.".green
end

.start(options = {}) ⇒ Object

Start the server.



4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
# File 'lib/bukkit/start.rb', line 4

def self.start(options = {})
  if File.exists?("craftbukkit.jar")
    ram = options[:ram]

    puts "Starting your CraftBukkit server...".green
    if ram.nil? or ram == 0
      system 'java -jar craftbukkit.jar'
    else
      puts "Ram: ".blue + ram.to_s.light_blue
      system "java -Xmx#{ram}M -Xms#{ram}M -jar craftbukkit.jar"
    end
  else
    abort "You're not in your server's root directory.".red
  end
end

.update(options = { :build => :rb }) ⇒ Object

Download a new version of bukkit.



4
5
6
# File 'lib/bukkit/update.rb', line 4

def self.update(options = { :build => :rb }) # Options: { :build => :rb/:beta/:dev }
  Bukkit::Server.download("http://dl.bukkit.org/latest-#{options[:build].to_s}/craftbukkit.jar")
end

Instance Method Details

#create(options = {}) ⇒ Object

Create a new server.



5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/bukkit/create.rb', line 5

def create(options = {}) # Options: { :build => :rb/:beta/:dev, :force => false/true }
  @force = options[:force]

  # If the dir already exists, fail gracefully.
  check_for_dir(@name)

  # Let the dark side of the force flow through you...
  if @force == true
    puts "Overwriting: ".light_red + "#{@name}/"
    FileUtils.rm_rf(@name)
  end

  # Create the server directory and cd into it.
  Dir.mkdir(@name)
  puts "     Create: ".green + "#{@name}/"
  Dir.chdir(@name)

  # Fail gracefully on control + C (SIGINT)
  trap("SIGINT") {
    FileUtils.rm_rf(@name)
    puts "#{@name} not created.".red
  }

  # Download build.
  Bukkit::Server.download("http://dl.bukkit.org/latest-#{options[:build].to_s}/craftbukkit.jar")

  # Give some friendly output.
  puts "New server build at: ".blue + "#{Dir.pwd}/".light_blue
end

#update(options = { :build => :rb }) ⇒ Object

Download a new version of bukkit.



9
10
11
# File 'lib/bukkit/update.rb', line 9

def update(options = { :build => :rb }) # Options: { :build => :rb/:beta/:dev }
  Bukkit::Server.download("http://dl.bukkit.org/latest-#{options[:build].to_s}/craftbukkit.jar")
end