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:
Pim van der Loos 2021-05-18 16:31:11 +02:00
parent fb449397a3
commit 8f1a8baf76
No known key found for this signature in database
GPG Key ID: C16F020ADAE6D5A8
3 changed files with 37 additions and 3 deletions

View File

@ -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();

View File

@ -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);
}

View File

@ -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);
}
}