diff --git a/src/main/java/nl/pim16aap2/armoredElytra/handlers/AnvilHandler.java b/src/main/java/nl/pim16aap2/armoredElytra/handlers/AnvilHandler.java index 3320d5a..62bd43d 100644 --- a/src/main/java/nl/pim16aap2/armoredElytra/handlers/AnvilHandler.java +++ b/src/main/java/nl/pim16aap2/armoredElytra/handlers/AnvilHandler.java @@ -6,6 +6,7 @@ import nl.pim16aap2.armoredElytra.util.ArmorTier; import nl.pim16aap2.armoredElytra.util.EnchantmentContainer; import nl.pim16aap2.armoredElytra.util.Util; import nl.pim16aap2.armoredElytra.util.XMaterial; +import org.bukkit.ChatColor; import org.bukkit.Material; import org.bukkit.entity.Player; import org.bukkit.event.EventHandler; @@ -16,6 +17,7 @@ import org.bukkit.event.inventory.InventoryType; import org.bukkit.event.inventory.PrepareAnvilEvent; import org.bukkit.inventory.AnvilInventory; import org.bukkit.inventory.ItemStack; +import org.bukkit.inventory.meta.ItemMeta; import java.util.logging.Level; @@ -100,13 +102,12 @@ public class AnvilHandler extends ArmoredElytraHandler implements Listener ItemStack result = null; if (itemA != null && itemB != null) - // If itemB is the elytra, while itemA isn't, switch itemA and itemB. + // If itemB is the (armored) elytra, while itemA isn't, switch itemA and itemB. if (itemB.getType() == Material.ELYTRA && itemA.getType() != Material.ELYTRA) { - result = itemA; + ItemStack tmp = itemA; itemA = itemB; - itemB = result; - result = null; + itemB = tmp; } // Check if there are items in both input slots. @@ -163,8 +164,10 @@ public class AnvilHandler extends ArmoredElytraHandler implements Listener enchantments.applyEnchantments(result); result.setDurability(durability); + final String name = getElytraResultName(itemA, action, newTier, event.getInventory().getRenameText()); + result = ArmoredElytra.getInstance().getNbtEditor() - .addArmorNBTTags(result, newTier, plugin.getConfigLoader().unbreakable()); + .addArmorNBTTags(result, newTier, plugin.getConfigLoader().unbreakable(), name); event.setResult(result); return; @@ -178,6 +181,26 @@ public class AnvilHandler extends ArmoredElytraHandler implements Listener event.setResult(null); } + private String getElytraResultName(final ItemStack baseItem, final Action action, + final ArmorTier armorTier, final String renameText) + { + final String tierName = ArmoredElytra.getInstance().getArmoredElytraName(armorTier); + if (renameText == null || !plugin.getConfigLoader().allowRenaming()) + return tierName; + + final ItemMeta meta = baseItem.getItemMeta(); + final String currentName = meta == null ? null : meta.getDisplayName(); + + // When the rename text is empty, give it the default tier-name when creating a new armored elytra + // (so it's named properly) or when the current name is already the tier name (just returning the current + // name would strip the tier's color in this case). + if ((action == Action.CREATE && renameText.equals("")) || + ChatColor.stripColor(tierName).equals(ChatColor.stripColor(renameText))) + return tierName; + + return renameText.equals("") ? currentName : renameText; + } + // Let the player take items out of the anvil. @EventHandler public void onInventoryClick(InventoryClickEvent e) @@ -215,14 +238,7 @@ public class AnvilHandler extends ArmoredElytraHandler implements Listener // If there's an armored elytra in the final slot... if (armortier != ArmorTier.NONE && plugin.playerHasCraftPerm(player, armortier)) { - // Create a new armored elytra and give that one to the player instead of the - // result. - // This is done because after putting item0 in AFTER item1, the first letter of - // the color code shows up, this gets rid of that problem. - ItemStack result = ArmoredElytra.getInstance().getNbtEditor() - .addArmorNBTTags(anvilInventory.getItem(2), armortier, - plugin.getConfigLoader().unbreakable()); - + final ItemStack result = anvilInventory.getItem(2); // Give the result to the player and clear the anvil's inventory. if (!giveItemToPlayer(player, result, e.isShiftClick())) return; diff --git a/src/main/java/nl/pim16aap2/armoredElytra/nbtEditor/INBTEditor.java b/src/main/java/nl/pim16aap2/armoredElytra/nbtEditor/INBTEditor.java index 00fdbd0..6ab5196 100644 --- a/src/main/java/nl/pim16aap2/armoredElytra/nbtEditor/INBTEditor.java +++ b/src/main/java/nl/pim16aap2/armoredElytra/nbtEditor/INBTEditor.java @@ -1,5 +1,6 @@ package nl.pim16aap2.armoredElytra.nbtEditor; +import nl.pim16aap2.armoredElytra.ArmoredElytra; import nl.pim16aap2.armoredElytra.util.ArmorTier; import org.bukkit.inventory.ItemStack; @@ -7,7 +8,8 @@ public interface INBTEditor { /** * Adds a given {@link ArmorTier} to an item. The item will be cloned. Note that setting the armor tier to {@link - * ArmorTier#NONE} has no effect (besides making a copy of the item). + * ArmorTier#NONE} has no effect (besides making a copy of the item). The default name for the given tier is + * applied. See {@link ArmoredElytra#getArmoredElytraName(ArmorTier)}. * * @param item The item. * @param armorTier The {@link ArmorTier} that will be added to it. @@ -16,6 +18,18 @@ public interface INBTEditor */ ItemStack addArmorNBTTags(ItemStack item, ArmorTier armorTier, boolean unbreakable); + /** + * Adds a given {@link ArmorTier} to an item. The item will be cloned. Note that setting the armor tier to {@link + * ArmorTier#NONE} has no effect (besides making a copy of the item). + * + * @param item The item. + * @param armorTier The {@link ArmorTier} that will be added to it. + * @param unbreakable Whether the resulting item should be unbreakable. + * @param name The name fo the item. + * @return The NEW item. + */ + ItemStack addArmorNBTTags(ItemStack item, ArmorTier armorTier, boolean unbreakable, final String name); + /** * Checks which {@link ArmorTier} is on an item. * diff --git a/src/main/java/nl/pim16aap2/armoredElytra/nbtEditor/NBTEditor.java b/src/main/java/nl/pim16aap2/armoredElytra/nbtEditor/NBTEditor.java index cd6b54b..e313c38 100644 --- a/src/main/java/nl/pim16aap2/armoredElytra/nbtEditor/NBTEditor.java +++ b/src/main/java/nl/pim16aap2/armoredElytra/nbtEditor/NBTEditor.java @@ -19,12 +19,15 @@ public class NBTEditor implements INBTEditor private static final NamespacedKey armorTierKey = new NamespacedKey(ArmoredElytra.getInstance(), "ARMOR_TIER_LEVEL"); - public NBTEditor() + @Override + public ItemStack addArmorNBTTags(ItemStack item, ArmorTier armorTier, boolean unbreakable) { + return addArmorNBTTags(item, armorTier, unbreakable, + ArmoredElytra.getInstance().getArmoredElytraName(armorTier)); } @Override - public ItemStack addArmorNBTTags(ItemStack item, ArmorTier armorTier, boolean unbreakable) + public ItemStack addArmorNBTTags(ItemStack item, ArmorTier armorTier, boolean unbreakable, final String name) { if (armorTier == null || armorTier == ArmorTier.NONE) return new ItemStack(item); @@ -45,7 +48,7 @@ public class NBTEditor implements INBTEditor "generic.knockback_resistance"); meta.setUnbreakable(unbreakable); - meta.setDisplayName(ArmoredElytra.getInstance().getArmoredElytraName(armorTier)); + meta.setDisplayName(name); ret.setItemMeta(meta); return ret; diff --git a/src/main/java/nl/pim16aap2/armoredElytra/util/ConfigLoader.java b/src/main/java/nl/pim16aap2/armoredElytra/util/ConfigLoader.java index ce87fd5..6dde07e 100644 --- a/src/main/java/nl/pim16aap2/armoredElytra/util/ConfigLoader.java +++ b/src/main/java/nl/pim16aap2/armoredElytra/util/ConfigLoader.java @@ -36,6 +36,7 @@ public class ConfigLoader private boolean craftingInSmithingTable; private boolean bypassWearPerm; private boolean bypassCraftPerm; + private boolean allowRenaming; private final ArrayList> configOptionsList; private final ArmoredElytra plugin; @@ -118,6 +119,10 @@ public class ConfigLoader "This option only works on 1.16+! When enabled, armored elytra creation in anvils is disabled. ", "Instead, you will have to craft them in a smithy. Enchanting/repairing them still works via the anvil." }; + String[] allowRenamingComment = + { + "Whether or not to allow renaming of armored elytras in anvils." + }; // Set default list of allowed enchantments. @@ -172,6 +177,8 @@ public class ConfigLoader allowMultipleProtectionEnchantments = addNewConfigOption(config, "allowMultipleProtectionEnchantments", false, allowMultipleProtectionEnchantmentsComment); + allowRenaming = addNewConfigOption(config, "allowRenaming", true, allowRenamingComment); + checkForUpdates = addNewConfigOption(config, "checkForUpdates", true, updateComment); allowStats = addNewConfigOption(config, "allowStats", true, bStatsComment); enableDebug = addNewConfigOption(config, "enableDebug", false, debugComment); @@ -287,6 +294,11 @@ public class ConfigLoader return allowMultipleProtectionEnchantments; } + public boolean allowRenaming() + { + return allowRenaming; + } + public boolean uninstallMode() { return uninstallMode;