mirror of
https://github.com/Minestom/Minestom.git
synced 2025-03-02 11:21:15 +01:00
Implement ItemStack#equals & use a soft reference to store meta cached NBT
This commit is contained in:
parent
7dcc52de57
commit
2b0c525ca2
@ -8,6 +8,7 @@ import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
import org.jglrxavpok.hephaistos.nbt.NBTCompound;
|
||||
|
||||
import java.lang.ref.SoftReference;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
@ -29,7 +30,7 @@ public class ItemMeta implements Cloneable {
|
||||
|
||||
private final int customModelData;
|
||||
|
||||
private NBTCompound cache = null;
|
||||
private SoftReference<NBTCompound> cache;
|
||||
|
||||
protected ItemMeta(@NotNull ItemMetaBuilder metaBuilder) {
|
||||
this.builder = metaBuilder.clone();
|
||||
@ -85,10 +86,12 @@ public class ItemMeta implements Cloneable {
|
||||
}
|
||||
|
||||
public @NotNull NBTCompound toNBT() {
|
||||
if (cache == null) {
|
||||
this.cache = NBTUtils.metaToNBT(this);
|
||||
var nbt = cache != null ? cache.get() : null;
|
||||
if (nbt == null) {
|
||||
nbt = NBTUtils.metaToNBT(this);
|
||||
cache = new SoftReference<>(nbt);
|
||||
}
|
||||
return cache;
|
||||
return nbt;
|
||||
}
|
||||
|
||||
@Override
|
||||
|
@ -111,21 +111,48 @@ public class ItemStack {
|
||||
return withLore(loreUnaryOperator.apply(getLore()));
|
||||
}
|
||||
|
||||
@Contract(pure = true)
|
||||
public @NotNull StackingRule getStackingRule() {
|
||||
return stackingRule;
|
||||
}
|
||||
|
||||
@Contract(pure = true)
|
||||
public @NotNull ItemMeta getMeta() {
|
||||
return meta;
|
||||
}
|
||||
|
||||
@Contract(pure = true)
|
||||
public boolean isAir() {
|
||||
return equals(AIR);
|
||||
}
|
||||
|
||||
@Contract(pure = true)
|
||||
public boolean isSimilar(@NotNull ItemStack itemStack) {
|
||||
return material.equals(itemStack.material) &&
|
||||
meta.equals(itemStack.meta);
|
||||
}
|
||||
|
||||
public boolean isAir() {
|
||||
return equals(AIR);
|
||||
@Override
|
||||
public boolean equals(Object o) {
|
||||
if (this == o) return true;
|
||||
if (o == null || getClass() != o.getClass()) return false;
|
||||
|
||||
ItemStack itemStack = (ItemStack) o;
|
||||
if (uuid.equals(itemStack.uuid)) return true;
|
||||
|
||||
if (amount != itemStack.amount) return false;
|
||||
if (!stackingRule.equals(itemStack.stackingRule)) return false;
|
||||
if (material != itemStack.material) return false;
|
||||
return meta.equals(itemStack.meta);
|
||||
}
|
||||
|
||||
@Override
|
||||
public int hashCode() {
|
||||
int result = stackingRule.hashCode();
|
||||
result = 31 * result + material.hashCode();
|
||||
result = 31 * result + amount;
|
||||
result = 31 * result + meta.hashCode();
|
||||
return result;
|
||||
}
|
||||
|
||||
@Contract(value = "-> new", pure = true)
|
||||
|
Loading…
Reference in New Issue
Block a user