Explorar el Código

Cleanup listeners a bit more. Also fix bug in recent dev builds where
placed blocks were not properly tracked.

GJ hace 12 años
padre
commit
a7be57241c

+ 40 - 45
src/main/java/com/gmail/nossr50/listeners/BlockListener.java

@@ -32,6 +32,7 @@ import com.gmail.nossr50.skills.Skills;
 import com.gmail.nossr50.skills.ToolType;
 import com.gmail.nossr50.skills.excavation.Excavation;
 import com.gmail.nossr50.skills.herbalism.Herbalism;
+import com.gmail.nossr50.skills.mining.Mining;
 import com.gmail.nossr50.skills.mining.MiningManager;
 import com.gmail.nossr50.skills.repair.Repair;
 import com.gmail.nossr50.skills.repair.Salvage;
@@ -97,7 +98,7 @@ public class BlockListener implements Listener {
     /**
      * Monitor BlockPlace events.
      *
-     * @param event The event to monitor
+     * @param event The event to watch
      */
     @EventHandler(priority = EventPriority.MONITOR, ignoreCancelled = true)
     public void onBlockPlace(BlockPlaceEvent event) {
@@ -114,18 +115,16 @@ public class BlockListener implements Listener {
             mcMMO.placeStore.setTrue(block);
         }
 
-        if (Config.getInstance().getRepairAnvilMessagesEnabled()) {
-            int id = block.getTypeId();
+        if (Repair.anvilMessagesEnabled) {
+            int blockID = block.getTypeId();
 
-            if (id == Config.getInstance().getRepairAnvilId()) {
-                Repair.placedAnvilCheck(player, id);
+            if (blockID == Repair.anvilID) {
+                Repair.placedAnvilCheck(player, blockID);
             }
-            else if (id == Config.getInstance().getSalvageAnvilId()) {
-                Salvage.placedAnvilCheck(player, id);
+            else if (blockID == Salvage.anvilID) {
+                Salvage.placedAnvilCheck(player, blockID);
             }
         }
-
-
     }
 
     /**
@@ -140,58 +139,54 @@ public class BlockListener implements Listener {
         }
 
         Player player = event.getPlayer();
-
-        if (player.hasMetadata("NPC")) return; // Check if this player is a Citizens NPC
-
         PlayerProfile profile = Users.getProfile(player);
 
-        if (profile == null) {
+        if (Misc.isNPCPlayer(player, profile)) {
             return;
         }
 
         Block block = event.getBlock();
-        ItemStack inHand = player.getItemInHand();
-
-        Config configInstance = Config.getInstance();
+        ItemStack heldItem = player.getItemInHand();
 
         /* HERBALISM */
         if (BlockChecks.canBeGreenTerra(block)) {
-            /* Green Terra */
-            if (profile.getToolPreparationMode(ToolType.HOE) && Permissions.greenTerra(player)) {
-                Skills.abilityCheck(player, SkillType.HERBALISM);
-            }
-
-            /* Triple drops */
-            if (profile.getAbilityMode(AbilityType.GREEN_TERRA)) {
-                Herbalism.herbalismProcCheck(block, player, event, plugin);
-            }
+            Skills.abilityCheck(player, SkillType.HERBALISM); //Green Terra
 
+            /*
+             * We don't check the block store here because herbalism has too many unusual edge cases.
+             * Instead, we check it inside the drops handler.
+             */
             if (Permissions.herbalism(player)) {
-                Herbalism.herbalismProcCheck(block, player, event, plugin);
+                Herbalism.herbalismProcCheck(block, player, event, plugin); //Double drops
+
+                if (profile.getAbilityMode(AbilityType.GREEN_TERRA)) {
+                    Herbalism.herbalismProcCheck(block, player, event, plugin); //Triple drops
+                }
             }
         }
 
         /* MINING */
-        else if (BlockChecks.canBeSuperBroken(block) && Permissions.mining(player)) {
-            MiningManager miningManager = new MiningManager(player);
-            if (configInstance.getMiningRequiresTool()) {
-                if (ItemChecks.isPickaxe(inHand)) {
+        else if (BlockChecks.canBeSuperBroken(block) && Permissions.mining(player) && !mcMMO.placeStore.isTrue(block)) {
+            if (Mining.requiresTool) {
+                if (ItemChecks.isPickaxe(heldItem)) {
+                    MiningManager miningManager = new MiningManager(player);
                     miningManager.miningBlockCheck(block);
                 }
             }
             else {
+                MiningManager miningManager = new MiningManager(player);
                 miningManager.miningBlockCheck(block);
             }
         }
 
         /* WOOD CUTTING */
         else if (BlockChecks.isLog(block) && Permissions.woodcutting(player) && !mcMMO.placeStore.isTrue(block)) {
-            if (profile.getAbilityMode(AbilityType.TREE_FELLER) && Permissions.treeFeller(player) && ItemChecks.isAxe(inHand)) {
+            if (profile.getAbilityMode(AbilityType.TREE_FELLER) && Permissions.treeFeller(player) && ItemChecks.isAxe(heldItem)) {
                 Woodcutting.beginTreeFeller(event);
             }
             else {
-                if (configInstance.getWoodcuttingRequiresTool()) {
-                    if (ItemChecks.isAxe(inHand)) {
+                if (Woodcutting.requiresTool) {
+                    if (ItemChecks.isAxe(heldItem)) {
                         Woodcutting.beginWoodcutting(player, block);
                     }
                 }
@@ -203,8 +198,8 @@ public class BlockListener implements Listener {
 
         /* EXCAVATION */
         else if (BlockChecks.canBeGigaDrillBroken(block) && Permissions.excavation(player) && !mcMMO.placeStore.isTrue(block)) {
-            if (configInstance.getExcavationRequiresTool()) {
-                if (ItemChecks.isShovel(inHand)) {
+            if (Excavation.requiresTool) {
+                if (ItemChecks.isShovel(heldItem)) {
                     Excavation.excavationProcCheck(block, player);
                 }
             }
@@ -213,12 +208,17 @@ public class BlockListener implements Listener {
             }
         }
 
-        //Remove metadata when broken
-        if (BlockChecks.shouldBeWatched(block)) {
+        /* Remove metadata from placed watched blocks */
+        if (BlockChecks.shouldBeWatched(block) && mcMMO.placeStore.isTrue(block)) {
             mcMMO.placeStore.setFalse(block);
         }
     }
 
+    /**
+     * Handle BlockBreak events where the event is modified.
+     *
+     * @param event The event to modify
+     */
     @EventHandler(priority = EventPriority.HIGHEST, ignoreCancelled = true)
     public void onBlockBreakHigher(BlockBreakEvent event) {
         if (event instanceof FakeBlockBreakEvent) {
@@ -227,21 +227,16 @@ public class BlockListener implements Listener {
 
         Player player = event.getPlayer();
         Block block = event.getBlock();
-        ItemStack inHand = player.getItemInHand();
+        ItemStack heldItem = player.getItemInHand();
 
         if (Misc.isNPCPlayer(player)) {
             return;
         }
 
-        if (mcMMO.placeStore.isTrue(block)) {
-            mcMMO.placeStore.setFalse(block);
-            return;
-        }
-
-        if (Permissions.hylianLuck(player) && ItemChecks.isSword(inHand)) {
+        if (Permissions.hylianLuck(player) && ItemChecks.isSword(heldItem)) {
             Herbalism.hylianLuck(block, player, event);
         }
-        else if (BlockChecks.canBeFluxMined(block) && ItemChecks.isPickaxe(inHand) && !inHand.containsEnchantment(Enchantment.SILK_TOUCH)) {
+        else if (BlockChecks.canBeFluxMined(block) && ItemChecks.isPickaxe(heldItem) && !heldItem.containsEnchantment(Enchantment.SILK_TOUCH) && Permissions.fluxMining(player) && !mcMMO.placeStore.isTrue(block)) {
             SmeltingManager smeltingManager = new SmeltingManager(player);
             smeltingManager.fluxMining(event);
         }

+ 7 - 15
src/main/java/com/gmail/nossr50/skills/Skills.java

@@ -422,19 +422,15 @@ public class Skills {
      */
     public static void abilityCheck(Player player, SkillType type) {
         PlayerProfile profile = Users.getProfile(player);
-        if (profile == null) {
-            return;
-        }
         ToolType tool = type.getTool();
+        AbilityType ability = type.getAbility();
 
-        if (!profile.getToolPreparationMode(tool)) {
+        if (!profile.getToolPreparationMode(tool) || !ability.getPermissions(player)) {
             return;
         }
 
         profile.setToolPreparationMode(tool, false);
 
-        AbilityType ability = type.getAbility();
-
         /* Axes and Woodcutting are odd because they share the same tool.
          * We show them the too tired message when they take action.
          */
@@ -532,15 +528,11 @@ public class Skills {
      * @param xp the amount of XP to gain
      */
     public static void xpProcessing(Player player, PlayerProfile profile, SkillType type, int xp) {
-        if (type.getPermissions(player)) {
-            if (Users.getPlayer(player) == null)
-                return;
-
-            if ((type.getMaxLevel() < profile.getSkillLevel(type) + 1) || (Misc.getPowerLevelCap() < Users.getPlayer(player).getPowerLevel() + 1))
-                return;
-
-            Users.getPlayer(player).addXP(type, xp);
-            xpCheckSkill(type, player, profile);
+        if ((type.getMaxLevel() < profile.getSkillLevel(type) + 1) || (Misc.getPowerLevelCap() < Users.getPlayer(player).getPowerLevel() + 1)) {
+            return;
         }
+
+        Users.getPlayer(player).addXP(type, xp);
+        xpCheckSkill(type, player, profile);
     }
 }

+ 1 - 0
src/main/java/com/gmail/nossr50/skills/excavation/Excavation.java

@@ -27,6 +27,7 @@ import com.gmail.nossr50.util.Permissions;
 import com.gmail.nossr50.util.Users;
 
 public class Excavation {
+    public static boolean requiresTool = Config.getInstance().getExcavationRequiresTool();
 
     /**
      * Check to see if treasures were found.

+ 6 - 9
src/main/java/com/gmail/nossr50/skills/herbalism/Herbalism.java

@@ -106,9 +106,6 @@ public class Herbalism {
      * @param plugin mcMMO plugin instance
      */
     public static void herbalismProcCheck(final Block block, Player player, BlockBreakEvent event, mcMMO plugin) {
-        if (player == null)
-            return;
-
         final PlayerProfile profile = Users.getProfile(player);
 
         int herbLevel = profile.getSkillLevel(SkillType.HERBALISM);
@@ -557,7 +554,7 @@ public class Herbalism {
         if (chance > Misc.getRandom().nextInt(activationChance)) {
             Location location = block.getLocation();
             int dropNumber = Misc.getRandom().nextInt(3);
-            ItemStack item = null;
+            ItemStack item;
 
             switch (block.getType()) {
             case DEAD_BUSH:
@@ -584,6 +581,11 @@ public class Herbalism {
 
             case RED_ROSE:
             case YELLOW_FLOWER:
+                if (mcMMO.placeStore.isTrue(block)) {
+                    mcMMO.placeStore.setFalse(block);
+                    return;
+                }
+
                 if (dropNumber == 0) {
                     item = new ItemStack(Material.POTATO);
                 }
@@ -594,7 +596,6 @@ public class Herbalism {
                     item = new ItemStack(Material.APPLE);
                 }
 
-                mcMMO.placeStore.setFalse(block);
                 break;
 
             case FLOWER_POT:
@@ -610,10 +611,6 @@ public class Herbalism {
                 break;
 
             default:
-                break;
-            }
-
-            if (item == null) {
                 return;
             }
 

+ 2 - 0
src/main/java/com/gmail/nossr50/skills/mining/Mining.java

@@ -26,6 +26,8 @@ public class Mining {
     public static double doubleDropsMaxChance = advancedConfig.getMiningDoubleDropChance();
     public static boolean doubleDropsDisabled = config.miningDoubleDropsDisabled();
 
+    public static boolean requiresTool = Config.getInstance().getMiningRequiresTool();
+
     public static final int DIAMOND_TOOL_TIER = 4;
     public static final int IRON_TOOL_TIER = 3;
     public static final int STONE_TOOL_TIER = 2;

+ 0 - 5
src/main/java/com/gmail/nossr50/skills/mining/MiningManager.java

@@ -112,12 +112,7 @@ public class MiningManager extends SkillManager{
      * @param block The block being broken
      */
     public void miningBlockCheck(Block block) {
-        if (mcMMO.placeStore.isTrue(block)) {
-            return;
-        }
-
         MiningBlockEventHandler eventHandler = new MiningBlockEventHandler(this, block);
-
         eventHandler.processXPGain();
 
         if (!Permissions.miningDoubleDrops(player)) {

+ 1 - 0
src/main/java/com/gmail/nossr50/skills/repair/Repair.java

@@ -34,6 +34,7 @@ public class Repair {
     public static boolean arcaneForgingDowngrades = advancedConfig.getArcaneForgingDowngradeEnabled();
     public static boolean arcaneForgingEnchantLoss = advancedConfig.getArcaneForgingEnchantLossEnabled();
 
+    public static boolean anvilMessagesEnabled = Config.getInstance().getRepairAnvilMessagesEnabled();
     public static int anvilID = Config.getInstance().getRepairAnvilId();
 
     /**

+ 10 - 3
src/main/java/com/gmail/nossr50/skills/smelting/FluxMiningEventHandler.java

@@ -3,6 +3,7 @@ package com.gmail.nossr50.skills.smelting;
 import org.bukkit.Location;
 import org.bukkit.Material;
 import org.bukkit.block.Block;
+import org.bukkit.entity.Player;
 import org.bukkit.event.block.BlockBreakEvent;
 import org.bukkit.inventory.ItemStack;
 
@@ -10,14 +11,17 @@ import com.gmail.nossr50.locale.LocaleLoader;
 import com.gmail.nossr50.skills.SkillType;
 import com.gmail.nossr50.skills.mining.Mining;
 import com.gmail.nossr50.util.Misc;
+import com.gmail.nossr50.util.Permissions;
 
 public class FluxMiningEventHandler {
     private SmeltingManager manager;
+    private Player player;
     private BlockBreakEvent event;
     private Block block;
 
     protected FluxMiningEventHandler(SmeltingManager manager, BlockBreakEvent event) {
         this.manager = manager;
+        this.player = manager.getPlayer();
         this.event = event;
         this.block = event.getBlock();
     }
@@ -43,9 +47,12 @@ public class FluxMiningEventHandler {
         }
 
         Location location = block.getLocation();
-        int chance = (int) ((Mining.doubleDropsMaxChance / Mining.doubleDropsMaxLevel) * (Misc.skillCheck(manager.getProfile().getSkillLevel(SkillType.MINING), Mining.doubleDropsMaxLevel)));
         Misc.dropItem(location, item);
-        Misc.randomDropItem(location, item, chance);
+
+        if (Permissions.secondSmelt(player)) {
+            int chance = (int) ((Mining.doubleDropsMaxChance / Mining.doubleDropsMaxLevel) * (Misc.skillCheck(manager.getProfile().getSkillLevel(SkillType.MINING), Mining.doubleDropsMaxLevel)));
+            Misc.randomDropItem(location, item, chance);
+        }
     }
 
     protected void eventCancellationAndProcessing() {
@@ -54,6 +61,6 @@ public class FluxMiningEventHandler {
     }
 
     protected void sendAbilityMessage() {
-        manager.getPlayer().sendMessage(LocaleLoader.getString("Smelting.FluxMining.Success"));
+        player.sendMessage(LocaleLoader.getString("Smelting.FluxMining.Success"));
     }
 }

+ 1 - 1
src/main/java/com/gmail/nossr50/skills/smelting/SmeltingManager.java

@@ -56,7 +56,7 @@ public class SmeltingManager extends SkillManager {
     }
 
     public void fluxMining(BlockBreakEvent event) {
-        if (skillLevel < Smelting.fluxMiningUnlockLevel || !Permissions.fluxMining(player)) {
+        if (skillLevel < Smelting.fluxMiningUnlockLevel) {
             return;
         }
 

+ 2 - 0
src/main/java/com/gmail/nossr50/skills/woodcutting/Woodcutting.java

@@ -29,6 +29,8 @@ public abstract class Woodcutting {
     public static final boolean DOUBLE_DROP_DISABLED = Config.getInstance().woodcuttingDoubleDropsDisabled();
     public static final int TREE_FELLER_THRESHOLD = Config.getInstance().getTreeFellerThreshold();
 
+    public static boolean requiresTool = Config.getInstance().getWoodcuttingRequiresTool();
+
     /**
      * Begins the Tree Feller ability
      *