Saber-Factions/src/com/bukkit/mcteam/factions/Factions.java

186 lines
6.0 KiB
Java
Raw Normal View History

2011-02-06 13:36:11 +01:00
package com.bukkit.mcteam.factions;
2011-03-18 17:33:23 +01:00
import java.lang.reflect.Modifier;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.bukkit.command.Command;
import org.bukkit.command.CommandSender;
2011-02-06 13:36:11 +01:00
import org.bukkit.event.Event;
2011-03-18 17:33:23 +01:00
import org.bukkit.plugin.Plugin;
2011-02-06 13:36:11 +01:00
import org.bukkit.plugin.PluginManager;
import org.bukkit.plugin.java.JavaPlugin;
2011-03-18 17:33:23 +01:00
import com.bukkit.mcteam.factions.commands.FCommand;
import com.bukkit.mcteam.factions.listeners.FactionsBlockListener;
import com.bukkit.mcteam.factions.listeners.FactionsEntityListener;
import com.bukkit.mcteam.factions.listeners.FactionsPlayerListener;
2011-03-18 17:33:23 +01:00
import com.bukkit.mcteam.gson.Gson;
import com.bukkit.mcteam.gson.GsonBuilder;
import com.nijiko.permissions.PermissionHandler;
import com.nijikokun.bukkit.Permissions.Permissions;
import me.taylorkelly.help.Help;
2011-02-06 13:36:11 +01:00
public class Factions extends JavaPlugin {
2011-03-18 17:33:23 +01:00
// -------------------------------------------- //
// Fields
// -------------------------------------------- //
public static Factions instance;
public final static Gson gson = new GsonBuilder()
.setPrettyPrinting()
.excludeFieldsWithModifiers(Modifier.TRANSIENT, Modifier.VOLATILE)
.create();
2011-02-06 13:36:11 +01:00
private final FactionsPlayerListener playerListener = new FactionsPlayerListener(this);
private final FactionsEntityListener entityListener = new FactionsEntityListener(this);
private final FactionsBlockListener blockListener = new FactionsBlockListener(this);
2011-03-18 17:33:23 +01:00
public static PermissionHandler Permissions;
public static Help helpPlugin;
2011-02-13 17:04:06 +01:00
2011-03-18 17:33:23 +01:00
// Commands
public List<FCommand> commands = new ArrayList<FCommand>();
public Factions() {
Factions.instance = this;
2011-02-13 17:04:06 +01:00
}
2011-03-18 17:33:23 +01:00
2011-02-13 17:04:06 +01:00
@Override
public void onEnable() {
2011-03-18 17:33:23 +01:00
// Add the commands
/*commands.add(new VCommandBlood());
commands.add(new VCommandInfect());
commands.add(new VCommandLoad());
commands.add(new VCommandSave());
commands.add(new VCommandTime());
commands.add(new VCommandTurn());
commands.add(new VCommandCure());
commands.add(new VCommandList());
commands.add(new VCommandVersion());*/
2011-02-06 13:36:11 +01:00
2011-03-18 17:33:23 +01:00
setupPermissions();
setupHelp();
log("=== INIT START ===");
2011-02-06 13:36:11 +01:00
long timeInitStart = System.currentTimeMillis();
2011-03-18 17:33:23 +01:00
FPlayer.load();
Faction.load();
Board.load();
2011-02-06 13:36:11 +01:00
// Register events
2011-02-13 17:04:06 +01:00
PluginManager pm = this.getServer().getPluginManager();
pm.registerEvent(Event.Type.PLAYER_CHAT, this.playerListener, Event.Priority.Highest, this);
2011-03-04 17:41:04 +01:00
pm.registerEvent(Event.Type.PLAYER_COMMAND_PREPROCESS, this.playerListener, Event.Priority.Normal, this);
pm.registerEvent(Event.Type.PLAYER_ITEM, this.playerListener, Event.Priority.Normal, this);
2011-02-06 13:36:11 +01:00
pm.registerEvent(Event.Type.PLAYER_JOIN, this.playerListener, Event.Priority.Normal, this);
pm.registerEvent(Event.Type.PLAYER_MOVE, this.playerListener, Event.Priority.Normal, this);
pm.registerEvent(Event.Type.PLAYER_QUIT, this.playerListener, Event.Priority.Normal, this);
pm.registerEvent(Event.Type.ENTITY_DEATH, this.entityListener, Event.Priority.Normal, this);
2011-02-13 17:04:06 +01:00
pm.registerEvent(Event.Type.ENTITY_DAMAGED, this.entityListener, Event.Priority.Normal, this);
pm.registerEvent(Event.Type.ENTITY_EXPLODE, this.entityListener, Event.Priority.Normal, this);
2011-02-06 13:36:11 +01:00
pm.registerEvent(Event.Type.BLOCK_DAMAGED, this.blockListener, Event.Priority.Normal, this);
pm.registerEvent(Event.Type.BLOCK_PLACED, this.blockListener, Event.Priority.Normal, this);
pm.registerEvent(Event.Type.BLOCK_INTERACT, this.blockListener, Event.Priority.Normal, this);
2011-02-06 13:36:11 +01:00
2011-03-18 17:33:23 +01:00
log("=== INIT DONE (Took "+(System.currentTimeMillis()-timeInitStart)+"ms) ===");
}
@Override
public void onDisable() {
// TODO Auto-generated method stub
}
// -------------------------------------------- //
// Integration with other plugins
// -------------------------------------------- //
private void setupPermissions() {
Plugin test = this.getServer().getPluginManager().getPlugin("Permissions");
if (Permissions != null) {
return;
}
if (test != null) {
Permissions = ((Permissions)test).getHandler();
Factions.log("Found and will use plugin "+((Permissions)test).getDescription().getFullName());
} else {
Factions.log("Permission system not detected, defaulting to OP");
}
}
private void setupHelp() {
Plugin test = this.getServer().getPluginManager().getPlugin("Help");
if (helpPlugin != null) {
return;
}
if (test != null) {
helpPlugin = ((Help) test);
Factions.log("Found and will use plugin "+helpPlugin.getDescription().getFullName());
for(FCommand fcommand : commands) {
fcommand.helpRegister();
}
helpPlugin.registerCommand("help vampire", "help for the vampire plugin.", helpPlugin, true);
} else {
Factions.log(Level.WARNING, "'Help' plugin isn't detected. No /help support.");
}
}
// -------------------------------------------- //
// Commands
// -------------------------------------------- //
@Override
public boolean onCommand(CommandSender sender, Command cmd, String commandLabel, String[] args) {
List<String> parameters = new ArrayList<String>(Arrays.asList(args));
this.handleCommand(sender, parameters);
return true;
}
public void handleCommand(CommandSender sender, List<String> parameters) {
if (parameters.size() == 0) {
this.commands.get(0).execute(sender, parameters);
return;
}
String commandName = parameters.get(0).toLowerCase();
parameters.remove(0);
for (FCommand fcommand : this.commands) {
if (fcommand.getAliases().contains(commandName)) {
fcommand.execute(sender, parameters);
return;
}
}
sender.sendMessage(Conf.colorSystem+"Unknown faction command \""+commandName+"\". Try /help faction"); // TODO test help messages exists....
//TODO should we use internal help system instead?
}
// -------------------------------------------- //
// Logging
// -------------------------------------------- //
public static void log(String msg) {
log(Level.INFO, msg);
}
public static void log(Level level, String msg) {
Logger.getLogger("Minecraft").log(level, "["+instance.getDescription().getFullName()+"] "+msg);
2011-02-06 13:36:11 +01:00
}
}