Class: Bukkit::Plugin

Inherits:
Object
  • Object
show all
Defined in:
lib/bukkit.rb,
lib/bukkit/install.rb,
lib/bukkit/website.rb,
lib/bukkit/uninstall.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name) ⇒ Plugin

Returns a new instance of Plugin.



25
26
27
28
# File 'lib/bukkit.rb', line 25

def initialize(name)
  @name = name.downcase
  @plugin_api = JSON.parse(open("http://api.bukget.org/3/plugins/bukkit/#{@name}").read)
end

Class Method Details

.uninstall(name) ⇒ Object



3
4
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
34
35
36
37
38
39
40
41
42
# File 'lib/bukkit/uninstall.rb', line 3

def self.uninstall(name)
  # First check if in root dir.
  abort "You're not in a server's root directory!".red unless File.exists? "craftbukkit.jar"
  
  if Dir.exists?("plugins")
    # The plugins dir exists and is not empty.
    Dir.chdir("plugins")

    plugins = Dir.glob("*")
    delete = []

    # Case insensitively checks each file/dir for a match.
    plugins.each do |plugin|
      if /#{name}/i =~ plugin
        # Make the plugin name look the way the plugin dev wanted.
        name = plugin if File.ftype(plugin) == "directory"

        delete << plugin
      end
    end

    # Delete each matching file.
    if delete.empty?
      abort "No plugins found matching '#{name}'".yellow
    else
      delete.each do |file|
        if File.ftype(file) == "directory"
          file = "#{file}/"
        end

        FileUtils.rm_rf file
        puts file.light_red + " deleted".red
      end

      puts "#{name} has been successfully uninstalled.".green
    end
  else
    abort "You have no plugins to uninstall."
  end
end

Instance Method Details

#installObject

Install a new plugin.



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
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/bukkit/install.rb', line 8

def install
  # Get server's download link.
  @download_url = @plugin_api["versions"][0]["download"]
  @filename = @plugin_api["versions"][0]["filename"]

  # Fail... gracefully, if craftbukkit.jar does not exist.
  abort "You're not in a server's root directory!".red unless File.exists? "craftbukkit.jar"

  # Go into plugins and download the plugin.
  Dir.chdir("plugins")
  Bukkit::Server.download(@download_url, :filename => @filename)

  file_ext = File.extname(@filename)

  # Unzip if it's a zip
  case file_ext
  when ".zip"
    # Extract Zip Archive
    Archive::Zip.extract(@filename, @name)
    Dir.chdir(@name)
    jarfiles = Dir.glob("*.jar")
    # Move each jar file outside the folder.
    jarfiles.each do |jar|
      FileUtils.mv(jar, "../")
    end
    puts " Unarchived: ".yellow + @filename

    Dir.chdir("../")
    # Delete the extracted folder.
    FileUtils.rm_rf("#{@name}/")
    # Delete the archive.
    FileUtils.rm_rf(@filename)
  when ".jar"
    nil
  else
    abort "Something weird happened...\nThe file extension is #{file_ext}, not '.zip' or '.jar'."
  end
  puts "  Installed: ".light_green + @name
  puts @name.light_green + " successfully installed!".green
end

#view_websiteObject

Open website in default browser.



12
13
14
15
16
17
18
# File 'lib/bukkit/website.rb', line 12

def view_website
  Launchy.open(website)
  puts "Opening #{@name}'s Website...".light_green
  puts "    Website: ".yellow + website
  sleep 1.5
  return website
end

#websiteObject

Return the plugin’s website URL.



6
7
8
# File 'lib/bukkit/website.rb', line 6

def website
  @website = @plugin_api["website"]
end