#1325: Use CraftBlockType and CraftItemType instead of CraftMagicNumbers to convert between minecraft and bukkit block / item representation

By: DerFrZocker <derrieple@gmail.com>
This commit is contained in:
CraftBukkit/Spigot
2023-12-29 08:36:38 +11:00
parent b245f22351
commit d15575db91
32 changed files with 137 additions and 102 deletions

View File

@@ -221,7 +221,7 @@ public class CraftBlock implements Block {
@Override
public Material getType() {
return CraftMagicNumbers.getMaterial(world.getBlockState(position).getBlock());
return CraftBlockType.minecraftToBukkit(world.getBlockState(position).getBlock());
}
@Override

View File

@@ -165,13 +165,13 @@ public class CraftBlockState implements BlockState {
Preconditions.checkArgument(type.isBlock(), "Material must be a block!");
if (this.getType() != type) {
this.data = CraftMagicNumbers.getBlock(type).defaultBlockState();
this.data = CraftBlockType.bukkitToMinecraft(type).defaultBlockState();
}
}
@Override
public Material getType() {
return CraftMagicNumbers.getMaterial(data.getBlock());
return CraftBlockType.minecraftToBukkit(data.getBlock());
}
public void setFlag(int flag) {

View File

@@ -61,7 +61,6 @@ import org.bukkit.World;
import org.bukkit.block.Block;
import org.bukkit.block.BlockState;
import org.bukkit.craftbukkit.CraftWorld;
import org.bukkit.craftbukkit.util.CraftMagicNumbers;
public final class CraftBlockStates {
@@ -121,7 +120,7 @@ public final class CraftBlockStates {
// block with unhandled TileEntity:
return new CraftBlockEntityState<>(world, tileEntity);
}
Preconditions.checkState(tileEntity == null, "Unexpected BlockState for %s", CraftMagicNumbers.getMaterial(blockData.getBlock()));
Preconditions.checkState(tileEntity == null, "Unexpected BlockState for %s", CraftBlockType.minecraftToBukkit(blockData.getBlock()));
return new CraftBlockState(world, blockPosition, blockData);
}
};
@@ -377,7 +376,7 @@ public final class CraftBlockStates {
BlockStateFactory<?> factory = getFactory(material);
if (factory instanceof BlockEntityStateFactory) {
return ((BlockEntityStateFactory<?, ?>) factory).createTileEntity(BlockPosition.ZERO, CraftMagicNumbers.getBlock(material).defaultBlockState());
return ((BlockEntityStateFactory<?, ?>) factory).createTileEntity(BlockPosition.ZERO, CraftBlockType.bukkitToMinecraft(material).defaultBlockState());
}
return null;
@@ -401,7 +400,7 @@ public final class CraftBlockStates {
public static BlockState getBlockState(BlockPosition blockPosition, Material material, @Nullable NBTTagCompound blockEntityTag) {
Preconditions.checkNotNull(material, "material is null");
IBlockData blockData = CraftMagicNumbers.getBlock(material).defaultBlockState();
IBlockData blockData = CraftBlockType.bukkitToMinecraft(material).defaultBlockState();
return getBlockState(blockPosition, blockData, blockEntityTag);
}
@@ -418,7 +417,7 @@ public final class CraftBlockStates {
// See BlockStateFactory#createBlockState(World, BlockPosition, IBlockData, TileEntity)
private static CraftBlockState getBlockState(World world, BlockPosition blockPosition, IBlockData blockData, TileEntity tileEntity) {
Material material = CraftMagicNumbers.getMaterial(blockData.getBlock());
Material material = CraftBlockType.minecraftToBukkit(blockData.getBlock());
BlockStateFactory<?> factory;
// For some types of TileEntity blocks (eg. moving pistons), Minecraft may in some situations (eg. when using Block#setType or the
// setBlock command) not create a corresponding TileEntity in the world. We return a normal BlockState in this case.

View File

@@ -0,0 +1,16 @@
package org.bukkit.craftbukkit.block;
import net.minecraft.world.level.block.Block;
import org.bukkit.Material;
import org.bukkit.craftbukkit.util.CraftMagicNumbers;
public class CraftBlockType {
public static Material minecraftToBukkit(Block block) {
return CraftMagicNumbers.getMaterial(block);
}
public static Block bukkitToMinecraft(Material material) {
return CraftMagicNumbers.getBlock(material);
}
}

View File

@@ -13,7 +13,7 @@ import org.bukkit.Tag;
import org.bukkit.World;
import org.bukkit.block.DecoratedPot;
import org.bukkit.craftbukkit.inventory.CraftInventoryDecoratedPot;
import org.bukkit.craftbukkit.util.CraftMagicNumbers;
import org.bukkit.craftbukkit.inventory.CraftItemType;
import org.bukkit.inventory.DecoratedPotInventory;
public class CraftDecoratedPot extends CraftBlockEntityState<DecoratedPotBlockEntity> implements DecoratedPot {
@@ -45,7 +45,7 @@ public class CraftDecoratedPot extends CraftBlockEntityState<DecoratedPotBlockEn
Preconditions.checkArgument(face != null, "face must not be null");
Preconditions.checkArgument(sherd == null || sherd == Material.BRICK || Tag.ITEMS_DECORATED_POT_SHERDS.isTagged(sherd), "sherd is not a valid sherd material: %s", sherd);
Item sherdItem = (sherd != null) ? CraftMagicNumbers.getItem(sherd) : Items.BRICK;
Item sherdItem = (sherd != null) ? CraftItemType.bukkitToMinecraft(sherd) : Items.BRICK;
DecoratedPotBlockEntity.Decoration decorations = getSnapshot().getDecorations();
switch (face) {
@@ -70,7 +70,7 @@ public class CraftDecoratedPot extends CraftBlockEntityState<DecoratedPotBlockEn
default -> throw new IllegalArgumentException("Unexpected value: " + face);
};
return CraftMagicNumbers.getMaterial(sherdItem);
return CraftItemType.minecraftToBukkit(sherdItem);
}
@Override
@@ -78,16 +78,16 @@ public class CraftDecoratedPot extends CraftBlockEntityState<DecoratedPotBlockEn
DecoratedPotBlockEntity.Decoration decorations = getSnapshot().getDecorations();
Map<Side, Material> sherds = new EnumMap<>(Side.class);
sherds.put(Side.BACK, CraftMagicNumbers.getMaterial(decorations.back()));
sherds.put(Side.LEFT, CraftMagicNumbers.getMaterial(decorations.left()));
sherds.put(Side.RIGHT, CraftMagicNumbers.getMaterial(decorations.right()));
sherds.put(Side.FRONT, CraftMagicNumbers.getMaterial(decorations.front()));
sherds.put(Side.BACK, CraftItemType.minecraftToBukkit(decorations.back()));
sherds.put(Side.LEFT, CraftItemType.minecraftToBukkit(decorations.left()));
sherds.put(Side.RIGHT, CraftItemType.minecraftToBukkit(decorations.right()));
sherds.put(Side.FRONT, CraftItemType.minecraftToBukkit(decorations.front()));
return sherds;
}
@Override
public List<Material> getShards() {
return getSnapshot().getDecorations().sorted().map(CraftMagicNumbers::getMaterial).collect(Collectors.toUnmodifiableList());
return getSnapshot().getDecorations().sorted().map(CraftItemType::minecraftToBukkit).collect(Collectors.toUnmodifiableList());
}
@Override

View File

@@ -11,7 +11,7 @@ import org.bukkit.block.Jukebox;
import org.bukkit.craftbukkit.CraftWorld;
import org.bukkit.craftbukkit.inventory.CraftInventoryJukebox;
import org.bukkit.craftbukkit.inventory.CraftItemStack;
import org.bukkit.craftbukkit.util.CraftMagicNumbers;
import org.bukkit.craftbukkit.inventory.CraftItemType;
import org.bukkit.inventory.JukeboxInventory;
public class CraftJukebox extends CraftBlockEntityState<TileEntityJukeBox> implements Jukebox {
@@ -68,7 +68,7 @@ public class CraftJukebox extends CraftBlockEntityState<TileEntityJukeBox> imple
@Override
public void setPlaying(Material record) {
if (record == null || CraftMagicNumbers.getItem(record) == null) {
if (record == null || CraftItemType.bukkitToMinecraft(record) == null) {
record = Material.AIR;
}
@@ -122,7 +122,7 @@ public class CraftJukebox extends CraftBlockEntityState<TileEntityJukeBox> imple
jukebox.isPlaying = true;
jukebox.recordStartedTick = jukebox.tickCount;
getWorld().playEffect(getLocation(), Effect.RECORD_PLAY, CraftMagicNumbers.getMaterial(record.getItem()));
getWorld().playEffect(getLocation(), Effect.RECORD_PLAY, CraftItemType.minecraftToBukkit(record.getItem()));
return true;
}

View File

@@ -9,7 +9,6 @@ import org.bukkit.DyeColor;
import org.bukkit.World;
import org.bukkit.block.ShulkerBox;
import org.bukkit.craftbukkit.inventory.CraftInventory;
import org.bukkit.craftbukkit.util.CraftMagicNumbers;
import org.bukkit.inventory.Inventory;
public class CraftShulkerBox extends CraftLootable<TileEntityShulkerBox> implements ShulkerBox {
@@ -38,7 +37,7 @@ public class CraftShulkerBox extends CraftLootable<TileEntityShulkerBox> impleme
@Override
public DyeColor getColor() {
EnumColor color = ((BlockShulkerBox) CraftMagicNumbers.getBlock(this.getType())).color;
EnumColor color = ((BlockShulkerBox) CraftBlockType.bukkitToMinecraft(this.getType())).color;
return (color == null) ? null : DyeColor.getByWoolData((byte) color.getId());
}

View File

@@ -41,9 +41,10 @@ import org.bukkit.craftbukkit.CraftWorld;
import org.bukkit.craftbukkit.block.CraftBlock;
import org.bukkit.craftbukkit.block.CraftBlockStates;
import org.bukkit.craftbukkit.block.CraftBlockSupport;
import org.bukkit.craftbukkit.block.CraftBlockType;
import org.bukkit.craftbukkit.inventory.CraftItemStack;
import org.bukkit.craftbukkit.inventory.CraftItemType;
import org.bukkit.craftbukkit.util.CraftLocation;
import org.bukkit.craftbukkit.util.CraftMagicNumbers;
import org.bukkit.inventory.ItemStack;
import org.jetbrains.annotations.NotNull;
@@ -62,7 +63,7 @@ public class CraftBlockData implements BlockData {
@Override
public Material getMaterial() {
return CraftMagicNumbers.getMaterial(state.getBlock());
return CraftBlockType.minecraftToBukkit(state.getBlock());
}
public IBlockData getState() {
@@ -542,7 +543,7 @@ public class CraftBlockData implements BlockData {
Preconditions.checkArgument(material == null || material.isBlock(), "Cannot get data for not block %s", material);
IBlockData blockData;
Block block = CraftMagicNumbers.getBlock(material);
Block block = CraftBlockType.bukkitToMinecraft(material);
Map<IBlockState<?>, Comparable<?>> parsed = null;
// Data provided, use it
@@ -646,7 +647,7 @@ public class CraftBlockData implements BlockData {
@Override
public Material getPlacementMaterial() {
return CraftMagicNumbers.getMaterial(state.getBlock().asItem());
return CraftItemType.minecraftToBukkit(state.getBlock().asItem());
}
@Override