mirror of
https://github.com/Minestom/Minestom.git
synced 2025-01-02 14:38:26 +01:00
experimental tags, docs, shorten item creation
This commit is contained in:
parent
8291639749
commit
5b8051e2e8
@ -80,16 +80,22 @@ public final class ItemStack implements TagReadable, HoverEventSource<HoverEvent
|
|||||||
return fromNBT(material, nbtCompound, 1);
|
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) {
|
public static @NotNull ItemStack fromItemNBT(@NotNull NBTCompound nbtCompound) {
|
||||||
String id = nbtCompound.getString("id");
|
String id = nbtCompound.getString("id");
|
||||||
Check.notNull(id, "Item NBT must contain an id field.");
|
Check.notNull(id, "Item NBT must contain an id field.");
|
||||||
Material material = Material.fromNamespaceId(id);
|
Material material = Material.fromNamespaceId(id);
|
||||||
Check.notNull(material, "Unknown material: " + id);
|
Check.notNull(material, "Unknown material: {0}", id);
|
||||||
|
|
||||||
Byte amount = nbtCompound.getByte("Count");
|
Byte amount = nbtCompound.getByte("Count");
|
||||||
return fromNBT(material,
|
return fromNBT(material,
|
||||||
nbtCompound.getCompound("tag"),
|
nbtCompound.getCompound("tag"),
|
||||||
amount == null ? 0 : amount);
|
amount == null ? 1 : amount);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Contract(pure = true)
|
@Contract(pure = true)
|
||||||
@ -241,6 +247,12 @@ public final class ItemStack implements TagReadable, HoverEventSource<HoverEvent
|
|||||||
NBTUtils.asBinaryTagHolder(this.meta.toNBT().getCompound("tag")))));
|
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() {
|
public @NotNull NBTCompound toItemNBT() {
|
||||||
final NBTCompound nbtCompound = new NBTCompound();
|
final NBTCompound nbtCompound = new NBTCompound();
|
||||||
nbtCompound.setString("id", getMaterial().namespace().asString());
|
nbtCompound.setString("id", getMaterial().namespace().asString());
|
||||||
|
@ -3,6 +3,7 @@ package net.minestom.server.item.metadata;
|
|||||||
import net.minestom.server.item.ItemMeta;
|
import net.minestom.server.item.ItemMeta;
|
||||||
import net.minestom.server.item.ItemMetaBuilder;
|
import net.minestom.server.item.ItemMetaBuilder;
|
||||||
import net.minestom.server.item.ItemStack;
|
import net.minestom.server.item.ItemStack;
|
||||||
|
import org.jetbrains.annotations.ApiStatus;
|
||||||
import org.jetbrains.annotations.NotNull;
|
import org.jetbrains.annotations.NotNull;
|
||||||
import org.jetbrains.annotations.Nullable;
|
import org.jetbrains.annotations.Nullable;
|
||||||
import org.jglrxavpok.hephaistos.nbt.NBTCompound;
|
import org.jglrxavpok.hephaistos.nbt.NBTCompound;
|
||||||
@ -37,12 +38,14 @@ public class BundleMeta extends ItemMeta implements ItemMetaBuilder.Provider<Bun
|
|||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ApiStatus.Experimental
|
||||||
public Builder addItem(@NotNull ItemStack item) {
|
public Builder addItem(@NotNull ItemStack item) {
|
||||||
items.add(item);
|
items.add(item);
|
||||||
updateItems();
|
updateItems();
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ApiStatus.Experimental
|
||||||
public Builder removeItem(@NotNull ItemStack item) {
|
public Builder removeItem(@NotNull ItemStack item) {
|
||||||
items.remove(item);
|
items.remove(item);
|
||||||
updateItems();
|
updateItems();
|
||||||
|
@ -108,8 +108,8 @@ public class PlayerInit {
|
|||||||
|
|
||||||
ItemStack bundle = ItemStack.builder(Material.BUNDLE)
|
ItemStack bundle = ItemStack.builder(Material.BUNDLE)
|
||||||
.meta(BundleMeta.class, bundleMetaBuilder -> {
|
.meta(BundleMeta.class, bundleMetaBuilder -> {
|
||||||
bundleMetaBuilder.addItem(ItemStack.builder(Material.DIAMOND).amount(5).build());
|
bundleMetaBuilder.addItem(ItemStack.of(Material.DIAMOND, 5));
|
||||||
bundleMetaBuilder.addItem(ItemStack.builder(Material.RABBIT_FOOT).amount(5).build());
|
bundleMetaBuilder.addItem(ItemStack.of(Material.RABBIT_FOOT, 5));
|
||||||
})
|
})
|
||||||
.build();
|
.build();
|
||||||
player.getInventory().addItemStack(bundle);
|
player.getInventory().addItemStack(bundle);
|
||||||
|
Loading…
Reference in New Issue
Block a user