From 0f023f627c7e84fe85db8032f0c4d9d158eb5c87 Mon Sep 17 00:00:00 2001 From: GJ Date: Tue, 2 Apr 2013 08:41:51 -0400 Subject: [PATCH] Metadata > Hashmap --- .../nossr50/listeners/EntityListener.java | 20 ++++---- src/main/java/com/gmail/nossr50/mcMMO.java | 50 ++----------------- .../nossr50/skills/mining/MiningManager.java | 3 +- 3 files changed, 17 insertions(+), 56 deletions(-) diff --git a/src/main/java/com/gmail/nossr50/listeners/EntityListener.java b/src/main/java/com/gmail/nossr50/listeners/EntityListener.java index 352e72dc5..c8a94b6cc 100644 --- a/src/main/java/com/gmail/nossr50/listeners/EntityListener.java +++ b/src/main/java/com/gmail/nossr50/listeners/EntityListener.java @@ -317,14 +317,15 @@ public class EntityListener implements Listener { public void onExplosionPrime(ExplosionPrimeEvent event) { Entity entity = event.getEntity(); - if (entity instanceof TNTPrimed) { - int id = entity.getEntityId(); + if (entity instanceof TNTPrimed && entity.hasMetadata(mcMMO.tntMetadataKey)) { + // We can make this assumption because we (should) be the only ones using this exact metadata + Player player = plugin.getServer().getPlayer(entity.getMetadata(mcMMO.tntMetadataKey).get(0).asString()); - if (!plugin.tntIsTracked(id)) { + if (Misc.isNPCEntity(player)) { return; } - MiningManager miningManager = UserManager.getPlayer(plugin.getTNTPlayer(id)).getMiningManager(); + MiningManager miningManager = UserManager.getPlayer(player).getMiningManager(); if (miningManager.canUseBiggerBombs()) { event.setRadius(miningManager.biggerBombs(event.getRadius())); @@ -341,21 +342,20 @@ public class EntityListener implements Listener { public void onEnitityExplode(EntityExplodeEvent event) { Entity entity = event.getEntity(); - if (entity instanceof TNTPrimed) { - int id = entity.getEntityId(); + if (entity instanceof TNTPrimed && entity.hasMetadata(mcMMO.tntMetadataKey)) { + // We can make this assumption because we (should) be the only ones using this exact metadata + Player player = plugin.getServer().getPlayer(entity.getMetadata(mcMMO.tntMetadataKey).get(0).asString()); - if (!plugin.tntIsTracked(id)) { + if (Misc.isNPCEntity(player)) { return; } - MiningManager miningManager = UserManager.getPlayer(plugin.getTNTPlayer(id)).getMiningManager(); + MiningManager miningManager = UserManager.getPlayer(player).getMiningManager(); if (miningManager.canUseBlastMining()) { miningManager.blastMiningDropProcessing(event.getYield(), event.blockList()); event.setYield(0); } - - plugin.removeFromTNTTracker(id); } } diff --git a/src/main/java/com/gmail/nossr50/mcMMO.java b/src/main/java/com/gmail/nossr50/mcMMO.java index 9f99bbfdc..6ed786522 100644 --- a/src/main/java/com/gmail/nossr50/mcMMO.java +++ b/src/main/java/com/gmail/nossr50/mcMMO.java @@ -3,7 +3,6 @@ package com.gmail.nossr50; import java.io.File; import java.io.IOException; import java.util.ArrayList; -import java.util.HashMap; import java.util.List; import net.shatteredlands.shatt.backup.ZipLibrary; @@ -65,8 +64,6 @@ public class mcMMO extends JavaPlugin { private final SelfListener selfListener = new SelfListener(); private final WorldListener worldListener = new WorldListener(); - private HashMap tntTracker = new HashMap(); - public static mcMMO p; public static ChunkManager placeStore; @@ -91,10 +88,12 @@ public class mcMMO extends JavaPlugin { private boolean xpEventEnabled = false; // Metadata Values - public static FixedMetadataValue metadataValue; - public final static String entityMetadataKey = "mcMMO: Spawned Entity"; - public final static String blockMetadataKey = "mcMMO: Piston Tracking"; + public final static String entityMetadataKey = "mcMMO: Spawned Entity"; + public final static String blockMetadataKey = "mcMMO: Piston Tracking"; public final static String furnaceMetadataKey = "mcMMO: Tracked Furnace"; + public final static String tntMetadataKey = "mcMMO: Tracked TNT"; + + public static FixedMetadataValue metadataValue; /** * Things to be run when the plugin is enabled. @@ -200,45 +199,6 @@ public class mcMMO extends JavaPlugin { getLogger().info("Was disabled."); // How informative! } - /** - * 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); - } - public static String getMainDirectory() { return mainDirectory; } diff --git a/src/main/java/com/gmail/nossr50/skills/mining/MiningManager.java b/src/main/java/com/gmail/nossr50/skills/mining/MiningManager.java index 6e8151259..cd985ce97 100644 --- a/src/main/java/com/gmail/nossr50/skills/mining/MiningManager.java +++ b/src/main/java/com/gmail/nossr50/skills/mining/MiningManager.java @@ -10,6 +10,7 @@ import org.bukkit.block.BlockState; import org.bukkit.enchantments.Enchantment; import org.bukkit.entity.Player; import org.bukkit.entity.TNTPrimed; +import org.bukkit.metadata.FixedMetadataValue; import com.gmail.nossr50.mcMMO; import com.gmail.nossr50.config.Config; @@ -100,7 +101,7 @@ public class MiningManager extends SkillManager{ SkillUtils.sendSkillMessage(player, AbilityType.BLAST_MINING.getAbilityPlayer(player)); player.sendMessage(LocaleLoader.getString("Mining.Blast.Boom")); - mcMMO.p.addToTNTTracker(tnt.getEntityId(), player.getName()); + tnt.setMetadata(mcMMO.tntMetadataKey, new FixedMetadataValue(mcMMO.p, player.getName())); tnt.setFuseTicks(0); targetBlock.setData((byte) 0x0); targetBlock.setType(Material.AIR);