Improve Block interface

This commit is contained in:
TheMode 2021-05-29 00:34:01 +02:00
parent e0d54f5958
commit d937660cf8
3 changed files with 56 additions and 13 deletions

View File

@ -109,10 +109,17 @@ public class DynamicChunk extends Chunk {
@Override @Override
public @NotNull Block getBlock(int x, int y, int z) { public @NotNull Block getBlock(int x, int y, int z) {
final int index = ChunkUtils.getBlockIndex(x, y, z);
final short blockStateId = getBlockAt(blockPalette, x, y, z); final short blockStateId = getBlockAt(blockPalette, x, y, z);
BlockHandler handler = handlerMap.get(index);
NBTCompound nbt = nbtMap.get(index);
Block block = Block.fromStateId(blockStateId); Block block = Block.fromStateId(blockStateId);
// TODO nbt/handler if (block == null) {
return block; return Block.AIR;
}
return block
.withHandler(handler)
.withNbt(nbt);
} }
@NotNull @NotNull

View File

@ -8,6 +8,7 @@ import net.minestom.server.utils.NamespaceID;
import net.minestom.server.utils.math.IntRange; import net.minestom.server.utils.math.IntRange;
import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable; import org.jetbrains.annotations.Nullable;
import org.jglrxavpok.hephaistos.nbt.NBTCompound;
import java.util.Map; import java.util.Map;
import java.util.function.BiPredicate; import java.util.function.BiPredicate;
@ -18,6 +19,12 @@ public interface Block extends Keyed, TagReadable, BlockConstants {
<T> @NotNull Block withTag(@NotNull Tag<T> tag, @Nullable T value); <T> @NotNull Block withTag(@NotNull Tag<T> tag, @Nullable T value);
@NotNull Block withNbt(@Nullable NBTCompound compound);
@NotNull Block withHandler(@Nullable BlockHandler handler);
@Nullable BlockHandler getHandler();
@NotNull Block getDefaultBlock(); @NotNull Block getDefaultBlock();
@NotNull NamespaceID getNamespaceId(); @NotNull NamespaceID getNamespaceId();
@ -39,8 +46,6 @@ 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.test(this, block); return comparator.test(this, block);
} }

View File

@ -31,14 +31,19 @@ class BlockImpl implements Block {
loadBlockData(); loadBlockData();
} }
private final NamespaceID namespaceID; private NamespaceID namespaceID;
private final int blockId; private int blockId;
private final short minStateId, stateId; private short minStateId, stateId;
private final List<BlockProperty<?>> properties; private List<BlockProperty<?>> properties;
protected BlockImpl original = null; protected BlockImpl original = null;
private LinkedHashMap<BlockProperty<?>, Object> propertiesMap; private LinkedHashMap<BlockProperty<?>, Object> propertiesMap;
private BlockHandler handler;
private NBTCompound compound; private NBTCompound compound;
private BlockImpl() {
}
private BlockImpl(NamespaceID namespaceID, private BlockImpl(NamespaceID namespaceID,
int blockId, int blockId,
short minStateId, short stateId, short minStateId, short stateId,
@ -85,8 +90,9 @@ class BlockImpl implements Block {
map.put(property, value); map.put(property, value);
} }
var block = new BlockImpl(namespaceID, blockId, minStateId, computeId(minStateId, properties, map), properties, map); var block = shallowClone();
block.original = original; block.stateId = computeId(minStateId, properties, map);
block.propertiesMap = map;
return block; return block;
} }
@ -113,9 +119,20 @@ class BlockImpl implements Block {
if (compound.getKeys().isEmpty()) { if (compound.getKeys().isEmpty()) {
compound = null; compound = null;
} }
return withNbt(compound);
}
var block = new BlockImpl(namespaceID, blockId, minStateId, stateId, properties, propertiesMap, compound); @Override
block.original = original; public @NotNull Block withNbt(@Nullable NBTCompound compound) {
var block = shallowClone();
block.compound = compound;
return block;
}
@Override
public @NotNull Block withHandler(@Nullable BlockHandler handler) {
var block = shallowClone();
block.handler = handler;
return block; return block;
} }
@ -153,7 +170,21 @@ class BlockImpl implements Block {
@Override @Override
public @Nullable BlockHandler getHandler() { public @Nullable BlockHandler getHandler() {
return null; return handler;
}
private @NotNull BlockImpl shallowClone() {
var block = new BlockImpl();
block.namespaceID = namespaceID;
block.blockId = blockId;
block.minStateId = minStateId;
block.stateId = stateId;
block.properties = properties;
block.original = original;
block.propertiesMap = propertiesMap;
block.handler = handler;
block.compound = compound;
return block;
} }
protected static BlockImpl create(NamespaceID namespaceID, short blockId, short minStateId, short maxStateId, protected static BlockImpl create(NamespaceID namespaceID, short blockId, short minStateId, short maxStateId,