experimental tags, docs, shorten item creation

This commit is contained in:
Matt Worzala 2021-08-30 18:00:57 -04:00
parent 8291639749
commit 5b8051e2e8
No known key found for this signature in database
GPG Key ID: 439DBBE092854841
3 changed files with 19 additions and 4 deletions

View File

@ -80,16 +80,22 @@ public final class ItemStack implements TagReadable, HoverEventSource<HoverEvent
return fromNBT(material, nbtCompound, 1);
}
/**
* Converts this item to an NBT tag containing the id (material), count (amount), and tag (meta).
*
* @param nbtCompound The nbt representation of the item
*/
@ApiStatus.Experimental
public static @NotNull ItemStack fromItemNBT(@NotNull NBTCompound nbtCompound) {
String id = nbtCompound.getString("id");
Check.notNull(id, "Item NBT must contain an id field.");
Material material = Material.fromNamespaceId(id);
Check.notNull(material, "Unknown material: " + id);
Check.notNull(material, "Unknown material: {0}", id);
Byte amount = nbtCompound.getByte("Count");
return fromNBT(material,
nbtCompound.getCompound("tag"),
amount == null ? 0 : amount);
amount == null ? 1 : amount);
}
@Contract(pure = true)
@ -241,6 +247,12 @@ public final class ItemStack implements TagReadable, HoverEventSource<HoverEvent
NBTUtils.asBinaryTagHolder(this.meta.toNBT().getCompound("tag")))));
}
/**
* Converts this item to an NBT tag containing the id (material), count (amount), and tag (meta)
*
* @return The nbt representation of the item
*/
@ApiStatus.Experimental
public @NotNull NBTCompound toItemNBT() {
final NBTCompound nbtCompound = new NBTCompound();
nbtCompound.setString("id", getMaterial().namespace().asString());

View File

@ -3,6 +3,7 @@ package net.minestom.server.item.metadata;
import net.minestom.server.item.ItemMeta;
import net.minestom.server.item.ItemMetaBuilder;
import net.minestom.server.item.ItemStack;
import org.jetbrains.annotations.ApiStatus;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.jglrxavpok.hephaistos.nbt.NBTCompound;
@ -37,12 +38,14 @@ public class BundleMeta extends ItemMeta implements ItemMetaBuilder.Provider<Bun
return this;
}
@ApiStatus.Experimental
public Builder addItem(@NotNull ItemStack item) {
items.add(item);
updateItems();
return this;
}
@ApiStatus.Experimental
public Builder removeItem(@NotNull ItemStack item) {
items.remove(item);
updateItems();

View File

@ -108,8 +108,8 @@ public class PlayerInit {
ItemStack bundle = ItemStack.builder(Material.BUNDLE)
.meta(BundleMeta.class, bundleMetaBuilder -> {
bundleMetaBuilder.addItem(ItemStack.builder(Material.DIAMOND).amount(5).build());
bundleMetaBuilder.addItem(ItemStack.builder(Material.RABBIT_FOOT).amount(5).build());
bundleMetaBuilder.addItem(ItemStack.of(Material.DIAMOND, 5));
bundleMetaBuilder.addItem(ItemStack.of(Material.RABBIT_FOOT, 5));
})
.build();
player.getInventory().addItemStack(bundle);