Support item stacking, cleanup

This commit is contained in:
themode 2021-04-02 18:25:20 +02:00
parent 22be400fb3
commit 7dcc52de57
4 changed files with 24 additions and 15 deletions

View File

@ -84,13 +84,27 @@ public class ItemMeta implements Cloneable {
return customModelData; return customModelData;
} }
public NBTCompound toNBT() { public @NotNull NBTCompound toNBT() {
if (cache == null) { if (cache == null) {
this.cache = NBTUtils.metaToNBT(this); this.cache = NBTUtils.metaToNBT(this);
} }
return cache; return cache;
} }
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
ItemMeta itemMeta = (ItemMeta) o;
return toNBT().equals(itemMeta.toNBT());
}
@Override
public int hashCode() {
return toNBT().hashCode();
}
protected @NotNull ItemMetaBuilder builder() { protected @NotNull ItemMetaBuilder builder() {
return builder.clone(); return builder.clone();
} }

View File

@ -14,7 +14,7 @@ import java.util.function.UnaryOperator;
public class ItemStack { public class ItemStack {
public static final ItemStack AIR = ItemStack.builder(Material.AIR).build(); public static final ItemStack AIR = ItemStack.of(Material.AIR);
private final UUID uuid = UUID.randomUUID(); private final UUID uuid = UUID.randomUUID();
private final StackingRule stackingRule = new VanillaStackingRule(64); private final StackingRule stackingRule = new VanillaStackingRule(64);
@ -34,7 +34,7 @@ public class ItemStack {
return new ItemBuilder(material); return new ItemBuilder(material);
} }
@Contract(value = "_ -> new", pure = true) @Contract(value = "_ ,_ -> new", pure = true)
public static @NotNull ItemStack of(@NotNull Material material, int amount) { public static @NotNull ItemStack of(@NotNull Material material, int amount) {
return builder(material).amount(amount).build(); return builder(material).amount(amount).build();
} }

View File

@ -24,7 +24,6 @@ import net.minestom.server.utils.chunk.ChunkUtils;
import net.minestom.server.world.biomes.Biome; import net.minestom.server.world.biomes.Biome;
import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable; import org.jetbrains.annotations.Nullable;
import org.jglrxavpok.hephaistos.nbt.NBT;
import org.jglrxavpok.hephaistos.nbt.NBTCompound; import org.jglrxavpok.hephaistos.nbt.NBTCompound;
import org.jglrxavpok.hephaistos.nbt.NBTException; import org.jglrxavpok.hephaistos.nbt.NBTException;

View File

@ -6,15 +6,11 @@ import net.kyori.adventure.text.serializer.gson.GsonComponentSerializer;
import net.kyori.adventure.util.Codec; import net.kyori.adventure.util.Codec;
import net.minestom.server.MinecraftServer; import net.minestom.server.MinecraftServer;
import net.minestom.server.adventure.AdventureSerializer; import net.minestom.server.adventure.AdventureSerializer;
import net.minestom.server.attribute.Attribute;
import net.minestom.server.attribute.AttributeOperation;
import net.minestom.server.data.Data;
import net.minestom.server.inventory.Inventory; import net.minestom.server.inventory.Inventory;
import net.minestom.server.item.Enchantment; import net.minestom.server.item.Enchantment;
import net.minestom.server.item.ItemMeta; import net.minestom.server.item.ItemMeta;
import net.minestom.server.item.ItemStack; import net.minestom.server.item.ItemStack;
import net.minestom.server.item.Material; import net.minestom.server.item.Material;
import net.minestom.server.item.attribute.AttributeSlot;
import net.minestom.server.item.attribute.ItemAttribute; import net.minestom.server.item.attribute.ItemAttribute;
import net.minestom.server.registry.Registries; import net.minestom.server.registry.Registries;
import net.minestom.server.utils.binary.BinaryReader; import net.minestom.server.utils.binary.BinaryReader;
@ -28,8 +24,6 @@ import org.slf4j.LoggerFactory;
import java.io.IOException; import java.io.IOException;
import java.io.StringReader; import java.io.StringReader;
import java.util.ArrayList;
import java.util.List;
import java.util.Map; import java.util.Map;
import java.util.UUID; import java.util.UUID;
@ -286,7 +280,7 @@ public final class NBTUtils {
} }
} }
public static NBTCompound metaToNBT(@NotNull ItemMeta itemMeta) { public static @NotNull NBTCompound metaToNBT(@NotNull ItemMeta itemMeta) {
final NBTCompound itemNBT = new NBTCompound(); final NBTCompound itemNBT = new NBTCompound();
// Unbreakable // Unbreakable
@ -302,7 +296,7 @@ public final class NBTUtils {
} }
} }
// Display // Start display
{ {
final var displayName = itemMeta.getDisplayName(); final var displayName = itemMeta.getDisplayName();
final var lore = itemMeta.getLore(); final var lore = itemMeta.getLore();
@ -326,14 +320,16 @@ public final class NBTUtils {
itemNBT.set("display", displayNBT); itemNBT.set("display", displayNBT);
} }
} }
// End display
// Enchantment // Start enchantment
{ {
final var enchantmentMap = itemMeta.getEnchantmentMap(); final var enchantmentMap = itemMeta.getEnchantmentMap();
if (!enchantmentMap.isEmpty()) { if (!enchantmentMap.isEmpty()) {
NBTUtils.writeEnchant(itemNBT, "Enchantments", enchantmentMap); NBTUtils.writeEnchant(itemNBT, "Enchantments", enchantmentMap);
} }
} }
// End enchantment
// Start attribute // Start attribute
{ {
@ -358,14 +354,14 @@ public final class NBTUtils {
} }
// End attribute // End attribute
// Start hide flags // Start hide flag
{ {
final int hideFlag = itemMeta.getHideFlag(); final int hideFlag = itemMeta.getHideFlag();
if (hideFlag != 0) { if (hideFlag != 0) {
itemNBT.setInt("HideFlags", hideFlag); itemNBT.setInt("HideFlags", hideFlag);
} }
} }
// End hide flags // End hide flag
// Start custom model data // Start custom model data
{ {