Merge pull request #440 from Minestom/add_bundle_meta

Add `BundleMeta`
This commit is contained in:
TheMode 2021-08-31 00:09:49 +02:00 committed by GitHub
commit 5b48a4f4cd
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 127 additions and 0 deletions

View File

@ -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,24 @@ 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: {0}", id);
Byte amount = nbtCompound.getByte("Count");
return fromNBT(material,
nbtCompound.getCompound("tag"),
amount == null ? 1 : amount);
}
@Contract(pure = true)
public @NotNull Material getMaterial() {
return material;
@ -228,6 +247,20 @@ 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().name());
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())

View File

@ -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);

View File

@ -0,0 +1,84 @@
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.jglrxavpok.hephaistos.nbt.NBTCompound;
import org.jglrxavpok.hephaistos.nbt.NBTList;
import org.jglrxavpok.hephaistos.nbt.NBTTypes;
import java.util.ArrayList;
import java.util.List;
import java.util.function.Supplier;
@ApiStatus.Experimental
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 = new ArrayList<>(items); // defensive copy
updateItems();
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();
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;
}
}
}

View File

@ -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.of(Material.DIAMOND, 5));
bundleMetaBuilder.addItem(ItemStack.of(Material.RABBIT_FOOT, 5));
})
.build();
player.getInventory().addItemStack(bundle);
});
static {