mirror of
https://github.com/Minestom/Minestom.git
synced 2024-09-24 20:42:39 +02:00
Make BlockImpl a record
This commit is contained in:
parent
9927665d91
commit
f3221ff412
@ -93,7 +93,9 @@ public sealed interface Block extends ProtocolObject, TagReadable, Blocks permit
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Contract(pure = true)
|
@Contract(pure = true)
|
||||||
boolean hasNbt();
|
default boolean hasNbt() {
|
||||||
|
return nbt() != null;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Returns the block handler.
|
* Returns the block handler.
|
||||||
|
@ -12,29 +12,35 @@ import org.jglrxavpok.hephaistos.nbt.NBTCompound;
|
|||||||
import org.jglrxavpok.hephaistos.nbt.mutable.MutableNBTCompound;
|
import org.jglrxavpok.hephaistos.nbt.mutable.MutableNBTCompound;
|
||||||
|
|
||||||
import java.time.Duration;
|
import java.time.Duration;
|
||||||
import java.util.*;
|
import java.util.Collection;
|
||||||
import java.util.concurrent.atomic.AtomicReference;
|
import java.util.HashMap;
|
||||||
|
import java.util.Map;
|
||||||
|
import java.util.Objects;
|
||||||
import java.util.function.Function;
|
import java.util.function.Function;
|
||||||
|
|
||||||
final class BlockImpl implements Block {
|
record BlockImpl(@NotNull Registry.BlockEntry registry,
|
||||||
|
@NotNull Map<String, String> properties,
|
||||||
|
@Nullable NBTCompound nbt,
|
||||||
|
@Nullable BlockHandler handler) implements Block {
|
||||||
// Block state -> block object
|
// Block state -> block object
|
||||||
private static final ObjectArray<Block> BLOCK_STATE_MAP = new ObjectArray<>();
|
private static final ObjectArray<Block> BLOCK_STATE_MAP = new ObjectArray<>();
|
||||||
|
// Block id -> Map<Properties, Block>
|
||||||
|
private static final ObjectArray<Map<Map<String, String>, Block>> POSSIBLE_STATES = new ObjectArray<>();
|
||||||
private static final Registry.Container<Block> CONTAINER = new Registry.Container<>(Registry.Resource.BLOCKS,
|
private static final Registry.Container<Block> CONTAINER = new Registry.Container<>(Registry.Resource.BLOCKS,
|
||||||
(container, namespace, object) -> {
|
(container, namespace, object) -> {
|
||||||
final var stateObject = (Map<String, Object>) object.get("states");
|
final var stateObject = (Map<String, Object>) object.get("states");
|
||||||
// Loop each state
|
// Loop each state
|
||||||
var propertyEntry = new HashMap<Map<String, String>, Block>();
|
var propertyEntry = new HashMap<Map<String, String>, Block>();
|
||||||
AtomicReference<Map<Map<String, String>, Block>> ref = new AtomicReference<>();
|
|
||||||
for (var stateEntry : stateObject.entrySet()) {
|
for (var stateEntry : stateObject.entrySet()) {
|
||||||
final String query = stateEntry.getKey();
|
final String query = stateEntry.getKey();
|
||||||
final var stateOverride = (Map<String, Object>) stateEntry.getValue();
|
final var stateOverride = (Map<String, Object>) stateEntry.getValue();
|
||||||
final var propertyMap = BlockUtils.parseProperties(query);
|
final var propertyMap = BlockUtils.parseProperties(query);
|
||||||
final Block block = new BlockImpl(Registry.block(namespace, object, stateOverride),
|
final Block block = new BlockImpl(Registry.block(namespace, object, stateOverride),
|
||||||
ref, propertyMap, null, null);
|
propertyMap, null, null);
|
||||||
BLOCK_STATE_MAP.set(block.stateId(), block);
|
BLOCK_STATE_MAP.set(block.stateId(), block);
|
||||||
propertyEntry.put(propertyMap, block);
|
propertyEntry.put(propertyMap, block);
|
||||||
}
|
}
|
||||||
ref.setPlain(Map.copyOf(propertyEntry));
|
POSSIBLE_STATES.set(((Number) object.get("id")).intValue(), Map.copyOf(propertyEntry));
|
||||||
// Register default state
|
// Register default state
|
||||||
final int defaultState = ((Number) object.get("defaultStateId")).intValue();
|
final int defaultState = ((Number) object.get("defaultStateId")).intValue();
|
||||||
container.register(getState(defaultState));
|
container.register(getState(defaultState));
|
||||||
@ -46,6 +52,7 @@ final class BlockImpl implements Block {
|
|||||||
|
|
||||||
static {
|
static {
|
||||||
BLOCK_STATE_MAP.trim();
|
BLOCK_STATE_MAP.trim();
|
||||||
|
POSSIBLE_STATES.trim();
|
||||||
}
|
}
|
||||||
|
|
||||||
static Block get(@NotNull String namespace) {
|
static Block get(@NotNull String namespace) {
|
||||||
@ -68,30 +75,16 @@ final class BlockImpl implements Block {
|
|||||||
return CONTAINER.values();
|
return CONTAINER.values();
|
||||||
}
|
}
|
||||||
|
|
||||||
private final Registry.BlockEntry registry;
|
public BlockImpl {
|
||||||
private final AtomicReference<Map<Map<String, String>, Block>> possibleProperties;
|
properties = Map.copyOf(properties);
|
||||||
private final Map<String, String> properties;
|
|
||||||
private final NBTCompound nbt;
|
|
||||||
private final BlockHandler handler;
|
|
||||||
|
|
||||||
private int hashCode; // Cache
|
|
||||||
|
|
||||||
BlockImpl(@NotNull Registry.BlockEntry registry,
|
|
||||||
@NotNull AtomicReference<Map<Map<String, String>, Block>> possibleProperties,
|
|
||||||
@NotNull Map<String, String> properties,
|
|
||||||
@Nullable NBTCompound nbt,
|
|
||||||
@Nullable BlockHandler handler) {
|
|
||||||
this.registry = registry;
|
|
||||||
this.possibleProperties = possibleProperties;
|
|
||||||
this.properties = properties;
|
|
||||||
this.nbt = nbt;
|
|
||||||
this.handler = handler;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public @NotNull Block withProperty(@NotNull String property, @NotNull String value) {
|
public @NotNull Block withProperty(@NotNull String property, @NotNull String value) {
|
||||||
var properties = new HashMap<>(this.properties);
|
var properties = new HashMap<>(this.properties);
|
||||||
properties.replace(property, value);
|
final String oldProperty = properties.replace(property, value);
|
||||||
|
if (oldProperty == null)
|
||||||
|
throw new IllegalArgumentException("Property " + property + " does not exist");
|
||||||
return compute(properties);
|
return compute(properties);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -111,27 +104,12 @@ final class BlockImpl implements Block {
|
|||||||
var temporaryNbt = new MutableNBTCompound(Objects.requireNonNullElse(nbt, NBTCompound.EMPTY));
|
var temporaryNbt = new MutableNBTCompound(Objects.requireNonNullElse(nbt, NBTCompound.EMPTY));
|
||||||
tag.write(temporaryNbt, value);
|
tag.write(temporaryNbt, value);
|
||||||
final var finalNbt = temporaryNbt.getSize() > 0 ? NBT_CACHE.get(temporaryNbt.toCompound(), Function.identity()) : null;
|
final var finalNbt = temporaryNbt.getSize() > 0 ? NBT_CACHE.get(temporaryNbt.toCompound(), Function.identity()) : null;
|
||||||
return new BlockImpl(registry, possibleProperties, properties, finalNbt, handler);
|
return new BlockImpl(registry, properties, finalNbt, handler);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public @NotNull Block withHandler(@Nullable BlockHandler handler) {
|
public @NotNull Block withHandler(@Nullable BlockHandler handler) {
|
||||||
return new BlockImpl(registry, possibleProperties, properties, nbt, handler);
|
return new BlockImpl(registry, properties, nbt, handler);
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public boolean hasNbt() {
|
|
||||||
return nbt != null;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public @Nullable BlockHandler handler() {
|
|
||||||
return handler;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public @NotNull Map<String, String> properties() {
|
|
||||||
return Collections.unmodifiableMap(properties);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@ -139,47 +117,18 @@ final class BlockImpl implements Block {
|
|||||||
return possibleProperties().values();
|
return possibleProperties().values();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
|
||||||
public @NotNull Registry.BlockEntry registry() {
|
|
||||||
return registry;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public <T> @Nullable T getTag(@NotNull Tag<T> tag) {
|
public <T> @Nullable T getTag(@NotNull Tag<T> tag) {
|
||||||
return nbt != null ? tag.read(nbt) : null;
|
return nbt != null ? tag.read(nbt) : null;
|
||||||
}
|
}
|
||||||
|
|
||||||
private Map<Map<String, String>, Block> possibleProperties() {
|
private Map<Map<String, String>, Block> possibleProperties() {
|
||||||
return possibleProperties.getPlain();
|
return POSSIBLE_STATES.get(id());
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public boolean equals(Object o) {
|
|
||||||
if (this == o) return true;
|
|
||||||
if (o == null || getClass() != o.getClass()) return false;
|
|
||||||
BlockImpl block = (BlockImpl) o;
|
|
||||||
return stateId() == block.stateId() &&
|
|
||||||
Objects.equals(nbt, block.nbt) &&
|
|
||||||
Objects.equals(handler, block.handler);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public int hashCode() {
|
|
||||||
int result = hashCode;
|
|
||||||
if (result == 0) {
|
|
||||||
result = Objects.hash(stateId(), nbt, handler);
|
|
||||||
this.hashCode = result;
|
|
||||||
}
|
|
||||||
return result;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public String toString() {
|
public String toString() {
|
||||||
return name() + "{" +
|
return String.format("%s{properties=%s, nbt=%s, handler=%s}", name(), properties, nbt, handler);
|
||||||
"properties=" + properties +
|
|
||||||
", nbt=" + nbt +
|
|
||||||
", handler=" + handler +
|
|
||||||
'}';
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private Block compute(Map<String, String> properties) {
|
private Block compute(Map<String, String> properties) {
|
||||||
@ -187,7 +136,6 @@ final class BlockImpl implements Block {
|
|||||||
Block block = possibleProperties().get(properties);
|
Block block = possibleProperties().get(properties);
|
||||||
if (block == null)
|
if (block == null)
|
||||||
throw new IllegalArgumentException("Invalid properties: " + properties + " for block " + this);
|
throw new IllegalArgumentException("Invalid properties: " + properties + " for block " + this);
|
||||||
return nbt == null && handler == null ? block :
|
return nbt == null && handler == null ? block : new BlockImpl(block.registry(), block.properties(), nbt, handler);
|
||||||
new BlockImpl(block.registry(), possibleProperties, block.properties(), nbt, handler);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -50,7 +50,14 @@ public class Tag<T> {
|
|||||||
* Writing will override all tags. Proceed with caution.
|
* Writing will override all tags. Proceed with caution.
|
||||||
*/
|
*/
|
||||||
@ApiStatus.Experimental
|
@ApiStatus.Experimental
|
||||||
public static final Tag<NBTCompound> NBT = new Tag<>(null, NBTCompoundLike::toCompound, MutableNBTCompound::copyFrom);
|
public static final Tag<NBTCompound> NBT = new Tag<>(null, NBTCompoundLike::toCompound,
|
||||||
|
(original, updated) -> {
|
||||||
|
if (updated == null) {
|
||||||
|
original.clear();
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
original.copyFrom(updated);
|
||||||
|
});
|
||||||
|
|
||||||
private final String key;
|
private final String key;
|
||||||
private final Function<NBTCompoundLike, T> readFunction;
|
private final Function<NBTCompoundLike, T> readFunction;
|
||||||
|
@ -1,19 +1,51 @@
|
|||||||
package instance;
|
package instance;
|
||||||
|
|
||||||
import net.minestom.server.instance.block.Block;
|
import net.minestom.server.instance.block.Block;
|
||||||
|
import org.jglrxavpok.hephaistos.nbt.NBT;
|
||||||
import org.jglrxavpok.hephaistos.nbt.NBTCompound;
|
import org.jglrxavpok.hephaistos.nbt.NBTCompound;
|
||||||
import org.jglrxavpok.hephaistos.nbt.NBTInt;
|
|
||||||
import org.junit.jupiter.api.Test;
|
import org.junit.jupiter.api.Test;
|
||||||
|
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
|
import java.util.Objects;
|
||||||
|
|
||||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
import static org.junit.jupiter.api.Assertions.*;
|
||||||
|
|
||||||
public class BlockTest {
|
public class BlockTest {
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testNBT() {
|
||||||
|
Block block = Block.CHEST;
|
||||||
|
assertFalse(block.hasNbt());
|
||||||
|
assertNull(block.nbt());
|
||||||
|
|
||||||
|
var nbt = new NBTCompound(Map.of("key", NBT.Int(5)));
|
||||||
|
block = block.withNbt(nbt);
|
||||||
|
assertTrue(block.hasNbt());
|
||||||
|
assertEquals(block.nbt(), nbt);
|
||||||
|
|
||||||
|
block = block.withNbt(null);
|
||||||
|
assertFalse(block.hasNbt());
|
||||||
|
assertNull(block.nbt());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testProperty() {
|
||||||
|
Block block = Block.CHEST;
|
||||||
|
assertEquals(block.properties(), Objects.requireNonNull(Block.fromBlockId(block.id())).properties());
|
||||||
|
|
||||||
|
for (var possible : block.possibleStates()) {
|
||||||
|
assertEquals(possible, block.withProperties(possible.properties()));
|
||||||
|
}
|
||||||
|
|
||||||
|
assertEquals(block.withProperty("facing", "north").getProperty("facing"), "north");
|
||||||
|
assertNotEquals(block.withProperty("facing", "north"), block.withProperty("facing", "south"));
|
||||||
|
|
||||||
|
assertThrows(Exception.class, () -> block.withProperty("random", "randomKey"));
|
||||||
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testEquality() {
|
public void testEquality() {
|
||||||
var nbt = new NBTCompound(Map.of("key", new NBTInt(5)));
|
var nbt = new NBTCompound(Map.of("key", NBT.Int(5)));
|
||||||
Block b1 = Block.CHEST;
|
Block b1 = Block.CHEST;
|
||||||
Block b2 = Block.CHEST;
|
Block b2 = Block.CHEST;
|
||||||
assertEquals(b1.withNbt(nbt), b2.withNbt(nbt));
|
assertEquals(b1.withNbt(nbt), b2.withNbt(nbt));
|
||||||
@ -21,4 +53,11 @@ public class BlockTest {
|
|||||||
assertEquals(b1.withProperty("facing", "north").getProperty("facing"), "north");
|
assertEquals(b1.withProperty("facing", "north").getProperty("facing"), "north");
|
||||||
assertEquals(b1.withProperty("facing", "north"), b2.withProperty("facing", "north"));
|
assertEquals(b1.withProperty("facing", "north"), b2.withProperty("facing", "north"));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testMutability() {
|
||||||
|
Block block = Block.CHEST;
|
||||||
|
assertThrows(Exception.class, () -> block.properties().put("facing", "north"));
|
||||||
|
assertThrows(Exception.class, () -> block.withProperty("facing", "north").properties().put("facing", "south"));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user