Make AE colors persistent
- Colors of Leather Armored Elytras (as copied from the leather chestplate, if it had a color) is now persistent through AE enchanting/repairing. However, upgrading the AE to a different tier will still remove the data from the AE (as there is no reason to keep it around).
This commit is contained in:
		@@ -76,10 +76,13 @@ abstract class ArmoredElytraHandler
 | 
			
		||||
 | 
			
		||||
    private Color getItemColor(final ItemStack itemStack)
 | 
			
		||||
    {
 | 
			
		||||
        if (itemStack == null || !itemStack.hasItemMeta())
 | 
			
		||||
        if (itemStack == null)
 | 
			
		||||
            return null;
 | 
			
		||||
 | 
			
		||||
        if (!(itemStack.getItemMeta() instanceof LeatherArmorMeta))
 | 
			
		||||
        if (itemStack.getType() == Material.ELYTRA)
 | 
			
		||||
            return ArmoredElytra.getInstance().getNbtEditor().getColorOfArmoredElytra(itemStack);
 | 
			
		||||
 | 
			
		||||
        if (!itemStack.hasItemMeta() || !(itemStack.getItemMeta() instanceof LeatherArmorMeta))
 | 
			
		||||
            return null;
 | 
			
		||||
 | 
			
		||||
        return ((LeatherArmorMeta) itemStack.getItemMeta()).getColor();
 | 
			
		||||
 
 | 
			
		||||
@@ -75,4 +75,14 @@ public interface INBTEditor
 | 
			
		||||
     * @return The {@link ArmorTier} that is on the item. If none is found, {@link ArmorTier#NONE} is returned.
 | 
			
		||||
     */
 | 
			
		||||
    ArmorTier getArmorTier(ItemStack item);
 | 
			
		||||
 | 
			
		||||
    /**
 | 
			
		||||
     * Gets the Color of an armored elytra.
 | 
			
		||||
     * <p>
 | 
			
		||||
     * If the provided {@link ItemStack} is not an AE, null is returned.
 | 
			
		||||
     *
 | 
			
		||||
     * @param item The armored elytra to check.
 | 
			
		||||
     * @return The color of the armored elytra, if the input is a color armored elytra, otherwise null.
 | 
			
		||||
     */
 | 
			
		||||
    Color getColorOfArmoredElytra(ItemStack item);
 | 
			
		||||
}
 | 
			
		||||
 
 | 
			
		||||
@@ -4,12 +4,14 @@ import nl.pim16aap2.armoredElytra.ArmoredElytra;
 | 
			
		||||
import nl.pim16aap2.armoredElytra.util.ArmorTier;
 | 
			
		||||
import org.bukkit.Bukkit;
 | 
			
		||||
import org.bukkit.Color;
 | 
			
		||||
import org.bukkit.Material;
 | 
			
		||||
import org.bukkit.NamespacedKey;
 | 
			
		||||
import org.bukkit.attribute.Attribute;
 | 
			
		||||
import org.bukkit.attribute.AttributeModifier;
 | 
			
		||||
import org.bukkit.inventory.EquipmentSlot;
 | 
			
		||||
import org.bukkit.inventory.ItemStack;
 | 
			
		||||
import org.bukkit.inventory.meta.ItemMeta;
 | 
			
		||||
import org.bukkit.persistence.PersistentDataContainer;
 | 
			
		||||
import org.bukkit.persistence.PersistentDataType;
 | 
			
		||||
 | 
			
		||||
import java.util.Collection;
 | 
			
		||||
@@ -34,7 +36,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)
 | 
			
		||||
 | 
			
		||||
        if (color != null && armorTier == ArmorTier.LEATHER)
 | 
			
		||||
            meta.getPersistentDataContainer().set(armorColorKey, PersistentDataType.INTEGER, color.asRGB());
 | 
			
		||||
 | 
			
		||||
        overwriteNBTValue(meta, Attribute.GENERIC_ARMOR, ArmorTier.getArmor(armorTier), "generic.armor");
 | 
			
		||||
@@ -91,4 +94,22 @@ public class NBTEditor implements INBTEditor
 | 
			
		||||
 | 
			
		||||
        return ArmorTier.NONE;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    @Override
 | 
			
		||||
    public Color getColorOfArmoredElytra(final ItemStack item)
 | 
			
		||||
    {
 | 
			
		||||
        if (item == null || item.getType() != Material.ELYTRA || !item.hasItemMeta())
 | 
			
		||||
            return null;
 | 
			
		||||
 | 
			
		||||
        final ItemMeta meta = item.getItemMeta();
 | 
			
		||||
        if (meta == null)
 | 
			
		||||
            return null;
 | 
			
		||||
 | 
			
		||||
        final PersistentDataContainer container = meta.getPersistentDataContainer();
 | 
			
		||||
        if (!container.has(armorColorKey, PersistentDataType.INTEGER))
 | 
			
		||||
            return null;
 | 
			
		||||
 | 
			
		||||
        final Integer rgb = container.get(armorColorKey, PersistentDataType.INTEGER);
 | 
			
		||||
        return rgb == null ? null : Color.fromRGB(rgb);
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user