Fix more errors

This commit is contained in:
TheMode 2021-05-24 21:39:30 +02:00
parent 7d47f46261
commit 86f7fa7e27
10 changed files with 31 additions and 106 deletions

View File

@ -161,8 +161,7 @@ public class CollisionUtils {
} }
} }
final short blockStateId = chunk.getBlockStateId(blockPos.getX(), blockPos.getY(), blockPos.getZ()); final Block block = chunk.getBlock(blockPos.getX(), blockPos.getY(), blockPos.getZ());
final Block block = Block.fromStateId(blockStateId);
// TODO: block collision boxes // TODO: block collision boxes
// TODO: for the moment, always consider a full block // TODO: for the moment, always consider a full block

View File

@ -1,28 +1,18 @@
package net.minestom.server.event.player; package net.minestom.server.event.player;
import net.minestom.server.MinecraftServer;
import net.minestom.server.data.Data;
import net.minestom.server.entity.Player; import net.minestom.server.entity.Player;
import net.minestom.server.event.CancellableEvent; import net.minestom.server.event.CancellableEvent;
import net.minestom.server.event.PlayerEvent; import net.minestom.server.event.PlayerEvent;
import net.minestom.server.instance.block.Block; import net.minestom.server.instance.block.Block;
import net.minestom.server.instance.block.BlockManager;
import net.minestom.server.instance.block.CustomBlock;
import net.minestom.server.utils.BlockPosition; import net.minestom.server.utils.BlockPosition;
import net.minestom.server.utils.validate.Check;
import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
/** /**
* Called when a player tries placing a block. * Called when a player tries placing a block.
*/ */
public class PlayerBlockPlaceEvent extends PlayerEvent implements CancellableEvent { public class PlayerBlockPlaceEvent extends PlayerEvent implements CancellableEvent {
private static final BlockManager BLOCK_MANAGER = MinecraftServer.getBlockManager(); private Block block;
private short blockStateId;
private short customBlockId;
private Data blockData;
private final BlockPosition blockPosition; private final BlockPosition blockPosition;
private final Player.Hand hand; private final Player.Hand hand;
@ -33,100 +23,28 @@ public class PlayerBlockPlaceEvent extends PlayerEvent implements CancellableEve
public PlayerBlockPlaceEvent(@NotNull Player player, @NotNull Block block, public PlayerBlockPlaceEvent(@NotNull Player player, @NotNull Block block,
@NotNull BlockPosition blockPosition, @NotNull Player.Hand hand) { @NotNull BlockPosition blockPosition, @NotNull Player.Hand hand) {
super(player); super(player);
this.blockStateId = block.getBlockId(); this.block = block;
this.blockPosition = blockPosition; this.blockPosition = blockPosition;
this.hand = hand; this.hand = hand;
this.consumeBlock = true; this.consumeBlock = true;
} }
/** /**
* Sets both the blockId and customBlockId. * Gets the block which will be placed.
* *
* @param customBlock the custom block to place * @return the block to place
*/ */
public void setCustomBlock(@NotNull CustomBlock customBlock) { public @NotNull Block getBlock() {
setBlockStateId(customBlock.getDefaultBlockStateId()); return block;
setCustomBlockId(customBlock.getCustomBlockId());
} }
/** /**
* Sets both the blockStateId and customBlockId. * Changes the block to be placed.
* *
* @param customBlockId the custom block id to place * @param block the new block
*/ */
public void setCustomBlock(short customBlockId) { public void setBlock(@NotNull Block block) {
final CustomBlock customBlock = BLOCK_MANAGER.getCustomBlock(customBlockId); this.block = block;
Check.notNull(customBlock, "The custom block with the id '" + customBlockId + "' does not exist");
setCustomBlock(customBlock);
}
/**
* Sets both the blockId and customBlockId.
*
* @param customBlockId the custom block id to place
*/
public void setCustomBlock(@NotNull String customBlockId) {
final CustomBlock customBlock = BLOCK_MANAGER.getCustomBlock(customBlockId);
Check.notNull(customBlock, "The custom block with the identifier '" + customBlockId + "' does not exist");
setCustomBlock(customBlock);
}
/**
* Gets the custom block id.
*
* @return the custom block id
*/
public short getCustomBlockId() {
return customBlockId;
}
/**
* Sets the custom block id to place.
* <p>
* WARNING: this does not change the visual block id, see {@link #setBlockStateId(short)}
* or {@link #setCustomBlock(short)}.
*
* @param customBlockId the custom block id
*/
public void setCustomBlockId(short customBlockId) {
this.customBlockId = customBlockId;
}
/**
* Gets the block state id.
*
* @return the block state id
*/
public short getBlockStateId() {
return blockStateId;
}
/**
* Changes the visual block id.
*
* @param blockStateId the new block state id
*/
public void setBlockStateId(short blockStateId) {
this.blockStateId = blockStateId;
}
/**
* Gets the data that the (not placed yet) block should have
*
* @return the block data, null if not any
*/
@Nullable
public Data getBlockData() {
return blockData;
}
/**
* Sets the data of the block to place.
*
* @param blockData the block data, null if not any
*/
public void setBlockData(@Nullable Data blockData) {
this.blockData = blockData;
} }
/** /**

View File

@ -1,8 +1,7 @@
package net.minestom.server.instance.batch; package net.minestom.server.instance.batch;
import net.minestom.server.data.Data;
import net.minestom.server.instance.*; import net.minestom.server.instance.*;
import net.minestom.server.utils.block.CustomBlockUtils; import net.minestom.server.instance.block.Block;
import net.minestom.server.utils.chunk.ChunkCallback; import net.minestom.server.utils.chunk.ChunkCallback;
import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable; import org.jetbrains.annotations.Nullable;
@ -21,8 +20,8 @@ public class ChunkGenerationBatch extends ChunkBatch {
} }
@Override @Override
public void setSeparateBlocks(int x, int y, int z, short blockStateId, short customBlockId, @Nullable Data data) { public void setBlock(int x, int y, int z, @NotNull Block block) {
chunk.UNSAFE_setBlock(x, y, z, blockStateId, customBlockId, data, CustomBlockUtils.hasUpdate(customBlockId)); chunk.UNSAFE_setBlock(x, y, z, block);
} }
public void generate(@NotNull ChunkGenerator chunkGenerator, @Nullable ChunkCallback callback) { public void generate(@NotNull ChunkGenerator chunkGenerator, @Nullable ChunkCallback callback) {

View File

@ -38,6 +38,8 @@ public interface Block extends Keyed, TagReadable, BlockConstants {
@NotNull BlockData getData(); @NotNull BlockData getData();
@Nullable BlockHandler getHandler();
default boolean compare(@NotNull Block block, @NotNull Comparator comparator) { default boolean compare(@NotNull Block block, @NotNull Comparator comparator) {
return comparator.equals(this, block); return comparator.equals(this, block);
} }

View File

@ -0,0 +1,4 @@
package net.minestom.server.instance.block;
public interface BlockHandler {
}

View File

@ -151,11 +151,16 @@ class BlockImpl implements Block {
return blockData.get(stateId); return blockData.get(stateId);
} }
@Override
public @Nullable BlockHandler getHandler() {
return null;
}
protected static BlockImpl create(NamespaceID namespaceID, short blockId, short minStateId, short maxStateId, protected static BlockImpl create(NamespaceID namespaceID, short blockId, short minStateId, short maxStateId,
short defaultStateId, List<BlockProperty<?>> properties) { short defaultStateId, List<BlockProperty<?>> properties) {
var block = new BlockImpl(namespaceID, blockId, minStateId, defaultStateId, properties, computeMap(defaultStateId, properties)); var block = new BlockImpl(namespaceID, blockId, minStateId, defaultStateId, properties, computeMap(defaultStateId, properties));
block.original = block; block.original = block;
Block.REGISTRY.register(namespaceID, block, Block.register(namespaceID, block,
new IntRange((int) minStateId, (int) maxStateId), requestedStateId -> { new IntRange((int) minStateId, (int) maxStateId), requestedStateId -> {
var requestedBlock = new BlockImpl(namespaceID, blockId, minStateId, requestedStateId, properties, computeMap(requestedStateId, properties)); var requestedBlock = new BlockImpl(namespaceID, blockId, minStateId, requestedStateId, properties, computeMap(requestedStateId, properties));
requestedBlock.original = block; requestedBlock.original = block;

View File

@ -60,7 +60,6 @@ public class WallPlacementRule extends BlockPlacementRule {
} }
private boolean isBlock(Instance instance, int x, int y, int z) { private boolean isBlock(Instance instance, int x, int y, int z) {
final short blockStateId = instance.getBlockStateId(x, y, z); return instance.getBlock(x, y, z).isSolid();
return Block.fromStateId(blockStateId).getData().isSolid();
} }
} }

View File

@ -227,7 +227,7 @@ public final class NBTUtils {
NBTList<NBTString> canPlaceOn = nbt.getList("CanPlaceOn"); NBTList<NBTString> canPlaceOn = nbt.getList("CanPlaceOn");
Set<Block> blocks = new HashSet<>(); Set<Block> blocks = new HashSet<>();
for (NBTString blockNamespace : canPlaceOn) { for (NBTString blockNamespace : canPlaceOn) {
Block block = Registries.getBlock(blockNamespace.getValue()); Block block = Block.fromNamespaceId(blockNamespace.getValue());
blocks.add(block); blocks.add(block);
} }
metaBuilder.canPlaceOn(blocks); metaBuilder.canPlaceOn(blocks);
@ -239,7 +239,7 @@ public final class NBTUtils {
NBTList<NBTString> canDestroy = nbt.getList("CanDestroy"); NBTList<NBTString> canDestroy = nbt.getList("CanDestroy");
Set<Block> blocks = new HashSet<>(); Set<Block> blocks = new HashSet<>();
for (NBTString blockNamespace : canDestroy) { for (NBTString blockNamespace : canDestroy) {
Block block = Registries.getBlock(blockNamespace.getValue()); Block block = Block.fromNamespaceId(blockNamespace.getValue());
blocks.add(block); blocks.add(block);
} }
metaBuilder.canDestroy(blocks); metaBuilder.canDestroy(blocks);

View File

@ -44,7 +44,7 @@ public class BlockUtils {
} }
public Block getBlock() { public Block getBlock() {
return Block.fromStateId(instance.getBlockStateId(position)); return instance.getBlock(position);
} }
public boolean equals(Block block) { public boolean equals(Block block) {

View File

@ -62,10 +62,9 @@ public final class EntityUtils {
// TODO: check entire bounding box // TODO: check entire bounding box
final BlockPosition blockPosition = entityPosition.toBlockPosition().subtract(0, 1, 0); final BlockPosition blockPosition = entityPosition.toBlockPosition().subtract(0, 1, 0);
try { try {
final short blockStateId = chunk.getBlockStateId(blockPosition.getX(), final Block block = chunk.getBlock(blockPosition.getX(),
blockPosition.getY(), blockPosition.getY(),
blockPosition.getZ()); blockPosition.getZ());
final Block block = Block.fromStateId(blockStateId);
return block.isSolid(); return block.isSolid();
} catch (NullPointerException e) { } catch (NullPointerException e) {
// Probably an entity at the border of an unloaded chunk // Probably an entity at the border of an unloaded chunk