Java tutorial
/* * The MIT License (MIT) * * Copyright (c) 2013 Avalon Realms * * Permission is hereby granted, free of charge, to any person obtaining a copy * of this software and associated documentation files (the "Software"), to deal * in the Software without restriction, including without limitation the rights * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell * copies of the Software, and to permit persons to whom the Software is * furnished to do so, subject to the following conditions: * * The above copyright notice and this permission notice shall be included in all * copies or substantial portions of the Software. * * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE * SOFTWARE. */ package net.avalonrealms.plugins.core; import com.google.common.collect.Maps; import net.avalonrealms.plugins.core.listener.WGListener; import net.avalonrealms.plugins.core.tool.ClasspathTools; import net.avalonrealms.plugins.core.tool.SVPTools; import net.avalonrealms.plugins.core.updater.Updater; import net.avalonrealms.plugins.core.util.ErrorUtils; import net.avalonrealms.plugins.core.util.HookUtils; import net.minecraft.util.org.apache.commons.io.FileUtils; import org.bukkit.command.Command; import org.bukkit.command.CommandMap; import org.bukkit.plugin.PluginManager; import org.bukkit.plugin.java.JavaPlugin; import java.io.File; import java.io.IOException; import java.lang.reflect.Field; import java.util.*; import java.util.jar.JarFile; public class Core extends JavaPlugin { private final SortedMap<String, CoreAddon> coreAddons = new TreeMap<String, CoreAddon>(); private ErrorUtils errorUtils; private HookUtils hookUtils; private Updater updater; private CommandMap commandMap; private File downloadFolder, errorFolder, addonFolder, repoFolder; @Override public void onEnable() { // Folders downloadFolder = new File(getDataFolder(), "downloads"); errorFolder = new File(getDataFolder(), "errors"); addonFolder = new File(getDataFolder(), "addons"); repoFolder = new File(getDataFolder(), "repo"); // Make folders TODO check result downloadFolder.mkdirs(); errorFolder.mkdirs(); addonFolder.mkdirs(); repoFolder.mkdirs(); // Utils errorUtils = new ErrorUtils(this); hookUtils = new HookUtils(this); // Updater updater = new Updater(this); // CommandInfo map try { Class<?> craftServer = SVPTools.getCurrentCBClass("CraftServer"); if (craftServer == null) throw new RuntimeException("Computer says no to getting the craft server"); Field f = craftServer.getDeclaredField("commandMap"); f.setAccessible(true); commandMap = (CommandMap) f.get(getServer()); } catch (Exception e) { errorUtils.logError(e); getServer().getPluginManager().disablePlugin(this); return; } // Listeners PluginManager p = getServer().getPluginManager(); if (getHookUtils().isHooked(HookUtils.Hook.WORLD_GUARD)) { p.registerEvents(new WGListener(this, (com.sk89q.worldguard.bukkit.WorldGuardPlugin) getHookUtils() .getHook(HookUtils.Hook.WORLD_GUARD)), this); } else { getLogger().warning("WorldGuard was not found. WorldGuard events will not run."); } // Addons File[] addons = getAddonFolder().listFiles(); if (addons == null) { throw new RuntimeException("Unable to create addon directory"); } for (File file : addons) { if (file.isDirectory() || !file.getName().endsWith(".jar")) { continue; } try { ClasspathTools.addFileToClasspath(getClassLoader(), file); JarFile jar = new JarFile(file); Class<?> main = Class.forName(jar.getManifest().getMainAttributes().getValue("Addon-Package")); CoreAddon addon = (CoreAddon) main.newInstance(); addon.setManifestValues(jar.getManifest()); coreAddons.put(addon.getName(), addon); jar.close(); } catch (Exception e) { errorUtils.logError(e); } } for (CoreAddon addon : Maps.newHashMap(coreAddons).values()) { addon.initialise(this); addon.onEnable(); } getLogger().info("Enabled " + coreAddons.size() + " addons."); } @Override public void onDisable() { // Clear download folder try { FileUtils.cleanDirectory(getDownloadFolder()); } catch (IOException e) { getErrorUtils().logError(e); } for (CoreAddon addon : coreAddons.values()) { addon.onDisable(); } getLogger().info("Disabled " + coreAddons.size() + " addons."); } public ErrorUtils getErrorUtils() { return errorUtils; } public HookUtils getHookUtils() { return hookUtils; } public Updater getUpdater() { return updater; } public Collection<CoreAddon> getAddons() { return Collections.unmodifiableCollection(coreAddons.values()); } public CoreAddon getAddonFromName(String name) { return coreAddons.get(name); } public void disableAddon(CoreAddon addon) { addon.onDisable(); coreAddons.remove(addon.getName()); } public void registerCommand(Command command) { commandMap.register(command.getName(), command); } public File getDownloadFolder() { return downloadFolder; } public File getErrorFolder() { return errorFolder; } public File getAddonFolder() { return addonFolder; } public File getRepoFolder() { return repoFolder; } }