Java tutorial
package com.titankingdoms.nodinchan.mobjockeys; import java.io.File; import java.io.FileOutputStream; import java.io.InputStreamReader; import java.net.URL; import java.nio.channels.Channels; import java.nio.channels.ReadableByteChannel; import java.util.logging.Level; import java.util.logging.Logger; import org.bukkit.command.Command; import org.bukkit.command.CommandSender; import org.bukkit.plugin.PluginManager; import org.bukkit.plugin.java.JavaPlugin; import org.json.simple.JSONArray; import org.json.simple.JSONObject; import org.json.simple.parser.JSONParser; import com.nodinchan.ncbukkit.NCBL; import com.titankingdoms.nodinchan.mobjockeys.metrics.Metrics; import com.titankingdoms.nodinchan.mobjockeys.commands.JockeyCommand; import com.titankingdoms.nodinchan.mobjockeys.feature.FeatureManager; import com.titankingdoms.nodinchan.mobjockeys.jockey.JockeyManager; /* Copyright (C) 2012 Nodin Chan <nodinchan@live.com> * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program. If not, see <http://www.gnu.org/licenses/>. */ /** * MobJockeys - Main Class * * @author NodinChan * */ public class MobJockeys extends JavaPlugin { private static MobJockeys instance; private String NAME; private static final Logger log = Logger.getLogger("TitanLog"); private JockeyCommand command; private JockeyManager jockeys; private FeatureManager features; /** * Gets an instance of this * * @return MobJockeys instance */ public static MobJockeys getInstance() { return instance; } /** * Gets the FeatureManager * * @return The FeatureManager */ public FeatureManager getFeatureManager() { return features; } /** * Gets the JockeyManager * * @return The JockeyManager */ public JockeyManager getJockeyManager() { return jockeys; } /** * Initialises Metrics * * @return True is Metrics is initialised */ private boolean initMetrics() { log(Level.INFO, "Hooking Metrics"); try { Metrics metrics = new Metrics(this); if (metrics.isOptOut()) return true; return metrics.start(); } catch (Exception e) { return false; } } /** * Sends the message to the log * * @param level Level of the announcement * * @param msg The message to send */ public void log(Level level, String msg) { log.log(level, "[" + NAME + "] " + msg); } @Override public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) { if (cmd.getName().equalsIgnoreCase("jockey")) { if (args.length < 1) { command.help(sender, args); } else { if (args[0].equalsIgnoreCase("help") || args[0].equalsIgnoreCase("?")) command.help(sender, args); else if (args[0].equalsIgnoreCase("info") || args[0].equalsIgnoreCase("i")) command.info(sender, args); else if (args[0].equalsIgnoreCase("reload") || args[0].equalsIgnoreCase("r")) command.reload(sender, args); else if (args[0].equalsIgnoreCase("spawn") || args[0].equalsIgnoreCase("s")) command.spawn(sender, args); } return true; } return false; } /** * Called when the Plugin disables */ @Override public void onDisable() { features.unload(); jockeys.unload(); log(Level.INFO, "is now disabled"); } /** * Called when the Plugin enables */ @Override public void onEnable() { log(Level.INFO, "is now enabling"); if (!initMetrics()) log(Level.WARNING, "Failed to hook into Metrics"); getConfig().options().copyDefaults(true); saveConfig(); command = new JockeyCommand(); features = new FeatureManager(); features.load(); jockeys = new JockeyManager(); jockeys.load(); log(Level.INFO, "is now enabled"); } /** * Called when Plugin loads */ @Override public void onLoad() { instance = this; NAME = "MobJockeys " + instance.toString().split(" ")[1]; if (getConfig().getBoolean("auto-library-update")) updateLib(); } /** * Checks for update of the library */ private void updateLib() { PluginManager pm = getServer().getPluginManager(); NCBL libPlugin = (NCBL) pm.getPlugin("NC-BukkitLib"); File destination = new File(getDataFolder().getParentFile().getParentFile(), "lib"); destination.mkdirs(); File lib = new File(destination, "NC-BukkitLib.jar"); File pluginLib = new File(getDataFolder().getParentFile(), "NC-BukkitLib.jar"); boolean inPlugins = false; boolean download = false; try { URL url = new URL("http://bukget.org/api/plugin/nc-bukkitlib"); JSONObject jsonPlugin = (JSONObject) new JSONParser().parse(new InputStreamReader(url.openStream())); JSONArray versions = (JSONArray) jsonPlugin.get("versions"); if (libPlugin == null) { getLogger().log(Level.INFO, "Missing NC-Bukkit lib"); inPlugins = true; download = true; } else { double currentVer = Double.parseDouble(libPlugin.getDescription().getVersion()); double newVer = currentVer; for (int ver = 0; ver < versions.size(); ver++) { JSONObject version = (JSONObject) versions.get(ver); if (version.get("type").equals("Release")) { newVer = Double .parseDouble(((String) version.get("name")).split(" ")[1].trim().substring(1)); break; } } if (newVer > currentVer) { getLogger().log(Level.INFO, "NC-Bukkit lib outdated"); download = true; } } if (download) { System.out.println("Downloading NC-Bukkit lib..."); String dl_link = ""; for (int ver = 0; ver < versions.size(); ver++) { JSONObject version = (JSONObject) versions.get(ver); if (version.get("type").equals("Release")) { dl_link = (String) version.get("dl_link"); break; } } if (dl_link == null) throw new Exception(); URL link = new URL(dl_link); ReadableByteChannel rbc = Channels.newChannel(link.openStream()); if (inPlugins) { FileOutputStream output = new FileOutputStream(pluginLib); output.getChannel().transferFrom(rbc, 0, 1 << 24); pm.loadPlugin(pluginLib); } else { FileOutputStream output = new FileOutputStream(lib); output.getChannel().transferFrom(rbc, 0, 1 << 24); } getLogger().log(Level.INFO, "Downloaded NC-Bukkit lib"); } } catch (Exception e) { System.out.println("Failed to check for library update"); } } }