SPIGOT-7146: Reduce use of Material switch in ItemMeta

The Java compiler expands switch statements into a synthetic method that generates an int array with the enum's ordinal. For small enums this is fine, but with an enum as large as Material, this can add over one hundred thousand additional bytes of data. This unnecessarily increases CraftBukkit's overall file size by over 2MB. Where possible, simple conditions are returned. In cases where more than 2 constants are referenced, a HashSet is used (as opposed to an EnumSet which generates a large backing array into memory).

By: Parker Hawke <hawkeboyz2@hotmail.com>
This commit is contained in:
CraftBukkit/Spigot 2022-09-11 10:26:21 +10:00
parent 2935ef7994
commit 803175b864
20 changed files with 264 additions and 354 deletions

View file

@ -6,7 +6,6 @@ import net.minecraft.nbt.NBTBase;
import net.minecraft.nbt.NBTTagCompound;
import org.bukkit.Material;
import org.bukkit.configuration.serialization.DelegateDeserialization;
import org.bukkit.craftbukkit.inventory.CraftMetaItem.ItemMetaKey;
@DelegateDeserialization(CraftMetaItem.SerializableMeta.class)
public class CraftMetaArmorStand extends CraftMetaItem {
@ -64,12 +63,7 @@ public class CraftMetaArmorStand extends CraftMetaItem {
@Override
boolean applicableTo(Material type) {
switch (type) {
case ARMOR_STAND:
return true;
default:
return false;
}
return type == Material.ARMOR_STAND;
}
@Override

View file

@ -83,12 +83,7 @@ public class CraftMetaAxolotlBucket extends CraftMetaItem implements AxolotlBuck
@Override
boolean applicableTo(Material type) {
switch (type) {
case AXOLOTL_BUCKET:
return true;
default:
return false;
}
return type == Material.AXOLOTL_BUCKET;
}
@Override

View file

@ -2,9 +2,11 @@ package org.bukkit.craftbukkit.inventory;
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.nbt.NBTTagCompound;
import net.minecraft.nbt.NBTTagList;
import org.bukkit.DyeColor;
@ -12,14 +14,47 @@ import org.bukkit.Material;
import org.bukkit.block.banner.Pattern;
import org.bukkit.block.banner.PatternType;
import org.bukkit.configuration.serialization.DelegateDeserialization;
import org.bukkit.craftbukkit.inventory.CraftMetaItem.ItemMetaKey;
import org.bukkit.craftbukkit.inventory.CraftMetaItem.SerializableMeta;
import org.bukkit.craftbukkit.util.CraftMagicNumbers;
import org.bukkit.inventory.meta.BannerMeta;
@DelegateDeserialization(CraftMetaItem.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 ItemMetaKey BASE = new ItemMetaKey("Base", "base-color");
static final ItemMetaKey PATTERNS = new ItemMetaKey("Patterns", "patterns");
static final ItemMetaKey COLOR = new ItemMetaKey("Color", "color");
@ -205,43 +240,7 @@ public class CraftMetaBanner extends CraftMetaItem implements BannerMeta {
@Override
boolean applicableTo(Material type) {
switch (type) {
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 true;
default:
return false;
}
return BANNER_MATERIALS.contains(type);
}
@Override

View file

@ -2,7 +2,9 @@ package org.bukkit.craftbukkit.inventory;
import com.google.common.base.Objects;
import com.google.common.collect.ImmutableMap;
import com.google.common.collect.Sets;
import java.util.Map;
import java.util.Set;
import net.minecraft.nbt.NBTBase;
import net.minecraft.nbt.NBTTagCompound;
import org.apache.commons.lang.Validate;
@ -12,14 +14,90 @@ import org.bukkit.configuration.serialization.DelegateDeserialization;
import org.bukkit.craftbukkit.block.CraftBanner;
import org.bukkit.craftbukkit.block.CraftBlockEntityState;
import org.bukkit.craftbukkit.block.CraftBlockStates;
import org.bukkit.craftbukkit.inventory.CraftMetaItem.ItemMetaKey;
import org.bukkit.craftbukkit.inventory.CraftMetaItem.SerializableMeta;
import org.bukkit.craftbukkit.util.CraftMagicNumbers;
import org.bukkit.inventory.meta.BlockStateMeta;
@DelegateDeserialization(CraftMetaItem.SerializableMeta.class)
public class CraftMetaBlockState extends CraftMetaItem implements BlockStateMeta {
private static final Set<Material> SHULKER_BOX_MATERIALS = Sets.newHashSet(
Material.SHULKER_BOX,
Material.WHITE_SHULKER_BOX,
Material.ORANGE_SHULKER_BOX,
Material.MAGENTA_SHULKER_BOX,
Material.LIGHT_BLUE_SHULKER_BOX,
Material.YELLOW_SHULKER_BOX,
Material.LIME_SHULKER_BOX,
Material.PINK_SHULKER_BOX,
Material.GRAY_SHULKER_BOX,
Material.LIGHT_GRAY_SHULKER_BOX,
Material.CYAN_SHULKER_BOX,
Material.PURPLE_SHULKER_BOX,
Material.BLUE_SHULKER_BOX,
Material.BROWN_SHULKER_BOX,
Material.GREEN_SHULKER_BOX,
Material.RED_SHULKER_BOX,
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_SIGN,
Material.ACACIA_WALL_SIGN,
Material.BIRCH_SIGN,
Material.BIRCH_WALL_SIGN,
Material.CRIMSON_SIGN,
Material.CRIMSON_WALL_SIGN,
Material.DARK_OAK_SIGN,
Material.DARK_OAK_WALL_SIGN,
Material.JUNGLE_SIGN,
Material.JUNGLE_WALL_SIGN,
Material.MANGROVE_SIGN,
Material.MANGROVE_WALL_SIGN,
Material.OAK_SIGN,
Material.OAK_WALL_SIGN,
Material.SPRUCE_SIGN,
Material.SPRUCE_WALL_SIGN,
Material.WARPED_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.SCULK_SENSOR
);
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 ItemMetaKey BLOCK_ENTITY_TAG = new ItemMetaKey("BlockEntityTag");
@ -129,77 +207,7 @@ public class CraftMetaBlockState extends CraftMetaItem implements BlockStateMeta
@Override
boolean applicableTo(Material type) {
switch (type) {
case FURNACE:
case CHEST:
case TRAPPED_CHEST:
case JUKEBOX:
case DISPENSER:
case DROPPER:
case ACACIA_SIGN:
case ACACIA_WALL_SIGN:
case BIRCH_SIGN:
case BIRCH_WALL_SIGN:
case CRIMSON_SIGN:
case CRIMSON_WALL_SIGN:
case DARK_OAK_SIGN:
case DARK_OAK_WALL_SIGN:
case JUNGLE_SIGN:
case JUNGLE_WALL_SIGN:
case MANGROVE_SIGN:
case MANGROVE_WALL_SIGN:
case OAK_SIGN:
case OAK_WALL_SIGN:
case SPRUCE_SIGN:
case SPRUCE_WALL_SIGN:
case WARPED_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:
return true;
}
return false;
return BLOCK_STATE_MATERIALS.contains(type);
}
@Override
@ -220,33 +228,12 @@ public class CraftMetaBlockState extends CraftMetaItem implements BlockStateMeta
public BlockState getBlockState() {
Material stateMaterial = (material != Material.SHIELD) ? material : shieldToBannerHack(blockEntityTag); // Only actually used for jigsaws
if (blockEntityTag != null) {
switch (material) {
case SHIELD:
blockEntityTag.putString("id", "minecraft:banner");
break;
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:
blockEntityTag.putString("id", "minecraft:shulker_box");
break;
case BEE_NEST:
case BEEHIVE:
blockEntityTag.putString("id", "minecraft:beehive");
break;
if (material == Material.SHIELD) {
blockEntityTag.putString("id", "minecraft:banner");
} else if (material == Material.BEE_NEST || material == Material.BEEHIVE) {
blockEntityTag.putString("id", "minecraft:beehive");
} else if (SHULKER_BOX_MATERIALS.contains(material)) {
blockEntityTag.putString("id", "minecraft:shulker_box");
}
}

View file

@ -15,12 +15,10 @@ import net.minecraft.network.chat.IChatBaseComponent;
import org.apache.commons.lang.Validate;
import org.bukkit.Material;
import org.bukkit.configuration.serialization.DelegateDeserialization;
import org.bukkit.craftbukkit.inventory.CraftMetaItem.ItemMetaKey;
import org.bukkit.craftbukkit.inventory.CraftMetaItem.SerializableMeta;
import org.bukkit.craftbukkit.util.CraftChatMessage;
import org.bukkit.craftbukkit.util.CraftMagicNumbers;
import org.bukkit.inventory.meta.BookMeta;
import org.bukkit.inventory.meta.BookMeta.Generation;
@DelegateDeserialization(SerializableMeta.class)
public class CraftMetaBook extends CraftMetaItem implements BookMeta {
@ -195,13 +193,7 @@ public class CraftMetaBook extends CraftMetaItem implements BookMeta {
@Override
boolean applicableTo(Material type) {
switch (type) {
case WRITTEN_BOOK:
case WRITABLE_BOOK:
return true;
default:
return false;
}
return type == Material.WRITTEN_BOOK || type == Material.WRITABLE_BOOK;
}
@Override

View file

@ -51,13 +51,7 @@ class CraftMetaBookSigned extends CraftMetaBook implements BookMeta {
@Override
boolean applicableTo(Material type) {
switch (type) {
case WRITTEN_BOOK:
case WRITABLE_BOOK:
return true;
default:
return false;
}
return type == Material.WRITTEN_BOOK || type == Material.WRITABLE_BOOK;
}
@Override

View file

@ -10,8 +10,6 @@ import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.nbt.NBTTagList;
import org.bukkit.Material;
import org.bukkit.configuration.serialization.DelegateDeserialization;
import org.bukkit.craftbukkit.inventory.CraftMetaItem.ItemMetaKey;
import org.bukkit.craftbukkit.inventory.CraftMetaItem.SerializableMeta;
import org.bukkit.craftbukkit.util.CraftMagicNumbers;
import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.meta.BundleMeta;
@ -87,12 +85,7 @@ public class CraftMetaBundle extends CraftMetaItem implements BundleMeta {
@Override
boolean applicableTo(Material type) {
switch (type) {
case BUNDLE:
return true;
default:
return false;
}
return type == Material.BUNDLE;
}
@Override

View file

@ -6,7 +6,6 @@ import net.minecraft.nbt.NBTTagCompound;
import org.bukkit.FireworkEffect;
import org.bukkit.Material;
import org.bukkit.configuration.serialization.DelegateDeserialization;
import org.bukkit.craftbukkit.inventory.CraftMetaItem.ItemMetaKey;
import org.bukkit.craftbukkit.inventory.CraftMetaItem.SerializableMeta;
import org.bukkit.inventory.meta.FireworkEffectMeta;
@ -68,12 +67,7 @@ class CraftMetaCharge extends CraftMetaItem implements FireworkEffectMeta {
@Override
boolean applicableTo(Material type) {
switch (type) {
case FIREWORK_STAR:
return true;
default:
return false;
}
return type == Material.FIREWORK_STAR;
}
@Override

View file

@ -17,8 +17,6 @@ import org.bukkit.Material;
import org.bukkit.World;
import org.bukkit.configuration.serialization.DelegateDeserialization;
import org.bukkit.craftbukkit.CraftWorld;
import org.bukkit.craftbukkit.inventory.CraftMetaItem.ItemMetaKey;
import org.bukkit.craftbukkit.inventory.CraftMetaItem.SerializableMeta;
import org.bukkit.inventory.meta.CompassMeta;
@DelegateDeserialization(CraftMetaItem.SerializableMeta.class)

View file

@ -11,8 +11,6 @@ import net.minecraft.nbt.NBTTagList;
import net.minecraft.world.item.ItemArrow;
import org.bukkit.Material;
import org.bukkit.configuration.serialization.DelegateDeserialization;
import org.bukkit.craftbukkit.inventory.CraftMetaItem.ItemMetaKey;
import org.bukkit.craftbukkit.inventory.CraftMetaItem.SerializableMeta;
import org.bukkit.craftbukkit.util.CraftMagicNumbers;
import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.meta.CrossbowMeta;
@ -99,12 +97,7 @@ public class CraftMetaCrossbow extends CraftMetaItem implements CrossbowMeta {
@Override
boolean applicableTo(Material type) {
switch (type) {
case CROSSBOW:
return true;
default:
return false;
}
return type == Material.CROSSBOW;
}
@Override

View file

@ -7,7 +7,6 @@ import java.util.Map;
import net.minecraft.nbt.NBTTagCompound;
import org.bukkit.Material;
import org.bukkit.configuration.serialization.DelegateDeserialization;
import org.bukkit.craftbukkit.inventory.CraftMetaItem.ItemMetaKey;
import org.bukkit.craftbukkit.inventory.CraftMetaItem.SerializableMeta;
import org.bukkit.enchantments.Enchantment;
import org.bukkit.inventory.meta.EnchantmentStorageMeta;
@ -57,12 +56,7 @@ class CraftMetaEnchantedBook extends CraftMetaItem implements EnchantmentStorage
@Override
boolean applicableTo(Material type) {
switch (type) {
case ENCHANTED_BOOK:
return true;
default:
return false;
}
return type == Material.ENCHANTED_BOOK;
}
@Override

View file

@ -1,16 +1,26 @@
package org.bukkit.craftbukkit.inventory;
import com.google.common.collect.ImmutableMap.Builder;
import com.google.common.collect.Sets;
import java.util.Map;
import java.util.Set;
import net.minecraft.nbt.NBTBase;
import net.minecraft.nbt.NBTTagCompound;
import org.bukkit.Material;
import org.bukkit.configuration.serialization.DelegateDeserialization;
import org.bukkit.craftbukkit.inventory.CraftMetaItem.ItemMetaKey;
@DelegateDeserialization(CraftMetaItem.SerializableMeta.class)
public class CraftMetaEntityTag extends CraftMetaItem {
private static final Set<Material> ENTITY_TAGGABLE_MATERIALS = Sets.newHashSet(
Material.COD_BUCKET,
Material.PUFFERFISH_BUCKET,
Material.SALMON_BUCKET,
Material.ITEM_FRAME,
Material.GLOW_ITEM_FRAME,
Material.PAINTING
);
static final ItemMetaKey ENTITY_TAG = new ItemMetaKey("EntityTag", "entity-tag");
NBTTagCompound entityTag;
@ -64,17 +74,7 @@ public class CraftMetaEntityTag extends CraftMetaItem {
@Override
boolean applicableTo(Material type) {
switch (type) {
case COD_BUCKET:
case PUFFERFISH_BUCKET:
case SALMON_BUCKET:
case ITEM_FRAME:
case GLOW_ITEM_FRAME:
case PAINTING:
return true;
default:
return false;
}
return ENTITY_TAGGABLE_MATERIALS.contains(type);
}
@Override

View file

@ -14,7 +14,6 @@ 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;
import org.bukkit.craftbukkit.inventory.CraftMetaItem.ItemMetaKey.Specific;
import org.bukkit.craftbukkit.inventory.CraftMetaItem.ItemMetaKey.Specific.To;
import org.bukkit.craftbukkit.inventory.CraftMetaItem.SerializableMeta;
@ -255,12 +254,7 @@ class CraftMetaFirework extends CraftMetaItem implements FireworkMeta {
@Override
boolean applicableTo(Material type) {
switch (type) {
case FIREWORK_ROCKET:
return true;
default:
return false;
}
return type == Material.FIREWORK_ROCKET;
}
@Override
@ -346,7 +340,7 @@ class CraftMetaFirework extends CraftMetaItem implements FireworkMeta {
}
@Override
public void addEffects(FireworkEffect...effects) {
public void addEffects(FireworkEffect... effects) {
Validate.notNull(effects, "Effects cannot be null");
if (effects.length == 0) {
return;

View file

@ -11,7 +11,6 @@ import net.minecraft.nbt.NBTTagString;
import org.bukkit.Material;
import org.bukkit.NamespacedKey;
import org.bukkit.configuration.serialization.DelegateDeserialization;
import org.bukkit.craftbukkit.inventory.CraftMetaItem.ItemMetaKey;
import org.bukkit.craftbukkit.inventory.CraftMetaItem.SerializableMeta;
import org.bukkit.craftbukkit.util.CraftNamespacedKey;
import org.bukkit.inventory.meta.KnowledgeBookMeta;
@ -84,12 +83,7 @@ public class CraftMetaKnowledgeBook extends CraftMetaItem implements KnowledgeBo
@Override
boolean applicableTo(Material type) {
switch (type) {
case KNOWLEDGE_BOOK:
return true;
default:
return false;
}
return type == Material.KNOWLEDGE_BOOK;
}
@Override

View file

@ -2,18 +2,28 @@ 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.nbt.NBTTagCompound;
import net.minecraft.nbt.NBTTagInt;
import org.bukkit.Color;
import org.bukkit.Material;
import org.bukkit.configuration.serialization.DelegateDeserialization;
import org.bukkit.craftbukkit.inventory.CraftMetaItem.ItemMetaKey;
import org.bukkit.craftbukkit.inventory.CraftMetaItem.SerializableMeta;
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
);
static final ItemMetaKey COLOR = new ItemMetaKey("color");
private Color color = DEFAULT_LEATHER_COLOR;
@ -67,16 +77,7 @@ class CraftMetaLeatherArmor extends CraftMetaItem implements LeatherArmorMeta {
@Override
boolean applicableTo(Material type) {
switch (type) {
case LEATHER_HELMET:
case LEATHER_HORSE_ARMOR:
case LEATHER_CHESTPLATE:
case LEATHER_LEGGINGS:
case LEATHER_BOOTS:
return true;
default:
return false;
}
return LEATHER_ARMOR_MATERIALS.contains(type);
}
@Override

View file

@ -10,7 +10,6 @@ import org.bukkit.Bukkit;
import org.bukkit.Color;
import org.bukkit.Material;
import org.bukkit.configuration.serialization.DelegateDeserialization;
import org.bukkit.craftbukkit.inventory.CraftMetaItem.ItemMetaKey;
import org.bukkit.craftbukkit.inventory.CraftMetaItem.SerializableMeta;
import org.bukkit.craftbukkit.util.CraftMagicNumbers;
import org.bukkit.inventory.meta.MapMeta;
@ -120,12 +119,7 @@ class CraftMetaMap extends CraftMetaItem implements MapMeta {
@Override
boolean applicableTo(Material type) {
switch (type) {
case FILLED_MAP:
return true;
default:
return false;
}
return type == Material.FILLED_MAP;
}
@Override

View file

@ -2,17 +2,18 @@ package org.bukkit.craftbukkit.inventory;
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.Set;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.nbt.NBTTagList;
import org.apache.commons.lang.Validate;
import org.bukkit.Color;
import org.bukkit.Material;
import org.bukkit.configuration.serialization.DelegateDeserialization;
import org.bukkit.craftbukkit.inventory.CraftMetaItem.ItemMetaKey;
import org.bukkit.craftbukkit.inventory.CraftMetaItem.SerializableMeta;
import org.bukkit.craftbukkit.potion.CraftPotionUtil;
import org.bukkit.craftbukkit.util.CraftMagicNumbers;
@ -24,6 +25,14 @@ 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
);
static final ItemMetaKey AMPLIFIER = new ItemMetaKey("Amplifier", "amplifier");
static final ItemMetaKey AMBIENT = new ItemMetaKey("Ambient", "ambient");
static final ItemMetaKey DURATION = new ItemMetaKey("Duration", "duration");
@ -148,15 +157,7 @@ class CraftMetaPotion extends CraftMetaItem implements PotionMeta {
@Override
boolean applicableTo(Material type) {
switch (type) {
case POTION:
case SPLASH_POTION:
case LINGERING_POTION:
case TIPPED_ARROW:
return true;
default:
return false;
}
return POTION_MATERIALS.contains(type);
}
@Override

View file

@ -1,8 +1,10 @@
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.Set;
import java.util.UUID;
import net.minecraft.nbt.GameProfileSerializer;
import net.minecraft.nbt.NBTTagCompound;
@ -12,7 +14,6 @@ import org.bukkit.Material;
import org.bukkit.OfflinePlayer;
import org.bukkit.configuration.serialization.DelegateDeserialization;
import org.bukkit.craftbukkit.entity.CraftPlayer;
import org.bukkit.craftbukkit.inventory.CraftMetaItem.ItemMetaKey;
import org.bukkit.craftbukkit.inventory.CraftMetaItem.SerializableMeta;
import org.bukkit.craftbukkit.profile.CraftPlayerProfile;
import org.bukkit.craftbukkit.util.CraftMagicNumbers;
@ -22,6 +23,21 @@ 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.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 ItemMetaKey SKULL_PROFILE = new ItemMetaKey("SkullProfile");
@ -109,23 +125,7 @@ class CraftMetaSkull extends CraftMetaItem implements SkullMeta {
@Override
boolean applicableTo(Material type) {
switch (type) {
case CREEPER_HEAD:
case CREEPER_WALL_HEAD:
case DRAGON_HEAD:
case DRAGON_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 true;
default:
return false;
}
return SKULL_MATERIALS.contains(type);
}
@Override

View file

@ -1,14 +1,14 @@
package org.bukkit.craftbukkit.inventory;
import com.google.common.collect.ImmutableMap.Builder;
import com.google.common.collect.Sets;
import java.util.Map;
import java.util.Set;
import net.minecraft.nbt.NBTBase;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.resources.MinecraftKey;
import org.bukkit.Material;
import org.bukkit.configuration.serialization.DelegateDeserialization;
import org.bukkit.craftbukkit.inventory.CraftMetaItem.ItemMetaKey;
import org.bukkit.craftbukkit.inventory.CraftMetaItem.SerializableMeta;
import org.bukkit.craftbukkit.util.CraftLegacy;
import org.bukkit.entity.EntityType;
import org.bukkit.inventory.meta.SpawnEggMeta;
@ -17,6 +17,76 @@ import org.bukkit.material.MaterialData;
@DelegateDeserialization(CraftMetaItem.SerializableMeta.class)
public class CraftMetaSpawnEgg extends CraftMetaItem implements SpawnEggMeta {
private static final Set<Material> SPAWN_EGG_MATERIALS = Sets.newHashSet(
Material.AXOLOTL_SPAWN_EGG,
Material.BAT_SPAWN_EGG,
Material.BEE_SPAWN_EGG,
Material.BLAZE_SPAWN_EGG,
Material.CAT_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.ENDERMAN_SPAWN_EGG,
Material.ENDERMITE_SPAWN_EGG,
Material.EVOKER_SPAWN_EGG,
Material.FOX_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.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.SPIDER_SPAWN_EGG,
Material.SQUID_SPAWN_EGG,
Material.STRAY_SPAWN_EGG,
Material.STRIDER_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.WANDERING_TRADER_SPAWN_EGG,
Material.WITCH_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 ItemMetaKey ENTITY_TAG = new ItemMetaKey("EntityTag", "entity-tag");
@ItemMetaKey.Specific(ItemMetaKey.Specific.To.NBT)
static final ItemMetaKey ENTITY_ID = new ItemMetaKey("id");
@ -111,78 +181,7 @@ public class CraftMetaSpawnEgg extends CraftMetaItem implements SpawnEggMeta {
@Override
boolean applicableTo(Material type) {
switch (type) {
case AXOLOTL_SPAWN_EGG:
case BAT_SPAWN_EGG:
case BEE_SPAWN_EGG:
case BLAZE_SPAWN_EGG:
case CAT_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 ENDERMAN_SPAWN_EGG:
case ENDERMITE_SPAWN_EGG:
case EVOKER_SPAWN_EGG:
case FOX_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 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 SPIDER_SPAWN_EGG:
case SQUID_SPAWN_EGG:
case STRAY_SPAWN_EGG:
case STRIDER_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 WITCH_SPAWN_EGG:
case WITHER_SKELETON_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 true;
default:
return false;
}
return SPAWN_EGG_MATERIALS.contains(type);
}
@Override

View file

@ -8,7 +8,6 @@ import org.bukkit.DyeColor;
import org.bukkit.Material;
import org.bukkit.configuration.serialization.DelegateDeserialization;
import org.bukkit.craftbukkit.entity.CraftTropicalFish;
import org.bukkit.craftbukkit.inventory.CraftMetaItem.ItemMetaKey;
import org.bukkit.craftbukkit.inventory.CraftMetaItem.SerializableMeta;
import org.bukkit.craftbukkit.util.CraftMagicNumbers;
import org.bukkit.entity.TropicalFish;
@ -86,12 +85,7 @@ class CraftMetaTropicalFishBucket extends CraftMetaItem implements TropicalFishB
@Override
boolean applicableTo(Material type) {
switch (type) {
case TROPICAL_FISH_BUCKET:
return true;
default:
return false;
}
return type == Material.TROPICAL_FISH_BUCKET;
}
@Override