123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369 |
- package com.gmail.nossr50.skills.repair;
- import com.gmail.nossr50.datatypes.interactions.NotificationType;
- import com.gmail.nossr50.datatypes.player.McMMOPlayer;
- import com.gmail.nossr50.datatypes.skills.PrimarySkillType;
- import com.gmail.nossr50.datatypes.skills.SubSkillType;
- import com.gmail.nossr50.mcMMO;
- import com.gmail.nossr50.skills.SkillManager;
- import com.gmail.nossr50.util.Misc;
- import com.gmail.nossr50.util.Permissions;
- import com.gmail.nossr50.util.random.RandomChanceSkillStatic;
- import com.gmail.nossr50.util.skills.SkillActivationType;
- import com.gmail.nossr50.util.sounds.SoundManager;
- import com.gmail.nossr50.util.sounds.SoundType;
- import org.bukkit.enchantments.Enchantment;
- import org.bukkit.entity.Player;
- import org.bukkit.inventory.ItemStack;
- import java.util.Map;
- import java.util.Map.Entry;
- public class RepairManager extends SkillManager {
- private boolean placedAnvil;
- private int lastClick;
- public RepairManager(mcMMO pluginRef, McMMOPlayer mcMMOPlayer) {
- super(pluginRef, mcMMOPlayer, PrimarySkillType.REPAIR);
- }
- /**
- * Handles notifications for placing an anvil.
- */
- public void placedAnvilCheck() {
- Player player = getPlayer();
- if (getPlacedAnvil()) {
- return;
- }
- if (pluginRef.getConfigManager().getConfigRepair().getRepairGeneral().isAnvilMessages()) {
- pluginRef.getNotificationManager().sendPlayerInformation(player, NotificationType.SUBSKILL_MESSAGE, "Repair.Listener.Anvil");
- }
- if (pluginRef.getConfigManager().getConfigRepair().getRepairGeneral().isAnvilPlacedSounds()) {
- SoundManager.sendSound(player, player.getLocation(), SoundType.ANVIL);
- }
- togglePlacedAnvil();
- }
- public void handleRepair(ItemStack item) {
- // Player player = getPlayer();
- // Repairable repairable = mcMMO.getRepairableManager().getRepairable(item.getType());
- //
- // if(item.getItemMeta() != null && item.getItemMeta().isUnbreakable()) {
- // mcMMO.getNotificationManager().sendPlayerInformation(player, NotificationType.SUBSKILL_MESSAGE_FAILED, "Anvil.Unbreakable");
- // return;
- // }
- //
- // // Permissions checks on material and item types
- //// if (!Permissions.repairMaterialType(player, repairable.getRepairItemMaterialCategory())) {
- //// mcMMO.getNotificationManager().sendPlayerInformation(player, NotificationType.NO_PERMISSION, "mcMMO.NoPermission");
- //// return;
- //// }
- ////
- //// if (!Permissions.repairItemType(player, repairable.getRepairItemType())) {
- //// mcMMO.getNotificationManager().sendPlayerInformation(player, NotificationType.NO_PERMISSION, "mcMMO.NoPermission");
- //// return;
- //// }
- //
- // int skillLevel = getSkillLevel();
- // int minimumRepairableLevel = repairable.getMinimumLevel();
- //
- // // Level check
- // if (skillLevel < minimumRepairableLevel) {
- // mcMMO.getNotificationManager().sendPlayerInformation(player, NotificationType.SUBSKILL_MESSAGE_FAILED, "Repair.Skills.Adept", String.valueOf(minimumRepairableLevel), StringUtils.getPrettyItemString(item.getType()));
- // return;
- // }
- //
- // PlayerInventory inventory = player.getInventory();
- // Material repairMaterial = null;
- // boolean foundNonBasicMaterial = false;
- //
- // //Find the first compatible repair material
- // for (Material repairMaterialCandidate : repairable.getRepairMaterials()) {
- // for (ItemStack is : player.getInventory().getContents()) {
- // if(is == null)
- // continue; //Ignore IntelliJ this can be null
- //
- // //Match to repair material
- // if (is.getType() == repairMaterialCandidate) {
- // //Check for item meta
- // if(is.getItemMeta() != null) {
- // //Check for lore
- // if(is.getItemMeta().getLore() != null) {
- // if(is.getItemMeta().getLore().isEmpty()) {
- // //Lore is empty so this item is fine
- // repairMaterial = repairMaterialCandidate;
- // break;
- // } else {
- // foundNonBasicMaterial = true;
- // }
- // } else {
- // //No lore so this item is fine
- // repairMaterial = repairMaterialCandidate;
- // break;
- // }
- // } else {
- // //No Item Meta so this item is fine
- // repairMaterial = repairMaterialCandidate;
- // break;
- // }
- // }
- // }
- // }
- //
- // /* Abort the repair if no compatible basic repairing item found */
- // if (repairMaterial == null && foundNonBasicMaterial == true) {
- // player.sendMessage(pluginRef.getLocaleManager().getString("Repair.NoBasicRepairMatsFound"));
- // return;
- // }
- //
- // ItemStack toRemove = new ItemStack(repairMaterial);
- // toRemove.setAmount(1);
- //
- // short startDurability = item.getDurability();
- //
- // // Do not repair if at full durability
- // if (startDurability <= 0) {
- // mcMMO.getNotificationManager().sendPlayerInformation(player, NotificationType.SUBSKILL_MESSAGE_FAILED, "Repair.Skills.FullDurability");
- // return;
- // }
- //
- // // Clear ability buffs before trying to repair.
- // SkillUtils.removeAbilityBuff(item);
- //
- // // Lets get down to business,
- // // To defeat, the huns.
- // int baseRepairAmount = repairable.getBaseRepairDurability(); // Did they send me daughters?
- // short newDurability = repairCalculate(startDurability, baseRepairAmount); // When I asked for sons?
- //
- // // Call event
- // if (EventUtils.callRepairCheckEvent(player, (short) (startDurability - newDurability), toRemove, item).isCancelled()) {
- // return;
- // }
- //
- // // Handle the enchants
- // if (mcMMO.getConfigManager().getConfigRepair().getArcaneForging().isMayLoseEnchants() && !Permissions.hasRepairEnchantBypassPerk(player)) {
- // addEnchants(item);
- // }
- //
- // // Remove the item
- // inventory.removeItem(toRemove);
- //
- // // Give out XP like candy
- // applyXpGain(((getPercentageRepaired(startDurability, newDurability, repairable.getMaximumDurability())
- // * repairable.getXpMultiplier())
- // * mcMMO.getConfigManager().getConfigExperience().getRepairXPBase())
- // * mcMMO.getConfigManager().getConfigExperience().getExperienceRepair().getItemMaterialXPMultiplier(repairable.getRepairItemMaterialCategory()), XPGainReason.PVE);
- //
- // // BWONG BWONG BWONG
- // if (mcMMO.getConfigManager().getConfigRepair().getRepairGeneral().isAnvilUseSounds()) {
- // SoundManager.sendSound(player, player.getLocation(), SoundType.ANVIL);
- // SoundManager.sendSound(player, player.getLocation(), SoundType.ITEM_BREAK);
- // }
- //
- // // Repair the item!
- // item.setDurability(newDurability);
- }
- private double getPercentageRepaired(short startDurability, short newDurability, short totalDurability) {
- return ((startDurability - newDurability) / (float) totalDurability);
- }
- /**
- * Check if the player has tried to use an Anvil before.
- *
- * @return true if the player has confirmed using an Anvil
- */
- public boolean checkConfirmation(boolean actualize) {
- Player player = getPlayer();
- long lastUse = getLastAnvilUse();
- if (!pluginRef.getSkillTools().cooldownExpired(lastUse, 3) || !pluginRef.getConfigManager().getConfigRepair().getRepairGeneral().isEnchantedItemsRequireConfirm()) {
- return true;
- }
- if (!actualize) {
- return false;
- }
- actualizeLastAnvilUse();
- pluginRef.getNotificationManager().sendPlayerInformation(player, NotificationType.SUBSKILL_MESSAGE, "Skills.ConfirmOrCancel", pluginRef.getLocaleManager().getString("Repair.Pretty.Name"));
- return false;
- }
- /**
- * Gets the Arcane Forging rank
- *
- * @return the current Arcane Forging rank
- */
- public int getArcaneForgingRank() {
- return pluginRef.getRankTools().getRank(getPlayer(), SubSkillType.REPAIR_ARCANE_FORGING);
- }
- /**
- * Gets chance of keeping enchantment during repair.
- *
- * @return The chance of keeping the enchantment
- */
- public double getKeepEnchantChance() {
- return pluginRef.getConfigManager().getConfigRepair().getArcaneForging().getKeepEnchantChanceMap().get(getArcaneForgingRank());
- }
- /**
- * Gets chance of enchantment being downgraded during repair.
- *
- * @return The chance of the enchantment being downgraded
- */
- public double getDowngradeEnchantChance() {
- return pluginRef.getConfigManager().getConfigRepair().getArcaneForging().getDowngradeChanceMap().get(getArcaneForgingRank());
- }
- /**
- * Computes repair bonuses.
- *
- * @param durability The durability of the item being repaired
- * @param repairAmount The base amount of durability repaired to the item
- * @return The final amount of durability repaired to the item
- */
- private short repairCalculate(short durability, int repairAmount) {
- Player player = getPlayer();
- if (Permissions.isSubSkillEnabled(player, SubSkillType.REPAIR_REPAIR_MASTERY)
- && pluginRef.getRankTools().hasUnlockedSubskill(getPlayer(), SubSkillType.REPAIR_REPAIR_MASTERY)) {
- double maxBonusCalc = pluginRef.getDynamicSettingsManager().getSkillPropertiesManager().getMaxBonus(SubSkillType.REPAIR_REPAIR_MASTERY) / 100.0D;
- double skillLevelBonusCalc = (maxBonusCalc / pluginRef.getDynamicSettingsManager().getSkillPropertiesManager().getMaxBonusLevel(SubSkillType.REPAIR_REPAIR_MASTERY)) * (getSkillLevel() / 100.0D);
- double bonus = repairAmount * Math.min(skillLevelBonusCalc, maxBonusCalc);
- repairAmount += bonus;
- }
- if (Permissions.isSubSkillEnabled(player, SubSkillType.REPAIR_SUPER_REPAIR) && checkPlayerProcRepair()) {
- repairAmount *= 2.0D;
- }
- if (repairAmount <= 0 || repairAmount > Short.MAX_VALUE) {
- repairAmount = Short.MAX_VALUE;
- }
- return (short) Math.max(durability - repairAmount, 0);
- }
- /**
- * Checks for Super Repair bonus.
- *
- * @return true if bonus granted, false otherwise
- */
- private boolean checkPlayerProcRepair() {
- if (!pluginRef.getRankTools().hasUnlockedSubskill(getPlayer(), SubSkillType.REPAIR_SUPER_REPAIR))
- return false;
- if (pluginRef.getRandomChanceTools().isActivationSuccessful(SkillActivationType.RANDOM_LINEAR_100_SCALE_WITH_CAP, SubSkillType.REPAIR_SUPER_REPAIR, getPlayer())) {
- pluginRef.getNotificationManager().sendPlayerInformation(getPlayer(), NotificationType.SUBSKILL_MESSAGE, "Repair.Skills.FeltEasy");
- return true;
- }
- return false;
- }
- /**
- * Handles removing & downgrading enchants.
- *
- * @param item Item being repaired
- */
- private void addEnchants(ItemStack item) {
- Player player = getPlayer();
- Map<Enchantment, Integer> enchants = item.getEnchantments();
- if (enchants.isEmpty()) {
- return;
- }
- if (Permissions.arcaneBypass(player)) {
- pluginRef.getNotificationManager().sendPlayerInformation(getPlayer(), NotificationType.SUBSKILL_MESSAGE, "Repair.Arcane.Perfect");
- return;
- }
- if (getArcaneForgingRank() == 0 || !Permissions.isSubSkillEnabled(player, SubSkillType.REPAIR_ARCANE_FORGING)) {
- for (Enchantment enchant : enchants.keySet()) {
- item.removeEnchantment(enchant);
- }
- pluginRef.getNotificationManager().sendPlayerInformation(getPlayer(), NotificationType.SUBSKILL_MESSAGE_FAILED, "Repair.Arcane.Lost");
- return;
- }
- boolean downgraded = false;
- for (Entry<Enchantment, Integer> enchant : enchants.entrySet()) {
- int enchantLevel = enchant.getValue();
- if(!pluginRef.getConfigManager().getConfigExploitPrevention().getConfigSectionExploitRepair().isAllowUnsafeEnchants()) {
- if(enchantLevel > enchant.getKey().getMaxLevel()) {
- enchantLevel = enchant.getKey().getMaxLevel();
- item.addEnchantment(enchant.getKey(), enchantLevel);
- }
- }
- Enchantment enchantment = enchant.getKey();
- if (pluginRef.getRandomChanceTools().checkRandomChanceExecutionSuccess(new RandomChanceSkillStatic(getKeepEnchantChance(), getPlayer(), SubSkillType.REPAIR_ARCANE_FORGING))) {
- if (pluginRef.getConfigManager().getConfigRepair().getArcaneForging().isDowngradesEnabled() && enchantLevel > 1
- && (!pluginRef.getRandomChanceTools().checkRandomChanceExecutionSuccess(new RandomChanceSkillStatic(100 - getDowngradeEnchantChance(), getPlayer(), SubSkillType.REPAIR_ARCANE_FORGING)))) {
- item.addUnsafeEnchantment(enchantment, enchantLevel - 1);
- downgraded = true;
- }
- } else {
- item.removeEnchantment(enchantment);
- }
- }
- Map<Enchantment, Integer> newEnchants = item.getEnchantments();
- if (newEnchants.isEmpty()) {
- pluginRef.getNotificationManager().sendPlayerInformationChatOnly(getPlayer(), "Repair.Arcane.Fail");
- }
- else if (downgraded || newEnchants.size() < enchants.size()) {
- pluginRef.getNotificationManager().sendPlayerInformationChatOnly(getPlayer(), "Repair.Arcane.Downgrade");
- }
- else {
- pluginRef.getNotificationManager().sendPlayerInformationChatOnly(getPlayer(), "Repair.Arcane.Perfect");
- }
- }
- /*
- * Repair Anvil Placement
- */
- public boolean getPlacedAnvil() {
- return placedAnvil;
- }
- public void togglePlacedAnvil() {
- placedAnvil = !placedAnvil;
- }
- /*
- * Repair Anvil Usage
- */
- public int getLastAnvilUse() {
- return lastClick;
- }
- public void setLastAnvilUse(int value) {
- lastClick = value;
- }
- public void actualizeLastAnvilUse() {
- lastClick = (int) (System.currentTimeMillis() / Misc.TIME_CONVERSION_FACTOR);
- }
- }
|