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

303 lines
9.7 KiB
Java
Raw Normal View History

package com.gmail.nossr50;
import com.gmail.nossr50.datatypes.Mob;
import com.gmail.nossr50.datatypes.PlayerProfile;
2011-07-19 16:17:14 +02:00
import com.gmail.nossr50.datatypes.SkillType;
import com.gmail.nossr50.command.Commands;
import com.gmail.nossr50.config.*;
2011-08-14 10:37:03 +02:00
import com.gmail.nossr50.spout.SpoutStuff;
2011-08-23 03:22:16 +02:00
import com.gmail.nossr50.spout.mmoHelper;
import com.gmail.nossr50.listeners.mcBlockListener;
import com.gmail.nossr50.listeners.mcEntityListener;
import com.gmail.nossr50.listeners.mcPlayerListener;
import com.gmail.nossr50.party.Party;
import com.gmail.nossr50.skills.*;
import com.nijikokun.bukkit.Permissions.Permissions;
2011-08-03 03:54:14 +02:00
import org.bukkit.Bukkit;
2011-07-22 02:53:16 +02:00
import java.io.BufferedInputStream;
import java.io.BufferedReader;
2011-07-22 02:53:16 +02:00
import java.io.BufferedWriter;
import java.io.File;
2011-07-22 02:53:16 +02:00
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileReader;
2011-07-22 02:53:16 +02:00
import java.io.FileWriter;
2011-07-20 19:25:40 +02:00
import java.io.IOException;
import java.util.ArrayList;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.bukkit.command.Command;
import org.bukkit.command.CommandSender;
import org.bukkit.event.Event.Priority;
import org.bukkit.event.Event;
import org.bukkit.plugin.PluginDescriptionFile;
import org.bukkit.plugin.java.JavaPlugin;
import org.bukkit.plugin.PluginManager;
import org.bukkit.entity.Player;
2011-08-29 02:57:19 +02:00
import org.getspout.spoutapi.SpoutManager;
import org.getspout.spoutapi.player.FileManager;
public class mcMMO extends JavaPlugin
{
/*
* I never expected mcMMO to get so popular!
* Thanks for all the support for the mod
* Thanks to the people who have worked on the code
* Thanks to the donators who helped me out financially
* Thanks to the server admins who use my mod :)
*
* This mod is open source, and its going to stay that way >:3
*
* Donate via paypal to nossr50@gmail.com (A million thanks to anyone that does!)
*/
2011-07-18 20:35:56 +02:00
public static String maindirectory = "plugins" + File.separator + "mcMMO";
File file = new File(maindirectory + File.separator + "config.yml");
2011-07-22 02:53:16 +02:00
static File versionFile = new File(maindirectory + File.separator + "VERSION");
public static final Logger log = Logger.getLogger("Minecraft");
2011-08-04 02:59:34 +02:00
private final mcPlayerListener playerListener = new mcPlayerListener(this);
private final mcBlockListener blockListener = new mcBlockListener(this);
private final mcEntityListener entityListener = new mcEntityListener(this);
2011-08-04 02:59:34 +02:00
public static mcPermissions permissionHandler = new mcPermissions();
private Permissions permissions;
private Runnable mcMMO_Timer = new mcTimer(this); //BLEED AND REGENERATION
//private Timer mcMMO_SpellTimer = new Timer(true);
public static Database database = null;
public Mob mob = new Mob();
public Misc misc = new Misc(this);
//Config file stuff
LoadProperties config = new LoadProperties();
//Jar stuff
public static File mcmmo;
public void onEnable()
{
mcmmo = this.getFile();
new File(maindirectory).mkdir();
2011-07-23 22:23:38 +02:00
if(!versionFile.exists())
{
2011-07-22 02:53:16 +02:00
updateVersion();
2011-07-23 22:23:38 +02:00
} else
{
2011-07-22 02:53:16 +02:00
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);
}
mcPermissions.initialize(getServer());
2011-07-23 22:23:38 +02:00
config.configCheck();
2011-07-23 22:23:38 +02:00
Party.getInstance().loadParties();
new Party(this);
if(!LoadProperties.useMySQL)
Users.getInstance().loadUsers(); //Load Users file
/*
* REGISTER EVENTS
*/
PluginManager pm = getServer().getPluginManager();
if(pm.getPlugin("Spout") != null)
LoadProperties.spoutEnabled = true;
else
LoadProperties.spoutEnabled = false;
//Player Stuff
pm.registerEvent(Event.Type.PLAYER_QUIT, playerListener, Priority.Normal, this);
pm.registerEvent(Event.Type.PLAYER_JOIN, playerListener, Priority.Normal, this);
pm.registerEvent(Event.Type.PLAYER_LOGIN, playerListener, Priority.Normal, this);
pm.registerEvent(Event.Type.PLAYER_CHAT, playerListener, Priority.Lowest, this);
pm.registerEvent(Event.Type.PLAYER_INTERACT, playerListener, Priority.Monitor, this);
pm.registerEvent(Event.Type.PLAYER_RESPAWN, playerListener, Priority.Normal, this);
pm.registerEvent(Event.Type.PLAYER_PICKUP_ITEM, playerListener, Priority.Normal, this);
//Block Stuff
pm.registerEvent(Event.Type.BLOCK_DAMAGE, blockListener, Priority.Highest, this);
pm.registerEvent(Event.Type.BLOCK_BREAK, blockListener, Priority.Monitor, this);
pm.registerEvent(Event.Type.BLOCK_FROMTO, blockListener, Priority.Normal, this);
pm.registerEvent(Event.Type.BLOCK_PLACE, blockListener, Priority.Normal, this);
//Entity Stuff
pm.registerEvent(Event.Type.ENTITY_TARGET, entityListener, Priority.Normal, this);
pm.registerEvent(Event.Type.ENTITY_DEATH, entityListener, Priority.Normal, this);
2011-09-06 02:02:11 +02:00
pm.registerEvent(Event.Type.ENTITY_DAMAGE, entityListener, Priority.Monitor, this);
pm.registerEvent(Event.Type.CREATURE_SPAWN, entityListener, Priority.Normal, this);
2011-08-04 02:59:34 +02:00
//Spout Stuff
if(LoadProperties.spoutEnabled)
{
2011-08-21 07:58:25 +02:00
SpoutStuff.setupSpoutConfigs();
SpoutStuff.registerCustomEvent();
SpoutStuff.extractFiles(); //Extract source materials
2011-08-29 02:57:19 +02:00
FileManager FM = SpoutManager.getFileManager();
FM.addToPreLoginCache(this, SpoutStuff.getFiles());
Bukkit.getServer().getScheduler().scheduleSyncRepeatingTask(this,
new Runnable() {
@Override
public void run() {
mmoHelper.updateAll();
}
}, 20, 20);
2011-08-04 02:59:34 +02:00
}
PluginDescriptionFile pdfFile = this.getDescription();
mcPermissions.initialize(getServer());
if(LoadProperties.useMySQL)
{
database = new Database(this);
database.createStructure();
} else
Leaderboard.makeLeaderboards(); //Make the leaderboards
for(Player player : getServer().getOnlinePlayers()){Users.addUser(player);} //In case of reload add all users back into PlayerProfile
2011-07-23 22:23:38 +02:00
System.out.println(pdfFile.getName() + " version " + pdfFile.getVersion() + " is enabled!" );
2011-08-23 03:22:16 +02:00
Bukkit.getServer().getScheduler().scheduleAsyncRepeatingTask(this, mcMMO_Timer, 0, 20);
}
2011-07-19 16:17:14 +02:00
public PlayerProfile getPlayerProfile(Player player)
{
return Users.getProfile(player);
}
2011-07-20 19:25:40 +02:00
public void checkXp(Player player, SkillType skillType)
{
if(skillType == SkillType.ALL)
Skills.XpCheckAll(player);
else
Skills.XpCheckSkill(skillType, player);
}
2011-07-19 16:17:14 +02:00
public boolean inSameParty(Player playera, Player playerb)
{
if(Users.getProfile(playera).inParty() && Users.getProfile(playerb).inParty()){
if(Users.getProfile(playera).getParty().equals(Users.getProfile(playerb).getParty())){
return true;
} else {
return false;
}
} else {
return false;
}
}
public ArrayList<String> getParties(){
String location = "plugins/mcMMO/mcmmo.users";
ArrayList<String> parties = new ArrayList<String>();
try {
//Open the users file
FileReader file = new FileReader(location);
BufferedReader in = new BufferedReader(file);
String line = "";
while((line = in.readLine()) != null)
{
String[] character = line.split(":");
String theparty = null;
//Party
if(character.length > 3)
theparty = character[3];
if(!parties.contains(theparty))
parties.add(theparty);
}
in.close();
} catch (Exception e) {
log.log(Level.SEVERE, "Exception while reading "
+ location + " (Are you sure you formatted it correctly?)", e);
}
return parties;
}
public static String getPartyName(Player player){
PlayerProfile PP = Users.getProfile(player);
return PP.getParty();
}
public static boolean inParty(Player player){
PlayerProfile PP = Users.getProfile(player);
return PP.inParty();
}
public Permissions getPermissions() {
return permissions;
}
public void onDisable() {
2011-08-23 03:22:16 +02:00
Bukkit.getServer().getScheduler().cancelTasks(this);
System.out.println("mcMMO was disabled.");
}
public boolean onCommand(CommandSender sender, Command command, String label, String[] args)
{
return Commands.processCommands(sender, command, label, args);
}
2011-07-22 02:53:16 +02: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.
*/
public void updateFrom(int age) {
//No updater code needed, just update the version.
if(age == -1) {
updateVersion();
return;
}
//Updater code from age 1 goes here
if(age <= 1) {
//Since age 1 is an example for now, we will just let it do nothing.
}
//If we are updating from age 1 but we need more to reach age 2, this will run too.
if(age <= 2) {
}
updateVersion();
}
public void updateVersion() {
try {
versionFile.createNewFile();
BufferedWriter vout = new BufferedWriter(new FileWriter(versionFile));
vout.write(this.getDescription().getVersion());
vout.close();
} catch (IOException ex) {
ex.printStackTrace();
} catch (SecurityException ex) {
ex.printStackTrace();
}
}
public String readVersion() {
byte[] buffer = new byte[(int) versionFile.length()];
BufferedInputStream f = null;
try {
f = new BufferedInputStream(new FileInputStream(versionFile));
f.read(buffer);
} catch (FileNotFoundException ex) {
ex.printStackTrace();
} catch (IOException ex) {
ex.printStackTrace();
} finally {
if (f != null) try { f.close(); } catch (IOException ignored) { }
}
return new String(buffer);
}
2011-05-04 08:24:08 +02:00
}