Browse Source

Protect our hashmaps from bad people.

GJ 13 years ago
parent
commit
290032646f

+ 61 - 2
src/main/java/com/gmail/nossr50/McMMO.java

@@ -86,8 +86,8 @@ public class McMMO extends JavaPlugin {
     private final WorldListener worldListener = new WorldListener();
     private final HardcoreListener hardcoreListener = new HardcoreListener();
 
-    public HashMap<String, String> aliasMap = new HashMap<String, String>(); //Alias - Command
-    public HashMap<Integer, Player> tntTracker = new HashMap<Integer, Player>();
+    private HashMap<String, String> aliasMap = new HashMap<String, String>(); //Alias - Command
+    private HashMap<Integer, String> tntTracker = new HashMap<Integer, String>();
 
     public static File versionFile;
     public static Database database;
@@ -425,4 +425,63 @@ public class McMMO extends JavaPlugin {
 
         getCommand("mchud").setExecutor(new MchudCommand(this));
     }
+
+    /**
+     * 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);
+    }
 }

+ 5 - 5
src/main/java/com/gmail/nossr50/listeners/EntityListener.java

@@ -188,8 +188,8 @@ public class EntityListener implements Listener {
         if (entity instanceof TNTPrimed) {
             int id = entity.getEntityId();
 
-            if (plugin.tntTracker.containsKey(id)) {
-                Player player = plugin.tntTracker.get(id);
+            if (plugin.tntIsTracked(id)) {
+                Player player = plugin.getTNTPlayer(id);
 
                 if (Permissions.getInstance().biggerBombs(player)) {
                     BlastMining.biggerBombs(player, event);
@@ -210,10 +210,10 @@ public class EntityListener implements Listener {
         if (event.getEntity() instanceof TNTPrimed) {
             int id = entity.getEntityId();
 
-            if (plugin.tntTracker.containsKey(id)) {
-                Player player = plugin.tntTracker.get(id);
+            if (plugin.tntIsTracked(id)) {
+                Player player = plugin.getTNTPlayer(id);
                 BlastMining.dropProcessing(player, event);
-                plugin.tntTracker.remove(id);
+                plugin.removeFromTNTTracker(id);
             }
         }
     }

+ 3 - 3
src/main/java/com/gmail/nossr50/listeners/PlayerListener.java

@@ -373,13 +373,13 @@ public class PlayerListener implements Listener {
         String command = message.substring(1).split(" ")[0];
         String lowerCaseCommand = command.toLowerCase();
 
-        if (plugin.aliasMap.containsKey(lowerCaseCommand)) {
+        if (plugin.commandIsAliased(lowerCaseCommand)) {
             //We should find a better way to avoid string replacement where the alias is equals to the command
-            if (command.equals(plugin.aliasMap.get(lowerCaseCommand))) {
+            if (command.equals(plugin.getCommandAlias(lowerCaseCommand))) {
                 return;
             }
 
-            event.setMessage(message.replace(command, plugin.aliasMap.get(lowerCaseCommand)));
+            event.setMessage(message.replace(command, plugin.getCommandAlias(lowerCaseCommand)));
         }
     }
 }

+ 1 - 1
src/main/java/com/gmail/nossr50/skills/gathering/BlastMining.java

@@ -296,7 +296,7 @@ public class BlastMining {
 
         /* Create the TNT entity */
         TNTPrimed tnt = player.getWorld().spawn(block.getLocation(), TNTPrimed.class);
-        plugin.tntTracker.put(tnt.getEntityId(), player);
+        plugin.addToTNTTracker(tnt.getEntityId(), player.getName());
         tnt.setFuseTicks(0);
 
         /* Disable the original one */