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 = Block.fromStateId(blockStateId);
final Block block = chunk.getBlock(blockPos.getX(), blockPos.getY(), blockPos.getZ());
// TODO: block collision boxes
// TODO: for the moment, always consider a full block

View File

@ -1,28 +1,18 @@
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.event.CancellableEvent;
import net.minestom.server.event.PlayerEvent;
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.validate.Check;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
/**
* Called when a player tries placing a block.
*/
public class PlayerBlockPlaceEvent extends PlayerEvent implements CancellableEvent {
private static final BlockManager BLOCK_MANAGER = MinecraftServer.getBlockManager();
private short blockStateId;
private short customBlockId;
private Data blockData;
private Block block;
private final BlockPosition blockPosition;
private final Player.Hand hand;
@ -33,100 +23,28 @@ public class PlayerBlockPlaceEvent extends PlayerEvent implements CancellableEve
public PlayerBlockPlaceEvent(@NotNull Player player, @NotNull Block block,
@NotNull BlockPosition blockPosition, @NotNull Player.Hand hand) {
super(player);
this.blockStateId = block.getBlockId();
this.block = block;
this.blockPosition = blockPosition;
this.hand = hand;
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) {
setBlockStateId(customBlock.getDefaultBlockStateId());
setCustomBlockId(customBlock.getCustomBlockId());
public @NotNull Block getBlock() {
return block;
}
/**
* 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) {
final CustomBlock customBlock = BLOCK_MANAGER.getCustomBlock(customBlockId);
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;
public void setBlock(@NotNull Block block) {
this.block = block;
}
/**

View File

@ -1,8 +1,7 @@
package net.minestom.server.instance.batch;
import net.minestom.server.data.Data;
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 org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
@ -21,8 +20,8 @@ public class ChunkGenerationBatch extends ChunkBatch {
}
@Override
public void setSeparateBlocks(int x, int y, int z, short blockStateId, short customBlockId, @Nullable Data data) {
chunk.UNSAFE_setBlock(x, y, z, blockStateId, customBlockId, data, CustomBlockUtils.hasUpdate(customBlockId));
public void setBlock(int x, int y, int z, @NotNull Block block) {
chunk.UNSAFE_setBlock(x, y, z, block);
}
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();
@Nullable BlockHandler getHandler();
default boolean compare(@NotNull Block block, @NotNull Comparator comparator) {
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);
}
@Override
public @Nullable BlockHandler getHandler() {
return null;
}
protected static BlockImpl create(NamespaceID namespaceID, short blockId, short minStateId, short maxStateId,
short defaultStateId, List<BlockProperty<?>> properties) {
var block = new BlockImpl(namespaceID, blockId, minStateId, defaultStateId, properties, computeMap(defaultStateId, properties));
block.original = block;
Block.REGISTRY.register(namespaceID, block,
Block.register(namespaceID, block,
new IntRange((int) minStateId, (int) maxStateId), requestedStateId -> {
var requestedBlock = new BlockImpl(namespaceID, blockId, minStateId, requestedStateId, properties, computeMap(requestedStateId, properties));
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) {
final short blockStateId = instance.getBlockStateId(x, y, z);
return Block.fromStateId(blockStateId).getData().isSolid();
return instance.getBlock(x, y, z).isSolid();
}
}

View File

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

View File

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

View File

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