Permalink
Browse files

Logging refactoring,

Added external command injection into Bukkit
  • Loading branch information...
1 parent c364020 commit 109c62e00fc09319b3055ea1663343ab6e7978eb @GuntherDW committed Sep 1, 2012
@@ -181,7 +181,7 @@ public boolean canTalk(String player) {
}
if (checktime > muteTime) {
if (plugin.getConfigHandler().enableDebug)
- plugin.getLogger().info("[TweakcraftUtils] Mutes: auto-unmuting " + player + ", his mutetime was over!");
+ plugin.getLogger().info(" Mutes: auto-unmuting " + player + ", his mutetime was over!");
removeMute(player);
}
}
@@ -37,7 +37,7 @@
protected Logger logger = Logger.getLogger("Minecraft");
public ChatMode(ChatHandler instance) {
- subscribers = new HashSet<String>();
+ this.subscribers = new HashSet<String>();
this.chathandler = instance;
}
@@ -28,13 +28,17 @@
import org.bukkit.ChatColor;
import org.bukkit.command.CommandSender;
import org.bukkit.command.PluginCommand;
+import org.bukkit.command.SimpleCommandMap;
import org.bukkit.entity.Player;
+import org.bukkit.plugin.Plugin;
import org.bukkit.plugin.PluginDescriptionFile;
+import org.bukkit.plugin.SimplePluginManager;
+import java.lang.reflect.Constructor;
+import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.*;
-import java.util.logging.Logger;
/**
* @author GuntherDW
@@ -57,7 +61,11 @@
public TeleportationCommands teleportationCommands = null;
public WeatherCommands weatherCommands = null;
- private Logger logger = Logger.getLogger("Minecraft");
+ /** Bukkit Command Injection stuff **/
+ private SimplePluginManager simplePluginManager = null;
+ private SimpleCommandMap simpleCommandMap = null;
+ private Constructor<PluginCommand> pluginCommandConstructor = null;
+
private TweakcraftUtils plugin;
private Object getMethodInstance(Method method) {
@@ -113,9 +121,9 @@ public void injectCommand(Method method, Object instance, boolean injectIntoBukk
if (newCommandMap.containsKey(commandName)) {
- logger.warning("[TweakcraftUtils] Duplicate command found!");
- logger.warning("[TweakcraftUtils] Method : " + method.getName() + "!");
- logger.warning("[TweakcraftUtils] Command : " + commandName + "!");
+ plugin.getLogger().warning("Duplicate command found!");
+ plugin.getLogger().warning("Method : " + method.getName() + "!");
+ plugin.getLogger().warning("Command : " + commandName + "!");
return;
}
List<String> al = new ArrayList<String>();
@@ -126,10 +134,58 @@ public void injectCommand(Method method, Object instance, boolean injectIntoBukk
aliasCommandMap.put(alias, method);
}
-
this.commandAliases.put(commandName, al);
instanceMap.put(method, instance);
+
+ if(injectIntoBukkit) {
+ // plugin.getLogger().info("Injecting "+commandName+" into Bukkit!");
+ this.injectIntoBukkit(commandName, aliases, method, instance, annotation);
+ }
+ }
+ }
+
+ public void getBukkitCommandMap() {
+ try {
+ Object plmgr = plugin.getServer().getPluginManager();
+ if (plmgr instanceof SimplePluginManager) {
+ SimplePluginManager pluginmanager = (SimplePluginManager) plmgr;
+ Field f = SimplePluginManager.class.getDeclaredField("commandMap");
+ f.setAccessible(true);
+ SimpleCommandMap simpleCommandMap = (SimpleCommandMap) f.get(pluginmanager);
+ Constructor<PluginCommand> con = PluginCommand.class.getDeclaredConstructor(String.class, Plugin.class);
+ con.setAccessible(true);
+ this.simplePluginManager = pluginmanager;
+ this.simpleCommandMap = simpleCommandMap;
+ this.pluginCommandConstructor = con;
+ }
+ } catch (Exception e) {
+ plugin.getLogger().warning("Exception caught while getting the CommandMap, plugin command injection will NOT work!");
+ e.printStackTrace();
+ }
+
+ }
+
+ public void injectIntoBukkit(String command, String[] aliases, Method commandMethod, Object classInstance, aCommand annotation) {
+
+ if(this.simplePluginManager == null)
+ this.getBukkitCommandMap();
+
+ try {
+ if (this.simplePluginManager != null) {
+ PluginCommand cmd = pluginCommandConstructor.newInstance(command, this.plugin);
+ cmd.setAliases(Arrays.asList(aliases));
+ cmd.setPermission("tweakcraftutils."+annotation.permissionBase());
+ cmd.setDescription(annotation.description());
+ cmd.setUsage(annotation.usage());
+
+ cmd.setExecutor(plugin);
+
+ this.simpleCommandMap.register("tweakcraftutils", cmd);
+ }
+ } catch (Exception e) {
+ plugin.getLogger().warning("Exception caught while injecting '" + command + "' into Bukkit!");
+ e.printStackTrace();
}
}
@@ -162,7 +218,7 @@ public void checkCommands() {
Set<String> cmds = pluginCommands.keySet();
for (String c : cmds) {
if (!this.newCommandMap.containsKey(c) && !this.aliasCommandMap.containsKey(c))
- logger.warning("[TweakcraftUtils] WARNING: Unmapped command : " + c);
+ plugin.getLogger().warning("WARNING: Unmapped command : " + c);
}
}
}
@@ -216,33 +272,31 @@ public boolean executeCommand(CommandSender sender, String name, String[] args)
if (newCommandMap.containsKey(name) || aliasCommandMap.containsKey(name)) {
try {
- /* iCommand command = commandHandler.getCommand(cmd.getName()); */
- // public abstract boolean executeCommand(Server server, CommandSender sender, String command, String[] args, TweakcraftUtils plugin);
-
if (!runMethod(sender, name, args)) {
sender.sendMessage("This command did not go as intended!");
}
- if (sender instanceof Player) {
+ /* if (sender instanceof Player) {
final LocalPlayer lp = plugin.wrapPlayer((Player)sender);
- plugin.getLogger().info("[TweakcraftUtils] "+(lp.isInvisible()?"[INVIS] ":"") + sender.getName() + " issued: /" + name + " " + mess);
- } else
- plugin.getLogger().info("[TweakcraftUtils] CONSOLE issued: /" + name + " " + mess);
+ plugin.getLogger().info((lp.isInvisible()?"[INVIS] ":"") + sender.getName() + " issued: /" + name + " " + mess);
+ } else */
+ if(!(sender instanceof Player))
+ plugin.getLogger().info("CONSOLE issued: /" + name + " " + mess);
return true;
} /*catch (CommandNotFoundException e) {
sender.sendMessage("TweakcraftUtils error, command not found!");
}*/ catch (PermissionsException e) {
sender.sendMessage(ChatColor.RED + "You do not have the correct permissions for this command or usage!");
if (sender instanceof Player) {
final LocalPlayer lp = plugin.wrapPlayer((Player)sender);
- plugin.getLogger().info("[TweakcraftUtils] " + (lp.isInvisible()?"[INVIS] ":"") + ((Player) sender).getName() + " tried: /" + name + " " + mess);
+ plugin.getLogger().info((lp.isInvisible()?"[INVIS] ":"") + ((Player) sender).getName() + " tried: /" + name + " " + mess);
}
} catch (CommandUsageException e) {
sender.sendMessage(ChatColor.YELLOW + e.getMessage());
} catch (CommandSenderException e) {
sender.sendMessage(ChatColor.YELLOW + e.getMessage());
} catch (CommandException e) {
sender.sendMessage(ChatColor.YELLOW + e.getMessage());
- plugin.getLogger().info("[TweakcraftUtils] " + (sender instanceof Player ? ((Player) sender).getName() : "CONSOLE") + " got a CommandException on : /" + name + " " + mess);
+ plugin.getLogger().info((sender instanceof Player ? ((Player) sender).getName() : "CONSOLE") + " got a CommandException on : /" + name + " " + mess);
}
}
return false;
@@ -270,14 +324,14 @@ public boolean runMethod(CommandSender sender, String name, String[] args)
if (t instanceof CommandException)
throw (CommandException) t;
- logger.warning("[TweakcraftUtils] Error occured while executing command!");
- logger.warning("[TweakcraftUtils] Errornous command : "+name+" "+argsString+"!");
- logger.warning("[TweakcraftUtils] CommandSender : "+(sender instanceof Player?((Player)sender).getName():"CONSOLE"));
+ plugin.getLogger().warning("Error occured while executing command!");
+ plugin.getLogger().warning("Errornous command : " + name + " " + argsString + "!");
+ plugin.getLogger().warning("CommandSender : " + (sender instanceof Player ? ((Player) sender).getName() : "CONSOLE"));
e.printStackTrace();
} catch (IllegalAccessException e) {
- logger.warning("[TweakcraftUtils] Error occured while executing command!");
- logger.warning("[TweakcraftUtils] Errornous command : "+name+" "+argsString+"!");
- logger.warning("[TweakcraftUtils] CommandSender : "+(sender instanceof Player?((Player)sender).getName():"CONSOLE"));
+ plugin.getLogger().warning("Error occured while executing command!");
+ plugin.getLogger().warning("Errornous command : " + name + " " + argsString + "!");
+ plugin.getLogger().warning("CommandSender : " + (sender instanceof Player ? ((Player) sender).getName() : "CONSOLE"));
e.printStackTrace();
}
@@ -737,7 +737,7 @@ public boolean tweakcraft(CommandSender sender, String command, String[] args)
Player player = (Player) sender;
Set<Player> lijst = plugin.getCUIPlayers();
if (lijst != null && !lijst.contains(player)) {
- plugin.getLogger().info("[TweakcraftUtils] Adding " + player.getName() + " to the CUI list!");
+ plugin.getLogger().info("Adding " + player.getName() + " to the CUI list!");
lijst.add(player);
}
@@ -748,7 +748,7 @@ public boolean tweakcraft(CommandSender sender, String command, String[] args)
Player player = (Player) sender;
Set<Player> lijst = plugin.getMod_InfDuraplayers();
if (lijst != null && !lijst.contains(player)) {
- plugin.getLogger().info("[TweakcraftUtils] Adding " + player.getName() + " to the mod_InfDura list!");
+ plugin.getLogger().info("Adding " + player.getName() + " to the mod_InfDura list!");
lijst.add(player);
}
@@ -29,11 +29,8 @@
import org.bukkit.ChatColor;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
-import org.bukkit.event.player.PlayerChatEvent;
-import java.util.ArrayList;
-import java.util.Arrays;
-import java.util.List;
+import java.util.*;
/**
* @author GuntherDW
@@ -276,7 +273,10 @@ public boolean chatCommand(CommandSender sender, String command, String chatMode
}
// plugin.getServer().broadcastMessage(ChatColor.WHITE + "<" + (sender instanceof Player ? ((Player) sender).getDisplayName() : ChatColor.LIGHT_PURPLE + "CONSOLE") + ChatColor.WHITE + "> " + spam);
if(sender instanceof Player) {
- PlayerChatEvent playerChatEvent = new PlayerChatEvent((Player) sender, spam);
+ Set<Player> players = new HashSet<Player>();
+ players.addAll(Arrays.asList(plugin.getServer().getOnlinePlayers()));
+
+ org.bukkit.event.player.AsyncPlayerChatEvent playerChatEvent = new org.bukkit.event.player.AsyncPlayerChatEvent(false, (Player) sender, spam, players);
plugin.getServer().getPluginManager().callEvent(playerChatEvent);
} else {
String chat = ChatColor.WHITE + "<" + ChatColor.LIGHT_PURPLE + "CONSOLE" + ChatColor.WHITE + "> " + spam;
@@ -127,7 +127,7 @@ public boolean ban(CommandSender sender, String command, String[] realargs)
sender.sendMessage(ChatColor.YELLOW + "Kickbanning " + player.getName());
player.kickPlayer(reason);
}
- plugin.getLogger().info("[TweakcraftUtils] Banning " + playername + "!");
+ plugin.getLogger().info("Banning " + playername + "!");
handler.saveBans();
}
return true;
@@ -265,12 +265,6 @@ public boolean help(CommandSender sender, String command, String[] args)
boolean aliases = true;
String toadd = "";
for (Map.Entry<String, Method> entry : commh.getCommandMap().entrySet()) {
- // Method commandMethod = commh.getCommandMap().get(cname);
- // aCommand annotation = commandMethod.getAnnotation(aCommand.class);
- /* if (plugin.getCommand(entry.getKey()) == null) {
- System.out.println("Nullpointer for " + entry.getKey());
- continue;
- } */
if (addCommandToList(sender, entry.getValue())) {
// Fuck minecraft's font :<
@@ -330,7 +324,7 @@ public boolean help(CommandSender sender, String command, String[] args)
}
}
} else {
- plugin.getLogger().info("[TweakcraftUtils] EXTRAHELP error : " + plug + " is null!");
+ plugin.getLogger().info("EXTRAHELP error : " + plug + " is null!");
}
}
@@ -489,7 +483,7 @@ public boolean item(CommandSender sender, String command, String[] realargs)
stack.setData(Material.getMaterial(itemId).getNewData((byte) dataval));
receiver.getInventory().addItem(stack);
- plugin.getLogger().info("[TweakcraftUtils] " + giftfrom + " gave " + recvname + " " + itemAmount + "x" + itemId + " (" + itemDmg.intValue() + ")");
+ plugin.getLogger().info(giftfrom + " gave " + recvname + " " + itemAmount + "x" + itemId + " (" + itemDmg.intValue() + ")");
} else {
throw new CommandUsageException("Specified item is not valid!");
}
@@ -712,7 +706,7 @@ public boolean msg(CommandSender sender, String command, String[] args)
// plugin.setPlayerReply(playerto.getName(), ((Player) sender).getName());
}
- plugin.getLogger().info("[TweakcraftUtils] (MSG) " + clearName + " -> " + playerto.getName() + " : " + message);
+ plugin.getLogger().info("(MSG) " + clearName + " -> " + playerto.getName() + " : " + message);
} else if (args.length == 1) {
throw new CommandUsageException("I need a message!");
} else {
@@ -887,7 +881,7 @@ public boolean reply(CommandSender sender, String command, String[] args)
playerto.sendMessage("[" + player.getDisplayName() + " -> Me] " + message);
lpTo.setReplyTo(player.getName());
// plugin.setPlayerReply(playerto.getName(), player.getName());
- plugin.getLogger().info("[TweakcraftUtils] (MSG) " + player.getName() + " -> " + playerto.getName() + " : " + message);
+ plugin.getLogger().info("(MSG) " + player.getName() + " -> " + playerto.getName() + " : " + message);
} else if (args.length == 0) {
throw new CommandUsageException("I need a message!");
}
@@ -236,7 +236,7 @@ public boolean teleportToPlayer(CommandSender sender, String command, String[] r
if (lpto.isInvisible()) {
if (!plugin.check(player, "tpinvis")) {
player.sendMessage(ChatColor.YELLOW + "Can't find player!");
- plugin.getLogger().info("[TweakcraftUtils] " + player.getName() + " tried to tp to " + p.getName() + " <invisible>!");
+ plugin.getLogger().info(player.getName() + " tried to tp to " + p.getName() + " <invisible>!");
return true;
} else {
player.sendMessage(ChatColor.AQUA + "Stealth player TP!");
@@ -265,10 +265,10 @@ public boolean teleportToPlayer(CommandSender sender, String command, String[] r
if (override)
player.sendMessage(ChatColor.RED + "Forced tp!");
- plugin.getLogger().info("[TweakcraftUtils] " + player.getName() + " teleported to " + p.getName() + "!");
+ plugin.getLogger().info(player.getName() + " teleported to " + p.getName() + "!");
} else {
p.sendMessage(player.getDisplayName() + ChatColor.LIGHT_PURPLE + " failed to teleport to you!");
- plugin.getLogger().info("[TweakcraftUtils] " + player.getName() + " failed to teleport to " + p.getName() + "!");
+ plugin.getLogger().info(player.getName() + " failed to teleport to " + p.getName() + "!");
// This doesn't happen, CraftBukkit has return true; in it's sources!
}
}
@@ -337,7 +337,7 @@ private void tpfromto(TweakcraftUtils plugin, CommandSender sender, String p1, S
} else {
player = "CONSOLE";
}
- plugin.getLogger().info("[TweakcraftUtils] " + player + " teleported " + pfrom.getName() + " to " + pto.getName() + "!");
+ plugin.getLogger().info(player + " teleported " + pfrom.getName() + " to " + pto.getName() + "!");
plugin.getTelehistory().addHistory(pfrom.getName(), pfrom.getLocation());
pfrom.teleport(pto);
}
Oops, something went wrong.

0 comments on commit 109c62e

Please sign in to comment.