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

544 lines
20 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;
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.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.MmoupdateCommand;
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.McpurgeCommand;
2012-06-05 16:13:10 +02:00
import com.gmail.nossr50.commands.mc.McrefreshCommand;
import com.gmail.nossr50.commands.mc.McremoveCommand;
import com.gmail.nossr50.commands.mc.MctopCommand;
import com.gmail.nossr50.commands.party.ACommand;
import com.gmail.nossr50.commands.party.AcceptCommand;
import com.gmail.nossr50.commands.party.InviteCommand;
import com.gmail.nossr50.commands.party.PCommand;
import com.gmail.nossr50.commands.party.PartyCommand;
import com.gmail.nossr50.commands.party.PtpCommand;
import com.gmail.nossr50.commands.skills.AcrobaticsCommand;
import com.gmail.nossr50.commands.skills.ArcheryCommand;
import com.gmail.nossr50.commands.skills.AxesCommand;
import com.gmail.nossr50.commands.skills.ExcavationCommand;
import com.gmail.nossr50.commands.skills.FishingCommand;
import com.gmail.nossr50.commands.skills.HerbalismCommand;
import com.gmail.nossr50.commands.skills.MiningCommand;
import com.gmail.nossr50.commands.skills.RepairCommand;
import com.gmail.nossr50.commands.skills.SwordsCommand;
import com.gmail.nossr50.commands.skills.TamingCommand;
import com.gmail.nossr50.commands.skills.UnarmedCommand;
import com.gmail.nossr50.commands.skills.WoodcuttingCommand;
import com.gmail.nossr50.commands.spout.MchudCommand;
import com.gmail.nossr50.commands.spout.XplockCommand;
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;
import com.gmail.nossr50.config.mods.CustomArmorConfig;
2012-06-05 16:13:10 +02:00
import com.gmail.nossr50.config.mods.CustomBlocksConfig;
2012-05-17 16:26:21 +02:00
import com.gmail.nossr50.config.mods.CustomToolsConfig;
2012-06-28 15:01:02 +02:00
import com.gmail.nossr50.config.repair.RepairConfigManager;
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;
import com.gmail.nossr50.listeners.PlayerListener;
import com.gmail.nossr50.listeners.WorldListener;
import com.gmail.nossr50.locale.LocaleLoader;
2012-06-08 23:48:41 +02:00
import com.gmail.nossr50.party.PartyManager;
2012-06-05 16:13:10 +02:00
import com.gmail.nossr50.runnables.BleedTimer;
2012-06-28 23:20:03 +02:00
import com.gmail.nossr50.runnables.ChunkletUnloader;
import com.gmail.nossr50.runnables.MobStoreCleaner;
2012-06-05 16:13:10 +02:00
import com.gmail.nossr50.runnables.SaveTimer;
import com.gmail.nossr50.runnables.SkillMonitor;
import com.gmail.nossr50.runnables.SpoutStart;
import com.gmail.nossr50.runnables.UserPurgeTask;
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.util.Anniversary;
2012-04-27 11:47:11 +02:00
import com.gmail.nossr50.util.Database;
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;
2012-11-06 01:03:44 +01:00
import com.gmail.nossr50.util.blockmeta.chunkmeta.ChunkManager;
import com.gmail.nossr50.util.blockmeta.chunkmeta.ChunkManagerFactory;
2012-01-09 20:00:13 +01:00
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 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<String, String> aliasMap = new HashMap<String, String>(); //Alias - Command
private HashMap<Integer, String> tntTracker = new HashMap<Integer, String>();
2012-03-08 22:17:57 +01:00
2012-07-09 17:12:46 +02:00
private static Database database;
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 leaderboardDirectory;
private static String modDirectory;
//Spout Check
2012-06-05 16:42:40 +02:00
public static boolean spoutEnabled;
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();
//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();
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);
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 leaderboards
if (configInstance.getUseMySQL()) {
2012-03-08 22:17:57 +01:00
database = new Database(this);
database.createStructure();
}
else {
Leaderboard.makeLeaderboards();
}
for (Player player : getServer().getOnlinePlayers()) {
Users.addUser(player); //In case of reload add all users back into PlayerProfile
}
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
//Schedule Spout Activation 1 second after start-up
scheduler.scheduleSyncDelayedTask(this, new SpoutStart(this), 20);
2012-06-28 23:20:03 +02:00
//Periodic save timer (Saves every 10 minutes by default)
scheduler.scheduleSyncRepeatingTask(this, new SaveTimer(this), 0, configInstance.getSaveInterval() * 1200);
//Regen & Cooldown timer (Runs every second)
2012-04-27 11:47:11 +02:00
scheduler.scheduleSyncRepeatingTask(this, new SkillMonitor(this), 0, 20);
//Bleed timer (Runs every two seconds)
scheduler.scheduleSyncRepeatingTask(this, new BleedTimer(), 0, 40);
2012-06-28 23:20:03 +02:00
//Chunklet unloader (Runs every 20 seconds by default)
scheduler.scheduleSyncRepeatingTask(this, new ChunkletUnloader(), 0, ChunkletUnloader.RUN_INTERVAL * 20);
//Old & Powerless User remover
int purgeInterval = Config.getInstance().getPurgeInterval();
if (purgeInterval == 0) {
2013-01-14 06:47:47 +01:00
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
}
// Get our ChunkletManager
placeStore = ChunkManagerFactory.getChunkManager();
// Automatically starts and stores itself
MobStoreCleaner cleaner = new MobStoreCleaner();
// Create Anniversary files
Anniversary anniversary = new Anniversary();
anniversary.createAnniversaryFile();
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";
leaderboardDirectory = flatFileDirectory + "Leaderboards" + File.separator;
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() {
2012-06-25 14:30:55 +02:00
Users.saveAll(); //Make sure to save player information if the server shuts down
2012-06-08 23:48:41 +02:00
PartyManager.getInstance().saveParties();
getServer().getScheduler().cancelTasks(this); //This removes our tasks
2012-06-25 14:30:55 +02:00
placeStore.saveAll(); //Save our metadata
placeStore.cleanUp(); //Cleanup empty metadata stores
2012-05-01 00:48:59 +02:00
//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
}
Anniversary anniversary = new Anniversary();
anniversary.saveAnniversaryFiles();
2012-05-01 01:32:50 +02:00
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() {
//Register aliases with the aliasmap (used in the playercommandpreprocessevent to ugly alias them to actual commands)
//Skills commands
2012-04-27 11:47:11 +02:00
aliasMap.put(LocaleLoader.getString("Acrobatics.SkillName").toLowerCase(), "acrobatics");
aliasMap.put(LocaleLoader.getString("Archery.SkillName").toLowerCase(), "archery");
aliasMap.put(LocaleLoader.getString("Axes.SkillName").toLowerCase(), "axes");
aliasMap.put(LocaleLoader.getString("Excavation.SkillName").toLowerCase(), "excavation");
aliasMap.put(LocaleLoader.getString("Fishing.SkillName").toLowerCase(), "fishing");
aliasMap.put(LocaleLoader.getString("Herbalism.SkillName").toLowerCase(), "herbalism");
aliasMap.put(LocaleLoader.getString("Mining.SkillName").toLowerCase(), "mining");
aliasMap.put(LocaleLoader.getString("Repair.SkillName").toLowerCase(), "repair");
aliasMap.put(LocaleLoader.getString("Swords.SkillName").toLowerCase(), "swords");
aliasMap.put(LocaleLoader.getString("Taming.SkillName").toLowerCase(), "taming");
aliasMap.put(LocaleLoader.getString("Unarmed.SkillName").toLowerCase(), "unarmed");
aliasMap.put(LocaleLoader.getString("Woodcutting.SkillName").toLowerCase(), "woodcutting");
2012-03-08 22:55:43 +01:00
2012-03-08 22:17:57 +01:00
//Register commands
//Skills commands
getCommand("acrobatics").setExecutor(new AcrobaticsCommand());
2012-03-08 22:17:57 +01:00
getCommand("archery").setExecutor(new ArcheryCommand());
getCommand("axes").setExecutor(new AxesCommand());
getCommand("excavation").setExecutor(new ExcavationCommand());
getCommand("fishing").setExecutor(new FishingCommand());
getCommand("herbalism").setExecutor(new HerbalismCommand());
getCommand("mining").setExecutor(new MiningCommand());
getCommand("repair").setExecutor(new RepairCommand());
getCommand("swords").setExecutor(new SwordsCommand());
getCommand("taming").setExecutor(new TamingCommand());
getCommand("unarmed").setExecutor(new UnarmedCommand());
getCommand("woodcutting").setExecutor(new WoodcuttingCommand());
2012-03-08 22:55:43 +01:00
Config configInstance = Config.getInstance();
2012-03-08 22:55:43 +01:00
//mc* commands
if (configInstance.getCommandMCPurgeEnabled()) {
2013-01-14 06:47:47 +01:00
getCommand("mcpurge").setExecutor(new McpurgeCommand());
}
if (configInstance.getCommandMCRemoveEnabled()) {
2012-04-21 00:09:50 +02:00
getCommand("mcremove").setExecutor(new McremoveCommand(this));
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()) {
2012-03-08 22:55:43 +01:00
getCommand("mcrefresh").setExecutor(new McrefreshCommand(this));
}
if (configInstance.getCommandMCTopEnabled()) {
2012-03-08 22:55:43 +01:00
getCommand("mctop").setExecutor(new MctopCommand());
}
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());
}
2012-03-08 22:17:57 +01:00
//Party commands
if (configInstance.getCommandAcceptEnabled()) {
2012-04-21 00:09:50 +02:00
getCommand("accept").setExecutor(new AcceptCommand(this));
2012-03-08 22:55:43 +01:00
}
if (configInstance.getCommandAdminChatAEnabled()) {
2012-04-21 00:09:50 +02:00
getCommand("a").setExecutor(new ACommand(this));
2012-03-08 22:55:43 +01:00
}
if (configInstance.getCommandInviteEnabled()) {
2012-03-08 22:55:43 +01:00
getCommand("invite").setExecutor(new InviteCommand(this));
}
if (configInstance.getCommandPartyEnabled()) {
2012-04-21 00:09:50 +02:00
getCommand("party").setExecutor(new PartyCommand(this));
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));
}
2012-03-08 22:17:57 +01:00
//Other commands
if (configInstance.getCommandAddXPEnabled()) {
2012-03-08 22:55:43 +01:00
getCommand("addxp").setExecutor(new AddxpCommand(this));
}
if (configInstance.getCommandAddLevelsEnabled()) {
2012-03-08 22:55:43 +01:00
getCommand("addlevels").setExecutor(new AddlevelsCommand(this));
}
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()) {
2012-04-21 00:09:50 +02:00
getCommand("xprate").setExecutor(new XprateCommand(this));
2012-03-08 22:55:43 +01:00
}
2012-04-21 00:09:50 +02:00
getCommand("mmoupdate").setExecutor(new MmoupdateCommand(this));
2012-03-08 22:55:43 +01:00
2012-03-08 22:17:57 +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
/**
* Checks to see if the alias map contains the given key.
*
* @param command The command to check
* @return true if the command is in the map, false otherwise
*/
public boolean commandIsAliased(String command) {
return aliasMap.containsKey(command);
}
/**
* Get the alias of a given command.
*
* @param command The command to retrieve the alias of
* @return the alias of the command
*/
public String getCommandAlias(String command) {
return aliasMap.get(command);
}
/**
* 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
public static String getMainDirectory() {
return mainDirectory;
}
public static String getFlatFileDirectory() {
return flatFileDirectory;
}
public static String getUsersFile() {
return usersFile;
}
public static String getLeaderboardDirectory() {
return leaderboardDirectory;
}
public static String getModDirectory() {
return modDirectory;
}
2012-07-09 17:12:46 +02:00
public static Database getPlayerDatabase() {
return database;
}
2012-03-08 22:55:43 +01:00
}
2012-07-09 16:55:33 +02:00