Store armor color in NBT

- When a new AE is constructed using leather armor, the armor's color is now stored in the chestplate. Note that this is just an initial implementation, as enchanting/repairing will currently remove the color. This will be fixed in a future commit.
This commit is contained in:
Pim van der Loos 2021-05-17 21:54:08 +02:00
parent bea45dde30
commit fb449397a3
No known key found for this signature in database
GPG Key ID: C16F020ADAE6D5A8
5 changed files with 89 additions and 12 deletions

View File

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

View File

@ -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.
* <p>
* 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.
*

View File

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

View File

@ -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.

View File

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