Add ItemTag

This commit is contained in:
themode 2021-04-03 15:45:46 +02:00
parent f52e10c2b2
commit 4741c932a0
5 changed files with 69 additions and 9 deletions

View File

@ -1,9 +1,9 @@
package net.minestom.server.item; package net.minestom.server.item;
/** /**
* Represents a flag which can be applied to an {@link ItemStack} using {@link ItemStack#addItemFlags(ItemFlag...)}. * Represents a hide flag which can be applied to an {@link ItemStack} using {@link ItemMetaBuilder#hideFlag(int)}.
*/ */
public enum ItemFlag { public enum ItemHideFlag {
HIDE_ENCHANTS(1), HIDE_ENCHANTS(1),
HIDE_ATTRIBUTES(2), HIDE_ATTRIBUTES(2),
HIDE_UNBREAKABLE(4), HIDE_UNBREAKABLE(4),
@ -13,7 +13,7 @@ public enum ItemFlag {
private final int bitFieldPart; private final int bitFieldPart;
ItemFlag(int bit) { ItemHideFlag(int bit) {
this.bitFieldPart = bit; this.bitFieldPart = bit;
} }

View File

@ -95,6 +95,15 @@ public class ItemMeta {
return customModelData; return customModelData;
} }
@Contract("_, null -> null; _, !null -> !null")
public <T> T getOrDefault(@NotNull ItemTag<T> tag, @Nullable T defaultValue) {
return tag.read(toNBT());
}
public <T> @Nullable T get(@NotNull ItemTag<T> tag) {
return tag.read(toNBT());
}
public @NotNull NBTCompound toNBT() { public @NotNull NBTCompound toNBT() {
if (originalNbt != null) { if (originalNbt != null) {
// Return the nbt this meta has been created with // Return the nbt this meta has been created with
@ -127,6 +136,6 @@ public class ItemMeta {
@Contract(value = "-> new", pure = true) @Contract(value = "-> new", pure = true)
protected @NotNull ItemMetaBuilder builder() { protected @NotNull ItemMetaBuilder builder() {
return builder.clone(); return ItemMetaBuilder.fromNBT(builder, toNBT());
} }
} }

View File

@ -90,6 +90,22 @@ public abstract class ItemMetaBuilder implements Cloneable {
return this; return this;
} }
public <T> @NotNull ItemMetaBuilder set(@NotNull ItemTag<T> tag, @Nullable T value) {
if (originalNBT != null) {
// Item is from nbt
if (value != null) {
tag.write(originalNBT, value);
} else {
this.originalNBT.removeTag(tag.getKey());
}
return this;
} else {
// Create item meta based on nbt
var currentNbt = build().toNBT();
return fromNBT(this, currentNbt).set(tag, value);
}
}
@Contract("-> new") @Contract("-> new")
public abstract @NotNull ItemMeta build(); public abstract @NotNull ItemMeta build();

View File

@ -0,0 +1,36 @@
package net.minestom.server.item;
import org.jetbrains.annotations.NotNull;
import org.jglrxavpok.hephaistos.nbt.NBTCompound;
public abstract class ItemTag<T> {
private final String key;
private ItemTag(@NotNull String key) {
this.key = key;
}
public @NotNull String getKey() {
return key;
}
protected abstract T read(@NotNull NBTCompound nbtCompound);
protected abstract void write(@NotNull NBTCompound nbtCompound, @NotNull T value);
public static @NotNull ItemTag<Integer> Integer(@NotNull String key) {
return new ItemTag<>(key) {
@Override
protected Integer read(@NotNull NBTCompound nbtCompound) {
return nbtCompound.getInt(key);
}
@Override
protected void write(@NotNull NBTCompound nbtCompound, @NotNull Integer value) {
nbtCompound.setInt(key, value);
}
};
}
}

View File

@ -27,6 +27,7 @@ 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.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;
import net.minestom.server.network.ConnectionManager; import net.minestom.server.network.ConnectionManager;
@ -69,12 +70,10 @@ public class PlayerInit {
.build(); .build();
ItemStack itemStack = ItemStack.builder(Material.COMPASS) ItemStack itemStack = ItemStack.builder(Material.COMPASS)
.amount(5)
.meta(compassMeta)
.meta(CompassMeta.class, builder -> { .meta(CompassMeta.class, builder -> {
builder.lodestonePosition(new Position(0, 0, 0)); builder.lodestonePosition(new Position(0, 0, 0));
builder.set(ItemTag.Integer("int"), 25);
}) })
.displayName(Component.text("displayName"))
.build(); .build();
itemStack = itemStack.with(itemBuilder -> itemBuilder itemStack = itemStack.with(itemBuilder -> itemBuilder
@ -225,8 +224,8 @@ public class PlayerInit {
PlayerInventory inventory = player.getInventory(); PlayerInventory inventory = player.getInventory();
ItemStack itemStack = ItemStack.of(Material.STONE, 64); ItemStack itemStack = ItemStack.of(Material.STONE, 64);
inventory.addItemStack(itemStack); inventory.addItemStack(itemStack.withMeta(metaBuilder -> metaBuilder.set(ItemTag.Integer("int"), 25)));
inventory.replaceItemStack(0, i -> i.withAmount(32)); //inventory.replaceItemStack(0, i -> i.withAmount(32));
{ {
ItemStack item = ItemStack.builder(Material.DIAMOND_CHESTPLATE) ItemStack item = ItemStack.builder(Material.DIAMOND_CHESTPLATE)