mirror of
https://github.com/Minestom/Minestom.git
synced 2024-12-30 21:17:53 +01:00
add BundleMeta and full item to/from nbt
This commit is contained in:
parent
ebb179da7c
commit
8291639749
@ -7,6 +7,7 @@ import net.minestom.server.item.rule.VanillaStackingRule;
|
||||
import net.minestom.server.tag.Tag;
|
||||
import net.minestom.server.tag.TagReadable;
|
||||
import net.minestom.server.utils.NBTUtils;
|
||||
import net.minestom.server.utils.validate.Check;
|
||||
import org.jetbrains.annotations.ApiStatus;
|
||||
import org.jetbrains.annotations.Contract;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
@ -79,6 +80,18 @@ public final class ItemStack implements TagReadable, HoverEventSource<HoverEvent
|
||||
return fromNBT(material, nbtCompound, 1);
|
||||
}
|
||||
|
||||
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);
|
||||
|
||||
Byte amount = nbtCompound.getByte("Count");
|
||||
return fromNBT(material,
|
||||
nbtCompound.getCompound("tag"),
|
||||
amount == null ? 0 : amount);
|
||||
}
|
||||
|
||||
@Contract(pure = true)
|
||||
public @NotNull Material getMaterial() {
|
||||
return material;
|
||||
@ -228,6 +241,14 @@ public final class ItemStack implements TagReadable, HoverEventSource<HoverEvent
|
||||
NBTUtils.asBinaryTagHolder(this.meta.toNBT().getCompound("tag")))));
|
||||
}
|
||||
|
||||
public @NotNull NBTCompound toItemNBT() {
|
||||
final NBTCompound nbtCompound = new NBTCompound();
|
||||
nbtCompound.setString("id", getMaterial().namespace().asString());
|
||||
nbtCompound.setByte("Count", (byte) getAmount());
|
||||
nbtCompound.set("tag", getMeta().toNBT());
|
||||
return nbtCompound;
|
||||
}
|
||||
|
||||
@Contract(value = "-> new", pure = true)
|
||||
private @NotNull ItemStackBuilder builder() {
|
||||
return new ItemStackBuilder(material, meta.builder())
|
||||
|
@ -46,6 +46,7 @@ public class ItemStackBuilder {
|
||||
MATERIAL_SUPPLIER_MAP.put(Material.FIREWORK_STAR, FireworkEffectMeta.Builder::new);
|
||||
MATERIAL_SUPPLIER_MAP.put(Material.FIREWORK_ROCKET, FireworkMeta.Builder::new);
|
||||
MATERIAL_SUPPLIER_MAP.put(Material.PLAYER_HEAD, PlayerHeadMeta.Builder::new);
|
||||
MATERIAL_SUPPLIER_MAP.put(Material.BUNDLE, BundleMeta.Builder::new);
|
||||
|
||||
MATERIAL_SUPPLIER_MAP.put(Material.LEATHER_HELMET, LeatherArmorMeta.Builder::new);
|
||||
MATERIAL_SUPPLIER_MAP.put(Material.LEATHER_CHESTPLATE, LeatherArmorMeta.Builder::new);
|
||||
|
@ -0,0 +1,82 @@
|
||||
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.NotNull;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
import org.jglrxavpok.hephaistos.nbt.NBTCompound;
|
||||
import org.jglrxavpok.hephaistos.nbt.NBTList;
|
||||
import org.jglrxavpok.hephaistos.nbt.NBTTypes;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
import java.util.function.Supplier;
|
||||
|
||||
public class BundleMeta extends ItemMeta implements ItemMetaBuilder.Provider<BundleMeta.Builder> {
|
||||
|
||||
private final List<ItemStack> items;
|
||||
|
||||
protected BundleMeta(ItemMetaBuilder metaBuilder,
|
||||
@NotNull List<ItemStack> items) {
|
||||
super(metaBuilder);
|
||||
this.items = List.copyOf(items);
|
||||
}
|
||||
|
||||
public @NotNull List<ItemStack> getItems() {
|
||||
return items;
|
||||
}
|
||||
|
||||
public static class Builder extends ItemMetaBuilder {
|
||||
private List<ItemStack> items = new ArrayList<>();
|
||||
|
||||
public Builder items(@NotNull List<ItemStack> items) {
|
||||
this.items = items;
|
||||
updateItems();
|
||||
return this;
|
||||
}
|
||||
|
||||
public Builder addItem(@NotNull ItemStack item) {
|
||||
items.add(item);
|
||||
updateItems();
|
||||
return this;
|
||||
}
|
||||
|
||||
public Builder removeItem(@NotNull ItemStack item) {
|
||||
items.remove(item);
|
||||
updateItems();
|
||||
return this;
|
||||
}
|
||||
|
||||
@Override
|
||||
public @NotNull BundleMeta build() {
|
||||
return new BundleMeta(this, items);
|
||||
}
|
||||
|
||||
private void updateItems() {
|
||||
mutateNbt(compound -> {
|
||||
NBTList<NBTCompound> itemList = new NBTList<>(NBTTypes.TAG_Compound);
|
||||
for (ItemStack item : items) {
|
||||
itemList.add(item.toItemNBT());
|
||||
}
|
||||
compound.set("Items", itemList);
|
||||
});
|
||||
}
|
||||
|
||||
@Override
|
||||
public void read(@NotNull NBTCompound nbtCompound) {
|
||||
if (nbtCompound.containsKey("Items")) {
|
||||
final NBTList<NBTCompound> items = nbtCompound.getList("Items");
|
||||
for (NBTCompound item : items) {
|
||||
this.items.add(ItemStack.fromItemNBT(item));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
protected @NotNull Supplier<@NotNull ItemMetaBuilder> getSupplier() {
|
||||
return Builder::new;
|
||||
}
|
||||
}
|
||||
}
|
@ -29,6 +29,7 @@ import net.minestom.server.inventory.Inventory;
|
||||
import net.minestom.server.inventory.InventoryType;
|
||||
import net.minestom.server.item.ItemStack;
|
||||
import net.minestom.server.item.Material;
|
||||
import net.minestom.server.item.metadata.BundleMeta;
|
||||
import net.minestom.server.monitoring.BenchmarkManager;
|
||||
import net.minestom.server.monitoring.TickMonitor;
|
||||
import net.minestom.server.utils.MathUtils;
|
||||
@ -104,6 +105,14 @@ public class PlayerInit {
|
||||
.canDestroy(Set.of(Block.DIAMOND_ORE)))
|
||||
.build();
|
||||
player.getInventory().addItemStack(itemStack);
|
||||
|
||||
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());
|
||||
})
|
||||
.build();
|
||||
player.getInventory().addItemStack(bundle);
|
||||
});
|
||||
|
||||
static {
|
||||
|
Loading…
Reference in New Issue
Block a user