Fix original nbt overwriting modified meta

This commit is contained in:
themode 2021-04-04 21:34:48 +02:00
parent d34d3a2b50
commit 5a5531723c
4 changed files with 18 additions and 22 deletions

View File

@ -9,10 +9,7 @@ import org.jetbrains.annotations.Nullable;
import org.jglrxavpok.hephaistos.nbt.NBTCompound; import org.jglrxavpok.hephaistos.nbt.NBTCompound;
import java.lang.ref.SoftReference; import java.lang.ref.SoftReference;
import java.util.ArrayList; import java.util.*;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.function.Consumer; import java.util.function.Consumer;
public class ItemMeta { public class ItemMeta {
@ -110,14 +107,10 @@ public class ItemMeta {
} }
public @NotNull NBTCompound toNBT() { public @NotNull NBTCompound toNBT() {
if (originalNbt != null) {
// Return the nbt this meta has been created with
return originalNbt;
}
var nbt = cache != null ? cache.get() : null; var nbt = cache != null ? cache.get() : null;
if (nbt == null) { if (nbt == null) {
nbt = NBTUtils.metaToNBT(this); nbt = Objects.requireNonNullElseGet(originalNbt, NBTCompound::new);
NBTUtils.writeMetaNBT(this, nbt);
this.builder.write(nbt); this.builder.write(nbt);
this.cache = new SoftReference<>(nbt); this.cache = new SoftReference<>(nbt);
} }

View File

@ -193,7 +193,7 @@ public class CrossbowMeta extends ItemMeta {
@NotNull @NotNull
private NBTCompound getItemCompound(@NotNull ItemStack itemStack) { private NBTCompound getItemCompound(@NotNull ItemStack itemStack) {
NBTCompound compound = NBTUtils.metaToNBT(itemStack.getMeta()); NBTCompound compound = NBTUtils.getMetaNBT(itemStack.getMeta());
compound.setByte("Count", (byte) itemStack.getAmount()); compound.setByte("Count", (byte) itemStack.getAmount());
compound.setString("id", itemStack.getMaterial().getName()); compound.setString("id", itemStack.getMaterial().getName());
return compound; return compound;

View File

@ -86,7 +86,7 @@ public final class NBTUtils {
final ItemStack stack = inventory.getItemStack(i); final ItemStack stack = inventory.getItemStack(i);
NBTCompound nbt = new NBTCompound(); NBTCompound nbt = new NBTCompound();
NBTCompound tag = metaToNBT(stack.getMeta()); NBTCompound tag = getMetaNBT(stack.getMeta());
nbt.set("tag", tag); nbt.set("tag", tag);
nbt.setByte("Slot", (byte) i); nbt.setByte("Slot", (byte) i);
@ -290,9 +290,7 @@ public final class NBTUtils {
} }
} }
public static @NotNull NBTCompound metaToNBT(@NotNull ItemMeta itemMeta) { public static void writeMetaNBT(@NotNull ItemMeta itemMeta, @NotNull NBTCompound itemNBT) {
final NBTCompound itemNBT = new NBTCompound();
// Unbreakable // Unbreakable
if (itemMeta.isUnbreakable()) { if (itemMeta.isUnbreakable()) {
itemNBT.setInt("Unbreakable", 1); itemNBT.setInt("Unbreakable", 1);
@ -381,8 +379,12 @@ public final class NBTUtils {
} }
} }
// End custom model data // End custom model data
}
return itemNBT; public static @NotNull NBTCompound getMetaNBT(@NotNull ItemMeta itemMeta) {
var nbt = new NBTCompound();
writeMetaNBT(itemMeta, nbt);
return nbt;
} }
@FunctionalInterface @FunctionalInterface

View File

@ -27,7 +27,6 @@ import net.minestom.server.inventory.Inventory;
import net.minestom.server.inventory.InventoryType; import net.minestom.server.inventory.InventoryType;
import net.minestom.server.inventory.PlayerInventory; import net.minestom.server.inventory.PlayerInventory;
import net.minestom.server.item.ItemStack; import net.minestom.server.item.ItemStack;
import net.minestom.server.item.ItemStore;
import net.minestom.server.item.ItemTag; import net.minestom.server.item.ItemTag;
import net.minestom.server.item.Material; import net.minestom.server.item.Material;
import net.minestom.server.item.meta.CompassMeta; import net.minestom.server.item.meta.CompassMeta;
@ -38,6 +37,7 @@ import net.minestom.server.utils.Vector;
import net.minestom.server.utils.time.TimeUnit; import net.minestom.server.utils.time.TimeUnit;
import net.minestom.server.world.DimensionType; import net.minestom.server.world.DimensionType;
import java.util.ArrayList;
import java.util.Collection; import java.util.Collection;
import java.util.UUID; import java.util.UUID;
import java.util.concurrent.ThreadLocalRandom; import java.util.concurrent.ThreadLocalRandom;
@ -241,13 +241,14 @@ public class PlayerInit {
{ {
ItemStack item = ItemStack.builder(Material.DIAMOND_CHESTPLATE) ItemStack item = ItemStack.builder(Material.DIAMOND_CHESTPLATE)
.displayName(Component.text("test")) .displayName(Component.text("test"))
.lore(Component.text("lore"))
.build(); .build();
//inventory.setChestplate(item); inventory.setChestplate(item.withLore(components -> {
var list = new ArrayList<>(components);
list.add(Component.text("hey"));
return list;
}));
} }
player.openInventory(PlayerInit.inventory);
//player.getInventory().addItemStack(new ItemStack(Material.STONE, (byte) 32));
}); });
globalEventHandler.addEventCallback(PlayerBlockBreakEvent.class, event -> { globalEventHandler.addEventCallback(PlayerBlockBreakEvent.class, event -> {