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 java.lang.ref.SoftReference;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.*;
import java.util.function.Consumer;
public class ItemMeta {
@ -110,14 +107,10 @@ public class ItemMeta {
}
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;
if (nbt == null) {
nbt = NBTUtils.metaToNBT(this);
nbt = Objects.requireNonNullElseGet(originalNbt, NBTCompound::new);
NBTUtils.writeMetaNBT(this, nbt);
this.builder.write(nbt);
this.cache = new SoftReference<>(nbt);
}

View File

@ -193,7 +193,7 @@ public class CrossbowMeta extends ItemMeta {
@NotNull
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.setString("id", itemStack.getMaterial().getName());
return compound;

View File

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

View File

@ -27,7 +27,6 @@ import net.minestom.server.inventory.Inventory;
import net.minestom.server.inventory.InventoryType;
import net.minestom.server.inventory.PlayerInventory;
import net.minestom.server.item.ItemStack;
import net.minestom.server.item.ItemStore;
import net.minestom.server.item.ItemTag;
import net.minestom.server.item.Material;
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.world.DimensionType;
import java.util.ArrayList;
import java.util.Collection;
import java.util.UUID;
import java.util.concurrent.ThreadLocalRandom;
@ -241,13 +241,14 @@ public class PlayerInit {
{
ItemStack item = ItemStack.builder(Material.DIAMOND_CHESTPLATE)
.displayName(Component.text("test"))
.lore(Component.text("lore"))
.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 -> {