mirror of
https://github.com/Minestom/Minestom.git
synced 2025-01-08 17:37:42 +01:00
Add ItemTag
This commit is contained in:
parent
f52e10c2b2
commit
4741c932a0
@ -1,9 +1,9 @@
|
||||
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_ATTRIBUTES(2),
|
||||
HIDE_UNBREAKABLE(4),
|
||||
@ -13,7 +13,7 @@ public enum ItemFlag {
|
||||
|
||||
private final int bitFieldPart;
|
||||
|
||||
ItemFlag(int bit) {
|
||||
ItemHideFlag(int bit) {
|
||||
this.bitFieldPart = bit;
|
||||
}
|
||||
|
@ -95,6 +95,15 @@ public class ItemMeta {
|
||||
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() {
|
||||
if (originalNbt != null) {
|
||||
// Return the nbt this meta has been created with
|
||||
@ -127,6 +136,6 @@ public class ItemMeta {
|
||||
|
||||
@Contract(value = "-> new", pure = true)
|
||||
protected @NotNull ItemMetaBuilder builder() {
|
||||
return builder.clone();
|
||||
return ItemMetaBuilder.fromNBT(builder, toNBT());
|
||||
}
|
||||
}
|
||||
|
@ -90,6 +90,22 @@ public abstract class ItemMetaBuilder implements Cloneable {
|
||||
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")
|
||||
public abstract @NotNull ItemMeta build();
|
||||
|
||||
|
36
src/main/java/net/minestom/server/item/ItemTag.java
Normal file
36
src/main/java/net/minestom/server/item/ItemTag.java
Normal 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);
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
}
|
@ -27,6 +27,7 @@ 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.ItemTag;
|
||||
import net.minestom.server.item.Material;
|
||||
import net.minestom.server.item.meta.CompassMeta;
|
||||
import net.minestom.server.network.ConnectionManager;
|
||||
@ -69,12 +70,10 @@ public class PlayerInit {
|
||||
.build();
|
||||
|
||||
ItemStack itemStack = ItemStack.builder(Material.COMPASS)
|
||||
.amount(5)
|
||||
.meta(compassMeta)
|
||||
.meta(CompassMeta.class, builder -> {
|
||||
builder.lodestonePosition(new Position(0, 0, 0));
|
||||
builder.set(ItemTag.Integer("int"), 25);
|
||||
})
|
||||
.displayName(Component.text("displayName"))
|
||||
.build();
|
||||
|
||||
itemStack = itemStack.with(itemBuilder -> itemBuilder
|
||||
@ -225,8 +224,8 @@ public class PlayerInit {
|
||||
|
||||
PlayerInventory inventory = player.getInventory();
|
||||
ItemStack itemStack = ItemStack.of(Material.STONE, 64);
|
||||
inventory.addItemStack(itemStack);
|
||||
inventory.replaceItemStack(0, i -> i.withAmount(32));
|
||||
inventory.addItemStack(itemStack.withMeta(metaBuilder -> metaBuilder.set(ItemTag.Integer("int"), 25)));
|
||||
//inventory.replaceItemStack(0, i -> i.withAmount(32));
|
||||
|
||||
{
|
||||
ItemStack item = ItemStack.builder(Material.DIAMOND_CHESTPLATE)
|
||||
|
Loading…
Reference in New Issue
Block a user