#1275: Add internal ItemType and BlockType, delegate Material methods to them

By: Jishuna <joshl5324@gmail.com>
Also-by: Bjarne Koll <lynxplay101@gmail.com>
Also-by: DerFrZocker <derrieple@gmail.com>
Also-by: md_5 <git@md-5.net>
This commit is contained in:
CraftBukkit/Spigot
2024-05-05 10:08:54 +10:00
parent b4e6cc4dce
commit 8f55ed539f
14 changed files with 642 additions and 47 deletions

View File

@@ -20,11 +20,14 @@ import org.bukkit.Particle;
import org.bukkit.Registry;
import org.bukkit.attribute.Attribute;
import org.bukkit.block.Biome;
import org.bukkit.block.BlockType;
import org.bukkit.craftbukkit.block.CraftBlockType;
import org.bukkit.craftbukkit.damage.CraftDamageType;
import org.bukkit.craftbukkit.enchantments.CraftEnchantment;
import org.bukkit.craftbukkit.entity.CraftWolf;
import org.bukkit.craftbukkit.generator.structure.CraftStructure;
import org.bukkit.craftbukkit.generator.structure.CraftStructureType;
import org.bukkit.craftbukkit.inventory.CraftItemType;
import org.bukkit.craftbukkit.inventory.trim.CraftTrimMaterial;
import org.bukkit.craftbukkit.inventory.trim.CraftTrimPattern;
import org.bukkit.craftbukkit.legacy.FieldRename;
@@ -38,6 +41,7 @@ import org.bukkit.entity.EntityType;
import org.bukkit.entity.Wolf;
import org.bukkit.generator.structure.Structure;
import org.bukkit.generator.structure.StructureType;
import org.bukkit.inventory.ItemType;
import org.bukkit.inventory.meta.trim.TrimMaterial;
import org.bukkit.inventory.meta.trim.TrimPattern;
import org.bukkit.potion.PotionEffectType;
@@ -115,7 +119,7 @@ public class CraftRegistry<B extends Keyed, M> implements Registry<B> {
* @param registryHolder the minecraft registry holder
* @return the bukkit registry of the provided class
*/
public static <B extends Keyed> Registry<?> createRegistry(Class<B> bukkitClass, IRegistryCustom registryHolder) {
public static <B extends Keyed> Registry<?> createRegistry(Class<? super B> bukkitClass, IRegistryCustom registryHolder) {
if (bukkitClass == Enchantment.class) {
return new CraftRegistry<>(Enchantment.class, registryHolder.registryOrThrow(Registries.ENCHANTMENT), CraftEnchantment::new, FieldRename.ENCHANTMENT_RENAME);
}
@@ -146,6 +150,12 @@ public class CraftRegistry<B extends Keyed, M> implements Registry<B> {
if (bukkitClass == Wolf.Variant.class) {
return new CraftRegistry<>(Wolf.Variant.class, registryHolder.registryOrThrow(Registries.WOLF_VARIANT), CraftWolf.CraftVariant::new, NONE);
}
if (bukkitClass == BlockType.class) {
return new CraftRegistry<>(BlockType.class, registryHolder.registryOrThrow(Registries.BLOCK), CraftBlockType::new, NONE);
}
if (bukkitClass == ItemType.class) {
return new CraftRegistry<>(ItemType.class, registryHolder.registryOrThrow(Registries.ITEM), CraftItemType::new, NONE);
}
return null;
}

View File

@@ -121,6 +121,7 @@ import org.bukkit.ChatColor;
import org.bukkit.GameMode;
import org.bukkit.Keyed;
import org.bukkit.Location;
import org.bukkit.Material;
import org.bukkit.NamespacedKey;
import org.bukkit.OfflinePlayer;
import org.bukkit.Registry;
@@ -2364,14 +2365,14 @@ public final class CraftServer implements Server {
public BlockData createBlockData(String data) throws IllegalArgumentException {
Preconditions.checkArgument(data != null, "data cannot be null");
return createBlockData(null, data);
return createBlockData((Material) null, data);
}
@Override
public BlockData createBlockData(org.bukkit.Material material, String data) {
Preconditions.checkArgument(material != null || data != null, "Must provide one of material or data");
return CraftBlockData.newData(material, data);
return CraftBlockData.newData((material != null) ? material.asBlockType() : null, data);
}
@Override

View File

@@ -1,10 +1,42 @@
package org.bukkit.craftbukkit.block;
import com.google.common.base.Preconditions;
import java.util.function.Consumer;
import net.minecraft.core.BlockPosition;
import net.minecraft.core.registries.Registries;
import net.minecraft.world.EnumHand;
import net.minecraft.world.entity.player.EntityHuman;
import net.minecraft.world.item.Item;
import net.minecraft.world.item.Items;
import net.minecraft.world.level.BlockAccessAir;
import net.minecraft.world.level.block.Block;
import net.minecraft.world.level.block.BlockFalling;
import net.minecraft.world.level.block.BlockFire;
import net.minecraft.world.level.block.Blocks;
import net.minecraft.world.level.block.state.BlockBase;
import net.minecraft.world.level.block.state.IBlockData;
import net.minecraft.world.phys.MovingObjectPositionBlock;
import org.bukkit.Material;
import org.bukkit.NamespacedKey;
import org.bukkit.Registry;
import org.bukkit.World;
import org.bukkit.block.BlockType;
import org.bukkit.block.data.BlockData;
import org.bukkit.craftbukkit.CraftRegistry;
import org.bukkit.craftbukkit.CraftWorld;
import org.bukkit.craftbukkit.block.data.CraftBlockData;
import org.bukkit.craftbukkit.inventory.CraftItemType;
import org.bukkit.craftbukkit.util.CraftMagicNumbers;
import org.bukkit.craftbukkit.util.Handleable;
import org.bukkit.inventory.ItemType;
import org.jetbrains.annotations.NotNull;
public class CraftBlockType {
public class CraftBlockType<B extends BlockData> implements BlockType.Typed<B>, Handleable<Block> {
private final NamespacedKey key;
private final Block block;
private final Class<B> blockDataClass;
private final boolean interactable;
public static Material minecraftToBukkit(Block block) {
return CraftMagicNumbers.getMaterial(block);
@@ -13,4 +45,184 @@ public class CraftBlockType {
public static Block bukkitToMinecraft(Material material) {
return CraftMagicNumbers.getBlock(material);
}
public static BlockType minecraftToBukkitNew(Block minecraft) {
return CraftRegistry.minecraftToBukkit(minecraft, Registries.BLOCK, Registry.BLOCK);
}
public static Block bukkitToMinecraftNew(BlockType bukkit) {
return CraftRegistry.bukkitToMinecraft(bukkit);
}
private static boolean hasMethod(Class<?> clazz, String methodName, Class<?>... params) {
boolean hasMethod;
try {
hasMethod = clazz.getDeclaredMethod(methodName, params) != null;
} catch (NoSuchMethodException ex) {
hasMethod = false;
}
return hasMethod;
}
private static boolean isInteractable(Block block) {
Class<?> clazz = block.getClass();
boolean hasMethod = hasMethod(clazz, "useWithoutItem", IBlockData.class, net.minecraft.world.level.World.class, BlockPosition.class, EntityHuman.class, MovingObjectPositionBlock.class)
|| hasMethod(clazz, "useItemOn", net.minecraft.world.item.ItemStack.class, IBlockData.class, net.minecraft.world.level.World.class, BlockPosition.class, EntityHuman.class, EnumHand.class, MovingObjectPositionBlock.class);
if (!hasMethod && clazz.getSuperclass() != BlockBase.class) {
clazz = clazz.getSuperclass();
hasMethod = hasMethod(clazz, "useWithoutItem", IBlockData.class, net.minecraft.world.level.World.class, BlockPosition.class, EntityHuman.class, MovingObjectPositionBlock.class)
|| hasMethod(clazz, "useItemOn", net.minecraft.world.item.ItemStack.class, IBlockData.class, net.minecraft.world.level.World.class, BlockPosition.class, EntityHuman.class, EnumHand.class, MovingObjectPositionBlock.class);
}
return hasMethod;
}
public CraftBlockType(NamespacedKey key, Block block) {
this.key = key;
this.block = block;
this.blockDataClass = (Class<B>) CraftBlockData.fromData(block.defaultBlockState()).getClass().getInterfaces()[0];
this.interactable = isInteractable(block);
}
@Override
public Block getHandle() {
return block;
}
@NotNull
@Override
public Typed<BlockData> typed() {
return this.typed(BlockData.class);
}
@NotNull
@Override
@SuppressWarnings("unchecked")
public <Other extends BlockData> Typed<Other> typed(@NotNull Class<Other> blockDataType) {
if (blockDataType.isAssignableFrom(this.blockDataClass)) return (Typed<Other>) this;
throw new IllegalArgumentException("Cannot type block type " + this.key.toString() + " to blockdata type " + blockDataType.getSimpleName());
}
@Override
public boolean hasItemType() {
if (this == AIR) {
return true;
}
return block.asItem() != Items.AIR;
}
@NotNull
@Override
public ItemType getItemType() {
if (this == AIR) {
return ItemType.AIR;
}
Item item = block.asItem();
Preconditions.checkArgument(item != Items.AIR, "The block type %s has no corresponding item type", getKey());
return CraftItemType.minecraftToBukkitNew(item);
}
@Override
public Class<B> getBlockDataClass() {
return blockDataClass;
}
@Override
public B createBlockData() {
return createBlockData((String) null);
}
@Override
public B createBlockData(Consumer<? super B> consumer) {
B data = createBlockData();
if (consumer != null) {
consumer.accept(data);
}
return data;
}
@Override
public B createBlockData(String data) {
return (B) CraftBlockData.newData(this, data);
}
@Override
public boolean isSolid() {
return block.defaultBlockState().blocksMotion();
}
@Override
public boolean isAir() {
return block.defaultBlockState().isAir();
}
@Override
public boolean isEnabledByFeature(@NotNull World world) {
Preconditions.checkNotNull(world, "World cannot be null");
return getHandle().isEnabled(((CraftWorld) world).getHandle().enabledFeatures());
}
@Override
public boolean isFlammable() {
return block.defaultBlockState().ignitedByLava();
}
@Override
public boolean isBurnable() {
return ((BlockFire) Blocks.FIRE).igniteOdds.getOrDefault(block, 0) > 0;
}
@Override
public boolean isOccluding() {
return block.defaultBlockState().isRedstoneConductor(BlockAccessAir.INSTANCE, BlockPosition.ZERO);
}
@Override
public boolean hasGravity() {
return block instanceof BlockFalling;
}
@Override
public boolean isInteractable() {
return interactable;
}
@Override
public float getHardness() {
return block.defaultBlockState().destroySpeed;
}
@Override
public float getBlastResistance() {
return block.getExplosionResistance();
}
@Override
public float getSlipperiness() {
return block.getFriction();
}
@NotNull
@Override
public String getTranslationKey() {
return block.getDescriptionId();
}
@Override
public NamespacedKey getKey() {
return key;
}
@Override
public Material asMaterial() {
return Registry.MATERIAL.get(this.key);
}
}

View File

@@ -31,6 +31,7 @@ import org.bukkit.SoundGroup;
import org.bukkit.block.BlockFace;
import org.bukkit.block.BlockState;
import org.bukkit.block.BlockSupport;
import org.bukkit.block.BlockType;
import org.bukkit.block.PistonMoveReaction;
import org.bukkit.block.data.BlockData;
import org.bukkit.block.structure.Mirror;
@@ -540,11 +541,9 @@ public class CraftBlockData implements BlockData {
Preconditions.checkState(MAP.put(nms, bukkit) == null, "Duplicate mapping %s->%s", nms, bukkit);
}
public static CraftBlockData newData(Material material, String data) {
Preconditions.checkArgument(material == null || material.isBlock(), "Cannot get data for not block %s", material);
public static CraftBlockData newData(BlockType blockType, String data) {
IBlockData blockData;
Block block = CraftBlockType.bukkitToMinecraft(material);
Block block = blockType == null ? null : ((CraftBlockType<?>) blockType).getHandle();
Map<IBlockState<?>, Comparable<?>> parsed = null;
// Data provided, use it

View File

@@ -1,10 +1,44 @@
package org.bukkit.craftbukkit.inventory;
import com.google.common.base.Preconditions;
import com.google.common.collect.ImmutableMultimap;
import com.google.common.collect.Multimap;
import java.util.function.Consumer;
import net.minecraft.core.component.DataComponents;
import net.minecraft.core.registries.Registries;
import net.minecraft.world.item.Item;
import net.minecraft.world.item.ItemBlock;
import net.minecraft.world.item.ItemRecord;
import net.minecraft.world.item.component.ItemAttributeModifiers;
import net.minecraft.world.level.block.entity.TileEntityFurnace;
import org.bukkit.Material;
import org.bukkit.NamespacedKey;
import org.bukkit.Registry;
import org.bukkit.World;
import org.bukkit.attribute.Attribute;
import org.bukkit.attribute.AttributeModifier;
import org.bukkit.block.BlockType;
import org.bukkit.craftbukkit.CraftEquipmentSlot;
import org.bukkit.craftbukkit.CraftRegistry;
import org.bukkit.craftbukkit.CraftWorld;
import org.bukkit.craftbukkit.attribute.CraftAttribute;
import org.bukkit.craftbukkit.attribute.CraftAttributeInstance;
import org.bukkit.craftbukkit.block.CraftBlockType;
import org.bukkit.craftbukkit.util.CraftMagicNumbers;
import org.bukkit.craftbukkit.util.Handleable;
import org.bukkit.inventory.CreativeCategory;
import org.bukkit.inventory.EquipmentSlot;
import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.ItemType;
import org.bukkit.inventory.meta.ItemMeta;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
public class CraftItemType {
public class CraftItemType<M extends ItemMeta> implements ItemType.Typed<M>, Handleable<Item> {
private final NamespacedKey key;
private final Item item;
private final Class<M> itemMetaClass;
public static Material minecraftToBukkit(Item item) {
return CraftMagicNumbers.getMaterial(item);
@@ -13,4 +47,183 @@ public class CraftItemType {
public static Item bukkitToMinecraft(Material material) {
return CraftMagicNumbers.getItem(material);
}
public static ItemType minecraftToBukkitNew(Item minecraft) {
return CraftRegistry.minecraftToBukkit(minecraft, Registries.ITEM, Registry.ITEM);
}
public static Item bukkitToMinecraftNew(ItemType bukkit) {
return CraftRegistry.bukkitToMinecraft(bukkit);
}
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;
}
@NotNull
@Override
public Typed<ItemMeta> typed() {
return this.typed(ItemMeta.class);
}
@NotNull
@Override
@SuppressWarnings("unchecked")
public <Other extends ItemMeta> Typed<Other> typed(@NotNull final Class<Other> itemMetaType) {
if (itemMetaType.isAssignableFrom(this.itemMetaClass)) return (Typed<Other>) this;
throw new IllegalArgumentException("Cannot type item type " + this.key.toString() + " to meta type " + itemMetaType.getSimpleName());
}
@NotNull
@Override
public ItemStack createItemStack() {
return this.createItemStack(1, null);
}
@NotNull
@Override
public ItemStack createItemStack(final int amount) {
return this.createItemStack(amount, null);
}
@NotNull
@Override
public ItemStack createItemStack(Consumer<? super M> metaConfigurator) {
return this.createItemStack(1, metaConfigurator);
}
@NotNull
@Override
public ItemStack createItemStack(final int amount, @Nullable final Consumer<? super M> metaConfigurator) {
final ItemStack itemStack = new ItemStack(this.asMaterial(), amount);
if (metaConfigurator != null) {
final ItemMeta itemMeta = itemStack.getItemMeta();
metaConfigurator.accept((M) itemMeta);
itemStack.setItemMeta(itemMeta);
}
return itemStack;
}
@Override
public Item getHandle() {
return item;
}
@Override
public boolean hasBlockType() {
return item instanceof ItemBlock;
}
@NotNull
@Override
public BlockType getBlockType() {
if (!(item instanceof ItemBlock block)) {
throw new IllegalStateException("The item type " + getKey() + " has no corresponding block type");
}
return CraftBlockType.minecraftToBukkitNew(block.getBlock());
}
@Override
public Class<M> getItemMetaClass() {
if (this == ItemType.AIR) {
throw new UnsupportedOperationException("Air does not have ItemMeta");
}
return itemMetaClass;
}
@Override
public int getMaxStackSize() {
// Based of the material enum air is only 0, in PerMaterialTest it is also set as special case
// the item info itself would return 64
if (this == AIR) {
return 0;
}
return item.components().getOrDefault(DataComponents.MAX_STACK_SIZE, 64);
}
@Override
public short getMaxDurability() {
return item.components().getOrDefault(DataComponents.MAX_DAMAGE, 0).shortValue();
}
@Override
public boolean isEdible() {
return item.components().has(DataComponents.FOOD);
}
@Override
public boolean isRecord() {
return item instanceof ItemRecord;
}
@Override
public boolean isFuel() {
return TileEntityFurnace.isFuel(new net.minecraft.world.item.ItemStack(item));
}
@Override
public ItemType getCraftingRemainingItem() {
Item expectedItem = item.getCraftingRemainingItem();
return expectedItem == null ? null : minecraftToBukkitNew(expectedItem);
}
// @Override
// public EquipmentSlot getEquipmentSlot() {
// return CraftEquipmentSlot.getSlot(EntityInsentient.getEquipmentSlotForItem(CraftItemStack.asNMSCopy(ItemStack.of(this))));
// }
@Override
public Multimap<Attribute, AttributeModifier> getDefaultAttributeModifiers(EquipmentSlot slot) {
ImmutableMultimap.Builder<Attribute, AttributeModifier> defaultAttributes = ImmutableMultimap.builder();
ItemAttributeModifiers nmsDefaultAttributes = item.getDefaultAttributeModifiers();
nmsDefaultAttributes.forEach(CraftEquipmentSlot.getNMS(slot), (key, value) -> {
Attribute attribute = CraftAttribute.minecraftToBukkit(key.value());
defaultAttributes.put(attribute, CraftAttributeInstance.convert(value, slot));
});
return defaultAttributes.build();
}
@Override
public CreativeCategory getCreativeCategory() {
return CreativeCategory.BUILDING_BLOCKS;
}
@Override
public boolean isEnabledByFeature(@NotNull World world) {
Preconditions.checkNotNull(world, "World cannot be null");
return getHandle().isEnabled(((CraftWorld) world).getHandle().enabledFeatures());
}
@NotNull
@Override
public String getTranslationKey() {
return item.getDescriptionId();
}
@Override
public NamespacedKey getKey() {
return key;
}
@Override
public Material asMaterial() {
return Registry.MATERIAL.get(this.key);
}
}

View File

@@ -192,7 +192,7 @@ public final class CraftLegacy {
}
}
if (mappedData == null && material.isBlock()) {
if (mappedData == null) {
// Try exact match first
IBlockData iblock = materialToData.get(materialData);
if (iblock != null) {

View File

@@ -10,11 +10,13 @@ import net.minecraft.world.entity.EntityTypes;
import org.bukkit.Material;
import org.bukkit.NamespacedKey;
import org.bukkit.World;
import org.bukkit.block.BlockType;
import org.bukkit.craftbukkit.CraftWorld;
import org.bukkit.craftbukkit.block.CraftBlockType;
import org.bukkit.craftbukkit.entity.CraftEntityType;
import org.bukkit.craftbukkit.inventory.CraftItemType;
import org.bukkit.entity.EntityType;
import org.bukkit.inventory.ItemType;
import org.bukkit.packs.DataPack;
import org.bukkit.packs.DataPackManager;
@@ -89,6 +91,24 @@ public class CraftDataPackManager implements DataPackManager {
return false;
}
@Override
public boolean isEnabledByFeature(ItemType itemType, World world) {
Preconditions.checkArgument(itemType != null, "itemType cannot be null");
Preconditions.checkArgument(world != null, "world cannot be null");
CraftWorld craftWorld = ((CraftWorld) world);
return CraftItemType.bukkitToMinecraftNew(itemType.typed()).isEnabled(craftWorld.getHandle().enabledFeatures());
}
@Override
public boolean isEnabledByFeature(BlockType blockType, World world) {
Preconditions.checkArgument(blockType != null, "blockType cannot be null");
Preconditions.checkArgument(world != null, "world cannot be null");
CraftWorld craftWorld = ((CraftWorld) world);
return CraftBlockType.bukkitToMinecraftNew(blockType.typed()).isEnabled(craftWorld.getHandle().enabledFeatures());
}
@Override
public boolean isEnabledByFeature(EntityType entityType, World world) {
Preconditions.checkArgument(entityType != null, "entityType cannot be null");