mirror of
https://github.com/Minestom/Minestom.git
synced 2025-01-23 00:21:26 +01:00
Improve Block interface
This commit is contained in:
parent
e0d54f5958
commit
d937660cf8
@ -109,10 +109,17 @@ public class DynamicChunk extends Chunk {
|
||||
|
||||
@Override
|
||||
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);
|
||||
BlockHandler handler = handlerMap.get(index);
|
||||
NBTCompound nbt = nbtMap.get(index);
|
||||
Block block = Block.fromStateId(blockStateId);
|
||||
// TODO nbt/handler
|
||||
return block;
|
||||
if (block == null) {
|
||||
return Block.AIR;
|
||||
}
|
||||
return block
|
||||
.withHandler(handler)
|
||||
.withNbt(nbt);
|
||||
}
|
||||
|
||||
@NotNull
|
||||
|
@ -8,6 +8,7 @@ import net.minestom.server.utils.NamespaceID;
|
||||
import net.minestom.server.utils.math.IntRange;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
import org.jglrxavpok.hephaistos.nbt.NBTCompound;
|
||||
|
||||
import java.util.Map;
|
||||
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);
|
||||
|
||||
@NotNull Block withNbt(@Nullable NBTCompound compound);
|
||||
|
||||
@NotNull Block withHandler(@Nullable BlockHandler handler);
|
||||
|
||||
@Nullable BlockHandler getHandler();
|
||||
|
||||
@NotNull Block getDefaultBlock();
|
||||
|
||||
@NotNull NamespaceID getNamespaceId();
|
||||
@ -39,8 +46,6 @@ public interface Block extends Keyed, TagReadable, BlockConstants {
|
||||
|
||||
@NotNull BlockData getData();
|
||||
|
||||
@Nullable BlockHandler getHandler();
|
||||
|
||||
default boolean compare(@NotNull Block block, @NotNull Comparator comparator) {
|
||||
return comparator.test(this, block);
|
||||
}
|
||||
|
@ -31,14 +31,19 @@ class BlockImpl implements Block {
|
||||
loadBlockData();
|
||||
}
|
||||
|
||||
private final NamespaceID namespaceID;
|
||||
private final int blockId;
|
||||
private final short minStateId, stateId;
|
||||
private final List<BlockProperty<?>> properties;
|
||||
private NamespaceID namespaceID;
|
||||
private int blockId;
|
||||
private short minStateId, stateId;
|
||||
private List<BlockProperty<?>> properties;
|
||||
protected BlockImpl original = null;
|
||||
private LinkedHashMap<BlockProperty<?>, Object> propertiesMap;
|
||||
private BlockHandler handler;
|
||||
private NBTCompound compound;
|
||||
|
||||
private BlockImpl() {
|
||||
|
||||
}
|
||||
|
||||
private BlockImpl(NamespaceID namespaceID,
|
||||
int blockId,
|
||||
short minStateId, short stateId,
|
||||
@ -85,8 +90,9 @@ class BlockImpl implements Block {
|
||||
map.put(property, value);
|
||||
}
|
||||
|
||||
var block = new BlockImpl(namespaceID, blockId, minStateId, computeId(minStateId, properties, map), properties, map);
|
||||
block.original = original;
|
||||
var block = shallowClone();
|
||||
block.stateId = computeId(minStateId, properties, map);
|
||||
block.propertiesMap = map;
|
||||
return block;
|
||||
}
|
||||
|
||||
@ -113,9 +119,20 @@ class BlockImpl implements Block {
|
||||
if (compound.getKeys().isEmpty()) {
|
||||
compound = null;
|
||||
}
|
||||
return withNbt(compound);
|
||||
}
|
||||
|
||||
var block = new BlockImpl(namespaceID, blockId, minStateId, stateId, properties, propertiesMap, compound);
|
||||
block.original = original;
|
||||
@Override
|
||||
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;
|
||||
}
|
||||
|
||||
@ -153,7 +170,21 @@ class BlockImpl implements Block {
|
||||
|
||||
@Override
|
||||
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,
|
||||
|
Loading…
Reference in New Issue
Block a user