From 08960a2dbc98471399ec724e36277d142434f057 Mon Sep 17 00:00:00 2001 From: GJ Date: Wed, 6 Mar 2013 10:02:34 -0500 Subject: [PATCH] Move Salvage checks into our RepairManager --- .../nossr50/listeners/BlockListener.java | 12 +--- .../nossr50/listeners/PlayerListener.java | 2 +- .../nossr50/skills/repair/RepairManager.java | 66 ++++++++++++++++- .../gmail/nossr50/skills/repair/Salvage.java | 72 +------------------ 4 files changed, 69 insertions(+), 83 deletions(-) diff --git a/src/main/java/com/gmail/nossr50/listeners/BlockListener.java b/src/main/java/com/gmail/nossr50/listeners/BlockListener.java index 4bd87939a..608dc2179 100644 --- a/src/main/java/com/gmail/nossr50/listeners/BlockListener.java +++ b/src/main/java/com/gmail/nossr50/listeners/BlockListener.java @@ -35,8 +35,6 @@ import com.gmail.nossr50.skills.excavation.ExcavationManager; import com.gmail.nossr50.skills.herbalism.HerbalismManager; import com.gmail.nossr50.skills.mining.MiningManager; import com.gmail.nossr50.skills.repair.Repair; -import com.gmail.nossr50.skills.repair.RepairManager; -import com.gmail.nossr50.skills.repair.Salvage; import com.gmail.nossr50.skills.smelting.SmeltingManager; import com.gmail.nossr50.skills.unarmed.Unarmed; import com.gmail.nossr50.skills.woodcutting.Woodcutting; @@ -117,15 +115,7 @@ public class BlockListener implements Listener { } if (Repair.anvilMessagesEnabled) { - RepairManager repairManager = UserManager.getPlayer(player).getRepairManager(); - int blockID = blockState.getTypeId(); - - if (blockID == Repair.anvilID) { - repairManager.placedAnvilCheck(blockID); - } - else if (blockID == Salvage.anvilID) { - Salvage.placedAnvilCheck(player, blockID); - } + UserManager.getPlayer(player).getRepairManager().placedAnvilCheck(blockState.getTypeId()); } } diff --git a/src/main/java/com/gmail/nossr50/listeners/PlayerListener.java b/src/main/java/com/gmail/nossr50/listeners/PlayerListener.java index 837794269..d335c10b8 100644 --- a/src/main/java/com/gmail/nossr50/listeners/PlayerListener.java +++ b/src/main/java/com/gmail/nossr50/listeners/PlayerListener.java @@ -292,7 +292,7 @@ public class PlayerListener implements Listener { } /* SALVAGE CHECKS */ else if (blockID == Salvage.anvilID && Permissions.salvage(player) && Salvage.isSalvageable(heldItem)) { - Salvage.handleSalvage(player, block.getLocation(), heldItem); + UserManager.getPlayer(player).getRepairManager().handleSalvage(block.getLocation(), heldItem); event.setCancelled(true); player.updateInventory(); } diff --git a/src/main/java/com/gmail/nossr50/skills/repair/RepairManager.java b/src/main/java/com/gmail/nossr50/skills/repair/RepairManager.java index e3f509960..f3b07dc06 100644 --- a/src/main/java/com/gmail/nossr50/skills/repair/RepairManager.java +++ b/src/main/java/com/gmail/nossr50/skills/repair/RepairManager.java @@ -3,6 +3,8 @@ package com.gmail.nossr50.skills.repair; import java.util.Map; import java.util.Map.Entry; +import org.bukkit.GameMode; +import org.bukkit.Location; import org.bukkit.Material; import org.bukkit.Sound; import org.bukkit.enchantments.Enchantment; @@ -13,6 +15,7 @@ import org.getspout.spoutapi.SpoutManager; import org.getspout.spoutapi.player.SpoutPlayer; import com.gmail.nossr50.mcMMO; +import com.gmail.nossr50.config.Config; import com.gmail.nossr50.datatypes.player.McMMOPlayer; import com.gmail.nossr50.datatypes.skills.SkillType; import com.gmail.nossr50.events.skills.repair.McMMOPlayerRepairCheckEvent; @@ -29,12 +32,21 @@ public class RepairManager extends SkillManager { super(mcMMOPlayer, SkillType.REPAIR); } + public void placedAnvilCheck(int anvilId) { + if (anvilId == Repair.anvilID) { + repairAnvilCheck(anvilId); + } + else if (anvilId == Salvage.anvilID) { + salvageAnvilCheck(anvilId); + } + } + /** * Handles notifications for placing an anvil. * * @param anvilID The item ID of the anvil block */ - public void placedAnvilCheck(int anvilID) { + public void repairAnvilCheck(int anvilID) { Player player = getPlayer(); if (mcMMOPlayer.getPlacedAnvil()) { @@ -56,6 +68,32 @@ public class RepairManager extends SkillManager { mcMMOPlayer.togglePlacedAnvil(); } + /** + * Handles notifications for placing an anvil. + * + * @param player The player placing the anvil + * @param anvilID The item ID of the anvil block + */ + public void salvageAnvilCheck(int anvilID) { + Player player = getPlayer(); + + if (!mcMMOPlayer.getPlacedSalvageAnvil()) { + if (mcMMO.spoutEnabled) { + final SpoutPlayer spoutPlayer = SpoutManager.getPlayer(player); + + if (spoutPlayer.isSpoutCraftEnabled()) { + spoutPlayer.sendNotification("[mcMMO] Anvil Placed", "Right click to salvage!", Material.getMaterial(anvilID)); + } + } + else { + player.sendMessage(LocaleLoader.getString("Repair.Listener.Anvil2")); + } + + player.playSound(player.getLocation(), Sound.ANVIL_LAND, Misc.ANVIL_USE_VOLUME, Misc.ANVIL_USE_PITCH); + mcMMOPlayer.togglePlacedSalvageAnvil(); + } + } + public void handleRepair(ItemStack item) { Player player = getPlayer(); int itemId = item.getTypeId(); @@ -166,6 +204,32 @@ public class RepairManager extends SkillManager { item.setDurability(newDurability); } + public void handleSalvage(Location location, ItemStack item) { + Player player = getPlayer(); + + if (!Config.getInstance().getSalvageEnabled() || player.getGameMode() != GameMode.SURVIVAL) { + return; + } + + if (getSkillLevel() < Salvage.salvageUnlockLevel) { + player.sendMessage(LocaleLoader.getString("Repair.Skills.AdeptSalvage")); + return; + } + + if (item.getDurability() == 0) { + player.setItemInHand(new ItemStack(Material.AIR)); + location.setY(location.getY() + 1); + + Misc.dropItems(location, new ItemStack(Salvage.getSalvagedItem(item)), Salvage.getSalvagedAmount(item)); + + player.playSound(player.getLocation(), Sound.ANVIL_USE, Misc.ANVIL_USE_VOLUME, Misc.ANVIL_USE_PITCH); + player.sendMessage(LocaleLoader.getString("Repair.Skills.SalvageSuccess")); + } + else { + player.sendMessage(LocaleLoader.getString("Repair.Skills.NotFullDurability")); + } + } + /** * Gets the Arcane Forging rank * diff --git a/src/main/java/com/gmail/nossr50/skills/repair/Salvage.java b/src/main/java/com/gmail/nossr50/skills/repair/Salvage.java index 912abf387..9a1d32f27 100644 --- a/src/main/java/com/gmail/nossr50/skills/repair/Salvage.java +++ b/src/main/java/com/gmail/nossr50/skills/repair/Salvage.java @@ -1,83 +1,15 @@ package com.gmail.nossr50.skills.repair; -import org.bukkit.GameMode; -import org.bukkit.Location; import org.bukkit.Material; -import org.bukkit.Sound; -import org.bukkit.entity.Player; import org.bukkit.inventory.ItemStack; -import org.getspout.spoutapi.SpoutManager; -import org.getspout.spoutapi.player.SpoutPlayer; -import com.gmail.nossr50.mcMMO; import com.gmail.nossr50.config.Config; -import com.gmail.nossr50.datatypes.player.McMMOPlayer; -import com.gmail.nossr50.datatypes.skills.SkillType; -import com.gmail.nossr50.locale.LocaleLoader; import com.gmail.nossr50.util.ItemUtils; -import com.gmail.nossr50.util.Misc; -import com.gmail.nossr50.util.player.UserManager; public class Salvage { public static int salvageUnlockLevel = Config.getInstance().getSalvageUnlockLevel(); public static int anvilID = Config.getInstance().getSalvageAnvilId(); - public static void handleSalvage(final Player player, final Location location, final ItemStack item) { - if (!Config.getInstance().getSalvageEnabled()) { - return; - } - - if (player.getGameMode() == GameMode.SURVIVAL) { - final int skillLevel = UserManager.getPlayer(player).getProfile().getSkillLevel(SkillType.REPAIR); - - if (skillLevel < salvageUnlockLevel) { - player.sendMessage(LocaleLoader.getString("Repair.Skills.AdeptSalvage")); - return; - } - - final float currentDurability = item.getDurability(); - - if (currentDurability == 0) { - player.setItemInHand(new ItemStack(Material.AIR)); - location.setY(location.getY() + 1); - - Misc.dropItems(location, new ItemStack(getSalvagedItem(item)), getSalvagedAmount(item)); - - player.playSound(player.getLocation(), Sound.ANVIL_USE, Misc.ANVIL_USE_VOLUME, Misc.ANVIL_USE_PITCH); - player.sendMessage(LocaleLoader.getString("Repair.Skills.SalvageSuccess")); - } - else { - player.sendMessage(LocaleLoader.getString("Repair.Skills.NotFullDurability")); - } - } - } - - /** - * Handles notifications for placing an anvil. - * - * @param player The player placing the anvil - * @param anvilID The item ID of the anvil block - */ - public static void placedAnvilCheck(final Player player, final int anvilID) { - McMMOPlayer mcMMOPlayer = UserManager.getPlayer(player); - - if (!mcMMOPlayer.getPlacedSalvageAnvil()) { - if (mcMMO.spoutEnabled) { - final SpoutPlayer spoutPlayer = SpoutManager.getPlayer(player); - - if (spoutPlayer.isSpoutCraftEnabled()) { - spoutPlayer.sendNotification("[mcMMO] Anvil Placed", "Right click to salvage!", Material.getMaterial(anvilID)); - } - } - else { - player.sendMessage(LocaleLoader.getString("Repair.Listener.Anvil2")); - } - - player.playSound(player.getLocation(), Sound.ANVIL_LAND, Misc.ANVIL_USE_VOLUME, Misc.ANVIL_USE_PITCH); - mcMMOPlayer.togglePlacedSalvageAnvil(); - } - } - /** * Checks if the item is salvageable. * @@ -96,7 +28,7 @@ public class Salvage { return false; } - private static Material getSalvagedItem(final ItemStack inHand) { + protected static Material getSalvagedItem(final ItemStack inHand) { if (ItemUtils.isDiamondTool(inHand) || ItemUtils.isDiamondArmor(inHand)) { return Material.DIAMOND; } @@ -123,7 +55,7 @@ public class Salvage { } } - private static int getSalvagedAmount(final ItemStack inHand) { + protected static int getSalvagedAmount(final ItemStack inHand) { if (ItemUtils.isPickaxe(inHand) || ItemUtils.isAxe(inHand) || inHand.getType() == Material.BOW || inHand.getType() == Material.BUCKET) { return 3; }