#1284: Move ItemType <-> ItemMeta linking to a centralized place

By: DerFrZocker <derrieple@gmail.com>
This commit is contained in:
CraftBukkit/Spigot 2024-06-15 06:53:46 +10:00
parent c985be99a6
commit 26f44f56c1
27 changed files with 406 additions and 1047 deletions

View file

@ -81,317 +81,13 @@ public final class CraftItemFactory implements ItemFactory {
private ItemMeta getItemMeta(Material material, CraftMetaItem meta) {
material = CraftLegacy.fromLegacy(material); // This may be called from legacy item stacks, try to get the right material
switch (material) {
case AIR:
return null;
case WRITTEN_BOOK:
return meta instanceof CraftMetaBookSigned ? meta : new CraftMetaBookSigned(meta);
case WRITABLE_BOOK:
return meta != null && meta.getClass().equals(CraftMetaBook.class) ? meta : new CraftMetaBook(meta);
case CREEPER_HEAD:
case CREEPER_WALL_HEAD:
case DRAGON_HEAD:
case DRAGON_WALL_HEAD:
case PIGLIN_HEAD:
case PIGLIN_WALL_HEAD:
case PLAYER_HEAD:
case PLAYER_WALL_HEAD:
case SKELETON_SKULL:
case SKELETON_WALL_SKULL:
case WITHER_SKELETON_SKULL:
case WITHER_SKELETON_WALL_SKULL:
case ZOMBIE_HEAD:
case ZOMBIE_WALL_HEAD:
return meta instanceof CraftMetaSkull ? meta : new CraftMetaSkull(meta);
case CHAINMAIL_HELMET:
case CHAINMAIL_CHESTPLATE:
case CHAINMAIL_LEGGINGS:
case CHAINMAIL_BOOTS:
case DIAMOND_HELMET:
case DIAMOND_CHESTPLATE:
case DIAMOND_LEGGINGS:
case DIAMOND_BOOTS:
case GOLDEN_HELMET:
case GOLDEN_CHESTPLATE:
case GOLDEN_LEGGINGS:
case GOLDEN_BOOTS:
case IRON_HELMET:
case IRON_CHESTPLATE:
case IRON_LEGGINGS:
case IRON_BOOTS:
case NETHERITE_HELMET:
case NETHERITE_CHESTPLATE:
case NETHERITE_LEGGINGS:
case NETHERITE_BOOTS:
case TURTLE_HELMET:
return meta != null && meta.getClass().equals(CraftMetaArmor.class) ? meta : new CraftMetaArmor(meta);
case LEATHER_HELMET:
case LEATHER_CHESTPLATE:
case LEATHER_LEGGINGS:
case LEATHER_BOOTS:
case WOLF_ARMOR:
return meta instanceof CraftMetaColorableArmor ? meta : new CraftMetaColorableArmor(meta);
case LEATHER_HORSE_ARMOR:
return meta instanceof CraftMetaLeatherArmor ? meta : new CraftMetaLeatherArmor(meta);
case POTION:
case SPLASH_POTION:
case LINGERING_POTION:
case TIPPED_ARROW:
return meta instanceof CraftMetaPotion ? meta : new CraftMetaPotion(meta);
case FILLED_MAP:
return meta instanceof CraftMetaMap ? meta : new CraftMetaMap(meta);
case FIREWORK_ROCKET:
return meta instanceof CraftMetaFirework ? meta : new CraftMetaFirework(meta);
case FIREWORK_STAR:
return meta instanceof CraftMetaCharge ? meta : new CraftMetaCharge(meta);
case ENCHANTED_BOOK:
return meta instanceof CraftMetaEnchantedBook ? meta : new CraftMetaEnchantedBook(meta);
case BLACK_BANNER:
case BLACK_WALL_BANNER:
case BLUE_BANNER:
case BLUE_WALL_BANNER:
case BROWN_BANNER:
case BROWN_WALL_BANNER:
case CYAN_BANNER:
case CYAN_WALL_BANNER:
case GRAY_BANNER:
case GRAY_WALL_BANNER:
case GREEN_BANNER:
case GREEN_WALL_BANNER:
case LIGHT_BLUE_BANNER:
case LIGHT_BLUE_WALL_BANNER:
case LIGHT_GRAY_BANNER:
case LIGHT_GRAY_WALL_BANNER:
case LIME_BANNER:
case LIME_WALL_BANNER:
case MAGENTA_BANNER:
case MAGENTA_WALL_BANNER:
case ORANGE_BANNER:
case ORANGE_WALL_BANNER:
case PINK_BANNER:
case PINK_WALL_BANNER:
case PURPLE_BANNER:
case PURPLE_WALL_BANNER:
case RED_BANNER:
case RED_WALL_BANNER:
case WHITE_BANNER:
case WHITE_WALL_BANNER:
case YELLOW_BANNER:
case YELLOW_WALL_BANNER:
return meta instanceof CraftMetaBanner ? meta : new CraftMetaBanner(meta);
case ARMADILLO_SPAWN_EGG:
case ALLAY_SPAWN_EGG:
case AXOLOTL_SPAWN_EGG:
case BAT_SPAWN_EGG:
case BEE_SPAWN_EGG:
case BLAZE_SPAWN_EGG:
case BOGGED_SPAWN_EGG:
case BREEZE_SPAWN_EGG:
case CAT_SPAWN_EGG:
case CAMEL_SPAWN_EGG:
case CAVE_SPIDER_SPAWN_EGG:
case CHICKEN_SPAWN_EGG:
case COD_SPAWN_EGG:
case COW_SPAWN_EGG:
case CREEPER_SPAWN_EGG:
case DOLPHIN_SPAWN_EGG:
case DONKEY_SPAWN_EGG:
case DROWNED_SPAWN_EGG:
case ELDER_GUARDIAN_SPAWN_EGG:
case ENDER_DRAGON_SPAWN_EGG:
case ENDERMAN_SPAWN_EGG:
case ENDERMITE_SPAWN_EGG:
case EVOKER_SPAWN_EGG:
case FOX_SPAWN_EGG:
case FROG_SPAWN_EGG:
case GHAST_SPAWN_EGG:
case GLOW_SQUID_SPAWN_EGG:
case GOAT_SPAWN_EGG:
case GUARDIAN_SPAWN_EGG:
case HOGLIN_SPAWN_EGG:
case HORSE_SPAWN_EGG:
case HUSK_SPAWN_EGG:
case IRON_GOLEM_SPAWN_EGG:
case LLAMA_SPAWN_EGG:
case MAGMA_CUBE_SPAWN_EGG:
case MOOSHROOM_SPAWN_EGG:
case MULE_SPAWN_EGG:
case OCELOT_SPAWN_EGG:
case PANDA_SPAWN_EGG:
case PARROT_SPAWN_EGG:
case PHANTOM_SPAWN_EGG:
case PIGLIN_BRUTE_SPAWN_EGG:
case PIGLIN_SPAWN_EGG:
case PIG_SPAWN_EGG:
case PILLAGER_SPAWN_EGG:
case POLAR_BEAR_SPAWN_EGG:
case PUFFERFISH_SPAWN_EGG:
case RABBIT_SPAWN_EGG:
case RAVAGER_SPAWN_EGG:
case SALMON_SPAWN_EGG:
case SHEEP_SPAWN_EGG:
case SHULKER_SPAWN_EGG:
case SILVERFISH_SPAWN_EGG:
case SKELETON_HORSE_SPAWN_EGG:
case SKELETON_SPAWN_EGG:
case SLIME_SPAWN_EGG:
case SNIFFER_SPAWN_EGG:
case SNOW_GOLEM_SPAWN_EGG:
case SPIDER_SPAWN_EGG:
case SQUID_SPAWN_EGG:
case STRAY_SPAWN_EGG:
case STRIDER_SPAWN_EGG:
case TADPOLE_SPAWN_EGG:
case TRADER_LLAMA_SPAWN_EGG:
case TROPICAL_FISH_SPAWN_EGG:
case TURTLE_SPAWN_EGG:
case VEX_SPAWN_EGG:
case VILLAGER_SPAWN_EGG:
case VINDICATOR_SPAWN_EGG:
case WANDERING_TRADER_SPAWN_EGG:
case WARDEN_SPAWN_EGG:
case WITCH_SPAWN_EGG:
case WITHER_SKELETON_SPAWN_EGG:
case WITHER_SPAWN_EGG:
case WOLF_SPAWN_EGG:
case ZOGLIN_SPAWN_EGG:
case ZOMBIE_HORSE_SPAWN_EGG:
case ZOMBIE_SPAWN_EGG:
case ZOMBIE_VILLAGER_SPAWN_EGG:
case ZOMBIFIED_PIGLIN_SPAWN_EGG:
return meta instanceof CraftMetaSpawnEgg ? meta : new CraftMetaSpawnEgg(meta);
case ARMOR_STAND:
return meta instanceof CraftMetaArmorStand ? meta : new CraftMetaArmorStand(meta);
case KNOWLEDGE_BOOK:
return meta instanceof CraftMetaKnowledgeBook ? meta : new CraftMetaKnowledgeBook(meta);
case FURNACE:
case CHEST:
case TRAPPED_CHEST:
case JUKEBOX:
case DISPENSER:
case DROPPER:
case ACACIA_HANGING_SIGN:
case ACACIA_SIGN:
case ACACIA_WALL_HANGING_SIGN:
case ACACIA_WALL_SIGN:
case BAMBOO_HANGING_SIGN:
case BAMBOO_SIGN:
case BAMBOO_WALL_HANGING_SIGN:
case BAMBOO_WALL_SIGN:
case BIRCH_HANGING_SIGN:
case BIRCH_SIGN:
case BIRCH_WALL_HANGING_SIGN:
case BIRCH_WALL_SIGN:
case CHERRY_HANGING_SIGN:
case CHERRY_SIGN:
case CHERRY_WALL_HANGING_SIGN:
case CHERRY_WALL_SIGN:
case CRIMSON_HANGING_SIGN:
case CRIMSON_SIGN:
case CRIMSON_WALL_HANGING_SIGN:
case CRIMSON_WALL_SIGN:
case DARK_OAK_HANGING_SIGN:
case DARK_OAK_SIGN:
case DARK_OAK_WALL_HANGING_SIGN:
case DARK_OAK_WALL_SIGN:
case JUNGLE_HANGING_SIGN:
case JUNGLE_SIGN:
case JUNGLE_WALL_HANGING_SIGN:
case JUNGLE_WALL_SIGN:
case MANGROVE_HANGING_SIGN:
case MANGROVE_SIGN:
case MANGROVE_WALL_HANGING_SIGN:
case MANGROVE_WALL_SIGN:
case OAK_HANGING_SIGN:
case OAK_SIGN:
case OAK_WALL_HANGING_SIGN:
case OAK_WALL_SIGN:
case SPRUCE_HANGING_SIGN:
case SPRUCE_SIGN:
case SPRUCE_WALL_HANGING_SIGN:
case SPRUCE_WALL_SIGN:
case WARPED_HANGING_SIGN:
case WARPED_SIGN:
case WARPED_WALL_HANGING_SIGN:
case WARPED_WALL_SIGN:
case SPAWNER:
case BREWING_STAND:
case ENCHANTING_TABLE:
case COMMAND_BLOCK:
case REPEATING_COMMAND_BLOCK:
case CHAIN_COMMAND_BLOCK:
case BEACON:
case DAYLIGHT_DETECTOR:
case HOPPER:
case COMPARATOR:
case SHIELD:
case STRUCTURE_BLOCK:
case SHULKER_BOX:
case WHITE_SHULKER_BOX:
case ORANGE_SHULKER_BOX:
case MAGENTA_SHULKER_BOX:
case LIGHT_BLUE_SHULKER_BOX:
case YELLOW_SHULKER_BOX:
case LIME_SHULKER_BOX:
case PINK_SHULKER_BOX:
case GRAY_SHULKER_BOX:
case LIGHT_GRAY_SHULKER_BOX:
case CYAN_SHULKER_BOX:
case PURPLE_SHULKER_BOX:
case BLUE_SHULKER_BOX:
case BROWN_SHULKER_BOX:
case GREEN_SHULKER_BOX:
case RED_SHULKER_BOX:
case BLACK_SHULKER_BOX:
case ENDER_CHEST:
case BARREL:
case BELL:
case BLAST_FURNACE:
case CAMPFIRE:
case SOUL_CAMPFIRE:
case JIGSAW:
case LECTERN:
case SMOKER:
case BEEHIVE:
case BEE_NEST:
case SCULK_CATALYST:
case SCULK_SHRIEKER:
case SCULK_SENSOR:
case CALIBRATED_SCULK_SENSOR:
case CHISELED_BOOKSHELF:
case DECORATED_POT:
case SUSPICIOUS_SAND:
case SUSPICIOUS_GRAVEL:
case CRAFTER:
case TRIAL_SPAWNER:
case VAULT:
return new CraftMetaBlockState(meta, material);
case TROPICAL_FISH_BUCKET:
return meta instanceof CraftMetaTropicalFishBucket ? meta : new CraftMetaTropicalFishBucket(meta);
case AXOLOTL_BUCKET:
return meta instanceof CraftMetaAxolotlBucket ? meta : new CraftMetaAxolotlBucket(meta);
case CROSSBOW:
return meta instanceof CraftMetaCrossbow ? meta : new CraftMetaCrossbow(meta);
case SUSPICIOUS_STEW:
return meta instanceof CraftMetaSuspiciousStew ? meta : new CraftMetaSuspiciousStew(meta);
case COD_BUCKET:
case PUFFERFISH_BUCKET:
case SALMON_BUCKET:
case ITEM_FRAME:
case GLOW_ITEM_FRAME:
case PAINTING:
return meta instanceof CraftMetaEntityTag ? meta : new CraftMetaEntityTag(meta);
case COMPASS:
return meta instanceof CraftMetaCompass ? meta : new CraftMetaCompass(meta);
case BUNDLE:
return meta instanceof CraftMetaBundle ? meta : new CraftMetaBundle(meta);
case GOAT_HORN:
return meta instanceof CraftMetaMusicInstrument ? meta : new CraftMetaMusicInstrument(meta);
case OMINOUS_BOTTLE:
return meta instanceof CraftMetaOminousBottle ? meta : new CraftMetaOminousBottle(meta);
default:
if (!material.isItem()) {
// default behavior for none items is a new CraftMetaItem
return new CraftMetaItem(meta);
}
return ((CraftItemType<?>) material.asItemType()).getItemMeta(meta);
}
@Override

View file

@ -0,0 +1,316 @@
package org.bukkit.craftbukkit.inventory;
import java.util.function.BiFunction;
import java.util.function.Function;
import net.minecraft.world.item.ItemStack;
import org.bukkit.Tag;
import org.bukkit.inventory.ItemType;
import org.bukkit.inventory.meta.ArmorMeta;
import org.bukkit.inventory.meta.AxolotlBucketMeta;
import org.bukkit.inventory.meta.BannerMeta;
import org.bukkit.inventory.meta.BlockStateMeta;
import org.bukkit.inventory.meta.BookMeta;
import org.bukkit.inventory.meta.BundleMeta;
import org.bukkit.inventory.meta.ColorableArmorMeta;
import org.bukkit.inventory.meta.CompassMeta;
import org.bukkit.inventory.meta.CrossbowMeta;
import org.bukkit.inventory.meta.EnchantmentStorageMeta;
import org.bukkit.inventory.meta.FireworkEffectMeta;
import org.bukkit.inventory.meta.FireworkMeta;
import org.bukkit.inventory.meta.ItemMeta;
import org.bukkit.inventory.meta.KnowledgeBookMeta;
import org.bukkit.inventory.meta.LeatherArmorMeta;
import org.bukkit.inventory.meta.MapMeta;
import org.bukkit.inventory.meta.MusicInstrumentMeta;
import org.bukkit.inventory.meta.OminousBottleMeta;
import org.bukkit.inventory.meta.PotionMeta;
import org.bukkit.inventory.meta.SkullMeta;
import org.bukkit.inventory.meta.SpawnEggMeta;
import org.bukkit.inventory.meta.SuspiciousStewMeta;
import org.bukkit.inventory.meta.TropicalFishBucketMeta;
public final class CraftItemMetas {
public record ItemMetaData<I extends ItemMeta>(Class<I> metaClass, Function<ItemStack, I> fromItemStack,
BiFunction<ItemType.Typed<I>, CraftMetaItem, I> fromItemMeta) {
}
private static final ItemMetaData<ItemMeta> EMPTY_META_DATA = new ItemMetaData<>(ItemMeta.class,
item -> null,
(type, meta) -> null);
private static final ItemMetaData<ItemMeta> ITEM_META_DATA = new ItemMetaData<>(ItemMeta.class,
item -> new CraftMetaItem(item.getComponentsPatch()),
(type, meta) -> new CraftMetaItem(meta));
private static final ItemMetaData<BookMeta> SIGNED_BOOK_META_DATA = new ItemMetaData<>(BookMeta.class,
item -> new CraftMetaBookSigned(item.getComponentsPatch()),
(type, meta) -> meta instanceof CraftMetaBookSigned signed ? signed : new CraftMetaBookSigned(meta));
private static final ItemMetaData<BookMeta> WRITABLE_BOOK_META_DATA = new ItemMetaData<>(BookMeta.class,
item -> new CraftMetaBook(item.getComponentsPatch()),
(type, meta) -> meta != null && meta.getClass().equals(CraftMetaBook.class) ? (BookMeta) meta : new CraftMetaBook(meta));
private static final ItemMetaData<SkullMeta> SKULL_META_DATA = new ItemMetaData<>(SkullMeta.class,
item -> new CraftMetaSkull(item.getComponentsPatch()),
(type, meta) -> meta instanceof CraftMetaSkull skull ? skull : new CraftMetaSkull(meta));
private static final ItemMetaData<ArmorMeta> ARMOR_META_DATA = new ItemMetaData<>(ArmorMeta.class,
item -> new CraftMetaArmor(item.getComponentsPatch()),
(type, meta) -> meta != null && meta.getClass().equals(CraftMetaArmor.class) ? (ArmorMeta) meta : new CraftMetaArmor(meta));
private static final ItemMetaData<ColorableArmorMeta> COLORABLE_ARMOR_META_DATA = new ItemMetaData<>(ColorableArmorMeta.class,
item -> new CraftMetaColorableArmor(item.getComponentsPatch()),
(type, meta) -> meta instanceof ColorableArmorMeta colorable ? colorable : new CraftMetaColorableArmor(meta));
private static final ItemMetaData<LeatherArmorMeta> LEATHER_ARMOR_META_DATA = new ItemMetaData<>(LeatherArmorMeta.class,
item -> new CraftMetaLeatherArmor(item.getComponentsPatch()),
(type, meta) -> meta instanceof CraftMetaLeatherArmor leather ? leather : new CraftMetaLeatherArmor(meta));
private static final ItemMetaData<PotionMeta> POTION_META_DATA = new ItemMetaData<>(PotionMeta.class,
item -> new CraftMetaPotion(item.getComponentsPatch()),
(type, meta) -> meta instanceof CraftMetaPotion potion ? potion : new CraftMetaPotion(meta));
private static final ItemMetaData<MapMeta> MAP_META_DATA = new ItemMetaData<>(MapMeta.class,
item -> new CraftMetaMap(item.getComponentsPatch()),
(type, meta) -> meta instanceof CraftMetaMap map ? map : new CraftMetaMap(meta));
private static final ItemMetaData<FireworkMeta> FIREWORK_META_DATA = new ItemMetaData<>(FireworkMeta.class,
item -> new CraftMetaFirework(item.getComponentsPatch()),
(type, meta) -> meta instanceof CraftMetaFirework firework ? firework : new CraftMetaFirework(meta));
private static final ItemMetaData<FireworkEffectMeta> CHARGE_META_DATA = new ItemMetaData<>(FireworkEffectMeta.class,
item -> new CraftMetaCharge(item.getComponentsPatch()),
(type, meta) -> meta instanceof CraftMetaCharge charge ? charge : new CraftMetaCharge(meta));
private static final ItemMetaData<EnchantmentStorageMeta> ENCHANTED_BOOK_META_DATA = new ItemMetaData<>(EnchantmentStorageMeta.class,
item -> new CraftMetaEnchantedBook(item.getComponentsPatch()),
(type, meta) -> meta instanceof CraftMetaEnchantedBook enchantedBook ? enchantedBook : new CraftMetaEnchantedBook(meta));
private static final ItemMetaData<BannerMeta> BANNER_META_DATA = new ItemMetaData<>(BannerMeta.class,
item -> new CraftMetaBanner(item.getComponentsPatch()),
(type, meta) -> meta instanceof CraftMetaBanner banner ? banner : new CraftMetaBanner(meta));
private static final ItemMetaData<SpawnEggMeta> SPAWN_EGG_META_DATA = new ItemMetaData<>(SpawnEggMeta.class,
item -> new CraftMetaSpawnEgg(item.getComponentsPatch()),
(type, meta) -> meta instanceof CraftMetaSpawnEgg spawnEgg ? spawnEgg : new CraftMetaSpawnEgg(meta));
private static final ItemMetaData<ItemMeta> ARMOR_STAND_META_DATA = new ItemMetaData<>(ItemMeta.class,
item -> new CraftMetaArmorStand(item.getComponentsPatch()),
(type, meta) -> meta instanceof CraftMetaArmorStand armorStand ? armorStand : new CraftMetaArmorStand(meta));
private static final ItemMetaData<KnowledgeBookMeta> KNOWLEDGE_BOOK_META_DATA = new ItemMetaData<>(KnowledgeBookMeta.class,
item -> new CraftMetaKnowledgeBook(item.getComponentsPatch()),
(type, meta) -> meta instanceof CraftMetaKnowledgeBook knowledgeBook ? knowledgeBook : new CraftMetaKnowledgeBook(meta));
private static final ItemMetaData<BlockStateMeta> BLOCK_STATE_META_DATA = new ItemMetaData<>(BlockStateMeta.class,
item -> new CraftMetaBlockState(item.getComponentsPatch(), CraftItemType.minecraftToBukkit(item.getItem())),
(type, meta) -> new CraftMetaBlockState(meta, type.asMaterial()));
private static final ItemMetaData<TropicalFishBucketMeta> TROPICAL_FISH_BUCKET_META_DATA = new ItemMetaData<>(TropicalFishBucketMeta.class,
item -> new CraftMetaTropicalFishBucket(item.getComponentsPatch()),
(type, meta) -> meta instanceof CraftMetaTropicalFishBucket tropicalFishBucket ? tropicalFishBucket : new CraftMetaTropicalFishBucket(meta));
private static final ItemMetaData<AxolotlBucketMeta> AXOLOTL_BUCKET_META_DATA = new ItemMetaData<>(AxolotlBucketMeta.class,
item -> new CraftMetaAxolotlBucket(item.getComponentsPatch()),
(type, meta) -> meta instanceof CraftMetaAxolotlBucket axolotlBucket ? axolotlBucket : new CraftMetaAxolotlBucket(meta));
private static final ItemMetaData<CrossbowMeta> CROSSBOW_META_DATA = new ItemMetaData<>(CrossbowMeta.class,
item -> new CraftMetaCrossbow(item.getComponentsPatch()),
(type, meta) -> meta instanceof CraftMetaCrossbow crossbow ? crossbow : new CraftMetaCrossbow(meta));
private static final ItemMetaData<SuspiciousStewMeta> SUSPICIOUS_STEW_META_DATA = new ItemMetaData<>(SuspiciousStewMeta.class,
item -> new CraftMetaSuspiciousStew(item.getComponentsPatch()),
(type, meta) -> meta instanceof CraftMetaSuspiciousStew suspiciousStew ? suspiciousStew : new CraftMetaSuspiciousStew(meta));
private static final ItemMetaData<ItemMeta> ENTITY_TAG_META_DATA = new ItemMetaData<>(ItemMeta.class,
item -> new CraftMetaEntityTag(item.getComponentsPatch()),
(type, meta) -> meta instanceof CraftMetaEntityTag entityTag ? entityTag : new CraftMetaEntityTag(meta));
private static final ItemMetaData<CompassMeta> COMPASS_META_DATA = new ItemMetaData<>(CompassMeta.class,
item -> new CraftMetaCompass(item.getComponentsPatch()),
(type, meta) -> meta instanceof CraftMetaCompass compass ? compass : new CraftMetaCompass(meta));
private static final ItemMetaData<BundleMeta> BUNDLE_META_DATA = new ItemMetaData<>(BundleMeta.class,
item -> new CraftMetaBundle(item.getComponentsPatch()),
(type, meta) -> meta instanceof CraftMetaBundle bundle ? bundle : new CraftMetaBundle(meta));
private static final ItemMetaData<MusicInstrumentMeta> MUSIC_INSTRUMENT_META_DATA = new ItemMetaData<>(MusicInstrumentMeta.class,
item -> new CraftMetaMusicInstrument(item.getComponentsPatch()),
(type, meta) -> meta instanceof CraftMetaMusicInstrument musicInstrument ? musicInstrument : new CraftMetaMusicInstrument(meta));
private static final ItemMetaData<OminousBottleMeta> OMINOUS_BOTTLE_META_DATA = new ItemMetaData<>(OminousBottleMeta.class,
item -> new CraftMetaOminousBottle(item.getComponentsPatch()),
(type, meta) -> meta instanceof CraftMetaOminousBottle musicInstrument ? musicInstrument : new CraftMetaOminousBottle(meta));
// We use if instead of a set, since the result gets cached in CraftItemType,
// which would result in dead memory once all ItemTypes have cached the data.
public static <I extends ItemMeta> ItemMetaData<I> getItemMetaData(ItemType itemType) {
if (itemType == ItemType.AIR) {
return asType(EMPTY_META_DATA);
}
if (itemType == ItemType.WRITTEN_BOOK) {
return asType(SIGNED_BOOK_META_DATA);
}
if (itemType == ItemType.WRITABLE_BOOK) {
return asType(WRITABLE_BOOK_META_DATA);
}
if (itemType == ItemType.CREEPER_HEAD || itemType == ItemType.DRAGON_HEAD
|| itemType == ItemType.PIGLIN_HEAD || itemType == ItemType.PLAYER_HEAD
|| itemType == ItemType.SKELETON_SKULL || itemType == ItemType.WITHER_SKELETON_SKULL
|| itemType == ItemType.ZOMBIE_HEAD) {
return asType(SKULL_META_DATA);
}
if (itemType == ItemType.CHAINMAIL_HELMET || itemType == ItemType.CHAINMAIL_CHESTPLATE
|| itemType == ItemType.CHAINMAIL_LEGGINGS || itemType == ItemType.CHAINMAIL_BOOTS
|| itemType == ItemType.DIAMOND_HELMET || itemType == ItemType.DIAMOND_CHESTPLATE
|| itemType == ItemType.DIAMOND_LEGGINGS || itemType == ItemType.DIAMOND_BOOTS
|| itemType == ItemType.GOLDEN_HELMET || itemType == ItemType.GOLDEN_CHESTPLATE
|| itemType == ItemType.GOLDEN_LEGGINGS || itemType == ItemType.GOLDEN_BOOTS
|| itemType == ItemType.IRON_HELMET || itemType == ItemType.IRON_CHESTPLATE
|| itemType == ItemType.IRON_LEGGINGS || itemType == ItemType.IRON_BOOTS
|| itemType == ItemType.NETHERITE_HELMET || itemType == ItemType.NETHERITE_CHESTPLATE
|| itemType == ItemType.NETHERITE_LEGGINGS || itemType == ItemType.NETHERITE_BOOTS
|| itemType == ItemType.TURTLE_HELMET) {
return asType(ARMOR_META_DATA);
}
if (itemType == ItemType.LEATHER_HELMET || itemType == ItemType.LEATHER_CHESTPLATE
|| itemType == ItemType.LEATHER_LEGGINGS || itemType == ItemType.LEATHER_BOOTS
|| itemType == ItemType.WOLF_ARMOR) {
return asType(COLORABLE_ARMOR_META_DATA);
}
if (itemType == ItemType.LEATHER_HORSE_ARMOR) {
return asType(LEATHER_ARMOR_META_DATA);
}
if (itemType == ItemType.POTION || itemType == ItemType.SPLASH_POTION
|| itemType == ItemType.LINGERING_POTION || itemType == ItemType.TIPPED_ARROW) {
return asType(POTION_META_DATA);
}
if (itemType == ItemType.FILLED_MAP) {
return asType(MAP_META_DATA);
}
if (itemType == ItemType.FIREWORK_ROCKET) {
return asType(FIREWORK_META_DATA);
}
if (itemType == ItemType.FIREWORK_STAR) {
return asType(CHARGE_META_DATA);
}
if (itemType == ItemType.ENCHANTED_BOOK) {
return asType(ENCHANTED_BOOK_META_DATA);
}
if (itemType.hasBlockType() && Tag.BANNERS.isTagged(itemType.getBlockType().asMaterial())) {
return asType(BANNER_META_DATA);
}
if (itemType == ItemType.ARMADILLO_SPAWN_EGG || itemType == ItemType.ALLAY_SPAWN_EGG
|| itemType == ItemType.ARMADILLO_SPAWN_EGG || itemType == ItemType.ALLAY_SPAWN_EGG
|| itemType == ItemType.AXOLOTL_SPAWN_EGG || itemType == ItemType.BAT_SPAWN_EGG
|| itemType == ItemType.BEE_SPAWN_EGG || itemType == ItemType.BLAZE_SPAWN_EGG
|| itemType == ItemType.BOGGED_SPAWN_EGG || itemType == ItemType.BREEZE_SPAWN_EGG
|| itemType == ItemType.CAT_SPAWN_EGG || itemType == ItemType.CAMEL_SPAWN_EGG
|| itemType == ItemType.CAVE_SPIDER_SPAWN_EGG || itemType == ItemType.CHICKEN_SPAWN_EGG
|| itemType == ItemType.COD_SPAWN_EGG || itemType == ItemType.COW_SPAWN_EGG
|| itemType == ItemType.CREEPER_SPAWN_EGG || itemType == ItemType.DOLPHIN_SPAWN_EGG
|| itemType == ItemType.DONKEY_SPAWN_EGG || itemType == ItemType.DROWNED_SPAWN_EGG
|| itemType == ItemType.ELDER_GUARDIAN_SPAWN_EGG || itemType == ItemType.ENDER_DRAGON_SPAWN_EGG
|| itemType == ItemType.ENDERMAN_SPAWN_EGG || itemType == ItemType.ENDERMITE_SPAWN_EGG
|| itemType == ItemType.EVOKER_SPAWN_EGG || itemType == ItemType.FOX_SPAWN_EGG
|| itemType == ItemType.FROG_SPAWN_EGG || itemType == ItemType.GHAST_SPAWN_EGG
|| itemType == ItemType.GLOW_SQUID_SPAWN_EGG || itemType == ItemType.GOAT_SPAWN_EGG
|| itemType == ItemType.GUARDIAN_SPAWN_EGG || itemType == ItemType.HOGLIN_SPAWN_EGG
|| itemType == ItemType.HORSE_SPAWN_EGG || itemType == ItemType.HUSK_SPAWN_EGG
|| itemType == ItemType.IRON_GOLEM_SPAWN_EGG || itemType == ItemType.LLAMA_SPAWN_EGG
|| itemType == ItemType.MAGMA_CUBE_SPAWN_EGG || itemType == ItemType.MOOSHROOM_SPAWN_EGG
|| itemType == ItemType.MULE_SPAWN_EGG || itemType == ItemType.OCELOT_SPAWN_EGG
|| itemType == ItemType.PANDA_SPAWN_EGG || itemType == ItemType.PARROT_SPAWN_EGG
|| itemType == ItemType.PHANTOM_SPAWN_EGG || itemType == ItemType.PIGLIN_BRUTE_SPAWN_EGG
|| itemType == ItemType.PIGLIN_SPAWN_EGG || itemType == ItemType.PIG_SPAWN_EGG
|| itemType == ItemType.PILLAGER_SPAWN_EGG || itemType == ItemType.POLAR_BEAR_SPAWN_EGG
|| itemType == ItemType.PUFFERFISH_SPAWN_EGG || itemType == ItemType.RABBIT_SPAWN_EGG
|| itemType == ItemType.RAVAGER_SPAWN_EGG || itemType == ItemType.SALMON_SPAWN_EGG
|| itemType == ItemType.SHEEP_SPAWN_EGG || itemType == ItemType.SHULKER_SPAWN_EGG
|| itemType == ItemType.SILVERFISH_SPAWN_EGG || itemType == ItemType.SKELETON_HORSE_SPAWN_EGG
|| itemType == ItemType.SKELETON_SPAWN_EGG || itemType == ItemType.SLIME_SPAWN_EGG
|| itemType == ItemType.SNIFFER_SPAWN_EGG || itemType == ItemType.SNOW_GOLEM_SPAWN_EGG
|| itemType == ItemType.SPIDER_SPAWN_EGG || itemType == ItemType.SQUID_SPAWN_EGG
|| itemType == ItemType.STRAY_SPAWN_EGG || itemType == ItemType.STRIDER_SPAWN_EGG
|| itemType == ItemType.TADPOLE_SPAWN_EGG || itemType == ItemType.TRADER_LLAMA_SPAWN_EGG
|| itemType == ItemType.TROPICAL_FISH_SPAWN_EGG || itemType == ItemType.TURTLE_SPAWN_EGG
|| itemType == ItemType.VEX_SPAWN_EGG || itemType == ItemType.VILLAGER_SPAWN_EGG
|| itemType == ItemType.VINDICATOR_SPAWN_EGG || itemType == ItemType.WANDERING_TRADER_SPAWN_EGG
|| itemType == ItemType.WARDEN_SPAWN_EGG || itemType == ItemType.WITCH_SPAWN_EGG
|| itemType == ItemType.WITHER_SKELETON_SPAWN_EGG || itemType == ItemType.WITHER_SPAWN_EGG
|| itemType == ItemType.WOLF_SPAWN_EGG || itemType == ItemType.ZOGLIN_SPAWN_EGG
|| itemType == ItemType.ZOMBIE_HORSE_SPAWN_EGG || itemType == ItemType.ZOMBIE_SPAWN_EGG
|| itemType == ItemType.ZOMBIE_VILLAGER_SPAWN_EGG || itemType == ItemType.ZOMBIFIED_PIGLIN_SPAWN_EGG) {
return asType(SPAWN_EGG_META_DATA);
}
if (itemType == ItemType.ARMOR_STAND) {
return asType(ARMOR_STAND_META_DATA);
}
if (itemType == ItemType.KNOWLEDGE_BOOK) {
return asType(KNOWLEDGE_BOOK_META_DATA);
}
if (itemType == ItemType.FURNACE || itemType == ItemType.CHEST
|| itemType == ItemType.TRAPPED_CHEST || itemType == ItemType.JUKEBOX
|| itemType == ItemType.DISPENSER || itemType == ItemType.DROPPER
|| (itemType.hasBlockType() && Tag.SIGNS.isTagged(itemType.getBlockType().asMaterial())) || itemType == ItemType.SPAWNER
|| itemType == ItemType.BREWING_STAND || itemType == ItemType.ENCHANTING_TABLE
|| itemType == ItemType.COMMAND_BLOCK || itemType == ItemType.REPEATING_COMMAND_BLOCK
|| itemType == ItemType.CHAIN_COMMAND_BLOCK || itemType == ItemType.BEACON
|| itemType == ItemType.DAYLIGHT_DETECTOR || itemType == ItemType.HOPPER
|| itemType == ItemType.COMPARATOR || itemType == ItemType.SHIELD
|| itemType == ItemType.STRUCTURE_BLOCK || (itemType.hasBlockType() && Tag.SHULKER_BOXES.isTagged(itemType.getBlockType().asMaterial()))
|| itemType == ItemType.ENDER_CHEST || itemType == ItemType.BARREL
|| itemType == ItemType.BELL || itemType == ItemType.BLAST_FURNACE
|| itemType == ItemType.CAMPFIRE || itemType == ItemType.SOUL_CAMPFIRE
|| itemType == ItemType.JIGSAW || itemType == ItemType.LECTERN
|| itemType == ItemType.SMOKER || itemType == ItemType.BEEHIVE
|| itemType == ItemType.BEE_NEST || itemType == ItemType.SCULK_CATALYST
|| itemType == ItemType.SCULK_SHRIEKER || itemType == ItemType.SCULK_SENSOR
|| itemType == ItemType.CALIBRATED_SCULK_SENSOR || itemType == ItemType.CHISELED_BOOKSHELF
|| itemType == ItemType.DECORATED_POT || itemType == ItemType.SUSPICIOUS_SAND
|| itemType == ItemType.SUSPICIOUS_GRAVEL || itemType == ItemType.CRAFTER
|| itemType == ItemType.TRIAL_SPAWNER || itemType == ItemType.VAULT) {
return asType(BLOCK_STATE_META_DATA);
}
if (itemType == ItemType.TROPICAL_FISH_BUCKET) {
return asType(TROPICAL_FISH_BUCKET_META_DATA);
}
if (itemType == ItemType.AXOLOTL_BUCKET) {
return asType(AXOLOTL_BUCKET_META_DATA);
}
if (itemType == ItemType.CROSSBOW) {
return asType(CROSSBOW_META_DATA);
}
if (itemType == ItemType.SUSPICIOUS_STEW) {
return asType(SUSPICIOUS_STEW_META_DATA);
}
if (itemType == ItemType.COD_BUCKET || itemType == ItemType.PUFFERFISH_BUCKET
|| itemType == ItemType.SALMON_BUCKET || itemType == ItemType.ITEM_FRAME
|| itemType == ItemType.GLOW_ITEM_FRAME || itemType == ItemType.PAINTING) {
return asType(ENTITY_TAG_META_DATA);
}
if (itemType == ItemType.COMPASS) {
return asType(COMPASS_META_DATA);
}
if (itemType == ItemType.BUNDLE) {
return asType(BUNDLE_META_DATA);
}
if (itemType == ItemType.GOAT_HORN) {
return asType(MUSIC_INSTRUMENT_META_DATA);
}
if (itemType == ItemType.OMINOUS_BOTTLE) {
return asType(OMINOUS_BOTTLE_META_DATA);
}
return asType(ITEM_META_DATA);
}
private static <I extends ItemMeta> ItemMetaData<I> asType(ItemMetaData<?> metaData) {
return (ItemMetaData<I>) metaData;
}
private CraftItemMetas() {
}
}

View file

@ -291,315 +291,8 @@ public final class CraftItemStack extends ItemStack {
if (!hasItemMeta(item)) {
return CraftItemFactory.instance().getItemMeta(getType(item));
}
switch (getType(item)) {
case WRITTEN_BOOK:
return new CraftMetaBookSigned(item.getComponentsPatch());
case WRITABLE_BOOK:
return new CraftMetaBook(item.getComponentsPatch());
case CREEPER_HEAD:
case CREEPER_WALL_HEAD:
case DRAGON_HEAD:
case DRAGON_WALL_HEAD:
case PIGLIN_HEAD:
case PIGLIN_WALL_HEAD:
case PLAYER_HEAD:
case PLAYER_WALL_HEAD:
case SKELETON_SKULL:
case SKELETON_WALL_SKULL:
case WITHER_SKELETON_SKULL:
case WITHER_SKELETON_WALL_SKULL:
case ZOMBIE_HEAD:
case ZOMBIE_WALL_HEAD:
return new CraftMetaSkull(item.getComponentsPatch());
case CHAINMAIL_HELMET:
case CHAINMAIL_CHESTPLATE:
case CHAINMAIL_LEGGINGS:
case CHAINMAIL_BOOTS:
case DIAMOND_HELMET:
case DIAMOND_CHESTPLATE:
case DIAMOND_LEGGINGS:
case DIAMOND_BOOTS:
case GOLDEN_HELMET:
case GOLDEN_CHESTPLATE:
case GOLDEN_LEGGINGS:
case GOLDEN_BOOTS:
case IRON_HELMET:
case IRON_CHESTPLATE:
case IRON_LEGGINGS:
case IRON_BOOTS:
case NETHERITE_HELMET:
case NETHERITE_CHESTPLATE:
case NETHERITE_LEGGINGS:
case NETHERITE_BOOTS:
case TURTLE_HELMET:
return new CraftMetaArmor(item.getComponentsPatch());
case LEATHER_HELMET:
case LEATHER_CHESTPLATE:
case LEATHER_LEGGINGS:
case LEATHER_BOOTS:
case WOLF_ARMOR:
return new CraftMetaColorableArmor(item.getComponentsPatch());
case LEATHER_HORSE_ARMOR:
return new CraftMetaLeatherArmor(item.getComponentsPatch());
case POTION:
case SPLASH_POTION:
case LINGERING_POTION:
case TIPPED_ARROW:
return new CraftMetaPotion(item.getComponentsPatch());
case FILLED_MAP:
return new CraftMetaMap(item.getComponentsPatch());
case FIREWORK_ROCKET:
return new CraftMetaFirework(item.getComponentsPatch());
case FIREWORK_STAR:
return new CraftMetaCharge(item.getComponentsPatch());
case ENCHANTED_BOOK:
return new CraftMetaEnchantedBook(item.getComponentsPatch());
case BLACK_BANNER:
case BLACK_WALL_BANNER:
case BLUE_BANNER:
case BLUE_WALL_BANNER:
case BROWN_BANNER:
case BROWN_WALL_BANNER:
case CYAN_BANNER:
case CYAN_WALL_BANNER:
case GRAY_BANNER:
case GRAY_WALL_BANNER:
case GREEN_BANNER:
case GREEN_WALL_BANNER:
case LIGHT_BLUE_BANNER:
case LIGHT_BLUE_WALL_BANNER:
case LIGHT_GRAY_BANNER:
case LIGHT_GRAY_WALL_BANNER:
case LIME_BANNER:
case LIME_WALL_BANNER:
case MAGENTA_BANNER:
case MAGENTA_WALL_BANNER:
case ORANGE_BANNER:
case ORANGE_WALL_BANNER:
case PINK_BANNER:
case PINK_WALL_BANNER:
case PURPLE_BANNER:
case PURPLE_WALL_BANNER:
case RED_BANNER:
case RED_WALL_BANNER:
case WHITE_BANNER:
case WHITE_WALL_BANNER:
case YELLOW_BANNER:
case YELLOW_WALL_BANNER:
return new CraftMetaBanner(item.getComponentsPatch());
case ARMADILLO_SPAWN_EGG:
case ALLAY_SPAWN_EGG:
case AXOLOTL_SPAWN_EGG:
case BAT_SPAWN_EGG:
case BEE_SPAWN_EGG:
case BLAZE_SPAWN_EGG:
case BOGGED_SPAWN_EGG:
case BREEZE_SPAWN_EGG:
case CAT_SPAWN_EGG:
case CAMEL_SPAWN_EGG:
case CAVE_SPIDER_SPAWN_EGG:
case CHICKEN_SPAWN_EGG:
case COD_SPAWN_EGG:
case COW_SPAWN_EGG:
case CREEPER_SPAWN_EGG:
case DOLPHIN_SPAWN_EGG:
case DONKEY_SPAWN_EGG:
case DROWNED_SPAWN_EGG:
case ELDER_GUARDIAN_SPAWN_EGG:
case ENDER_DRAGON_SPAWN_EGG:
case ENDERMAN_SPAWN_EGG:
case ENDERMITE_SPAWN_EGG:
case EVOKER_SPAWN_EGG:
case FOX_SPAWN_EGG:
case FROG_SPAWN_EGG:
case GHAST_SPAWN_EGG:
case GLOW_SQUID_SPAWN_EGG:
case GOAT_SPAWN_EGG:
case GUARDIAN_SPAWN_EGG:
case HOGLIN_SPAWN_EGG:
case HORSE_SPAWN_EGG:
case HUSK_SPAWN_EGG:
case IRON_GOLEM_SPAWN_EGG:
case LLAMA_SPAWN_EGG:
case MAGMA_CUBE_SPAWN_EGG:
case MOOSHROOM_SPAWN_EGG:
case MULE_SPAWN_EGG:
case OCELOT_SPAWN_EGG:
case PANDA_SPAWN_EGG:
case PARROT_SPAWN_EGG:
case PHANTOM_SPAWN_EGG:
case PIGLIN_BRUTE_SPAWN_EGG:
case PIGLIN_SPAWN_EGG:
case PIG_SPAWN_EGG:
case PILLAGER_SPAWN_EGG:
case POLAR_BEAR_SPAWN_EGG:
case PUFFERFISH_SPAWN_EGG:
case RABBIT_SPAWN_EGG:
case RAVAGER_SPAWN_EGG:
case SALMON_SPAWN_EGG:
case SHEEP_SPAWN_EGG:
case SHULKER_SPAWN_EGG:
case SILVERFISH_SPAWN_EGG:
case SKELETON_HORSE_SPAWN_EGG:
case SKELETON_SPAWN_EGG:
case SLIME_SPAWN_EGG:
case SNIFFER_SPAWN_EGG:
case SNOW_GOLEM_SPAWN_EGG:
case SPIDER_SPAWN_EGG:
case SQUID_SPAWN_EGG:
case STRAY_SPAWN_EGG:
case STRIDER_SPAWN_EGG:
case TADPOLE_SPAWN_EGG:
case TRADER_LLAMA_SPAWN_EGG:
case TROPICAL_FISH_SPAWN_EGG:
case TURTLE_SPAWN_EGG:
case VEX_SPAWN_EGG:
case VILLAGER_SPAWN_EGG:
case VINDICATOR_SPAWN_EGG:
case WANDERING_TRADER_SPAWN_EGG:
case WARDEN_SPAWN_EGG:
case WITCH_SPAWN_EGG:
case WITHER_SKELETON_SPAWN_EGG:
case WITHER_SPAWN_EGG:
case WOLF_SPAWN_EGG:
case ZOGLIN_SPAWN_EGG:
case ZOMBIE_HORSE_SPAWN_EGG:
case ZOMBIE_SPAWN_EGG:
case ZOMBIE_VILLAGER_SPAWN_EGG:
case ZOMBIFIED_PIGLIN_SPAWN_EGG:
return new CraftMetaSpawnEgg(item.getComponentsPatch());
case ARMOR_STAND:
return new CraftMetaArmorStand(item.getComponentsPatch());
case KNOWLEDGE_BOOK:
return new CraftMetaKnowledgeBook(item.getComponentsPatch());
case FURNACE:
case CHEST:
case TRAPPED_CHEST:
case JUKEBOX:
case DISPENSER:
case DROPPER:
case ACACIA_HANGING_SIGN:
case ACACIA_SIGN:
case ACACIA_WALL_HANGING_SIGN:
case ACACIA_WALL_SIGN:
case BAMBOO_HANGING_SIGN:
case BAMBOO_SIGN:
case BAMBOO_WALL_HANGING_SIGN:
case BAMBOO_WALL_SIGN:
case BIRCH_HANGING_SIGN:
case BIRCH_SIGN:
case BIRCH_WALL_HANGING_SIGN:
case BIRCH_WALL_SIGN:
case CHERRY_HANGING_SIGN:
case CHERRY_SIGN:
case CHERRY_WALL_HANGING_SIGN:
case CHERRY_WALL_SIGN:
case CRIMSON_HANGING_SIGN:
case CRIMSON_SIGN:
case CRIMSON_WALL_HANGING_SIGN:
case CRIMSON_WALL_SIGN:
case DARK_OAK_HANGING_SIGN:
case DARK_OAK_SIGN:
case DARK_OAK_WALL_HANGING_SIGN:
case DARK_OAK_WALL_SIGN:
case JUNGLE_HANGING_SIGN:
case JUNGLE_SIGN:
case JUNGLE_WALL_HANGING_SIGN:
case JUNGLE_WALL_SIGN:
case MANGROVE_HANGING_SIGN:
case MANGROVE_SIGN:
case MANGROVE_WALL_HANGING_SIGN:
case MANGROVE_WALL_SIGN:
case OAK_HANGING_SIGN:
case OAK_SIGN:
case OAK_WALL_HANGING_SIGN:
case OAK_WALL_SIGN:
case SPRUCE_HANGING_SIGN:
case SPRUCE_SIGN:
case SPRUCE_WALL_HANGING_SIGN:
case SPRUCE_WALL_SIGN:
case WARPED_HANGING_SIGN:
case WARPED_SIGN:
case WARPED_WALL_HANGING_SIGN:
case WARPED_WALL_SIGN:
case SPAWNER:
case BREWING_STAND:
case ENCHANTING_TABLE:
case COMMAND_BLOCK:
case REPEATING_COMMAND_BLOCK:
case CHAIN_COMMAND_BLOCK:
case BEACON:
case DAYLIGHT_DETECTOR:
case HOPPER:
case COMPARATOR:
case SHIELD:
case STRUCTURE_BLOCK:
case SHULKER_BOX:
case WHITE_SHULKER_BOX:
case ORANGE_SHULKER_BOX:
case MAGENTA_SHULKER_BOX:
case LIGHT_BLUE_SHULKER_BOX:
case YELLOW_SHULKER_BOX:
case LIME_SHULKER_BOX:
case PINK_SHULKER_BOX:
case GRAY_SHULKER_BOX:
case LIGHT_GRAY_SHULKER_BOX:
case CYAN_SHULKER_BOX:
case PURPLE_SHULKER_BOX:
case BLUE_SHULKER_BOX:
case BROWN_SHULKER_BOX:
case GREEN_SHULKER_BOX:
case RED_SHULKER_BOX:
case BLACK_SHULKER_BOX:
case ENDER_CHEST:
case BARREL:
case BELL:
case BLAST_FURNACE:
case CAMPFIRE:
case SOUL_CAMPFIRE:
case JIGSAW:
case LECTERN:
case SMOKER:
case BEEHIVE:
case BEE_NEST:
case SCULK_CATALYST:
case SCULK_SHRIEKER:
case SCULK_SENSOR:
case CALIBRATED_SCULK_SENSOR:
case CHISELED_BOOKSHELF:
case DECORATED_POT:
case SUSPICIOUS_SAND:
case SUSPICIOUS_GRAVEL:
case CRAFTER:
case TRIAL_SPAWNER:
case VAULT:
return new CraftMetaBlockState(item.getComponentsPatch(), CraftItemType.minecraftToBukkit(item.getItem()));
case TROPICAL_FISH_BUCKET:
return new CraftMetaTropicalFishBucket(item.getComponentsPatch());
case AXOLOTL_BUCKET:
return new CraftMetaAxolotlBucket(item.getComponentsPatch());
case CROSSBOW:
return new CraftMetaCrossbow(item.getComponentsPatch());
case SUSPICIOUS_STEW:
return new CraftMetaSuspiciousStew(item.getComponentsPatch());
case COD_BUCKET:
case PUFFERFISH_BUCKET:
case SALMON_BUCKET:
case ITEM_FRAME:
case GLOW_ITEM_FRAME:
case PAINTING:
return new CraftMetaEntityTag(item.getComponentsPatch());
case COMPASS:
return new CraftMetaCompass(item.getComponentsPatch());
case BUNDLE:
return new CraftMetaBundle(item.getComponentsPatch());
case GOAT_HORN:
return new CraftMetaMusicInstrument(item.getComponentsPatch());
case OMINOUS_BOTTLE:
return new CraftMetaOminousBottle(item.getComponentsPatch());
default:
return new CraftMetaItem(item.getComponentsPatch());
}
return ((CraftItemType<?>) CraftItemType.minecraftToBukkitNew(item.getItem())).getItemMeta(item);
}
static Material getType(net.minecraft.world.item.ItemStack item) {

View file

@ -1,9 +1,11 @@
package org.bukkit.craftbukkit.inventory;
import com.google.common.base.Preconditions;
import com.google.common.base.Suppliers;
import com.google.common.collect.ImmutableMultimap;
import com.google.common.collect.Multimap;
import java.util.function.Consumer;
import java.util.function.Supplier;
import net.minecraft.core.component.DataComponents;
import net.minecraft.core.registries.Registries;
import net.minecraft.world.item.Item;
@ -38,7 +40,7 @@ public class CraftItemType<M extends ItemMeta> implements ItemType.Typed<M>, Han
private final NamespacedKey key;
private final Item item;
private final Class<M> itemMetaClass;
private final Supplier<CraftItemMetas.ItemMetaData<M>> itemMetaData;
public static Material minecraftToBukkit(Item item) {
return CraftMagicNumbers.getMaterial(item);
@ -59,18 +61,7 @@ public class CraftItemType<M extends ItemMeta> implements ItemType.Typed<M>, Han
public CraftItemType(NamespacedKey key, Item item) {
this.key = key;
this.item = item;
this.itemMetaClass = getItemMetaClass(item);
}
// Cursed, this should be refactored when possible
private Class<M> getItemMetaClass(Item item) {
ItemMeta meta = new ItemStack(asMaterial()).getItemMeta();
if (meta != null) {
if (CraftMetaEntityTag.class != meta.getClass() && CraftMetaArmorStand.class != meta.getClass()) {
return (Class<M>) meta.getClass().getInterfaces()[0];
}
}
return (Class<M>) ItemMeta.class;
this.itemMetaData = Suppliers.memoize(() -> CraftItemMetas.getItemMetaData(this));
}
@NotNull
@ -83,7 +74,7 @@ public class CraftItemType<M extends ItemMeta> implements ItemType.Typed<M>, Han
@Override
@SuppressWarnings("unchecked")
public <Other extends ItemMeta> Typed<Other> typed(@NotNull final Class<Other> itemMetaType) {
if (itemMetaType.isAssignableFrom(this.itemMetaClass)) return (Typed<Other>) this;
if (itemMetaType.isAssignableFrom(this.itemMetaData.get().metaClass())) return (Typed<Other>) this;
throw new IllegalArgumentException("Cannot type item type " + this.key.toString() + " to meta type " + itemMetaType.getSimpleName());
}
@ -123,6 +114,14 @@ public class CraftItemType<M extends ItemMeta> implements ItemType.Typed<M>, Han
return item;
}
public M getItemMeta(net.minecraft.world.item.ItemStack itemStack) {
return itemMetaData.get().fromItemStack().apply(itemStack);
}
public M getItemMeta(ItemMeta itemMeta) {
return itemMetaData.get().fromItemMeta().apply(this, (CraftMetaItem) itemMeta);
}
@Override
public boolean hasBlockType() {
return item instanceof ItemBlock;
@ -143,7 +142,7 @@ public class CraftItemType<M extends ItemMeta> implements ItemType.Typed<M>, Han
if (this == ItemType.AIR) {
throw new UnsupportedOperationException("Air does not have ItemMeta");
}
return itemMetaClass;
return itemMetaData.get().metaClass();
}
@Override

View file

@ -1,14 +1,11 @@
package org.bukkit.craftbukkit.inventory;
import com.google.common.collect.ImmutableMap.Builder;
import com.google.common.collect.Sets;
import java.util.HashMap;
import java.util.Map;
import java.util.Objects;
import java.util.Set;
import net.minecraft.core.component.DataComponentPatch;
import net.minecraft.core.component.DataComponents;
import org.bukkit.Material;
import org.bukkit.NamespacedKey;
import org.bukkit.Registry;
import org.bukkit.configuration.serialization.DelegateDeserialization;
@ -23,34 +20,6 @@ import org.bukkit.inventory.meta.trim.TrimPattern;
@DelegateDeserialization(SerializableMeta.class)
public class CraftMetaArmor extends CraftMetaItem implements ArmorMeta {
private static final Set<Material> ARMOR_MATERIALS = Sets.newHashSet(
Material.CHAINMAIL_HELMET,
Material.CHAINMAIL_CHESTPLATE,
Material.CHAINMAIL_LEGGINGS,
Material.CHAINMAIL_BOOTS,
Material.DIAMOND_HELMET,
Material.DIAMOND_CHESTPLATE,
Material.DIAMOND_LEGGINGS,
Material.DIAMOND_BOOTS,
Material.GOLDEN_HELMET,
Material.GOLDEN_CHESTPLATE,
Material.GOLDEN_LEGGINGS,
Material.GOLDEN_BOOTS,
Material.IRON_HELMET,
Material.IRON_CHESTPLATE,
Material.IRON_LEGGINGS,
Material.IRON_BOOTS,
Material.LEATHER_HELMET,
Material.LEATHER_CHESTPLATE,
Material.LEATHER_LEGGINGS,
Material.LEATHER_BOOTS,
Material.NETHERITE_HELMET,
Material.NETHERITE_CHESTPLATE,
Material.NETHERITE_LEGGINGS,
Material.NETHERITE_BOOTS,
Material.TURTLE_HELMET
);
static final ItemMetaKeyType<net.minecraft.world.item.armortrim.ArmorTrim> TRIM = new ItemMetaKeyType<>(DataComponents.TRIM, "trim");
static final ItemMetaKey TRIM_MATERIAL = new ItemMetaKey("material");
static final ItemMetaKey TRIM_PATTERN = new ItemMetaKey("pattern");
@ -113,11 +82,6 @@ public class CraftMetaArmor extends CraftMetaItem implements ArmorMeta {
}
}
@Override
boolean applicableTo(Material type) {
return ARMOR_MATERIALS.contains(type);
}
@Override
boolean equalsCommon(CraftMetaItem that) {
if (!super.equalsCommon(that)) {

View file

@ -7,7 +7,6 @@ import net.minecraft.core.component.DataComponents;
import net.minecraft.nbt.NBTBase;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.world.item.component.CustomData;
import org.bukkit.Material;
import org.bukkit.configuration.serialization.DelegateDeserialization;
import org.bukkit.craftbukkit.util.CraftMagicNumbers;
import org.bukkit.entity.Axolotl;
@ -108,11 +107,6 @@ public class CraftMetaAxolotlBucket extends CraftMetaItem implements AxolotlBuck
}
}
@Override
boolean applicableTo(Material type) {
return type == Material.AXOLOTL_BUCKET;
}
@Override
boolean isEmpty() {
return super.isEmpty() && isBucketEmpty();

View file

@ -3,17 +3,14 @@ package org.bukkit.craftbukkit.inventory;
import com.google.common.base.Preconditions;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableMap;
import com.google.common.collect.Sets;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Set;
import net.minecraft.core.component.DataComponentPatch;
import net.minecraft.core.component.DataComponents;
import net.minecraft.world.item.EnumColor;
import net.minecraft.world.level.block.entity.BannerPatternLayers;
import org.bukkit.DyeColor;
import org.bukkit.Material;
import org.bukkit.block.banner.Pattern;
import org.bukkit.block.banner.PatternType;
import org.bukkit.configuration.serialization.DelegateDeserialization;
@ -23,41 +20,6 @@ import org.bukkit.inventory.meta.BannerMeta;
@DelegateDeserialization(SerializableMeta.class)
public class CraftMetaBanner extends CraftMetaItem implements BannerMeta {
private static final Set<Material> BANNER_MATERIALS = Sets.newHashSet(
Material.BLACK_BANNER,
Material.BLACK_WALL_BANNER,
Material.BLUE_BANNER,
Material.BLUE_WALL_BANNER,
Material.BROWN_BANNER,
Material.BROWN_WALL_BANNER,
Material.CYAN_BANNER,
Material.CYAN_WALL_BANNER,
Material.GRAY_BANNER,
Material.GRAY_WALL_BANNER,
Material.GREEN_BANNER,
Material.GREEN_WALL_BANNER,
Material.LIGHT_BLUE_BANNER,
Material.LIGHT_BLUE_WALL_BANNER,
Material.LIGHT_GRAY_BANNER,
Material.LIGHT_GRAY_WALL_BANNER,
Material.LIME_BANNER,
Material.LIME_WALL_BANNER,
Material.MAGENTA_BANNER,
Material.MAGENTA_WALL_BANNER,
Material.ORANGE_BANNER,
Material.ORANGE_WALL_BANNER,
Material.PINK_BANNER,
Material.PINK_WALL_BANNER,
Material.PURPLE_BANNER,
Material.PURPLE_WALL_BANNER,
Material.RED_BANNER,
Material.RED_WALL_BANNER,
Material.WHITE_BANNER,
Material.WHITE_WALL_BANNER,
Material.YELLOW_BANNER,
Material.YELLOW_WALL_BANNER
);
static final ItemMetaKeyType<BannerPatternLayers> PATTERNS = new ItemMetaKeyType<>(DataComponents.BANNER_PATTERNS, "patterns");
private List<Pattern> patterns = new ArrayList<Pattern>();
@ -196,11 +158,6 @@ public class CraftMetaBanner extends CraftMetaItem implements BannerMeta {
return super.isEmpty() && patterns.isEmpty();
}
@Override
boolean applicableTo(Material type) {
return BANNER_MATERIALS.contains(type);
}
@Override
public CraftMetaBanner clone() {
CraftMetaBanner meta = (CraftMetaBanner) super.clone();

View file

@ -48,98 +48,6 @@ public class CraftMetaBlockState extends CraftMetaItem implements BlockStateMeta
Material.BLACK_SHULKER_BOX
);
private static final Set<Material> BLOCK_STATE_MATERIALS = Sets.newHashSet(
Material.FURNACE,
Material.CHEST,
Material.TRAPPED_CHEST,
Material.JUKEBOX,
Material.DISPENSER,
Material.DROPPER,
Material.ACACIA_HANGING_SIGN,
Material.ACACIA_SIGN,
Material.ACACIA_WALL_HANGING_SIGN,
Material.ACACIA_WALL_SIGN,
Material.BAMBOO_HANGING_SIGN,
Material.BAMBOO_SIGN,
Material.BAMBOO_WALL_HANGING_SIGN,
Material.BAMBOO_WALL_SIGN,
Material.BIRCH_HANGING_SIGN,
Material.BIRCH_SIGN,
Material.BIRCH_WALL_HANGING_SIGN,
Material.BIRCH_WALL_SIGN,
Material.CHERRY_HANGING_SIGN,
Material.CHERRY_SIGN,
Material.CHERRY_WALL_HANGING_SIGN,
Material.CHERRY_WALL_SIGN,
Material.CRIMSON_HANGING_SIGN,
Material.CRIMSON_SIGN,
Material.CRIMSON_WALL_HANGING_SIGN,
Material.CRIMSON_WALL_SIGN,
Material.DARK_OAK_HANGING_SIGN,
Material.DARK_OAK_SIGN,
Material.DARK_OAK_WALL_HANGING_SIGN,
Material.DARK_OAK_WALL_SIGN,
Material.JUNGLE_HANGING_SIGN,
Material.JUNGLE_SIGN,
Material.JUNGLE_WALL_HANGING_SIGN,
Material.JUNGLE_WALL_SIGN,
Material.MANGROVE_HANGING_SIGN,
Material.MANGROVE_SIGN,
Material.MANGROVE_WALL_HANGING_SIGN,
Material.MANGROVE_WALL_SIGN,
Material.OAK_HANGING_SIGN,
Material.OAK_SIGN,
Material.OAK_WALL_HANGING_SIGN,
Material.OAK_WALL_SIGN,
Material.SPRUCE_HANGING_SIGN,
Material.SPRUCE_SIGN,
Material.SPRUCE_WALL_HANGING_SIGN,
Material.SPRUCE_WALL_SIGN,
Material.WARPED_HANGING_SIGN,
Material.WARPED_SIGN,
Material.WARPED_WALL_HANGING_SIGN,
Material.WARPED_WALL_SIGN,
Material.SPAWNER,
Material.BREWING_STAND,
Material.ENCHANTING_TABLE,
Material.COMMAND_BLOCK,
Material.REPEATING_COMMAND_BLOCK,
Material.CHAIN_COMMAND_BLOCK,
Material.BEACON,
Material.DAYLIGHT_DETECTOR,
Material.HOPPER,
Material.COMPARATOR,
Material.SHIELD,
Material.STRUCTURE_BLOCK,
Material.ENDER_CHEST,
Material.BARREL,
Material.BELL,
Material.BLAST_FURNACE,
Material.CAMPFIRE,
Material.SOUL_CAMPFIRE,
Material.JIGSAW,
Material.LECTERN,
Material.SMOKER,
Material.BEEHIVE,
Material.BEE_NEST,
Material.SCULK_CATALYST,
Material.SCULK_SHRIEKER,
Material.CALIBRATED_SCULK_SENSOR,
Material.SCULK_SENSOR,
Material.CHISELED_BOOKSHELF,
Material.DECORATED_POT,
Material.SUSPICIOUS_SAND,
Material.SUSPICIOUS_GRAVEL,
Material.TRIAL_SPAWNER,
Material.CRAFTER,
Material.VAULT
);
static {
// Add shulker boxes to the list of block state materials too
BLOCK_STATE_MATERIALS.addAll(SHULKER_BOX_MATERIALS);
}
@ItemMetaKey.Specific(ItemMetaKey.Specific.To.NBT)
static final ItemMetaKeyType<CustomData> BLOCK_ENTITY_TAG = new ItemMetaKeyType<>(DataComponents.BLOCK_ENTITY_DATA, "BlockEntityTag");
@ -273,11 +181,6 @@ public class CraftMetaBlockState extends CraftMetaItem implements BlockStateMeta
return super.isEmpty() && blockEntityTag == null;
}
@Override
boolean applicableTo(Material type) {
return BLOCK_STATE_MATERIALS.contains(type);
}
@Override
public CraftMetaBlockState clone() {
CraftMetaBlockState meta = (CraftMetaBlockState) super.clone();

View file

@ -9,7 +9,6 @@ import java.util.Map;
import net.minecraft.core.component.DataComponentPatch;
import net.minecraft.core.component.DataComponents;
import net.minecraft.world.item.component.BundleContents;
import org.bukkit.Material;
import org.bukkit.configuration.serialization.DelegateDeserialization;
import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.meta.BundleMeta;
@ -77,11 +76,6 @@ public class CraftMetaBundle extends CraftMetaItem implements BundleMeta {
}
}
@Override
boolean applicableTo(Material type) {
return type == Material.BUNDLE;
}
@Override
boolean isEmpty() {
return super.isEmpty() && isBundleEmpty();

View file

@ -6,7 +6,6 @@ import net.minecraft.core.component.DataComponentPatch;
import net.minecraft.core.component.DataComponents;
import net.minecraft.world.item.component.FireworkExplosion;
import org.bukkit.FireworkEffect;
import org.bukkit.Material;
import org.bukkit.configuration.serialization.DelegateDeserialization;
import org.bukkit.inventory.meta.FireworkEffectMeta;
@ -66,11 +65,6 @@ class CraftMetaCharge extends CraftMetaItem implements FireworkEffectMeta {
}
}
@Override
boolean applicableTo(Material type) {
return type == Material.FIREWORK_STAR;
}
@Override
boolean isEmpty() {
return super.isEmpty() && !hasChargeMeta();

View file

@ -2,26 +2,15 @@ package org.bukkit.craftbukkit.inventory;
import static org.bukkit.craftbukkit.inventory.CraftItemFactory.*;
import com.google.common.collect.ImmutableMap.Builder;
import com.google.common.collect.Sets;
import java.util.Map;
import java.util.Set;
import net.minecraft.core.component.DataComponentPatch;
import org.bukkit.Color;
import org.bukkit.Material;
import org.bukkit.configuration.serialization.DelegateDeserialization;
import org.bukkit.inventory.meta.ColorableArmorMeta;
@DelegateDeserialization(SerializableMeta.class)
public class CraftMetaColorableArmor extends CraftMetaArmor implements ColorableArmorMeta {
private static final Set<Material> LEATHER_ARMOR_MATERIALS = Sets.newHashSet(
Material.LEATHER_HELMET,
Material.LEATHER_CHESTPLATE,
Material.LEATHER_LEGGINGS,
Material.LEATHER_BOOTS,
Material.WOLF_ARMOR
);
private Color color = DEFAULT_LEATHER_COLOR;
CraftMetaColorableArmor(CraftMetaItem meta) {
@ -54,11 +43,6 @@ public class CraftMetaColorableArmor extends CraftMetaArmor implements Colorable
return !(hasColor());
}
@Override
boolean applicableTo(Material type) {
return LEATHER_ARMOR_MATERIALS.contains(type);
}
@Override
public CraftMetaColorableArmor clone() {
CraftMetaColorableArmor clone = (CraftMetaColorableArmor) super.clone();

View file

@ -15,7 +15,6 @@ import net.minecraft.server.MinecraftServer;
import net.minecraft.server.level.WorldServer;
import net.minecraft.world.item.component.LodestoneTracker;
import org.bukkit.Location;
import org.bukkit.Material;
import org.bukkit.World;
import org.bukkit.configuration.serialization.DelegateDeserialization;
import org.bukkit.craftbukkit.CraftWorld;
@ -106,11 +105,6 @@ public class CraftMetaCompass extends CraftMetaItem implements CompassMeta {
return !(hasLodestone() || hasLodestoneTracked());
}
@Override
boolean applicableTo(Material type) {
return type == Material.COMPASS;
}
@Override
public CraftMetaCompass clone() {
CraftMetaCompass clone = ((CraftMetaCompass) super.clone());

View file

@ -82,11 +82,6 @@ public class CraftMetaCrossbow extends CraftMetaItem implements CrossbowMeta {
}
}
@Override
boolean applicableTo(Material type) {
return type == Material.CROSSBOW;
}
@Override
boolean isEmpty() {
return super.isEmpty() && isCrossbowEmpty();

View file

@ -7,7 +7,6 @@ import java.util.Map;
import net.minecraft.core.component.DataComponentPatch;
import net.minecraft.core.component.DataComponents;
import net.minecraft.world.item.enchantment.ItemEnchantments;
import org.bukkit.Material;
import org.bukkit.configuration.serialization.DelegateDeserialization;
import org.bukkit.enchantments.Enchantment;
import org.bukkit.inventory.ItemFlag;
@ -57,11 +56,6 @@ class CraftMetaEnchantedBook extends CraftMetaItem implements EnchantmentStorage
applyEnchantments(enchantments, itemTag, STORED_ENCHANTMENTS, ItemFlag.HIDE_ADDITIONAL_TOOLTIP);
}
@Override
boolean applicableTo(Material type) {
return type == Material.ENCHANTED_BOOK;
}
@Override
boolean isEmpty() {
return super.isEmpty() && isEnchantedEmpty();

View file

@ -15,7 +15,6 @@ import net.minecraft.world.item.component.Fireworks;
import org.bukkit.Color;
import org.bukkit.FireworkEffect;
import org.bukkit.FireworkEffect.Type;
import org.bukkit.Material;
import org.bukkit.configuration.serialization.DelegateDeserialization;
import org.bukkit.craftbukkit.inventory.CraftMetaItem.ItemMetaKey.Specific;
import org.bukkit.craftbukkit.inventory.CraftMetaItem.ItemMetaKey.Specific.To;
@ -207,11 +206,6 @@ class CraftMetaFirework extends CraftMetaItem implements FireworkMeta {
return IntList.of(colorArray);
}
@Override
boolean applicableTo(Material type) {
return type == Material.FIREWORK_ROCKET;
}
@Override
boolean isEmpty() {
return super.isEmpty() && isFireworkEmpty();

View file

@ -134,6 +134,7 @@ import org.bukkit.persistence.PersistentDataContainer;
* <li> SerializableMeta.Deserializers deserializer()
*/
@DelegateDeserialization(SerializableMeta.class)
// Important: ItemMeta needs to be the first interface see #applicableTo(Material)
class CraftMetaItem implements ItemMeta, Damageable, Repairable, BlockDataMeta {
static class ItemMetaKey {
@ -898,9 +899,17 @@ class CraftMetaItem implements ItemMeta, Damageable, Repairable, BlockDataMeta {
tag.put(ATTRIBUTES, list.build().withTooltip(!hasItemFlag(ItemFlag.HIDE_ATTRIBUTES)));
}
@Overridden
boolean applicableTo(Material type) {
return type != Material.AIR;
if (type == Material.AIR || !type.isItem()) {
return false;
}
if (getClass() == CraftMetaItem.class) {
return true;
}
// We assume that the corresponding bukkit interface is always the first one
return type.asItemType().getItemMetaClass() == getClass().getInterfaces()[0];
}
@Overridden

View file

@ -8,7 +8,6 @@ import java.util.Map;
import net.minecraft.core.component.DataComponentPatch;
import net.minecraft.core.component.DataComponents;
import net.minecraft.resources.MinecraftKey;
import org.bukkit.Material;
import org.bukkit.NamespacedKey;
import org.bukkit.configuration.serialization.DelegateDeserialization;
import org.bukkit.craftbukkit.util.CraftNamespacedKey;
@ -78,11 +77,6 @@ public class CraftMetaKnowledgeBook extends CraftMetaItem implements KnowledgeBo
return !(hasRecipes());
}
@Override
boolean applicableTo(Material type) {
return type == Material.KNOWLEDGE_BOOK;
}
@Override
public boolean hasRecipes() {
return !recipes.isEmpty();

View file

@ -2,9 +2,7 @@ package org.bukkit.craftbukkit.inventory;
import static org.bukkit.craftbukkit.inventory.CraftItemFactory.*;
import com.google.common.collect.ImmutableMap.Builder;
import com.google.common.collect.Sets;
import java.util.Map;
import java.util.Set;
import net.minecraft.core.component.DataComponentPatch;
import net.minecraft.core.component.DataComponents;
import net.minecraft.world.item.component.DyedItemColor;
@ -12,20 +10,12 @@ import org.bukkit.Color;
import org.bukkit.Material;
import org.bukkit.configuration.serialization.DelegateDeserialization;
import org.bukkit.inventory.ItemFlag;
import org.bukkit.inventory.meta.ColorableArmorMeta;
import org.bukkit.inventory.meta.LeatherArmorMeta;
@DelegateDeserialization(SerializableMeta.class)
class CraftMetaLeatherArmor extends CraftMetaItem implements LeatherArmorMeta {
private static final Set<Material> LEATHER_ARMOR_MATERIALS = Sets.newHashSet(
Material.LEATHER_HELMET,
Material.LEATHER_HORSE_ARMOR,
Material.LEATHER_CHESTPLATE,
Material.LEATHER_LEGGINGS,
Material.LEATHER_BOOTS,
Material.WOLF_ARMOR
);
static final ItemMetaKeyType<DyedItemColor> COLOR = new ItemMetaKeyType<>(DataComponents.DYED_COLOR, "color");
private Color color = DEFAULT_LEATHER_COLOR;
@ -62,7 +52,11 @@ class CraftMetaLeatherArmor extends CraftMetaItem implements LeatherArmorMeta {
@Override
boolean applicableTo(Material type) {
return LEATHER_ARMOR_MATERIALS.contains(type);
if (!type.isItem()) {
return false;
}
return type.asItemType().getItemMetaClass() == LeatherArmorMeta.class || type.asItemType().getItemMetaClass() == ColorableArmorMeta.class;
}
@Override

View file

@ -10,7 +10,6 @@ import net.minecraft.world.item.component.MapPostProcessing;
import net.minecraft.world.level.saveddata.maps.MapId;
import org.bukkit.Bukkit;
import org.bukkit.Color;
import org.bukkit.Material;
import org.bukkit.configuration.serialization.DelegateDeserialization;
import org.bukkit.inventory.meta.MapMeta;
import org.bukkit.map.MapView;
@ -106,11 +105,6 @@ class CraftMetaMap extends CraftMetaItem implements MapMeta {
}
}
@Override
boolean applicableTo(Material type) {
return type == Material.FILLED_MAP;
}
@Override
boolean isEmpty() {
return super.isEmpty() && isMapEmpty();

View file

@ -6,7 +6,6 @@ import net.minecraft.core.Holder;
import net.minecraft.core.component.DataComponentPatch;
import net.minecraft.core.component.DataComponents;
import net.minecraft.world.item.Instrument;
import org.bukkit.Material;
import org.bukkit.MusicInstrument;
import org.bukkit.configuration.serialization.DelegateDeserialization;
import org.bukkit.craftbukkit.CraftMusicInstrument;
@ -53,11 +52,6 @@ public class CraftMetaMusicInstrument extends CraftMetaItem implements MusicInst
}
}
@Override
boolean applicableTo(Material type) {
return type == Material.GOAT_HORN;
}
@Override
boolean equalsCommon(CraftMetaItem meta) {
if (!super.equalsCommon(meta)) {

View file

@ -5,7 +5,6 @@ import com.google.common.collect.ImmutableMap.Builder;
import java.util.Map;
import net.minecraft.core.component.DataComponentPatch;
import net.minecraft.core.component.DataComponents;
import org.bukkit.Material;
import org.bukkit.configuration.serialization.DelegateDeserialization;
import org.bukkit.inventory.meta.OminousBottleMeta;
@ -57,11 +56,6 @@ public class CraftMetaOminousBottle extends CraftMetaItem implements OminousBott
return !(hasAmplifier());
}
@Override
boolean applicableTo(Material type) {
return type == Material.OMINOUS_BOTTLE;
}
@Override
public CraftMetaOminousBottle clone() {
CraftMetaOminousBottle clone = ((CraftMetaOminousBottle) super.clone());

View file

@ -3,14 +3,12 @@ package org.bukkit.craftbukkit.inventory;
import com.google.common.base.Preconditions;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableMap.Builder;
import com.google.common.collect.Sets;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.Optional;
import java.util.Set;
import net.minecraft.core.Holder;
import net.minecraft.core.component.DataComponentPatch;
import net.minecraft.core.component.DataComponents;
@ -18,7 +16,6 @@ import net.minecraft.world.effect.MobEffect;
import net.minecraft.world.item.alchemy.PotionContents;
import net.minecraft.world.item.alchemy.PotionRegistry;
import org.bukkit.Color;
import org.bukkit.Material;
import org.bukkit.configuration.serialization.DelegateDeserialization;
import org.bukkit.craftbukkit.potion.CraftPotionEffectType;
import org.bukkit.craftbukkit.potion.CraftPotionType;
@ -32,13 +29,6 @@ import org.bukkit.potion.PotionType;
@DelegateDeserialization(SerializableMeta.class)
class CraftMetaPotion extends CraftMetaItem implements PotionMeta {
private static final Set<Material> POTION_MATERIALS = Sets.newHashSet(
Material.POTION,
Material.SPLASH_POTION,
Material.LINGERING_POTION,
Material.TIPPED_ARROW
);
@ItemMetaKey.Specific(ItemMetaKey.Specific.To.NBT)
static final ItemMetaKeyType<PotionContents> POTION_CONTENTS = new ItemMetaKeyType<>(DataComponents.POTION_CONTENTS);
static final ItemMetaKey POTION_EFFECTS = new ItemMetaKey("custom-effects");
@ -147,11 +137,6 @@ class CraftMetaPotion extends CraftMetaItem implements PotionMeta {
return (type == null) && !(hasCustomEffects() || hasColor());
}
@Override
boolean applicableTo(Material type) {
return POTION_MATERIALS.contains(type);
}
@Override
public CraftMetaPotion clone() {
CraftMetaPotion clone = (CraftMetaPotion) super.clone();

View file

@ -1,11 +1,9 @@
package org.bukkit.craftbukkit.inventory;
import com.google.common.collect.ImmutableMap.Builder;
import com.google.common.collect.Sets;
import com.mojang.authlib.GameProfile;
import java.util.Map;
import java.util.Objects;
import java.util.Set;
import java.util.UUID;
import net.minecraft.SystemUtils;
import net.minecraft.core.component.DataComponentPatch;
@ -15,7 +13,6 @@ import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.resources.MinecraftKey;
import net.minecraft.world.item.component.ResolvableProfile;
import org.bukkit.Bukkit;
import org.bukkit.Material;
import org.bukkit.NamespacedKey;
import org.bukkit.OfflinePlayer;
import org.bukkit.configuration.serialization.DelegateDeserialization;
@ -29,23 +26,6 @@ import org.bukkit.profile.PlayerProfile;
@DelegateDeserialization(SerializableMeta.class)
class CraftMetaSkull extends CraftMetaItem implements SkullMeta {
private static final Set<Material> SKULL_MATERIALS = Sets.newHashSet(
Material.CREEPER_HEAD,
Material.CREEPER_WALL_HEAD,
Material.DRAGON_HEAD,
Material.DRAGON_WALL_HEAD,
Material.PIGLIN_HEAD,
Material.PIGLIN_WALL_HEAD,
Material.PLAYER_HEAD,
Material.PLAYER_WALL_HEAD,
Material.SKELETON_SKULL,
Material.SKELETON_WALL_SKULL,
Material.WITHER_SKELETON_SKULL,
Material.WITHER_SKELETON_WALL_SKULL,
Material.ZOMBIE_HEAD,
Material.ZOMBIE_WALL_HEAD
);
@ItemMetaKey.Specific(ItemMetaKey.Specific.To.NBT)
static final ItemMetaKeyType<ResolvableProfile> SKULL_PROFILE = new ItemMetaKeyType<>(DataComponents.PROFILE, "SkullProfile");
@ -158,11 +138,6 @@ class CraftMetaSkull extends CraftMetaItem implements SkullMeta {
return profile == null && noteBlockSound == null;
}
@Override
boolean applicableTo(Material type) {
return SKULL_MATERIALS.contains(type);
}
@Override
public CraftMetaSkull clone() {
return (CraftMetaSkull) super.clone();

View file

@ -2,15 +2,12 @@ package org.bukkit.craftbukkit.inventory;
import com.google.common.base.Preconditions;
import com.google.common.collect.ImmutableMap.Builder;
import com.google.common.collect.Sets;
import java.util.Map;
import java.util.Set;
import net.minecraft.core.component.DataComponentPatch;
import net.minecraft.core.component.DataComponents;
import net.minecraft.nbt.NBTBase;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.world.item.component.CustomData;
import org.bukkit.Material;
import org.bukkit.configuration.serialization.DelegateDeserialization;
import org.bukkit.craftbukkit.entity.CraftEntitySnapshot;
import org.bukkit.entity.EntitySnapshot;
@ -20,89 +17,6 @@ import org.bukkit.inventory.meta.SpawnEggMeta;
@DelegateDeserialization(SerializableMeta.class)
public class CraftMetaSpawnEgg extends CraftMetaItem implements SpawnEggMeta {
private static final Set<Material> SPAWN_EGG_MATERIALS = Sets.newHashSet(
Material.ARMADILLO_SPAWN_EGG,
Material.ALLAY_SPAWN_EGG,
Material.AXOLOTL_SPAWN_EGG,
Material.BAT_SPAWN_EGG,
Material.BEE_SPAWN_EGG,
Material.BLAZE_SPAWN_EGG,
Material.BOGGED_SPAWN_EGG,
Material.BREEZE_SPAWN_EGG,
Material.CAT_SPAWN_EGG,
Material.CAMEL_SPAWN_EGG,
Material.CAVE_SPIDER_SPAWN_EGG,
Material.CHICKEN_SPAWN_EGG,
Material.COD_SPAWN_EGG,
Material.COW_SPAWN_EGG,
Material.CREEPER_SPAWN_EGG,
Material.DOLPHIN_SPAWN_EGG,
Material.DONKEY_SPAWN_EGG,
Material.DROWNED_SPAWN_EGG,
Material.ELDER_GUARDIAN_SPAWN_EGG,
Material.ENDER_DRAGON_SPAWN_EGG,
Material.ENDERMAN_SPAWN_EGG,
Material.ENDERMITE_SPAWN_EGG,
Material.EVOKER_SPAWN_EGG,
Material.FOX_SPAWN_EGG,
Material.FROG_SPAWN_EGG,
Material.GHAST_SPAWN_EGG,
Material.GLOW_SQUID_SPAWN_EGG,
Material.GOAT_SPAWN_EGG,
Material.GUARDIAN_SPAWN_EGG,
Material.HOGLIN_SPAWN_EGG,
Material.HORSE_SPAWN_EGG,
Material.HUSK_SPAWN_EGG,
Material.IRON_GOLEM_SPAWN_EGG,
Material.LLAMA_SPAWN_EGG,
Material.MAGMA_CUBE_SPAWN_EGG,
Material.MOOSHROOM_SPAWN_EGG,
Material.MULE_SPAWN_EGG,
Material.OCELOT_SPAWN_EGG,
Material.PANDA_SPAWN_EGG,
Material.PARROT_SPAWN_EGG,
Material.PHANTOM_SPAWN_EGG,
Material.PIGLIN_BRUTE_SPAWN_EGG,
Material.PIGLIN_SPAWN_EGG,
Material.PIG_SPAWN_EGG,
Material.PILLAGER_SPAWN_EGG,
Material.POLAR_BEAR_SPAWN_EGG,
Material.PUFFERFISH_SPAWN_EGG,
Material.RABBIT_SPAWN_EGG,
Material.RAVAGER_SPAWN_EGG,
Material.SALMON_SPAWN_EGG,
Material.SHEEP_SPAWN_EGG,
Material.SHULKER_SPAWN_EGG,
Material.SILVERFISH_SPAWN_EGG,
Material.SKELETON_HORSE_SPAWN_EGG,
Material.SKELETON_SPAWN_EGG,
Material.SLIME_SPAWN_EGG,
Material.SNIFFER_SPAWN_EGG,
Material.SNOW_GOLEM_SPAWN_EGG,
Material.SPIDER_SPAWN_EGG,
Material.SQUID_SPAWN_EGG,
Material.STRAY_SPAWN_EGG,
Material.STRIDER_SPAWN_EGG,
Material.TADPOLE_SPAWN_EGG,
Material.TRADER_LLAMA_SPAWN_EGG,
Material.TROPICAL_FISH_SPAWN_EGG,
Material.TURTLE_SPAWN_EGG,
Material.VEX_SPAWN_EGG,
Material.VILLAGER_SPAWN_EGG,
Material.VINDICATOR_SPAWN_EGG,
Material.WARDEN_SPAWN_EGG,
Material.WANDERING_TRADER_SPAWN_EGG,
Material.WITCH_SPAWN_EGG,
Material.WITHER_SPAWN_EGG,
Material.WITHER_SKELETON_SPAWN_EGG,
Material.WOLF_SPAWN_EGG,
Material.ZOGLIN_SPAWN_EGG,
Material.ZOMBIE_HORSE_SPAWN_EGG,
Material.ZOMBIE_SPAWN_EGG,
Material.ZOMBIE_VILLAGER_SPAWN_EGG,
Material.ZOMBIFIED_PIGLIN_SPAWN_EGG
);
static final ItemMetaKeyType<CustomData> ENTITY_TAG = new ItemMetaKeyType<>(DataComponents.ENTITY_DATA, "entity-tag");
@ItemMetaKey.Specific(ItemMetaKey.Specific.To.NBT)
static final ItemMetaKey ENTITY_ID = new ItemMetaKey("id");
@ -162,11 +76,6 @@ public class CraftMetaSpawnEgg extends CraftMetaItem implements SpawnEggMeta {
}
}
@Override
boolean applicableTo(Material type) {
return SPAWN_EGG_MATERIALS.contains(type);
}
@Override
boolean isEmpty() {
return super.isEmpty() && isSpawnEggEmpty();

View file

@ -10,7 +10,6 @@ import java.util.Map;
import net.minecraft.core.component.DataComponentPatch;
import net.minecraft.core.component.DataComponents;
import net.minecraft.world.item.component.SuspiciousStewEffects;
import org.bukkit.Material;
import org.bukkit.configuration.serialization.DelegateDeserialization;
import org.bukkit.craftbukkit.potion.CraftPotionEffectType;
import org.bukkit.inventory.meta.SuspiciousStewMeta;
@ -90,11 +89,6 @@ public class CraftMetaSuspiciousStew extends CraftMetaItem implements Suspicious
return !hasCustomEffects();
}
@Override
boolean applicableTo(Material type) {
return type == Material.SUSPICIOUS_STEW;
}
@Override
public CraftMetaSuspiciousStew clone() {
CraftMetaSuspiciousStew clone = ((CraftMetaSuspiciousStew) super.clone());

View file

@ -8,7 +8,6 @@ import net.minecraft.nbt.NBTBase;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.world.item.component.CustomData;
import org.bukkit.DyeColor;
import org.bukkit.Material;
import org.bukkit.configuration.serialization.DelegateDeserialization;
import org.bukkit.craftbukkit.entity.CraftTropicalFish;
import org.bukkit.craftbukkit.util.CraftMagicNumbers;
@ -110,11 +109,6 @@ class CraftMetaTropicalFishBucket extends CraftMetaItem implements TropicalFishB
}
}
@Override
boolean applicableTo(Material type) {
return type == Material.TROPICAL_FISH_BUCKET;
}
@Override
boolean isEmpty() {
return super.isEmpty() && isBucketEmpty();

View file

@ -1,11 +1,19 @@
package org.bukkit.support;
import static org.mockito.Mockito.*;
import com.google.common.base.Preconditions;
import java.util.HashMap;
import java.util.Map;
import java.util.logging.Logger;
import net.minecraft.core.registries.BuiltInRegistries;
import net.minecraft.core.registries.Registries;
import net.minecraft.resources.MinecraftKey;
import net.minecraft.resources.ResourceKey;
import net.minecraft.tags.TagKey;
import net.minecraft.world.entity.EntityTypes;
import net.minecraft.world.item.Item;
import net.minecraft.world.level.block.Block;
import net.minecraft.world.level.material.FluidType;
import org.bukkit.Bukkit;
import org.bukkit.Keyed;
import org.bukkit.Material;
@ -15,6 +23,10 @@ import org.bukkit.craftbukkit.CraftLootTable;
import org.bukkit.craftbukkit.CraftRegistry;
import org.bukkit.craftbukkit.block.data.CraftBlockData;
import org.bukkit.craftbukkit.inventory.CraftItemFactory;
import org.bukkit.craftbukkit.tag.CraftBlockTag;
import org.bukkit.craftbukkit.tag.CraftEntityTag;
import org.bukkit.craftbukkit.tag.CraftFluidTag;
import org.bukkit.craftbukkit.tag.CraftItemTag;
import org.bukkit.craftbukkit.util.CraftMagicNumbers;
import org.bukkit.craftbukkit.util.CraftNamespacedKey;
import org.bukkit.craftbukkit.util.Versioning;
@ -50,6 +62,46 @@ public final class DummyServer {
return registers.computeIfAbsent(aClass, key -> CraftRegistry.createRegistry(aClass, AbstractTestingBase.REGISTRY_CUSTOM));
});
when(instance.getTag(any(), any(), any())).then(mock -> {
String registry = mock.getArgument(0);
Class<?> clazz = mock.getArgument(2);
MinecraftKey key = CraftNamespacedKey.toMinecraft(mock.getArgument(1));
switch (registry) {
case org.bukkit.Tag.REGISTRY_BLOCKS -> {
Preconditions.checkArgument(clazz == org.bukkit.Material.class, "Block namespace must have block type");
TagKey<Block> blockTagKey = TagKey.create(Registries.BLOCK, key);
if (BuiltInRegistries.BLOCK.getTag(blockTagKey).isPresent()) {
return new CraftBlockTag(BuiltInRegistries.BLOCK, blockTagKey);
}
}
case org.bukkit.Tag.REGISTRY_ITEMS -> {
Preconditions.checkArgument(clazz == org.bukkit.Material.class, "Item namespace must have item type");
TagKey<Item> itemTagKey = TagKey.create(Registries.ITEM, key);
if (BuiltInRegistries.ITEM.getTag(itemTagKey).isPresent()) {
return new CraftItemTag(BuiltInRegistries.ITEM, itemTagKey);
}
}
case org.bukkit.Tag.REGISTRY_FLUIDS -> {
Preconditions.checkArgument(clazz == org.bukkit.Fluid.class, "Fluid namespace must have fluid type");
TagKey<FluidType> fluidTagKey = TagKey.create(Registries.FLUID, key);
if (BuiltInRegistries.FLUID.getTag(fluidTagKey).isPresent()) {
return new CraftFluidTag(BuiltInRegistries.FLUID, fluidTagKey);
}
}
case org.bukkit.Tag.REGISTRY_ENTITY_TYPES -> {
Preconditions.checkArgument(clazz == org.bukkit.entity.EntityType.class, "Entity type namespace must have entity type");
TagKey<EntityTypes<?>> entityTagKey = TagKey.create(Registries.ENTITY_TYPE, key);
if (BuiltInRegistries.ENTITY_TYPE.getTag(entityTagKey).isPresent()) {
return new CraftEntityTag(BuiltInRegistries.ENTITY_TYPE, entityTagKey);
}
}
default -> throw new IllegalArgumentException();
}
return null;
});
Bukkit.setServer(instance);
} catch (Throwable t) {
throw new Error(t);