Use proper meta based on material

This commit is contained in:
TheMode 2021-04-10 06:03:48 +02:00
parent 1d12a5de95
commit 50ceeb33e4
3 changed files with 55 additions and 9 deletions

View File

@ -1,13 +1,17 @@
package net.minestom.server.item;
import net.kyori.adventure.text.Component;
import net.minestom.server.item.meta.CompassMeta;
import net.minestom.server.item.meta.*;
import org.jetbrains.annotations.Contract;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.jglrxavpok.hephaistos.nbt.NBTCompound;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import java.util.function.Consumer;
import java.util.function.Supplier;
import java.util.function.UnaryOperator;
public class ItemStackBuilder {
@ -24,9 +28,34 @@ public class ItemStackBuilder {
this.storeBuilder = storeBuilder;
}
private static final Map<Material, Supplier<ItemMetaBuilder>> MATERIAL_SUPPLIER_MAP = new ConcurrentHashMap<>();
static {
MATERIAL_SUPPLIER_MAP.put(Material.POTION, PotionMeta.Builder::new);
MATERIAL_SUPPLIER_MAP.put(Material.LINGERING_POTION, PotionMeta.Builder::new);
MATERIAL_SUPPLIER_MAP.put(Material.SPLASH_POTION, PotionMeta.Builder::new);
MATERIAL_SUPPLIER_MAP.put(Material.TIPPED_ARROW, PotionMeta.Builder::new);
MATERIAL_SUPPLIER_MAP.put(Material.FILLED_MAP, MapMeta.Builder::new);
MATERIAL_SUPPLIER_MAP.put(Material.COMPASS, CompassMeta.Builder::new);
MATERIAL_SUPPLIER_MAP.put(Material.ENCHANTED_BOOK, EnchantedBookMeta.Builder::new);
MATERIAL_SUPPLIER_MAP.put(Material.CROSSBOW, CrossbowMeta.Builder::new);
MATERIAL_SUPPLIER_MAP.put(Material.WRITABLE_BOOK, WritableBookMeta.Builder::new);
MATERIAL_SUPPLIER_MAP.put(Material.WRITTEN_BOOK, WrittenBookMeta.Builder::new);
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.LEATHER_HELMET, LeatherArmorMeta.Builder::new);
MATERIAL_SUPPLIER_MAP.put(Material.LEATHER_CHESTPLATE, LeatherArmorMeta.Builder::new);
MATERIAL_SUPPLIER_MAP.put(Material.LEATHER_LEGGINGS, LeatherArmorMeta.Builder::new);
MATERIAL_SUPPLIER_MAP.put(Material.LEATHER_BOOTS, LeatherArmorMeta.Builder::new);
}
protected ItemStackBuilder(@NotNull Material material) {
// TODO: meta depends on material
this(material, new CompassMeta.Builder(), new ItemStoreBuilder());
this(material,
MATERIAL_SUPPLIER_MAP.getOrDefault(material, DefaultMeta::new).get(),
new ItemStoreBuilder());
}
@Contract(value = "_ -> this")
@ -88,4 +117,21 @@ public class ItemStackBuilder {
return new ItemStack(material, amount, metaBuilder.build(), storeBuilder.build());
}
private static final class DefaultMeta extends ItemMetaBuilder {
@Override
public @NotNull ItemMeta build() {
return new ItemMeta(this);
}
@Override
public void read(@NotNull NBTCompound nbtCompound) {
// Empty
}
@Override
protected @NotNull Supplier<@NotNull ItemMetaBuilder> getSupplier() {
return DefaultMeta::new;
}
}
}

View File

@ -8,6 +8,7 @@ import org.jetbrains.annotations.NotNull;
import org.jglrxavpok.hephaistos.nbt.NBTCompound;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
import java.util.function.Supplier;
@ -17,7 +18,7 @@ public class EnchantedBookMeta extends ItemMeta implements ItemMetaBuilder.Provi
protected EnchantedBookMeta(@NotNull ItemMetaBuilder metaBuilder, Map<Enchantment, Short> storedEnchantmentMap) {
super(metaBuilder);
this.storedEnchantmentMap = storedEnchantmentMap;
this.storedEnchantmentMap = new HashMap<>(storedEnchantmentMap);
}
/**
@ -32,7 +33,7 @@ public class EnchantedBookMeta extends ItemMeta implements ItemMetaBuilder.Provi
public static class Builder extends ItemMetaBuilder {
private Map<Enchantment, Short> enchantments;
private Map<Enchantment, Short> enchantments = new HashMap<>();
public Builder enchantments(Map<Enchantment, Short> enchantments) {
this.enchantments = enchantments;

View File

@ -13,7 +13,7 @@ import org.jglrxavpok.hephaistos.nbt.NBTCompound;
import org.jglrxavpok.hephaistos.nbt.NBTList;
import org.jglrxavpok.hephaistos.nbt.NBTTypes;
import java.util.Collections;
import java.util.ArrayList;
import java.util.List;
import java.util.function.Supplier;
@ -28,7 +28,7 @@ public class PotionMeta extends ItemMeta implements ItemMetaBuilder.Provider<Pot
Color color) {
super(metaBuilder);
this.potionType = potionType;
this.customPotionEffects = Collections.unmodifiableList(customPotionEffects);
this.customPotionEffects = new ArrayList<>(customPotionEffects);
this.color = color;
}
@ -40,7 +40,6 @@ public class PotionMeta extends ItemMeta implements ItemMetaBuilder.Provider<Pot
return customPotionEffects;
}
public Color getColor() {
return color;
}
@ -48,7 +47,7 @@ public class PotionMeta extends ItemMeta implements ItemMetaBuilder.Provider<Pot
public static class Builder extends ItemMetaBuilder {
private PotionType potionType;
private List<CustomPotionEffect> customPotionEffects;
private List<CustomPotionEffect> customPotionEffects = new ArrayList<>();
private Color color;
public Builder potionType(@NotNull PotionType potionType) {