From 968636b8d43cea0a045e23a5e8e44b5176de25af Mon Sep 17 00:00:00 2001 From: nossr50 Date: Fri, 22 Mar 2019 15:32:53 -0700 Subject: [PATCH] Repairables can now specify multiple items that can be used for repairs --- Changelog.txt | 3 + .../commands/skills/RepairCommand.java | 11 ++-- .../gmail/nossr50/config/ConfigManager.java | 1 - .../config/hocon/RepairableSerializer.java | 40 ++++++++++--- .../hocon/skills/repair/ConfigRepair.java | 13 +++-- src/main/java/com/gmail/nossr50/mcMMO.java | 4 +- .../nossr50/skills/repair/RepairManager.java | 15 ++++- .../skills/repair/repairables/Repairable.java | 58 +++++++++++++------ .../repair/repairables/SimpleRepairable.java | 58 +++++++++---------- .../repairables/SimpleRepairableManager.java | 2 +- .../com/gmail/nossr50/util/ItemUtils.java | 27 +++++++++ 11 files changed, 157 insertions(+), 75 deletions(-) diff --git a/Changelog.txt b/Changelog.txt index be8b950e7..c027cd3fe 100644 --- a/Changelog.txt +++ b/Changelog.txt @@ -11,6 +11,9 @@ Version 2.2.0 mcMMO's config system has been rewritten mcMMO will now warn you in the console if it thinks you are running incompatible server software Parties no longer have a cap, you can level them forever for bragging rights + You can now specify multiple repair-items for an item (such as specifying that a wooden sword can be repaired by all types of planks) + Simplified the config entries for Repairables in the Repair config + Repairables in the repair config now use their internal registry key names instead of Bukkit material names Optimizations were made for many anti-exploit behaviours Acrobatic's Dodge will no longer reward XP for a few seconds after a TP Roll will not give XP for a few seconds after a TP diff --git a/src/main/java/com/gmail/nossr50/commands/skills/RepairCommand.java b/src/main/java/com/gmail/nossr50/commands/skills/RepairCommand.java index 45f6e22ff..d33438e49 100644 --- a/src/main/java/com/gmail/nossr50/commands/skills/RepairCommand.java +++ b/src/main/java/com/gmail/nossr50/commands/skills/RepairCommand.java @@ -8,7 +8,7 @@ import com.gmail.nossr50.locale.LocaleLoader; import com.gmail.nossr50.mcMMO; import com.gmail.nossr50.skills.repair.ArcaneForging; import com.gmail.nossr50.skills.repair.RepairManager; -import com.gmail.nossr50.skills.repair.repairables.Repairable; +import com.gmail.nossr50.skills.repair.repairables.SimpleRepairable; import com.gmail.nossr50.util.Permissions; import com.gmail.nossr50.util.TextComponentFactory; import com.gmail.nossr50.util.player.UserManager; @@ -17,6 +17,7 @@ import com.gmail.nossr50.util.skills.SkillActivationType; import net.md_5.bungee.api.chat.TextComponent; import org.bukkit.Material; import org.bukkit.entity.Player; +import org.bukkit.inventory.meta.Repairable; import java.util.ArrayList; import java.util.List; @@ -50,10 +51,10 @@ public class RepairCommand extends SkillCommand { @Override protected void dataCalculations(Player player, float skillValue) { // We're using pickaxes here, not the best but it works - Repairable diamondRepairable = mcMMO.getRepairableManager().getRepairable(Material.DIAMOND_PICKAXE); - Repairable goldRepairable = mcMMO.getRepairableManager().getRepairable(Material.GOLDEN_PICKAXE); - Repairable ironRepairable = mcMMO.getRepairableManager().getRepairable(Material.IRON_PICKAXE); - Repairable stoneRepairable = mcMMO.getRepairableManager().getRepairable(Material.STONE_PICKAXE); + SimpleRepairable diamondRepairable = mcMMO.getRepairableManager().getRepairable(Material.DIAMOND_PICKAXE); + SimpleRepairable goldRepairable = mcMMO.getRepairableManager().getRepairable(Material.GOLDEN_PICKAXE); + SimpleRepairable ironRepairable = mcMMO.getRepairableManager().getRepairable(Material.IRON_PICKAXE); + SimpleRepairable stoneRepairable = mcMMO.getRepairableManager().getRepairable(Material.STONE_PICKAXE); // TODO: This isn't really accurate - if they don't have pickaxes loaded it doesn't always mean the repair level is 0 diamondLevel = (diamondRepairable == null) ? 0 : diamondRepairable.getMinimumLevel(); diff --git a/src/main/java/com/gmail/nossr50/config/ConfigManager.java b/src/main/java/com/gmail/nossr50/config/ConfigManager.java index 5381b3067..2cf6dd7d2 100644 --- a/src/main/java/com/gmail/nossr50/config/ConfigManager.java +++ b/src/main/java/com/gmail/nossr50/config/ConfigManager.java @@ -47,7 +47,6 @@ import com.gmail.nossr50.config.treasure.HerbalismTreasureConfig; import com.gmail.nossr50.datatypes.party.PartyFeature; import com.gmail.nossr50.datatypes.skills.SubSkillType; import com.gmail.nossr50.mcMMO; -import com.gmail.nossr50.skills.repair.repairables.Repairable; import com.gmail.nossr50.skills.repair.repairables.SimpleRepairable; import com.gmail.nossr50.skills.repair.repairables.SimpleRepairableManager; import com.gmail.nossr50.skills.salvage.salvageables.Salvageable; diff --git a/src/main/java/com/gmail/nossr50/config/hocon/RepairableSerializer.java b/src/main/java/com/gmail/nossr50/config/hocon/RepairableSerializer.java index 29b6b4a5b..7c74e80cf 100644 --- a/src/main/java/com/gmail/nossr50/config/hocon/RepairableSerializer.java +++ b/src/main/java/com/gmail/nossr50/config/hocon/RepairableSerializer.java @@ -5,7 +5,10 @@ import com.google.common.reflect.TypeToken; import ninja.leaping.configurate.ConfigurationNode; import ninja.leaping.configurate.objectmapping.ObjectMappingException; import ninja.leaping.configurate.objectmapping.serialize.TypeSerializer; -import org.bukkit.Material; +import ninja.leaping.configurate.util.EnumLookup; + +import java.util.List; +import java.util.Optional; public class RepairableSerializer implements TypeSerializer { @@ -37,23 +40,46 @@ public class RepairableSerializer implements TypeSerializer { /* SimpleRepairable(Material itemMaterial, Material repairMaterial, int minimumQuantity, int minimumLevel, double xpMultiplier) */ - Material item = value.getNode("Item").getValue(TypeToken.of(Material.class)); - Material repairItem = value.getNode("Item-Used-To-Repair").getValue(TypeToken.of(Material.class)); + String item = value.getNode("Item").getValue(TypeToken.of(String.class)); + List repairItems = value.getNode("Items-Used-To-Repair").getValue(new TypeToken>() {}); + + + /*String itemConstant = HOCONUtil.deserializeENUMName(value.getNode("Item").getString()); + String repairConstant = HOCONUtil.deserializeENUMName(value.getNode("Item-Used-To-Repair").getString()); + + Material item = (Material) getEnum(itemConstant, TypeToken.of(Material.class)); + Material repairItem = (Material) getEnum(repairConstant, TypeToken.of(Material.class));*/ + int minimumQuantity = value.getNode("Minimum-Quantity-Used-To-Repair").getValue(TypeToken.of(Integer.class)); int minimumLevel = value.getNode("Skill-Level-Required-To-Repair").getValue(TypeToken.of(Integer.class)); double xpMultiplier = value.getNode("XP-Multiplier").getValue(TypeToken.of(Double.class)); - return new SimpleRepairable(item, repairItem, minimumQuantity, minimumLevel, xpMultiplier); + return new SimpleRepairable(item, repairItems, minimumQuantity, minimumLevel, xpMultiplier); } @Override public void serialize(TypeToken type, SimpleRepairable obj, ConfigurationNode value) throws ObjectMappingException { - value.getNode("Item").setValue(obj.getItemMaterial()); - value.getNode("Item-Used-To-Repair").setValue(obj.getRepairMaterial()); + /*value.getNode("Item").setValue(HOCONUtil.serializeENUMName(obj.getItemMaterial().getKey().getKey())); + value.getNode("Item-Used-To-Repair").setValue(HOCONUtil.serializeENUMName(obj.getRepairMaterials().getKey().getKey()));*/ + value.getNode("Item").setValue(obj.getItemMaterial().getKey().toString()); + value.getNode("Items-Used-To-Repair").setValue(obj.getRepairMaterialsRegistryKeys()); value.getNode("Minimum-Quantity-Used-To-Repair").setValue(obj.getMinimumQuantity()); value.getNode("Skill-Level-Required-To-Repair").setValue(obj.getMinimumLevel()); value.getNode("XP-Multiplier").setValue(obj.getXpMultiplier()); - } + + private Enum getEnum(String enumConstant, TypeToken type) throws ObjectMappingException + { + Optional ret = (Optional) EnumLookup.lookupEnum(type.getRawType().asSubclass(Enum.class), + enumConstant); // XXX: intellij says this cast is optional but it isnt + + if (!ret.isPresent()) { + throw new ObjectMappingException("Invalid enum constant provided for " + enumConstant + ": " + + "Expected a value of enum " + type + ", got " + enumConstant); + } + + return ret.get(); + } + } diff --git a/src/main/java/com/gmail/nossr50/config/hocon/skills/repair/ConfigRepair.java b/src/main/java/com/gmail/nossr50/config/hocon/skills/repair/ConfigRepair.java index 99d7d6781..8caf15ccf 100644 --- a/src/main/java/com/gmail/nossr50/config/hocon/skills/repair/ConfigRepair.java +++ b/src/main/java/com/gmail/nossr50/config/hocon/skills/repair/ConfigRepair.java @@ -5,8 +5,10 @@ import com.gmail.nossr50.config.hocon.skills.repair.repairmastery.ConfigRepairMa import com.gmail.nossr50.skills.repair.repairables.SimpleRepairable; import ninja.leaping.configurate.objectmapping.Setting; import ninja.leaping.configurate.objectmapping.serialize.ConfigSerializable; +import org.bukkit.Material; import java.util.ArrayList; +import java.util.Arrays; import static org.bukkit.Material.*; @@ -14,14 +16,15 @@ import static org.bukkit.Material.*; public class ConfigRepair { public static final ArrayList CONFIG_REPAIRABLES_DEFAULTS; + public static final Material[] PLANKS = new Material[] { OAK_PLANKS, BIRCH_PLANKS, DARK_OAK_PLANKS, ACACIA_PLANKS, JUNGLE_PLANKS, SPRUCE_PLANKS}; static { CONFIG_REPAIRABLES_DEFAULTS = new ArrayList<>(); - CONFIG_REPAIRABLES_DEFAULTS.add(new SimpleRepairable(WOODEN_SWORD, OAK_PLANKS, 1, 0, .25D)); - CONFIG_REPAIRABLES_DEFAULTS.add(new SimpleRepairable(WOODEN_SHOVEL, OAK_PLANKS, 1, 0, .15D)); - CONFIG_REPAIRABLES_DEFAULTS.add(new SimpleRepairable(WOODEN_PICKAXE, OAK_PLANKS, 1, 0, .5D)); - CONFIG_REPAIRABLES_DEFAULTS.add(new SimpleRepairable(WOODEN_AXE, OAK_PLANKS, 1, 0, .5D)); - CONFIG_REPAIRABLES_DEFAULTS.add(new SimpleRepairable(WOODEN_HOE, OAK_PLANKS, 1, 0, .25D)); + CONFIG_REPAIRABLES_DEFAULTS.add(new SimpleRepairable(WOODEN_SWORD, Arrays.asList(PLANKS), 1, 0, .25D)); + CONFIG_REPAIRABLES_DEFAULTS.add(new SimpleRepairable(WOODEN_SHOVEL, Arrays.asList(PLANKS), 1, 0, .15D)); + CONFIG_REPAIRABLES_DEFAULTS.add(new SimpleRepairable(WOODEN_PICKAXE, Arrays.asList(PLANKS), 1, 0, .5D)); + CONFIG_REPAIRABLES_DEFAULTS.add(new SimpleRepairable(WOODEN_AXE, Arrays.asList(PLANKS), 1, 0, .5D)); + CONFIG_REPAIRABLES_DEFAULTS.add(new SimpleRepairable(WOODEN_HOE, Arrays.asList(PLANKS), 1, 0, .25D)); /* Repairables: diff --git a/src/main/java/com/gmail/nossr50/mcMMO.java b/src/main/java/com/gmail/nossr50/mcMMO.java index 8fa3cebdf..3d0e1f75f 100644 --- a/src/main/java/com/gmail/nossr50/mcMMO.java +++ b/src/main/java/com/gmail/nossr50/mcMMO.java @@ -203,7 +203,7 @@ public class mcMMO extends JavaPlugin { //getServer().getPluginManager().disablePlugin(this); } - if(isIncompatibleVersion(Bukkit.getVersion(), Bukkit.getBukkitVersion())) + /*if(isIncompatibleVersion(Bukkit.getVersion(), Bukkit.getBukkitVersion())) { getLogger().severe("mcMMO is not supported for your current server software and or Minecraft version"); @@ -220,7 +220,7 @@ public class mcMMO extends JavaPlugin { getLogger().severe("Compatible Minecraft Versions: "+ COMPATIBLE_MINECRAFT_VERSIONS); getLogger().info("TIP: Paper and Spigot are extensions of CraftBukkit and are completely safe to upgrade to from CraftBukkit, please consider upgrading."); } - } + }*/ } private boolean isIncompatibleVersion(String serverSoftwareString, String serverVersionString) 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 0c3c65f70..fc182b0f7 100644 --- a/src/main/java/com/gmail/nossr50/skills/repair/RepairManager.java +++ b/src/main/java/com/gmail/nossr50/skills/repair/RepairManager.java @@ -10,7 +10,7 @@ import com.gmail.nossr50.datatypes.skills.SubSkillType; import com.gmail.nossr50.locale.LocaleLoader; import com.gmail.nossr50.mcMMO; import com.gmail.nossr50.skills.SkillManager; -import com.gmail.nossr50.skills.repair.repairables.Repairable; +import com.gmail.nossr50.skills.repair.repairables.SimpleRepairable; import com.gmail.nossr50.util.EventUtils; import com.gmail.nossr50.util.Misc; import com.gmail.nossr50.util.Permissions; @@ -61,9 +61,11 @@ public class RepairManager extends SkillManager { togglePlacedAnvil(); } + + public void handleRepair(ItemStack item) { Player player = getPlayer(); - Repairable repairable = mcMMO.getRepairableManager().getRepairable(item.getType()); + SimpleRepairable repairable = mcMMO.getRepairableManager().getRepairable(item.getType()); if (item.getItemMeta().isUnbreakable()) { NotificationManager.sendPlayerInformation(player, NotificationType.SUBSKILL_MESSAGE_FAILED, "Anvil.Unbreakable"); @@ -91,8 +93,15 @@ public class RepairManager extends SkillManager { } PlayerInventory inventory = player.getInventory(); + Material repairMaterial = null; + + //Find the first compatible repair material + for(Material repairMaterialCandidate : repairable.getRepairMaterials()) + { + if(player.getInventory().contains(new ItemStack(repairMaterialCandidate))) + repairMaterial = repairMaterialCandidate; + } - Material repairMaterial = repairable.getRepairMaterial(); //byte repairMaterialMetadata = repairable.getRepairMaterialMetadata(); ItemStack toRemove = new ItemStack(repairMaterial); diff --git a/src/main/java/com/gmail/nossr50/skills/repair/repairables/Repairable.java b/src/main/java/com/gmail/nossr50/skills/repair/repairables/Repairable.java index 46be5d6e8..fbfb4e197 100644 --- a/src/main/java/com/gmail/nossr50/skills/repair/repairables/Repairable.java +++ b/src/main/java/com/gmail/nossr50/skills/repair/repairables/Repairable.java @@ -1,3 +1,4 @@ +/* package com.gmail.nossr50.skills.repair.repairables; import com.gmail.nossr50.datatypes.skills.ItemType; @@ -6,70 +7,89 @@ import org.bukkit.Material; public interface Repairable { - /** + */ +/** * Gets the type of this repairable item * * @return the type of this repairable - */ + *//* + public Material getItemMaterial(); - /** + */ +/** * Gets the id of the material used to repair this item * * @return the id of the repair material - */ - public Material getRepairMaterial(); + *//* - /** + public Material getRepairMaterials(); + + */ +/** * Gets the RepairItemType value for this repairable item * * @return the RepairItemType for this repairable - */ + *//* + public ItemType getRepairItemType(); - /** + */ +/** * Gets the RepairMaterialType value for this repairable item * * @return the RepairMaterialType for this repairable - */ + *//* + public MaterialType getRepairMaterialType(); - /** + */ +/** * Gets the minimum quantity of repair materials ignoring all other repair bonuses * * This is typically set to the number of items needed to create that item, for example 5 for helmets or 2 for swords * * @return the minimum number of items - */ + *//* + public int getMinimumQuantity(); - /** + */ +/** * Gets the maximum durability of this item before it breaks * * @return the maximum durability - */ + *//* + public short getMaximumDurability(); - /** + */ +/** * Gets the base repair durability on which to calculate bonuses. * * This is actually the maximum durability divided by the minimum quantity * * @return the base repair durability - */ + *//* + public short getBaseRepairDurability(); - /** + */ +/** * Gets the minimum repair level needed to repair this item * * @return the minimum level to repair this item, or 0 for no minimum - */ + *//* + public int getMinimumLevel(); - /** + */ +/** * Gets the xpMultiplier for this repairable * * @return the xpMultiplier of this repairable - */ + *//* + public double getXpMultiplier(); } +*/ diff --git a/src/main/java/com/gmail/nossr50/skills/repair/repairables/SimpleRepairable.java b/src/main/java/com/gmail/nossr50/skills/repair/repairables/SimpleRepairable.java index cea563d78..2d2478514 100644 --- a/src/main/java/com/gmail/nossr50/skills/repair/repairables/SimpleRepairable.java +++ b/src/main/java/com/gmail/nossr50/skills/repair/repairables/SimpleRepairable.java @@ -6,41 +6,39 @@ import com.gmail.nossr50.util.ItemUtils; import org.bukkit.Material; import org.bukkit.inventory.ItemStack; +import java.util.Arrays; +import java.util.List; -public class SimpleRepairable implements Repairable { - private final Material itemMaterial, repairMaterial; + +public class SimpleRepairable { + private final Material itemMaterial; + private final List repairMaterials; private final int minimumQuantity, minimumLevel; private final short maximumDurability, baseRepairDurability; - /*private String repairMaterialPrettyName;*/ private final ItemType repairItemType; private final MaterialType repairMaterialType; private final double xpMultiplier; -/* protected SimpleRepairable(Material type, Material repairMaterial, String repairMaterialPrettyName, int minimumLevel, int minimumQuantity, short maximumDurability, ItemType repairItemType, MaterialType repairMaterialType, double xpMultiplier) { - this.itemMaterial = type; - this.repairMaterial = repairMaterial; - this.repairMaterialPrettyName = repairMaterialPrettyName; - this.repairItemType = repairItemType; - this.repairMaterialType = repairMaterialType; - this.minimumLevel = minimumLevel; - this.minimumQuantity = minimumQuantity; - this.maximumDurability = maximumDurability; - this.baseRepairDurability = (short) (maximumDurability / minimumQuantity); - this.xpMultiplier = xpMultiplier; - }*/ - public SimpleRepairable(Material itemMaterial, Material repairMaterial, int minimumQuantity, int minimumLevel, double xpMultiplier) { - this.itemMaterial = itemMaterial; - this.repairMaterial = repairMaterial; + this(itemMaterial.getKey().getKey(), ItemUtils.getRepairItemMaterials(Arrays.asList(repairMaterial)), minimumQuantity, minimumLevel, xpMultiplier); + } + + public SimpleRepairable(Material itemMaterial, List repairMaterials, int minimumQuantity, int minimumLevel, double xpMultiplier) { + this(itemMaterial.getKey().getKey(), ItemUtils.getRepairItemMaterials(repairMaterials), minimumQuantity, minimumLevel, xpMultiplier); + } + + public SimpleRepairable(String itemMaterial, List repairMaterials, int minimumQuantity, int minimumLevel, double xpMultiplier) { + this.itemMaterial = Material.matchMaterial(itemMaterial); + this.repairMaterials = ItemUtils.matchMaterials(repairMaterials); this.minimumQuantity = minimumQuantity; this.minimumLevel = minimumLevel; this.xpMultiplier = xpMultiplier; - this.maximumDurability = itemMaterial.getMaxDurability(); + this.maximumDurability = this.itemMaterial.getMaxDurability(); this.baseRepairDurability = (short) (maximumDurability / minimumQuantity); - this.repairItemType = determineItemType(itemMaterial); - this.repairMaterialType = determineMaterialType(repairMaterial); + this.repairItemType = determineItemType(this.itemMaterial); + this.repairMaterialType = determineMaterialType(this.repairMaterials.get(0)); } public MaterialType determineMaterialType(Material material) { @@ -88,47 +86,43 @@ public class SimpleRepairable implements Repairable { } } - @Override public Material getItemMaterial() { return itemMaterial; } - @Override - public Material getRepairMaterial() { - return repairMaterial; + public List getRepairMaterials() { + return repairMaterials; } - @Override + public List getRepairMaterialsRegistryKeys() { + return ItemUtils.getRepairItemMaterials(repairMaterials); + } + + public ItemType getRepairItemType() { return repairItemType; } - @Override public MaterialType getRepairMaterialType() { return repairMaterialType; } - @Override public int getMinimumQuantity() { return minimumQuantity; } - @Override public short getMaximumDurability() { return maximumDurability; } - @Override public short getBaseRepairDurability() { return baseRepairDurability; } - @Override public int getMinimumLevel() { return minimumLevel; } - @Override public double getXpMultiplier() { return xpMultiplier; } diff --git a/src/main/java/com/gmail/nossr50/skills/repair/repairables/SimpleRepairableManager.java b/src/main/java/com/gmail/nossr50/skills/repair/repairables/SimpleRepairableManager.java index 4883bed5c..c50866a93 100644 --- a/src/main/java/com/gmail/nossr50/skills/repair/repairables/SimpleRepairableManager.java +++ b/src/main/java/com/gmail/nossr50/skills/repair/repairables/SimpleRepairableManager.java @@ -39,7 +39,7 @@ public class SimpleRepairableManager implements Unload { return isRepairable(itemStack.getType()); } - public Repairable getRepairable(Material type) { + public SimpleRepairable getRepairable(Material type) { return repairables.get(type); } } diff --git a/src/main/java/com/gmail/nossr50/util/ItemUtils.java b/src/main/java/com/gmail/nossr50/util/ItemUtils.java index 1eaaf0c18..b1a03ebdf 100644 --- a/src/main/java/com/gmail/nossr50/util/ItemUtils.java +++ b/src/main/java/com/gmail/nossr50/util/ItemUtils.java @@ -10,9 +10,36 @@ import org.bukkit.inventory.ItemStack; import org.bukkit.inventory.Recipe; import org.bukkit.inventory.meta.ItemMeta; +import java.util.ArrayList; +import java.util.List; + public final class ItemUtils { private ItemUtils() {} + public static ArrayList getRepairItemMaterials(List repairItemList) + { + ArrayList repairMaterialList = new ArrayList<>(); + + for(Material m : repairItemList) + { + repairMaterialList.add(m.getKey().toString()); + } + + return repairMaterialList; + } + + public static ArrayList matchMaterials(List ItemBlockRegistryKeyList) + { + ArrayList matchedMaterials = new ArrayList<>(); + + for(String s : ItemBlockRegistryKeyList) + { + matchedMaterials.add(Material.matchMaterial(s)); + } + + return matchedMaterials; + } + /** * Checks if the item is a bow. *