package com.gmail.nossr50; import java.io.File; import java.io.IOException; import java.util.ArrayList; import java.util.List; import org.bukkit.entity.Player; import org.bukkit.event.HandlerList; import org.bukkit.metadata.FixedMetadataValue; import org.bukkit.plugin.PluginManager; import org.bukkit.plugin.java.JavaPlugin; import com.gmail.nossr50.config.AdvancedConfig; import com.gmail.nossr50.config.Config; import com.gmail.nossr50.config.HiddenConfig; import com.gmail.nossr50.config.mods.CustomArmorConfig; import com.gmail.nossr50.config.mods.CustomBlockConfig; import com.gmail.nossr50.config.mods.CustomEntityConfig; import com.gmail.nossr50.config.mods.CustomToolConfig; import com.gmail.nossr50.config.treasure.TreasureConfig; import com.gmail.nossr50.database.DatabaseManager; import com.gmail.nossr50.database.DatabaseManagerFactory; import com.gmail.nossr50.listeners.BlockListener; import com.gmail.nossr50.listeners.EntityListener; import com.gmail.nossr50.listeners.InventoryListener; import com.gmail.nossr50.listeners.PlayerListener; import com.gmail.nossr50.listeners.ScoreboardsListener; import com.gmail.nossr50.listeners.SelfListener; import com.gmail.nossr50.listeners.WorldListener; import com.gmail.nossr50.locale.LocaleLoader; import com.gmail.nossr50.metrics.MetricsManager; import com.gmail.nossr50.party.PartyManager; import com.gmail.nossr50.runnables.SaveTimerTask; import com.gmail.nossr50.runnables.backups.CleanBackupsTask; import com.gmail.nossr50.runnables.database.UserPurgeTask; import com.gmail.nossr50.runnables.party.PartyAutoKickTask; import com.gmail.nossr50.runnables.player.PowerLevelUpdatingTask; import com.gmail.nossr50.runnables.skills.BleedTimerTask; import com.gmail.nossr50.skills.child.ChildConfig; import com.gmail.nossr50.skills.repair.config.RepairConfigManager; import com.gmail.nossr50.skills.repair.repairables.Repairable; import com.gmail.nossr50.skills.repair.repairables.RepairableManager; import com.gmail.nossr50.skills.repair.repairables.SimpleRepairableManager; import com.gmail.nossr50.util.ChimaeraWing; import com.gmail.nossr50.util.LogFilter; import com.gmail.nossr50.util.Misc; import com.gmail.nossr50.util.Permissions; import com.gmail.nossr50.util.blockmeta.chunkmeta.ChunkManager; import com.gmail.nossr50.util.blockmeta.chunkmeta.ChunkManagerFactory; import com.gmail.nossr50.util.commands.CommandRegistrationManager; import com.gmail.nossr50.util.experience.FormulaManager; import com.gmail.nossr50.util.player.UserManager; import com.gmail.nossr50.util.scoreboards.ScoreboardManager; import net.gravitydevelopment.updater.mcmmo.Updater; import net.gravitydevelopment.updater.mcmmo.Updater.UpdateResult; import net.gravitydevelopment.updater.mcmmo.Updater.UpdateType; import net.shatteredlands.shatt.backup.ZipLibrary; public class mcMMO extends JavaPlugin { /* Managers */ private static ChunkManager placeStore; private static RepairableManager repairableManager; private static DatabaseManager databaseManager; private static FormulaManager formulaManager; /* File Paths */ private static String mainDirectory; private static String flatFileDirectory; private static String usersFile; private static String modDirectory; public static mcMMO p; // Jar Stuff public static File mcmmo; // Update Check private boolean updateAvailable; /* Plugin Checks */ private static boolean combatTagEnabled; private static boolean healthBarPluginEnabled; private static boolean noCheatPlusPluginEnabled; private static boolean compatNoCheatPlusPluginEnabled; private static boolean mcpcEnabled; // Config Validation Check public boolean noErrorsInConfigFiles = true; // XP Event Check private boolean xpEventEnabled; /* Metadata Values */ public final static String entityMetadataKey = "mcMMO: Spawned Entity"; public final static String blockMetadataKey = "mcMMO: Piston Tracking"; public final static String furnaceMetadataKey = "mcMMO: Tracked Furnace"; public final static String tntMetadataKey = "mcMMO: Tracked TNT"; public final static String tntsafeMetadataKey = "mcMMO: Safe TNT"; public final static String customNameKey = "mcMMO: Custom Name"; public final static String customVisibleKey = "mcMMO: Name Visibility"; public final static String droppedItemKey = "mcMMO: Tracked Item"; public final static String infiniteArrowKey = "mcMMO: Infinite Arrow"; public final static String bowForceKey = "mcMMO: Bow Force"; public final static String arrowDistanceKey = "mcMMO: Arrow Distance"; public final static String customDamageKey = "mcMMO: Custom Damage"; public static FixedMetadataValue metadataValue; /** * Things to be run when the plugin is enabled. */ @Override public void onEnable() { try { p = this; getLogger().setFilter(new LogFilter(this)); metadataValue = new FixedMetadataValue(this, true); mcpcEnabled = getServer().getName().equals("MCPC+"); combatTagEnabled = getServer().getPluginManager().getPlugin("CombatTag") != null; healthBarPluginEnabled = getServer().getPluginManager().getPlugin("HealthBar") != null; noCheatPlusPluginEnabled = getServer().getPluginManager().getPlugin("NoCheatPlus") != null; compatNoCheatPlusPluginEnabled = getServer().getPluginManager().getPlugin("CompatNoCheatPlus") != null; setupFilePaths(); loadConfigFiles(); if (!noErrorsInConfigFiles) { return; } if (mcpcEnabled) { checkModConfigs(); } if (healthBarPluginEnabled) { getLogger().info("HealthBar plugin found, mcMMO's healthbars are automatically disabled."); } if (noCheatPlusPluginEnabled && !compatNoCheatPlusPluginEnabled) { getLogger().warning("NoCheatPlus plugin found, but CompatNoCheatPlus was not found!"); getLogger().warning("mcMMO will not work properly alongside NoCheatPlus without CompatNoCheatPlus"); } databaseManager = DatabaseManagerFactory.getDatabaseManager(); registerEvents(); registerCustomRecipes(); PartyManager.loadParties(); formulaManager = new FormulaManager(); for (Player player : getServer().getOnlinePlayers()) { UserManager.addUser(player); // In case of reload add all users back into UserManager ScoreboardManager.setupPlayer(player); } debug("Version " + getDescription().getVersion() + " is enabled!"); scheduleTasks(); CommandRegistrationManager.registerCommands(); MetricsManager.setup(); placeStore = ChunkManagerFactory.getChunkManager(); // Get our ChunkletManager checkForUpdates(); if (Config.getInstance().getPTPCommandWorldPermissions()) { Permissions.generateWorldTeleportPermissions(); } } catch (Throwable t) { getLogger().severe("There was an error while enabling mcMMO!"); if (!(t instanceof ExceptionInInitializerError)) { t.printStackTrace(); } else { getLogger().info("Please do not replace the mcMMO jar while the server is running."); } getServer().getPluginManager().disablePlugin(this); } } /** * Things to be run when the plugin is disabled. */ @Override public void onDisable() { try { UserManager.saveAll(); // Make sure to save player information if the server shuts down PartyManager.saveParties(); // Save our parties ScoreboardManager.teardownAll(); formulaManager.saveFormula(); placeStore.saveAll(); // Save our metadata placeStore.cleanUp(); // Cleanup empty metadata stores } catch (NullPointerException e) {} getServer().getScheduler().cancelTasks(this); // This removes our tasks HandlerList.unregisterAll(this); // Cancel event registrations if (Config.getInstance().getBackupsEnabled()) { // Remove other tasks BEFORE starting the Backup, or we just cancel it straight away. try { ZipLibrary.mcMMOBackup(); } catch (IOException e) { getLogger().severe(e.toString()); } catch (Throwable e) { if (e instanceof NoClassDefFoundError) { getLogger().severe("Backup class not found!"); getLogger().info("Please do not replace the mcMMO jar while the server is running."); } else { getLogger().severe(e.toString()); } } new CleanBackupsTask().runTaskAsynchronously(mcMMO.p); } debug("Was disabled."); // How informative! } public static String getMainDirectory() { return mainDirectory; } public static String getFlatFileDirectory() { return flatFileDirectory; } public static String getUsersFilePath() { return usersFile; } public static String getModDirectory() { return modDirectory; } public boolean isUpdateAvailable() { return updateAvailable; } public boolean isXPEventEnabled() { return xpEventEnabled; } public void setXPEventEnabled(boolean enabled) { this.xpEventEnabled = enabled; } public void toggleXpEventEnabled() { xpEventEnabled = !xpEventEnabled; } public void debug(String message) { getLogger().info("[Debug] " + message); } public static FormulaManager getFormulaManager() { return formulaManager; } public static ChunkManager getPlaceStore() { return placeStore; } public static RepairableManager getRepairableManager() { return repairableManager; } public static DatabaseManager getDatabaseManager() { return databaseManager; } @Deprecated public static void setDatabaseManager(DatabaseManager databaseManager) { mcMMO.databaseManager = databaseManager; } public static boolean isCombatTagEnabled() { return combatTagEnabled; } public static boolean isHealthBarPluginEnabled() { return healthBarPluginEnabled; } public static boolean isMCPCEnabled() { return mcpcEnabled; } /** * Setup the various storage file paths */ private void setupFilePaths() { mcmmo = getFile(); mainDirectory = getDataFolder().getPath() + File.separator; flatFileDirectory = mainDirectory + "flatfile" + File.separator; usersFile = flatFileDirectory + "mcmmo.users"; modDirectory = mainDirectory + "mods" + File.separator; fixFilePaths(); } private void fixFilePaths() { File oldFlatfilePath = new File(mainDirectory + "FlatFileStuff" + File.separator); File oldModPath = new File(mainDirectory + "ModConfigs" + File.separator); if (oldFlatfilePath.exists()) { oldFlatfilePath.renameTo(new File(flatFileDirectory)); } if (oldModPath.exists()) { oldModPath.renameTo(new File(modDirectory)); } } private void checkForUpdates() { if (!Config.getInstance().getUpdateCheckEnabled()) { return; } Updater updater = new Updater(this, 31030, mcmmo, UpdateType.NO_DOWNLOAD, false); if (updater.getResult() != UpdateResult.UPDATE_AVAILABLE) { this.updateAvailable = false; return; } if (updater.getLatestType().equals("beta") && !Config.getInstance().getPreferBeta()) { this.updateAvailable = false; return; } this.updateAvailable = true; getLogger().info(LocaleLoader.getString("UpdateChecker.Outdated")); getLogger().info(LocaleLoader.getString("UpdateChecker.NewAvailable")); } private void loadConfigFiles() { // Force the loading of config files TreasureConfig.getInstance(); HiddenConfig.getInstance(); AdvancedConfig.getInstance(); new ChildConfig(); List repairables = new ArrayList(); if (Config.getInstance().getToolModsEnabled()) { repairables.addAll(CustomToolConfig.getInstance().getLoadedRepairables()); } if (Config.getInstance().getArmorModsEnabled()) { repairables.addAll(CustomArmorConfig.getInstance().getLoadedRepairables()); } if (Config.getInstance().getBlockModsEnabled()) { CustomBlockConfig.getInstance(); } if (Config.getInstance().getEntityModsEnabled()) { CustomEntityConfig.getInstance(); } // Load repair configs, make manager, and register them at this time RepairConfigManager rManager = new RepairConfigManager(this); repairables.addAll(rManager.getLoadedRepairables()); repairableManager = new SimpleRepairableManager(repairables.size()); repairableManager.registerRepairables(repairables); } private void registerEvents() { PluginManager pluginManager = getServer().getPluginManager(); // Register events pluginManager.registerEvents(new PlayerListener(this), this); pluginManager.registerEvents(new BlockListener(this), this); pluginManager.registerEvents(new EntityListener(this), this); pluginManager.registerEvents(new InventoryListener(this), this); pluginManager.registerEvents(new SelfListener(), this); pluginManager.registerEvents(new ScoreboardsListener(), this); pluginManager.registerEvents(new WorldListener(this), this); } private void registerCustomRecipes() { if (Config.getInstance().getChimaeraEnabled()) { getServer().addRecipe(ChimaeraWing.getChimaeraWingRecipe()); } } private void scheduleTasks() { // Periodic save timer (Saves every 10 minutes by default) long saveIntervalTicks = Config.getInstance().getSaveInterval() * 1200; new SaveTimerTask().runTaskTimer(this, saveIntervalTicks, saveIntervalTicks); // Bleed timer (Runs every two seconds) new BleedTimerTask().runTaskTimer(this, 2 * Misc.TICK_CONVERSION_FACTOR, 2 * Misc.TICK_CONVERSION_FACTOR); // Old & Powerless User remover long purgeIntervalTicks = Config.getInstance().getPurgeInterval() * 60 * 60 * Misc.TICK_CONVERSION_FACTOR; if (purgeIntervalTicks == 0) { new UserPurgeTask().runTaskLater(this, 2 * Misc.TICK_CONVERSION_FACTOR); // Start 2 seconds after startup. } else if (purgeIntervalTicks > 0) { new UserPurgeTask().runTaskTimer(this, purgeIntervalTicks, purgeIntervalTicks); } // Automatically remove old members from parties long kickIntervalTicks = Config.getInstance().getAutoPartyKickInterval() * 60 * 60 * Misc.TICK_CONVERSION_FACTOR; if (kickIntervalTicks == 0) { new PartyAutoKickTask().runTaskLater(this, 2 * Misc.TICK_CONVERSION_FACTOR); // Start 2 seconds after startup. } else if (kickIntervalTicks > 0) { new PartyAutoKickTask().runTaskTimer(this, kickIntervalTicks, kickIntervalTicks); } // Update power level tag scoreboards new PowerLevelUpdatingTask().runTaskTimer(this, 2 * Misc.TICK_CONVERSION_FACTOR, 2 * Misc.TICK_CONVERSION_FACTOR); } private void checkModConfigs() { if (!Config.getInstance().getToolModsEnabled()) { getLogger().info("MCPC+ implementation found, but the custom tool config for mcMMO is disabled!"); getLogger().info("To enable, set Mods.Tool_Mods_Enabled to TRUE in config.yml."); } if (!Config.getInstance().getArmorModsEnabled()) { getLogger().info("MCPC+ implementation found, but the custom armor config for mcMMO is disabled!"); getLogger().info("To enable, set Mods.Armor_Mods_Enabled to TRUE in config.yml."); } if (!Config.getInstance().getBlockModsEnabled()) { getLogger().info("MCPC+ implementation found, but the custom block config for mcMMO is disabled!"); getLogger().info("To enable, set Mods.Block_Mods_Enabled to TRUE in config.yml."); } if (!Config.getInstance().getEntityModsEnabled()) { getLogger().info("MCPC+ implementation found, but the custom entity config for mcMMO is disabled!"); getLogger().info("To enable, set Mods.Entity_Mods_Enabled to TRUE in config.yml."); } } }