Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix rainbow armor recoloring other leather armor #3936

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -31,6 +33,7 @@ public final class HashedArmorpiece {

private int hash;
private Optional<SlimefunArmorPiece> item;
private Optional<Color> color;

/**
* This initializes a new {@link HashedArmorpiece} with no {@link SlimefunArmorPiece}
Expand All @@ -39,6 +42,7 @@ public final class HashedArmorpiece {
public HashedArmorpiece() {
this.hash = 0;
this.item = Optional.empty();
this.color = Optional.empty();
}

/**
Expand All @@ -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();
}
Expand All @@ -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;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package io.github.thebusybiscuit.slimefun4.implementation.tasks.armor;

import java.util.Optional;

import javax.annotation.ParametersAreNonnullByDefault;

import org.bukkit.Color;
Expand All @@ -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.
Expand All @@ -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<SlimefunArmorPiece> 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);
}
}
}
Expand Down