From a43a44ee777308de165d597b8406caaeddf9df09 Mon Sep 17 00:00:00 2001 From: iTwins Date: Sun, 6 Aug 2023 01:29:36 +0200 Subject: [PATCH 1/3] Fix #3931 --- .../tasks/armor/RainbowArmorTask.java | 13 ++++--------- 1 file changed, 4 insertions(+), 9 deletions(-) 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..9b8f1eb28c 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 @@ -7,7 +7,7 @@ import org.bukkit.inventory.ItemStack; import org.bukkit.inventory.meta.LeatherArmorMeta; -import io.github.thebusybiscuit.slimefun4.api.items.HashedArmorpiece; +import io.github.thebusybiscuit.slimefun4.api.items.SlimefunItem; import io.github.thebusybiscuit.slimefun4.api.player.PlayerProfile; import io.github.thebusybiscuit.slimefun4.implementation.items.armor.RainbowArmorPiece; @@ -30,15 +30,10 @@ protected void onTick() { protected void onPlayerTick(Player p, PlayerProfile profile) { for (int i = 0; i < 4; i++) { ItemStack item = p.getInventory().getArmorContents()[i]; + SlimefunItem sfItem = SlimefunItem.getByItem(item); - 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); - } - }); + if (sfItem instanceof RainbowArmorPiece rainbowArmorPiece && rainbowArmorPiece.canUse(p, true)) { + updateRainbowArmor(item, rainbowArmorPiece); } } } From 5ef2296f6f6c8dc11da9be0c861d429ff965cbf1 Mon Sep 17 00:00:00 2001 From: iTwins Date: Wed, 9 Aug 2023 20:30:57 +0200 Subject: [PATCH 2/3] optimized to use HashedArmorpiece instead of getByItem --- .../slimefun4/api/items/HashedArmorpiece.java | 24 +++++++++++++++++++ .../tasks/armor/RainbowArmorTask.java | 15 +++++++----- 2 files changed, 33 insertions(+), 6 deletions(-) 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..9d5aa6441d 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,9 @@ 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()); + } copy.setItemMeta(meta); this.hash = copy.hashCode(); } @@ -77,12 +84,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 9b8f1eb28c..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; @@ -7,9 +9,10 @@ import org.bukkit.inventory.ItemStack; import org.bukkit.inventory.meta.LeatherArmorMeta; -import io.github.thebusybiscuit.slimefun4.api.items.SlimefunItem; +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,12 +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]; - SlimefunItem sfItem = SlimefunItem.getByItem(item); - - if (sfItem 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); } } } From 6d67b86960fb8a970eeb9a8bc8b10d6156ceb1dc Mon Sep 17 00:00:00 2001 From: iTwins Date: Fri, 11 Aug 2023 18:22:14 +0200 Subject: [PATCH 3/3] fixed not emptying optional --- .../thebusybiscuit/slimefun4/api/items/HashedArmorpiece.java | 2 ++ 1 file changed, 2 insertions(+) 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 9d5aa6441d..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 @@ -63,6 +63,8 @@ public void update(@Nullable ItemStack stack, @Nullable SlimefunItem item) { ((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();