diff --git a/src/main/java/io/github/thebusybiscuit/slimefun4/api/items/HashedArmorpiece.java b/src/main/java/io/github/thebusybiscuit/slimefun4/api/items/HashedArmorpiece.java index 92a66e5a2f..eff397a435 100644 --- a/src/main/java/io/github/thebusybiscuit/slimefun4/api/items/HashedArmorpiece.java +++ b/src/main/java/io/github/thebusybiscuit/slimefun4/api/items/HashedArmorpiece.java @@ -5,11 +5,13 @@ import javax.annotation.Nonnull; import javax.annotation.Nullable; +import org.bukkit.Color; import org.bukkit.Material; import org.bukkit.entity.Player; import org.bukkit.inventory.ItemStack; import org.bukkit.inventory.meta.Damageable; import org.bukkit.inventory.meta.ItemMeta; +import org.bukkit.inventory.meta.LeatherArmorMeta; import io.github.thebusybiscuit.slimefun4.implementation.items.armor.SlimefunArmorPiece; import io.github.thebusybiscuit.slimefun4.implementation.tasks.armor.SlimefunArmorTask; @@ -31,6 +33,7 @@ public final class HashedArmorpiece { private int hash; private Optional item; + private Optional color; /** * This initializes a new {@link HashedArmorpiece} with no {@link SlimefunArmorPiece} @@ -39,6 +42,7 @@ public final class HashedArmorpiece { public HashedArmorpiece() { this.hash = 0; this.item = Optional.empty(); + this.color = Optional.empty(); } /** @@ -57,6 +61,11 @@ public void update(@Nullable ItemStack stack, @Nullable SlimefunItem item) { ItemStack copy = stack.clone(); ItemMeta meta = copy.getItemMeta(); ((Damageable) meta).setDamage(0); + if (meta instanceof LeatherArmorMeta leatherArmorMeta) { + color = Optional.of(leatherArmorMeta.getColor()); + } else { + color = Optional.empty(); + } copy.setItemMeta(meta); this.hash = copy.hashCode(); } @@ -77,12 +86,29 @@ public void update(@Nullable ItemStack stack, @Nullable SlimefunItem item) { * @return Whether the {@link HashedArmorpiece} and the given {@link ItemStack} mismatch */ public boolean hasDiverged(@Nullable ItemStack stack) { + return hasDiverged(stack, false); + } + + /** + * This method checks whether the given {@link ItemStack} is no longer similar to the + * one represented by this {@link HashedArmorpiece}. + * + * @param stack + * The {@link ItemStack} to compare + * @param ignoreColor + * Whether to ignore the color of the {@link ItemStack} + * @return Whether the {@link HashedArmorpiece} and the given {@link ItemStack} mismatch + */ + public boolean hasDiverged(@Nullable ItemStack stack, boolean ignoreColor) { if (stack == null || stack.getType() == Material.AIR) { return hash != 0; } else { ItemStack copy = stack.clone(); ItemMeta meta = copy.getItemMeta(); ((Damageable) meta).setDamage(0); + if (ignoreColor && color.isPresent() && meta instanceof LeatherArmorMeta leatherArmorMeta) { + leatherArmorMeta.setColor(color.get()); + } copy.setItemMeta(meta); return copy.hashCode() != hash; } diff --git a/src/main/java/io/github/thebusybiscuit/slimefun4/implementation/tasks/armor/RainbowArmorTask.java b/src/main/java/io/github/thebusybiscuit/slimefun4/implementation/tasks/armor/RainbowArmorTask.java index 7df8b7e846..b95b725cc2 100644 --- a/src/main/java/io/github/thebusybiscuit/slimefun4/implementation/tasks/armor/RainbowArmorTask.java +++ b/src/main/java/io/github/thebusybiscuit/slimefun4/implementation/tasks/armor/RainbowArmorTask.java @@ -1,5 +1,7 @@ package io.github.thebusybiscuit.slimefun4.implementation.tasks.armor; +import java.util.Optional; + import javax.annotation.ParametersAreNonnullByDefault; import org.bukkit.Color; @@ -10,6 +12,7 @@ import io.github.thebusybiscuit.slimefun4.api.items.HashedArmorpiece; import io.github.thebusybiscuit.slimefun4.api.player.PlayerProfile; import io.github.thebusybiscuit.slimefun4.implementation.items.armor.RainbowArmorPiece; +import io.github.thebusybiscuit.slimefun4.implementation.items.armor.SlimefunArmorPiece; /** * The {@link RainbowArmorTask} is responsible for handling the change in color of any Rainbow Armor piece. @@ -28,17 +31,12 @@ protected void onTick() { @Override @ParametersAreNonnullByDefault protected void onPlayerTick(Player p, PlayerProfile profile) { + final ItemStack[] armorContents = p.getInventory().getArmorContents(); + final HashedArmorpiece[] hashedArmorpieces = profile.getArmor(); for (int i = 0; i < 4; i++) { - ItemStack item = p.getInventory().getArmorContents()[i]; - - if (item != null && item.hasItemMeta()) { - HashedArmorpiece armorPiece = profile.getArmor()[i]; - - armorPiece.getItem().ifPresent(sfArmorPiece -> { - if (sfArmorPiece instanceof RainbowArmorPiece rainbowArmorPiece && rainbowArmorPiece.canUse(p, true)) { - updateRainbowArmor(item, rainbowArmorPiece); - } - }); + Optional sfArmorPiece = hashedArmorpieces[i].getItem(); + if (sfArmorPiece.isPresent() && sfArmorPiece.get() instanceof RainbowArmorPiece rainbowArmorPiece && rainbowArmorPiece.canUse(p, true) && armorContents[i].hasItemMeta() && !hashedArmorpieces[i].hasDiverged(armorContents[i], true)) { + updateRainbowArmor(armorContents[i], rainbowArmorPiece); } } }