diff --git a/src/main/java/nl/pim16aap2/armoredElytra/handlers/AnvilHandler.java b/src/main/java/nl/pim16aap2/armoredElytra/handlers/AnvilHandler.java index 62bd43d..23a8bdf 100644 --- a/src/main/java/nl/pim16aap2/armoredElytra/handlers/AnvilHandler.java +++ b/src/main/java/nl/pim16aap2/armoredElytra/handlers/AnvilHandler.java @@ -7,6 +7,7 @@ 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.Color; import org.bukkit.Material; import org.bukkit.entity.Player; import org.bukkit.event.EventHandler; @@ -165,9 +166,11 @@ public class AnvilHandler extends ArmoredElytraHandler implements Listener result.setDurability(durability); final String name = getElytraResultName(itemA, action, newTier, event.getInventory().getRenameText()); + final Color color = getItemColor(itemA, itemB); result = ArmoredElytra.getInstance().getNbtEditor() - .addArmorNBTTags(result, newTier, plugin.getConfigLoader().unbreakable(), name); + .addArmorNBTTags(result, newTier, plugin.getConfigLoader().unbreakable(), + name, color); event.setResult(result); return; diff --git a/src/main/java/nl/pim16aap2/armoredElytra/handlers/ArmoredElytraHandler.java b/src/main/java/nl/pim16aap2/armoredElytra/handlers/ArmoredElytraHandler.java index 580b89b..17eef77 100644 --- a/src/main/java/nl/pim16aap2/armoredElytra/handlers/ArmoredElytraHandler.java +++ b/src/main/java/nl/pim16aap2/armoredElytra/handlers/ArmoredElytraHandler.java @@ -2,9 +2,12 @@ package nl.pim16aap2.armoredElytra.handlers; import nl.pim16aap2.armoredElytra.ArmoredElytra; import nl.pim16aap2.armoredElytra.util.XMaterial; +import org.bukkit.Bukkit; +import org.bukkit.Color; import org.bukkit.Material; import org.bukkit.entity.Player; import org.bukkit.inventory.ItemStack; +import org.bukkit.inventory.meta.LeatherArmorMeta; import javax.annotation.CheckReturnValue; @@ -19,6 +22,8 @@ abstract class ArmoredElytraHandler protected final boolean creationEnabled; + private static final Color DEFAULT_LEATHER_COLOR = Bukkit.getServer().getItemFactory().getDefaultLeatherColor(); + public ArmoredElytraHandler(final ArmoredElytra plugin, final boolean creationEnabled) { this.plugin = plugin; @@ -50,6 +55,36 @@ abstract class ArmoredElytraHandler return (short) (Math.max(newDurability, 0)); } + /** + * Gets the color of the item if the item has a color. + *

+ * See {@link LeatherArmorMeta#getColor()}. + * + * @param itemA The first {@link ItemStack} to check. + * @param itemB The second {@link ItemStack} to check. + * @return The color of the item, if it has a color, otherwise null. + */ + protected Color getItemColor(final ItemStack itemA, final ItemStack itemB) + { + final Color colorA = getItemColor(itemA); + if (colorA != null && !colorA.equals(DEFAULT_LEATHER_COLOR)) + return colorA; + + final Color colorB = getItemColor(itemB); + return colorB != null ? colorB : colorA; + } + + private Color getItemColor(final ItemStack itemStack) + { + if (itemStack == null || !itemStack.hasItemMeta()) + return null; + + if (!(itemStack.getItemMeta() instanceof LeatherArmorMeta)) + return null; + + return ((LeatherArmorMeta) itemStack.getItemMeta()).getColor(); + } + /** * Attempts to move an item to a player's inventory. * diff --git a/src/main/java/nl/pim16aap2/armoredElytra/handlers/SmithingTableListener.java b/src/main/java/nl/pim16aap2/armoredElytra/handlers/SmithingTableListener.java index 2cafdaa..9849efc 100644 --- a/src/main/java/nl/pim16aap2/armoredElytra/handlers/SmithingTableListener.java +++ b/src/main/java/nl/pim16aap2/armoredElytra/handlers/SmithingTableListener.java @@ -3,6 +3,7 @@ package nl.pim16aap2.armoredElytra.handlers; import nl.pim16aap2.armoredElytra.ArmoredElytra; import nl.pim16aap2.armoredElytra.util.ArmorTier; import nl.pim16aap2.armoredElytra.util.EnchantmentContainer; +import org.bukkit.Color; import org.bukkit.Material; import org.bukkit.entity.Player; import org.bukkit.event.Listener; @@ -47,10 +48,11 @@ abstract class SmithingTableListener extends ArmoredElytraHandler implements Lis EnchantmentContainer enchantments = EnchantmentContainer.getEnchantments(itemStackA, plugin); enchantments.merge(EnchantmentContainer.getEnchantments(itemStackB, plugin)); + final Color color = getItemColor(itemStackA, itemStackB); result = ArmoredElytra.getInstance().getNbtEditor() .addArmorNBTTags(new ItemStack(Material.ELYTRA, 1), newTier, - plugin.getConfigLoader().unbreakable()); + plugin.getConfigLoader().unbreakable(), color); enchantments.applyEnchantments(result); event.setResult(result); } diff --git a/src/main/java/nl/pim16aap2/armoredElytra/nbtEditor/INBTEditor.java b/src/main/java/nl/pim16aap2/armoredElytra/nbtEditor/INBTEditor.java index 6ab5196..9bf62b6 100644 --- a/src/main/java/nl/pim16aap2/armoredElytra/nbtEditor/INBTEditor.java +++ b/src/main/java/nl/pim16aap2/armoredElytra/nbtEditor/INBTEditor.java @@ -2,6 +2,7 @@ package nl.pim16aap2.armoredElytra.nbtEditor; import nl.pim16aap2.armoredElytra.ArmoredElytra; import nl.pim16aap2.armoredElytra.util.ArmorTier; +import org.bukkit.Color; import org.bukkit.inventory.ItemStack; public interface INBTEditor @@ -16,7 +17,10 @@ public interface INBTEditor * @param unbreakable Whether the resulting item should be unbreakable. * @return The NEW item. */ - ItemStack addArmorNBTTags(ItemStack item, ArmorTier armorTier, boolean unbreakable); + default ItemStack addArmorNBTTags(ItemStack item, ArmorTier armorTier, boolean unbreakable) + { + return addArmorNBTTags(item, armorTier, unbreakable, (Color) null); + } /** * Adds a given {@link ArmorTier} to an item. The item will be cloned. Note that setting the armor tier to {@link @@ -28,7 +32,41 @@ public interface INBTEditor * @param name The name fo the item. * @return The NEW item. */ - ItemStack addArmorNBTTags(ItemStack item, ArmorTier armorTier, boolean unbreakable, final String name); + default ItemStack addArmorNBTTags(ItemStack item, ArmorTier armorTier, boolean unbreakable, final String name) + { + return addArmorNBTTags(item, armorTier, unbreakable, name, null); + } + + /** + * 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). 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. + * @param unbreakable Whether the resulting item should be unbreakable. + * @param color The color of the armor to store. May be null. + * @return The NEW item. + */ + default ItemStack addArmorNBTTags(ItemStack item, ArmorTier armorTier, boolean unbreakable, final Color color) + { + return addArmorNBTTags(item, armorTier, unbreakable, + ArmoredElytra.getInstance().getArmoredElytraName(armorTier), color); + } + + /** + * 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. + * @param color The color of the armor to store. May be null. + * @return The NEW item. + */ + ItemStack addArmorNBTTags(ItemStack item, ArmorTier armorTier, boolean unbreakable, final String name, + final Color color); /** * 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 e313c38..49fce7f 100644 --- a/src/main/java/nl/pim16aap2/armoredElytra/nbtEditor/NBTEditor.java +++ b/src/main/java/nl/pim16aap2/armoredElytra/nbtEditor/NBTEditor.java @@ -3,6 +3,7 @@ package nl.pim16aap2.armoredElytra.nbtEditor; import nl.pim16aap2.armoredElytra.ArmoredElytra; import nl.pim16aap2.armoredElytra.util.ArmorTier; import org.bukkit.Bukkit; +import org.bukkit.Color; import org.bukkit.NamespacedKey; import org.bukkit.attribute.Attribute; import org.bukkit.attribute.AttributeModifier; @@ -18,16 +19,12 @@ public class NBTEditor implements INBTEditor { private static final NamespacedKey armorTierKey = new NamespacedKey(ArmoredElytra.getInstance(), "ARMOR_TIER_LEVEL"); + private static final NamespacedKey armorColorKey = new NamespacedKey(ArmoredElytra.getInstance(), + "ARMORED_ELYTRA_COLOR"); @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, final String name) + public ItemStack addArmorNBTTags(ItemStack item, ArmorTier armorTier, boolean unbreakable, final String name, + final Color color) { if (armorTier == null || armorTier == ArmorTier.NONE) return new ItemStack(item); @@ -37,6 +34,8 @@ public class NBTEditor implements INBTEditor if (meta == null) throw new IllegalArgumentException("Tried to add armor to invalid item: " + item); meta.getPersistentDataContainer().set(armorTierKey, PersistentDataType.INTEGER, ArmorTier.getTierID(armorTier)); + if (color != null) + meta.getPersistentDataContainer().set(armorColorKey, PersistentDataType.INTEGER, color.asRGB()); overwriteNBTValue(meta, Attribute.GENERIC_ARMOR, ArmorTier.getArmor(armorTier), "generic.armor"); if (ArmorTier.getToughness(armorTier) > 0)