mcMMO/src/main/java/com/gmail/nossr50/mcMMO.java

501 lines
18 KiB
Java
Raw Normal View History

2012-01-09 20:00:13 +01:00
package com.gmail.nossr50;
2012-06-05 16:13:10 +02:00
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import net.shatteredlands.shatt.backup.ZipLibrary;
import org.bukkit.OfflinePlayer;
2013-01-23 22:34:01 +01:00
import org.bukkit.block.Block;
2012-06-05 16:13:10 +02:00
import org.bukkit.entity.Player;
import org.bukkit.plugin.PluginDescriptionFile;
import org.bukkit.plugin.PluginManager;
import org.bukkit.plugin.java.JavaPlugin;
import org.bukkit.scheduler.BukkitScheduler;
import com.gmail.nossr50.util.blockmeta.chunkmeta.ChunkManager;
import com.gmail.nossr50.util.blockmeta.chunkmeta.ChunkManagerFactory;
import com.gmail.nossr50.commands.CommandRegistrationHelper;
2012-06-05 16:13:10 +02:00
import com.gmail.nossr50.commands.general.AddlevelsCommand;
import com.gmail.nossr50.commands.general.AddxpCommand;
import com.gmail.nossr50.commands.general.InspectCommand;
import com.gmail.nossr50.commands.general.McstatsCommand;
import com.gmail.nossr50.commands.general.MmoeditCommand;
import com.gmail.nossr50.commands.general.SkillResetCommand;
2012-06-05 16:13:10 +02:00
import com.gmail.nossr50.commands.general.XprateCommand;
import com.gmail.nossr50.commands.mc.McabilityCommand;
import com.gmail.nossr50.commands.mc.MccCommand;
import com.gmail.nossr50.commands.mc.McgodCommand;
import com.gmail.nossr50.commands.mc.McmmoCommand;
import com.gmail.nossr50.commands.mc.McrankCommand;
2012-06-05 16:13:10 +02:00
import com.gmail.nossr50.commands.mc.McrefreshCommand;
import com.gmail.nossr50.commands.mc.MctopCommand;
2012-11-21 21:49:54 +01:00
import com.gmail.nossr50.config.AdvancedConfig;
import com.gmail.nossr50.config.Config;
import com.gmail.nossr50.config.HiddenConfig;
2012-05-17 16:26:21 +02:00
import com.gmail.nossr50.config.TreasuresConfig;
2013-01-25 04:53:02 +01:00
import com.gmail.nossr50.database.Database;
import com.gmail.nossr50.database.commands.McpurgeCommand;
import com.gmail.nossr50.database.commands.McremoveCommand;
import com.gmail.nossr50.database.commands.MmoupdateCommand;
import com.gmail.nossr50.database.runnables.UserPurgeTask;
2012-06-05 16:13:10 +02:00
import com.gmail.nossr50.datatypes.PlayerProfile;
import com.gmail.nossr50.listeners.BlockListener;
import com.gmail.nossr50.listeners.EntityListener;
import com.gmail.nossr50.listeners.HardcoreListener;
2013-01-23 22:34:01 +01:00
import com.gmail.nossr50.listeners.InventoryListener;
2012-06-05 16:13:10 +02:00
import com.gmail.nossr50.listeners.PlayerListener;
import com.gmail.nossr50.listeners.WorldListener;
2013-01-30 17:35:33 +01:00
import com.gmail.nossr50.mods.config.CustomArmorConfig;
import com.gmail.nossr50.mods.config.CustomBlocksConfig;
import com.gmail.nossr50.mods.config.CustomToolsConfig;
2012-06-08 23:48:41 +02:00
import com.gmail.nossr50.party.PartyManager;
import com.gmail.nossr50.party.commands.ACommand;
import com.gmail.nossr50.party.commands.PCommand;
import com.gmail.nossr50.party.commands.PartyCommand;
import com.gmail.nossr50.party.commands.PtpCommand;
import com.gmail.nossr50.runnables.MobStoreCleaner;
2012-06-05 16:13:10 +02:00
import com.gmail.nossr50.runnables.SaveTimer;
import com.gmail.nossr50.skills.repair.RepairManager;
import com.gmail.nossr50.skills.repair.RepairManagerFactory;
import com.gmail.nossr50.skills.repair.Repairable;
import com.gmail.nossr50.skills.repair.config.RepairConfigManager;
import com.gmail.nossr50.skills.runnables.BleedTimer;
import com.gmail.nossr50.skills.runnables.SkillMonitor;
import com.gmail.nossr50.spout.SpoutConfig;
import com.gmail.nossr50.spout.SpoutTools;
import com.gmail.nossr50.spout.commands.MchudCommand;
import com.gmail.nossr50.spout.commands.XplockCommand;
import com.gmail.nossr50.util.Anniversary;
2012-04-27 11:47:11 +02:00
import com.gmail.nossr50.util.Leaderboard;
import com.gmail.nossr50.util.Metrics;
import com.gmail.nossr50.util.Metrics.Graph;
2012-04-27 11:47:11 +02:00
import com.gmail.nossr50.util.Users;
public class mcMMO extends JavaPlugin {
2012-04-26 19:42:09 +02:00
private final PlayerListener playerListener = new PlayerListener(this);
private final BlockListener blockListener = new BlockListener(this);
private final EntityListener entityListener = new EntityListener(this);
private final InventoryListener inventoryListener = new InventoryListener(this);
private final WorldListener worldListener = new WorldListener();
private final HardcoreListener hardcoreListener = new HardcoreListener();
2012-03-08 22:17:57 +01:00
2012-06-06 21:38:44 +02:00
private HashMap<Integer, String> tntTracker = new HashMap<Integer, String>();
2013-01-23 22:34:01 +01:00
private HashMap<Block, String> furnaceTracker = new HashMap<Block, String>();
2012-03-08 22:17:57 +01:00
public static mcMMO p;
2012-03-08 22:17:57 +01:00
public static ChunkManager placeStore;
public static RepairManager repairManager;
// Jar Stuff
2012-06-05 17:05:44 +02:00
public static File mcmmo;
// File Paths
2012-07-09 16:55:33 +02:00
private static String mainDirectory;
private static String flatFileDirectory;
private static String usersFile;
private static String modDirectory;
// Spout Check
public static boolean spoutEnabled = false;
// XP Event Check
2013-01-24 20:19:26 +01:00
private boolean xpEventEnabled = false;
2012-03-08 22:17:57 +01:00
/**
* Things to be run when the plugin is enabled.
*/
2012-06-05 15:57:10 +02:00
@Override
2012-03-08 22:17:57 +01:00
public void onEnable() {
2012-04-21 00:09:50 +02:00
p = this;
setupFilePaths();
// Check for Spout
if (getServer().getPluginManager().isPluginEnabled("Spout")) {
spoutEnabled = true;
SpoutConfig.getInstance();
SpoutTools.setupSpoutConfigs();
SpoutTools.registerCustomEvent();
SpoutTools.preCacheFiles();
SpoutTools.reloadSpoutPlayers(); // Handle spout players after a /reload
}
// Force the loading of config files
Config configInstance = Config.getInstance();
2012-05-17 16:26:21 +02:00
TreasuresConfig.getInstance();
HiddenConfig.getInstance();
2012-11-21 21:49:54 +01:00
AdvancedConfig.getInstance();
PartyManager.loadParties();
List<Repairable> repairables = new ArrayList<Repairable>();
if (configInstance.getToolModsEnabled()) {
repairables.addAll(CustomToolsConfig.getInstance().getLoadedRepairables());
}
2012-05-15 22:12:59 +02:00
if (configInstance.getArmorModsEnabled()) {
repairables.addAll(CustomArmorConfig.getInstance().getLoadedRepairables());
2012-05-15 22:12:59 +02:00
}
if (configInstance.getBlockModsEnabled()) {
CustomBlocksConfig.getInstance();
}
// Load repair configs, make manager, and register them at this time
RepairConfigManager rManager = new RepairConfigManager(this);
repairables.addAll(rManager.getLoadedRepairables());
repairManager = RepairManagerFactory.getRepairManager(repairables.size());
repairManager.registerRepairables(repairables);
2012-12-24 22:56:25 +01:00
// Check if Repair Anvil and Salvage Anvil have different itemID's
2013-01-10 05:03:17 +01:00
if (configInstance.getSalvageAnvilId() == configInstance.getRepairAnvilId()) {
getLogger().warning("Can't use the same itemID for Repair/Salvage Anvils!");
}
if (!configInstance.getUseMySQL()) {
Users.loadUsers();
2012-03-08 22:17:57 +01:00
}
2012-07-04 21:38:09 +02:00
PluginManager pluginManager = getServer().getPluginManager();
2012-03-08 22:17:57 +01:00
// Register events
2012-07-04 21:38:09 +02:00
pluginManager.registerEvents(playerListener, this);
pluginManager.registerEvents(blockListener, this);
pluginManager.registerEvents(entityListener, this);
2013-01-23 22:34:01 +01:00
pluginManager.registerEvents(inventoryListener, this);
pluginManager.registerEvents(worldListener, this);
if (configInstance.getHardcoreEnabled()) {
2012-07-04 21:38:09 +02:00
pluginManager.registerEvents(hardcoreListener, this);
2012-05-01 10:43:04 +02:00
}
2012-03-08 22:17:57 +01:00
PluginDescriptionFile pdfFile = getDescription();
2012-03-08 22:17:57 +01:00
// Setup the leader boards
if (configInstance.getUseMySQL()) {
// TODO: Why do we have to check for a connection that hasn't be made yet?
Database.checkConnected();
2013-01-26 23:01:55 +01:00
Database.createStructure();
2012-03-08 22:17:57 +01:00
}
else {
Leaderboard.updateLeaderboards();
2012-03-08 22:17:57 +01:00
}
for (Player player : getServer().getOnlinePlayers()) {
Users.addUser(player); // In case of reload add all users back into PlayerProfile
2012-03-08 22:17:57 +01:00
}
getLogger().info("Version " + pdfFile.getVersion() + " is enabled!");
2012-03-08 22:17:57 +01:00
BukkitScheduler scheduler = getServer().getScheduler();
2012-03-08 22:17:57 +01:00
// Periodic save timer (Saves every 10 minutes by default)
2013-01-26 23:01:55 +01:00
scheduler.scheduleSyncRepeatingTask(this, new SaveTimer(), 0, configInstance.getSaveInterval() * 1200);
// Regen & Cooldown timer (Runs every second)
2013-01-26 23:01:55 +01:00
scheduler.scheduleSyncRepeatingTask(this, new SkillMonitor(), 0, 20);
// Bleed timer (Runs every two seconds)
scheduler.scheduleSyncRepeatingTask(this, new BleedTimer(), 0, 40);
// Old & Powerless User remover
int purgeInterval = Config.getInstance().getPurgeInterval();
if (purgeInterval == 0) {
scheduler.scheduleSyncDelayedTask(this, new UserPurgeTask(), 40); // Start 2 seconds after startup.
}
else if (purgeInterval > 0) {
2013-01-14 06:47:47 +01:00
scheduler.scheduleSyncRepeatingTask(this, new UserPurgeTask(), 0, purgeInterval * 60L * 60L * 20L);
}
2012-03-08 22:17:57 +01:00
registerCommands();
if (configInstance.getStatsTrackingEnabled()) {
try {
2012-05-01 01:32:50 +02:00
Metrics metrics = new Metrics(this);
Graph graph = metrics.createGraph("Percentage of servers using timings");
2012-07-04 21:38:09 +02:00
if (pluginManager.useTimings()) {
graph.addPlotter(new Metrics.Plotter("Enabled") {
@Override
public int getValue() {
return 1;
}
});
2012-05-23 17:16:23 +02:00
}
else {
graph.addPlotter(new Metrics.Plotter("Disabled") {
@Override
public int getValue() {
return 1;
}
});
}
2012-05-01 01:32:50 +02:00
metrics.start();
}
catch (IOException e) {
2012-05-01 01:32:50 +02:00
System.out.println("Failed to submit stats.");
}
2012-03-08 22:17:57 +01:00
}
placeStore = ChunkManagerFactory.getChunkManager(); // Get our ChunkletManager
new MobStoreCleaner(); // Automatically starts and stores itself
Anniversary.createAnniversaryFile(); // Create Anniversary files
2012-03-08 22:17:57 +01:00
}
2012-05-21 14:45:33 +02:00
/**
* Setup the various storage file paths
*/
public void setupFilePaths() {
mcmmo = getFile();
mainDirectory = getDataFolder().getPath() + File.separator;
flatFileDirectory = mainDirectory + "FlatFileStuff" + File.separator;
usersFile = flatFileDirectory + "mcmmo.users";
modDirectory = mainDirectory + "ModConfigs" + File.separator;
}
2012-03-08 22:17:57 +01:00
/**
* Get profile of the player by name.
* </br>
* This function is designed for API usage.
*
* @param playerName Name of player whose profile to get
* @return the PlayerProfile object
*/
2012-06-08 19:13:17 +02:00
public PlayerProfile getPlayerProfile(String playerName) {
return Users.getProfile(playerName);
}
/**
2012-06-08 19:13:17 +02:00
* Get profile of the player.
* </br>
* This function is designed for API usage.
*
2012-06-08 19:13:17 +02:00
* @param player player whose profile to get
* @return the PlayerProfile object
*/
2012-06-08 19:13:17 +02:00
public PlayerProfile getPlayerProfile(OfflinePlayer player) {
return Users.getProfile(player);
}
/**
* Get profile of the player.
* </br>
* This function is designed for API usage.
*
* @param player player whose profile to get
* @return the PlayerProfile object
*/
@Deprecated
public PlayerProfile getPlayerProfile(Player player) {
return Users.getProfile(player);
}
2012-03-08 22:17:57 +01:00
/**
* Things to be run when the plugin is disabled.
*/
2012-06-05 15:57:10 +02:00
@Override
2012-03-08 22:17:57 +01:00
public void onDisable() {
Users.saveAll(); // Make sure to save player information if the server shuts down
2013-01-26 23:01:55 +01:00
PartyManager.saveParties();
getServer().getScheduler().cancelTasks(this); // This removes our tasks
placeStore.saveAll(); // Save our metadata
placeStore.cleanUp(); // Cleanup empty metadata stores
// Remove other tasks BEFORE starting the Backup, or we just cancel it straight away.
2012-05-01 01:32:50 +02:00
try {
ZipLibrary.mcMMObackup();
}
catch (IOException e) {
getLogger().severe(e.toString());
2012-05-01 01:32:50 +02:00
}
2013-01-26 23:01:55 +01:00
Anniversary.saveAnniversaryFiles();
getLogger().info("Was disabled."); //How informative!
2012-03-08 22:17:57 +01:00
}
2012-03-08 22:55:43 +01:00
/**
* Register the commands.
*/
2012-03-08 22:17:57 +01:00
private void registerCommands() {
CommandRegistrationHelper.registerSkillCommands();
Config configInstance = Config.getInstance();
// mc* commands
if (configInstance.getCommandMCPurgeEnabled()) {
2013-01-14 06:47:47 +01:00
getCommand("mcpurge").setExecutor(new McpurgeCommand());
}
if (configInstance.getCommandMCRemoveEnabled()) {
2013-01-26 23:01:55 +01:00
getCommand("mcremove").setExecutor(new McremoveCommand());
2012-03-08 22:55:43 +01:00
}
if (configInstance.getCommandMCAbilityEnabled()) {
2012-03-08 22:55:43 +01:00
getCommand("mcability").setExecutor(new McabilityCommand());
}
if (configInstance.getCommandMCCEnabled()) {
2012-03-08 22:55:43 +01:00
getCommand("mcc").setExecutor(new MccCommand());
}
if (configInstance.getCommandMCGodEnabled()) {
2012-03-08 22:55:43 +01:00
getCommand("mcgod").setExecutor(new McgodCommand());
}
if (configInstance.getCommandmcMMOEnabled()) {
2012-06-05 16:42:40 +02:00
getCommand("mcmmo").setExecutor(new McmmoCommand());
2012-03-08 22:55:43 +01:00
}
if (configInstance.getCommandMCRefreshEnabled()) {
2013-01-26 23:01:55 +01:00
getCommand("mcrefresh").setExecutor(new McrefreshCommand());
2012-03-08 22:55:43 +01:00
}
if (configInstance.getCommandMCTopEnabled()) {
2012-03-08 22:55:43 +01:00
getCommand("mctop").setExecutor(new MctopCommand());
}
if(configInstance.getCommandMCRankEnabled()) {
getCommand("mcrank").setExecutor(new McrankCommand());
}
2012-03-08 22:55:43 +01:00
if (configInstance.getCommandMCStatsEnabled()) {
2012-03-08 22:55:43 +01:00
getCommand("mcstats").setExecutor(new McstatsCommand());
}
2012-07-30 06:02:41 +02:00
if (configInstance.getCommandSkillResetEnabled()) {
getCommand("skillreset").setExecutor(new SkillResetCommand());
}
// Party commands
if (configInstance.getCommandAdminChatAEnabled()) {
2013-01-26 23:01:55 +01:00
getCommand("a").setExecutor(new ACommand());
2012-03-08 22:55:43 +01:00
}
if (configInstance.getCommandPartyEnabled()) {
2013-01-26 23:01:55 +01:00
getCommand("party").setExecutor(new PartyCommand());
2012-03-08 22:55:43 +01:00
}
if (configInstance.getCommandPartyChatPEnabled()) {
2012-04-21 00:09:50 +02:00
getCommand("p").setExecutor(new PCommand(this));
2012-03-08 22:55:43 +01:00
}
if (configInstance.getCommandPTPEnabled()) {
2012-03-08 22:55:43 +01:00
getCommand("ptp").setExecutor(new PtpCommand(this));
}
// Other commands
if (configInstance.getCommandAddXPEnabled()) {
2013-01-26 23:01:55 +01:00
getCommand("addxp").setExecutor(new AddxpCommand());
2012-03-08 22:55:43 +01:00
}
if (configInstance.getCommandAddLevelsEnabled()) {
2013-01-26 23:01:55 +01:00
getCommand("addlevels").setExecutor(new AddlevelsCommand());
2012-03-08 22:55:43 +01:00
}
if (configInstance.getCommandMmoeditEnabled()) {
2012-07-07 19:35:32 +02:00
getCommand("mmoedit").setExecutor(new MmoeditCommand());
2012-03-08 22:55:43 +01:00
}
if (configInstance.getCommandInspectEnabled()) {
2012-07-07 19:39:22 +02:00
getCommand("inspect").setExecutor(new InspectCommand());
2012-03-08 22:55:43 +01:00
}
if (configInstance.getCommandXPRateEnabled()) {
2013-01-26 23:01:55 +01:00
getCommand("xprate").setExecutor(new XprateCommand());
2012-03-08 22:55:43 +01:00
}
2013-01-26 23:01:55 +01:00
getCommand("mmoupdate").setExecutor(new MmoupdateCommand());
2012-03-08 22:55:43 +01:00
// Spout commands
if (configInstance.getCommandXPLockEnabled()) {
2012-06-05 16:42:40 +02:00
getCommand("xplock").setExecutor(new XplockCommand());
2012-03-08 22:55:43 +01:00
}
getCommand("mchud").setExecutor(new MchudCommand());
2012-03-08 22:17:57 +01:00
}
2012-06-06 21:38:44 +02:00
/**
* Add a set of values to the TNT tracker.
*
* @param tntID The EntityID of the TNT
* @param playerName The name of the detonating player
*/
public void addToTNTTracker(int tntID, String playerName) {
tntTracker.put(tntID, playerName);
}
/**
* Check to see if a given TNT Entity is tracked.
*
* @param tntID The EntityID of the TNT
* @return true if the TNT is being tracked, false otherwise
*/
public boolean tntIsTracked(int tntID) {
return tntTracker.containsKey(tntID);
}
/**
* Get the player who detonated the TNT.
*
* @param tntID The EntityID of the TNT
* @return the Player who detonated it
*/
public Player getTNTPlayer(int tntID) {
return getServer().getPlayer(tntTracker.get(tntID));
}
/**
* Remove TNT from the tracker after it explodes.
*
* @param tntID The EntityID of the TNT
*/
public void removeFromTNTTracker(int tntID) {
tntTracker.remove(tntID);
}
2012-07-09 16:55:33 +02:00
2013-01-23 22:34:01 +01:00
public void addToOpenFurnaceTracker(Block furnace, String playerName) {
furnaceTracker.put(furnace, playerName);
}
public boolean furnaceIsTracked(Block furnace) {
return furnaceTracker.containsKey(furnace);
}
public void removeFromFurnaceTracker(Block furnace) {
furnaceTracker.remove(furnace);
}
public Player getFurnacePlayer(Block furnace) {
return getServer().getPlayer(furnaceTracker.get(furnace));
}
2012-07-09 16:55:33 +02:00
public static String getMainDirectory() {
return mainDirectory;
}
public static String getFlatFileDirectory() {
return flatFileDirectory;
}
2013-01-26 23:01:55 +01:00
public static String getUsersFilePath() {
2012-07-09 16:55:33 +02:00
return usersFile;
}
public static String getModDirectory() {
return modDirectory;
}
2012-07-09 17:12:46 +02:00
2013-01-24 20:19:26 +01:00
public boolean isXPEventEnabled() {
return xpEventEnabled;
}
public void setXPEventEnabled(boolean enabled) {
this.xpEventEnabled = enabled;
}
2012-03-08 22:55:43 +01:00
}
2012-07-09 16:55:33 +02:00