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

469 lines
16 KiB
Java
Raw Normal View History

2012-01-09 20:00:13 +01:00
package com.gmail.nossr50;
import com.gmail.nossr50.datatypes.PlayerProfile;
import com.gmail.nossr50.commands.skills.*;
import com.gmail.nossr50.commands.spout.*;
import com.gmail.nossr50.commands.mc.*;
import com.gmail.nossr50.commands.party.*;
import com.gmail.nossr50.commands.general.*;
import com.gmail.nossr50.config.*;
import com.gmail.nossr50.runnables.*;
2012-01-09 20:00:13 +01:00
import com.gmail.nossr50.listeners.mcBlockListener;
import com.gmail.nossr50.listeners.mcEntityListener;
import com.gmail.nossr50.listeners.mcPlayerListener;
import com.gmail.nossr50.locale.mcLocale;
import com.gmail.nossr50.party.Party;
import java.io.BufferedInputStream;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;
2012-01-09 20:00:13 +01:00
import java.util.HashMap;
import org.bukkit.OfflinePlayer;
2012-01-09 20:00:13 +01:00
import org.bukkit.plugin.PluginDescriptionFile;
import org.bukkit.plugin.java.JavaPlugin;
import org.bukkit.plugin.PluginManager;
import org.bukkit.scheduler.BukkitScheduler;
import org.bukkit.configuration.file.FileConfiguration;
import org.bukkit.configuration.file.YamlConfiguration;
import org.bukkit.entity.Entity;
2012-01-09 20:00:13 +01:00
import org.bukkit.entity.Player;
2012-03-08 22:17:57 +01:00
public class mcMMO extends JavaPlugin {
private final mcPlayerListener playerListener = new mcPlayerListener(this);
private final mcBlockListener blockListener = new mcBlockListener(this);
private final mcEntityListener entityListener = new mcEntityListener(this);
public HashMap<String, String> aliasMap = new HashMap<String, String>(); //Alias - Command
public HashMap<Entity, Integer> arrowTracker = new HashMap<Entity, Integer>();
public HashMap<Integer, Player> tntTracker = new HashMap<Integer, Player>();
2012-03-08 22:17:57 +01:00
public static File versionFile;
public static Database database;
2012-04-21 00:09:50 +02:00
public static mcMMO p;
2012-03-08 22:17:57 +01:00
//Config file stuff
LoadProperties config;
LoadTreasures config2;
//Jar stuff
public static File mcmmo;
//File Paths
public static String mainDirectory;
public static String flatFileDirectory;
public static String usersFile;
public static String leaderboardDirectory;
2012-03-08 22:17:57 +01:00
/**
* Things to be run when the plugin is enabled.
*/
public void onEnable() {
2012-04-21 00:09:50 +02:00
p = this;
mcmmo = getFile();
versionFile = new File(getDataFolder(), "VERSION");
mainDirectory = getDataFolder().getPath() + File.separator;
flatFileDirectory = mainDirectory + "FlatFileStuff" + File.separator;
leaderboardDirectory = flatFileDirectory + "Leaderboards" + File.separator;
usersFile = flatFileDirectory + "mcmmo.users";
2012-03-08 22:17:57 +01:00
if (!versionFile.exists()) {
updateVersion();
}
else {
String vnum = readVersion();
//This will be changed to whatever version preceded when we actually need updater code.
//Version 1.0.48 is the first to implement this, no checking before that version can be done.
if (vnum.equalsIgnoreCase("1.0.48")) {
updateFrom(1);
}
//Just add in more else if blocks for versions that need updater code. Increment the updateFrom age int as we do so.
//Catch all for versions not matching and no specific code being needed
else if (!vnum.equalsIgnoreCase(this.getDescription().getVersion())) {
updateFrom(-1);
}
}
this.config = new LoadProperties(this);
this.config.load();
this.config2 = new LoadTreasures(this);
this.config2.load();
2012-04-26 00:54:08 +02:00
new Party(this).loadParties();
2012-03-08 22:17:57 +01:00
if (!LoadProperties.useMySQL) {
Users.loadUsers();
2012-03-08 22:17:57 +01:00
}
PluginManager pm = getServer().getPluginManager();
//Register events
pm.registerEvents(playerListener, this);
pm.registerEvents(blockListener, this);
pm.registerEvents(entityListener, this);
PluginDescriptionFile pdfFile = this.getDescription();
//Setup the leaderboards
if (LoadProperties.useMySQL) {
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
}
System.out.println(pdfFile.getName() + " version " + pdfFile.getVersion() + " is enabled!" );
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);
//Periodic save timer (Saves every 10 minutes)
scheduler.scheduleSyncRepeatingTask(this, new mcSaveTimer(this), 0, LoadProperties.saveInterval * 1200);
//Regen & Cooldown timer (Runs every second)
scheduler.scheduleSyncRepeatingTask(this, new mcTimer(this), 0, 20);
//Bleed timer (Runs every two seconds)
scheduler.scheduleSyncRepeatingTask(this, new mcBleedTimer(this), 0, 40);
2012-03-08 22:17:57 +01:00
registerCommands();
if (LoadProperties.statsTracking) {
//Plugin Metrics running in a new thread
new Thread(new Runnable() {
public void run() {
try {
// create a new metrics object
Metrics metrics = new Metrics();
// 'this' in this context is the Plugin object
2012-04-21 00:09:50 +02:00
metrics.beginMeasuringPlugin(p);
2012-03-08 22:17:57 +01:00
}
catch (IOException e) {
System.out.println("Failed to submit stats.");
}
}
}).start();
}
}
/**
* Get profile of the player.
* </br>
* This function is designed for API usage.
*
* @param player Player whose profile to get
* @return the PlayerProfile object
*/
public PlayerProfile getPlayerProfile(Player player) {
2012-03-08 22:17:57 +01:00
return Users.getProfile(player);
}
/**
* Get profile of the player by name.
* </br>
* This function is designed for API usage.
*
* @param player Name of player whose profile to get
* @return the PlayerProfile object
*/
public PlayerProfile getPlayerProfileByName(String playerName) {
return Users.getProfileByName(playerName);
}
/**
* Get profile of the offline player.
* </br>
* This function is designed for API usage.
*
* @param player Offline player whose profile to get
* @return the PlayerProfile object
*/
public PlayerProfile getOfflinePlayerProfile(OfflinePlayer player) {
return Users.getProfile(player);
}
2012-03-08 22:17:57 +01:00
/**
* Things to be run when the plugin is disabled.
*/
public void onDisable() {
//Make sure to save player information if the server shuts down
for (PlayerProfile x : Users.getProfiles().values()) {
x.save();
2012-03-08 22:17:57 +01:00
}
2012-03-08 22:55:43 +01:00
this.getServer().getScheduler().cancelTasks(this); //This removes our tasks
2012-03-08 22:17:57 +01:00
System.out.println("mcMMO was disabled."); //How informative!
}
2012-03-08 22:55:43 +01:00
/**
* Register the commands.
*/
2012-03-08 22:17:57 +01:00
private void registerCommands() {
2012-03-08 22:55:43 +01:00
2012-03-08 22:17:57 +01:00
//Register aliases with the aliasmap (used in the playercommandpreprocessevent to ugly alias them to actual commands)
//Skills commands
2012-04-22 08:55:58 +02:00
aliasMap.put(mcLocale.getString("Acrobatics.SkillName").toLowerCase(), "acrobatics");
aliasMap.put(mcLocale.getString("Archery.SkillName").toLowerCase(), "archery");
aliasMap.put(mcLocale.getString("Axes.SkillName").toLowerCase(), "axes");
aliasMap.put(mcLocale.getString("Excavation.SkillName").toLowerCase(), "excavation");
aliasMap.put(mcLocale.getString("Fishing.SkillName").toLowerCase(), "fishing");
aliasMap.put(mcLocale.getString("Herbalism.SkillName").toLowerCase(), "herbalism");
aliasMap.put(mcLocale.getString("Mining.SkillName").toLowerCase(), "mining");
aliasMap.put(mcLocale.getString("Repair.SkillName").toLowerCase(), "repair");
aliasMap.put(mcLocale.getString("Swords.SkillName").toLowerCase(), "swords");
aliasMap.put(mcLocale.getString("Taming.SkillName").toLowerCase(), "taming");
aliasMap.put(mcLocale.getString("Unarmed.SkillName").toLowerCase(), "unarmed");
aliasMap.put(mcLocale.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());
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
//mc* commands
if (LoadProperties.mcremoveEnable) {
2012-04-21 00:09:50 +02:00
getCommand("mcremove").setExecutor(new McremoveCommand(this));
2012-03-08 22:55:43 +01:00
}
if (LoadProperties.mcabilityEnable) {
getCommand("mcability").setExecutor(new McabilityCommand());
}
if (LoadProperties.mccEnable) {
getCommand("mcc").setExecutor(new MccCommand());
}
if (LoadProperties.mcgodEnable) {
getCommand("mcgod").setExecutor(new McgodCommand());
}
if (LoadProperties.mcmmoEnable) {
getCommand("mcmmo").setExecutor(new McmmoCommand());
}
if (LoadProperties.mcrefreshEnable) {
getCommand("mcrefresh").setExecutor(new McrefreshCommand(this));
}
if (LoadProperties.mctopEnable) {
getCommand("mctop").setExecutor(new MctopCommand());
}
if (LoadProperties.mcstatsEnable) {
getCommand("mcstats").setExecutor(new McstatsCommand());
}
2012-03-08 22:17:57 +01:00
//Party commands
2012-03-08 22:55:43 +01:00
if (LoadProperties.acceptEnable) {
2012-04-21 00:09:50 +02:00
getCommand("accept").setExecutor(new AcceptCommand(this));
2012-03-08 22:55:43 +01:00
}
if (LoadProperties.aEnable) {
2012-04-21 00:09:50 +02:00
getCommand("a").setExecutor(new ACommand(this));
2012-03-08 22:55:43 +01:00
}
if (LoadProperties.inviteEnable) {
getCommand("invite").setExecutor(new InviteCommand(this));
}
if (LoadProperties.partyEnable) {
2012-04-21 00:09:50 +02:00
getCommand("party").setExecutor(new PartyCommand(this));
2012-03-08 22:55:43 +01:00
}
if (LoadProperties.pEnable) {
2012-04-21 00:09:50 +02:00
getCommand("p").setExecutor(new PCommand(this));
2012-03-08 22:55:43 +01:00
}
if (LoadProperties.ptpEnable) {
getCommand("ptp").setExecutor(new PtpCommand(this));
}
2012-03-08 22:17:57 +01:00
//Other commands
2012-03-08 22:55:43 +01:00
if (LoadProperties.addxpEnable) {
getCommand("addxp").setExecutor(new AddxpCommand(this));
}
if (LoadProperties.addlevelsEnable) {
getCommand("addlevels").setExecutor(new AddlevelsCommand(this));
}
if (LoadProperties.mmoeditEnable) {
getCommand("mmoedit").setExecutor(new MmoeditCommand(this));
2012-03-08 22:55:43 +01:00
}
if (LoadProperties.inspectEnable) {
getCommand("inspect").setExecutor(new InspectCommand(this));
}
if (LoadProperties.xprateEnable) {
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
2012-03-08 22:55:43 +01:00
if (LoadProperties.xplockEnable) {
getCommand("xplock").setExecutor(new XplockCommand());
}
2012-04-21 00:09:50 +02:00
getCommand("mchud").setExecutor(new MchudCommand(this));
2012-03-08 22:17:57 +01:00
}
2012-03-08 22:55:43 +01:00
/**
* Update mcMMO from a given version
* </p>
2012-03-08 22:17:57 +01:00
* It is important to always assume that you are updating from the lowest possible version.
* Thus, every block of updater code should be complete and self-contained; finishing all
* SQL transactions and closing all file handlers, such that the next block of updater code
* if called will handle updating as expected.
2012-03-08 22:55:43 +01:00
*
* @param age Specifies which updater code to run
2012-03-08 22:17:57 +01:00
*/
public void updateFrom(int age) {
2012-03-08 22:55:43 +01:00
2012-03-08 22:17:57 +01:00
//No updater code needed, just update the version.
2012-03-08 22:55:43 +01:00
if (age == -1) {
2012-03-08 22:17:57 +01:00
updateVersion();
return;
}
2012-03-08 22:55:43 +01:00
2012-03-08 22:17:57 +01:00
//Updater code from age 1 goes here
2012-03-08 22:55:43 +01:00
if (age <= 1) {
2012-03-08 22:17:57 +01:00
//Since age 1 is an example for now, we will just let it do nothing.
}
2012-03-08 22:55:43 +01:00
2012-03-08 22:17:57 +01:00
//If we are updating from age 1 but we need more to reach age 2, this will run too.
2012-03-08 22:55:43 +01:00
if (age <= 2) {
2012-03-08 22:17:57 +01:00
}
updateVersion();
}
2012-03-08 22:55:43 +01:00
/**
* Update the version file.
*/
2012-03-08 22:17:57 +01:00
public void updateVersion() {
try {
versionFile.createNewFile();
BufferedWriter vout = new BufferedWriter(new FileWriter(versionFile));
vout.write(this.getDescription().getVersion());
vout.close();
2012-03-08 22:55:43 +01:00
}
catch (IOException ex) {
2012-03-08 22:17:57 +01:00
ex.printStackTrace();
2012-03-08 22:55:43 +01:00
}
catch (SecurityException ex) {
2012-03-08 22:17:57 +01:00
ex.printStackTrace();
}
}
2012-01-09 20:00:13 +01:00
2012-03-08 22:55:43 +01:00
/**
* Get the current mcMMO version.
*
* @return a String representing the current mcMMO version
*/
2012-03-08 22:17:57 +01:00
public String readVersion() {
byte[] buffer = new byte[(int) versionFile.length()];
BufferedInputStream f = null;
2012-03-08 22:55:43 +01:00
2012-03-08 22:17:57 +01:00
try {
f = new BufferedInputStream(new FileInputStream(versionFile));
f.read(buffer);
2012-03-08 22:55:43 +01:00
}
catch (FileNotFoundException ex) {
2012-03-08 22:17:57 +01:00
ex.printStackTrace();
2012-03-08 22:55:43 +01:00
}
catch (IOException ex) {
2012-03-08 22:17:57 +01:00
ex.printStackTrace();
}
2012-03-08 22:55:43 +01:00
finally {
if (f != null) {
try {
f.close();
}
catch (IOException ignored) {}
}
}
2012-03-08 22:17:57 +01:00
return new String(buffer);
}
2012-03-08 22:55:43 +01:00
2012-03-08 22:17:57 +01:00
/*
* Boilerplate Custom Config Stuff
*/
2012-03-08 22:55:43 +01:00
2012-03-08 22:17:57 +01:00
private FileConfiguration treasuresConfig = null;
private File treasuresConfigFile = null;
2012-03-08 22:55:43 +01:00
/**
* Reload the Treasures.yml file.
*/
2012-03-08 22:17:57 +01:00
public void reloadTreasuresConfig() {
if (treasuresConfigFile == null) {
2012-03-08 22:55:43 +01:00
treasuresConfigFile = new File(getDataFolder(), "treasures.yml");
2012-03-08 22:17:57 +01:00
}
2012-03-08 22:55:43 +01:00
2012-03-08 22:17:57 +01:00
treasuresConfig = YamlConfiguration.loadConfiguration(treasuresConfigFile);
2012-03-08 22:55:43 +01:00
InputStream defConfigStream = getResource("treasures.yml"); // Look for defaults in the jar
2012-03-08 22:17:57 +01:00
if (defConfigStream != null) {
YamlConfiguration defConfig = YamlConfiguration.loadConfiguration(defConfigStream);
treasuresConfig.setDefaults(defConfig);
}
}
2012-03-08 22:55:43 +01:00
/**
* Get the Treasures config information.
*
* @return the configuration object for treasures.yml
*/
2012-03-08 22:17:57 +01:00
public FileConfiguration getTreasuresConfig() {
if (treasuresConfig == null) {
reloadTreasuresConfig();
}
2012-03-08 22:55:43 +01:00
2012-03-08 22:17:57 +01:00
return treasuresConfig;
}
2012-03-08 22:55:43 +01:00
/**
* Save the Treasures config informtion.
*/
2012-03-08 22:17:57 +01:00
public void saveTreasuresConfig() {
if (treasuresConfig == null || treasuresConfigFile == null) {
2012-03-08 22:55:43 +01:00
return;
2012-03-08 22:17:57 +01:00
}
2012-03-08 22:55:43 +01:00
2012-03-08 22:17:57 +01:00
try {
treasuresConfig.save(treasuresConfigFile);
2012-03-08 22:55:43 +01:00
}
catch (IOException ex) {
this.getLogger().severe("Could not save config to " + treasuresConfigFile + ex.toString());
2012-03-08 22:17:57 +01:00
}
}
2012-03-08 22:55:43 +01:00
}