Skip to content

Commit

Permalink
optimized to use HashedArmorpiece instead of getByItem
Browse files Browse the repository at this point in the history
  • Loading branch information
iTwins committed Aug 9, 2023
1 parent a43a44e commit 5ef2296
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 6 deletions.
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,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();
}
Expand All @@ -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;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,15 +1,18 @@
package io.github.thebusybiscuit.slimefun4.implementation.tasks.armor;

import java.util.Optional;

import javax.annotation.ParametersAreNonnullByDefault;

import org.bukkit.Color;
import org.bukkit.entity.Player;
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.
Expand All @@ -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<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

0 comments on commit 5ef2296

Please sign in to comment.