From 68a7b540c5f341608481ab11209c3a5f8cc6fb67 Mon Sep 17 00:00:00 2001 From: TfT_02 Date: Thu, 25 Apr 2013 23:29:49 +0200 Subject: [PATCH] Ask a confirmation when a player tries to repair an enchanted item --- Changelog.txt | 1 + .../java/com/gmail/nossr50/config/Config.java | 1 + .../nossr50/datatypes/player/McMMOPlayer.java | 38 +++++++++++++++++ .../nossr50/listeners/PlayerListener.java | 41 +++++++++++++++---- .../nossr50/skills/repair/RepairManager.java | 35 ++++++++++++++++ src/main/resources/config.yml | 2 + 6 files changed, 110 insertions(+), 8 deletions(-) diff --git a/Changelog.txt b/Changelog.txt index 12d15e407..647b27f2e 100644 --- a/Changelog.txt +++ b/Changelog.txt @@ -28,6 +28,7 @@ Version 1.4.06-dev ! Changed Berserk to add items to inventory rather than denying pickup ! Changed Call of the Wild, newly summoned pet's will have a custom name. (added permission node to disable this) ! Changed Chimaera Wing's recipe result to use the ingredient Material + ! Changed Repair to ask a confirmation of the player when he tries to repair an enchanted item ! Players will no longer pickup items to their hotbar while using Unarmed ! ExperienceAPI methods will now throw InvalidSkillException if the skill name passed in is invalid. ! Changed default value for recently-hurt cooldown between teleports, this is also fully configurable now diff --git a/src/main/java/com/gmail/nossr50/config/Config.java b/src/main/java/com/gmail/nossr50/config/Config.java index 482e6fce0..1b6599e20 100644 --- a/src/main/java/com/gmail/nossr50/config/Config.java +++ b/src/main/java/com/gmail/nossr50/config/Config.java @@ -208,6 +208,7 @@ public class Config extends AutoUpdateConfigLoader { public int getSalvageAnvilId() { return config.getInt("Skills.Repair.Salvage_Anvil_ID", 41); } public boolean getSalvageTools() { return config.getBoolean("Skills.Repair.Salvage_tools", true); } public boolean getSalvageArmor() { return config.getBoolean("Skills.Repair.Salvage_armor", true); } + public boolean getRepairConfirmRequired() { return config.getBoolean("Skills.Repair.Confirm_Required", true); } /* Unarmed */ public boolean getUnarmedBlockCrackerSmoothbrickToCracked() { return config.getBoolean("Skills.Unarmed.Block_Cracker.SmoothBrick_To_CrackedBrick", true); } diff --git a/src/main/java/com/gmail/nossr50/datatypes/player/McMMOPlayer.java b/src/main/java/com/gmail/nossr50/datatypes/player/McMMOPlayer.java index 1b3a8092a..edcd948d8 100644 --- a/src/main/java/com/gmail/nossr50/datatypes/player/McMMOPlayer.java +++ b/src/main/java/com/gmail/nossr50/datatypes/player/McMMOPlayer.java @@ -70,7 +70,9 @@ public class McMMOPlayer { private boolean abilityUse = true; private boolean placedRepairAnvil; + private int lastRepairClick; private boolean placedSalvageAnvil; + private int lastSalvageClick; private boolean godMode; private Map abilityMode = new HashMap(); @@ -369,6 +371,42 @@ public class McMMOPlayer { } } + /* + * Repair Anvil Usage + */ + + public int getLastAnvilUse(int anvilId) { + if (anvilId == Repair.repairAnvilId) { + return lastRepairClick; + } + + if (anvilId == Repair.salvageAnvilId) { + return lastSalvageClick; + } + + return 0; + } + + public void setLastAnvilUse(int anvilId, int value) { + if (anvilId == Repair.repairAnvilId) { + lastRepairClick = value; + } + + if (anvilId == Repair.salvageAnvilId) { + lastSalvageClick = value; + } + } + + public void actualizeLastAnvilUse(int anvilId) { + if (anvilId == Repair.repairAnvilId) { + lastRepairClick = (int) (System.currentTimeMillis() / Misc.TIME_CONVERSION_FACTOR); + } + + if (anvilId == Repair.salvageAnvilId) { + lastSalvageClick = (int) (System.currentTimeMillis() / Misc.TIME_CONVERSION_FACTOR); + } + } + /* * God Mode */ diff --git a/src/main/java/com/gmail/nossr50/listeners/PlayerListener.java b/src/main/java/com/gmail/nossr50/listeners/PlayerListener.java index 51005dadc..22431dd65 100644 --- a/src/main/java/com/gmail/nossr50/listeners/PlayerListener.java +++ b/src/main/java/com/gmail/nossr50/listeners/PlayerListener.java @@ -1,5 +1,6 @@ package com.gmail.nossr50.listeners; +import org.bukkit.ChatColor; import org.bukkit.GameMode; import org.bukkit.Material; import org.bukkit.Sound; @@ -41,6 +42,7 @@ import com.gmail.nossr50.skills.fishing.FishingManager; 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.taming.TamingManager; import com.gmail.nossr50.skills.unarmed.Unarmed; import com.gmail.nossr50.util.BlockUtils; @@ -306,6 +308,9 @@ public class PlayerListener implements Listener { @EventHandler(priority = EventPriority.LOWEST) public void onPlayerInteractLowest(PlayerInteractEvent event) { Player player = event.getPlayer(); + Block block = event.getClickedBlock(); + int blockID = block.getTypeId(); + ItemStack heldItem = player.getItemInHand(); if (Misc.isNPCEntity(player) || player.getGameMode() == GameMode.CREATIVE) { return; @@ -315,21 +320,27 @@ public class PlayerListener implements Listener { switch (event.getAction()) { case RIGHT_CLICK_BLOCK: - Block block = event.getClickedBlock(); - int blockID = block.getTypeId(); - ItemStack heldItem = player.getItemInHand(); - /* REPAIR CHECKS */ if (blockID == Repair.repairAnvilId && Permissions.skillEnabled(player, SkillType.REPAIR) && mcMMO.getRepairableManager().isRepairable(heldItem)) { - UserManager.getPlayer(player).getRepairManager().handleRepair(heldItem); + RepairManager repairManager = UserManager.getPlayer(player).getRepairManager(); event.setCancelled(true); - player.updateInventory(); + + // Make sure the player knows what he's doing when trying to repair an enchanted item + if (!(heldItem.getEnchantments().size() > 0) || repairManager.checkConfirmation(blockID, true)) { + repairManager.handleRepair(heldItem); + player.updateInventory(); + } } /* SALVAGE CHECKS */ else if (blockID == Repair.salvageAnvilId && Permissions.salvage(player) && Repair.isSalvageable(heldItem)) { - UserManager.getPlayer(player).getRepairManager().handleSalvage(block.getLocation(), heldItem); + RepairManager repairManager = UserManager.getPlayer(player).getRepairManager(); event.setCancelled(true); - player.updateInventory(); + + // Make sure the player knows what he's doing when trying to salvage an enchanted item + if (!(heldItem.getEnchantments().size() > 0) || repairManager.checkConfirmation(blockID, true)) { + repairManager.handleSalvage(block.getLocation(), heldItem); + player.updateInventory(); + } } /* BLAST MINING CHECK */ else if (miningManager.canDetonate()) { @@ -343,6 +354,20 @@ public class PlayerListener implements Listener { break; + case LEFT_CLICK_BLOCK: + /* REPAIR CHECKS */ + if (blockID == Repair.repairAnvilId && Permissions.skillEnabled(player, SkillType.REPAIR) && mcMMO.getRepairableManager().isRepairable(heldItem)) { + RepairManager repairManager = UserManager.getPlayer(player).getRepairManager(); + + // Cancel repairing an enchanted item + if (repairManager.checkConfirmation(blockID, false) && Config.getInstance().getRepairConfirmRequired()) { + UserManager.getPlayer(player).setLastAnvilUse(Repair.repairAnvilId, 0); + player.sendMessage(ChatColor.RED + "Repair cancelled!"); //TODO Locale! + } + } + + break; + case RIGHT_CLICK_AIR: /* BLAST MINING CHECK */ if (miningManager.canDetonate()) { 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 a5dd10fd4..fb261ed88 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,7 @@ package com.gmail.nossr50.skills.repair; import java.util.Map; import java.util.Map.Entry; +import org.bukkit.ChatColor; import org.bukkit.Location; import org.bukkit.Material; import org.bukkit.Sound; @@ -14,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; @@ -23,6 +25,7 @@ import com.gmail.nossr50.skills.repair.ArcaneForging.Tier; import com.gmail.nossr50.util.Misc; import com.gmail.nossr50.util.Permissions; import com.gmail.nossr50.util.StringUtils; +import com.gmail.nossr50.util.player.UserManager; import com.gmail.nossr50.util.skills.SkillUtils; public class RepairManager extends SkillManager { @@ -193,6 +196,38 @@ public class RepairManager extends SkillManager { } } + /** + * Check if the player has tried to use an Anvil before. + * + * @return true if the player has confirmed using an Anvil + */ + public boolean checkConfirmation(int anvilId, boolean actualize) { + Player player = getPlayer(); + McMMOPlayer mcMMOPlayer = UserManager.getPlayer(player); + + long LastUse = mcMMOPlayer.getLastAnvilUse(anvilId); + + // Don't use SkillUtils.cooldownOver() here since that also accounts for the cooldown perks + if ((((LastUse + 3) * Misc.TIME_CONVERSION_FACTOR) >= System.currentTimeMillis()) || !Config.getInstance().getRepairConfirmRequired()) { + return true; + } + + if (!actualize) { + return false; + } + + mcMMOPlayer.actualizeLastAnvilUse(anvilId); + + if (anvilId == Repair.repairAnvilId) { + player.sendMessage(ChatColor.GREEN + "Right-click again to confirm " + ChatColor.GOLD + "Repair" + ChatColor.GREEN + ". Left-click to cancel."); //TODO Locale + } + + if (anvilId == Repair.salvageAnvilId) { + player.sendMessage(ChatColor.GREEN + "Right-click again to confirm " + ChatColor.GOLD + "Salvage" + ChatColor.GREEN + ". Left-click to cancel."); //TODO Locale + } + return false; + } + /** * Gets the Arcane Forging rank * diff --git a/src/main/resources/config.yml b/src/main/resources/config.yml index 6ef98f377..f9a7e3e27 100644 --- a/src/main/resources/config.yml +++ b/src/main/resources/config.yml @@ -174,6 +174,8 @@ Skills: Salvage_Anvil_ID: 41 Salvage_tools: true Salvage_armor: true + # Ask for a confirmation when a player tries to repair an enchanted item + Confirm_Required: true Smelting: Level_Cap: 0 Swords: