2021-05-22 21:56:01 +02:00
|
|
|
package net.minestom.server.instance.block;
|
|
|
|
|
2021-07-10 18:42:02 +02:00
|
|
|
import com.github.benmanes.caffeine.cache.Cache;
|
|
|
|
import com.github.benmanes.caffeine.cache.Caffeine;
|
2021-06-23 17:41:46 +02:00
|
|
|
import net.minestom.server.registry.Registry;
|
|
|
|
import net.minestom.server.tag.Tag;
|
2021-11-06 14:07:42 +01:00
|
|
|
import net.minestom.server.utils.ObjectArray;
|
2021-07-30 17:16:52 +02:00
|
|
|
import net.minestom.server.utils.block.BlockUtils;
|
2021-06-23 17:41:46 +02:00
|
|
|
import org.jetbrains.annotations.NotNull;
|
|
|
|
import org.jetbrains.annotations.Nullable;
|
|
|
|
import org.jglrxavpok.hephaistos.nbt.NBTCompound;
|
2021-12-13 16:41:30 +01:00
|
|
|
import org.jglrxavpok.hephaistos.nbt.mutable.MutableNBTCompound;
|
2021-05-22 21:56:01 +02:00
|
|
|
|
2021-07-10 20:26:30 +02:00
|
|
|
import java.time.Duration;
|
2021-08-11 21:28:13 +02:00
|
|
|
import java.util.*;
|
2021-12-19 01:05:30 +01:00
|
|
|
import java.util.concurrent.atomic.AtomicReference;
|
2021-07-24 03:31:03 +02:00
|
|
|
import java.util.function.Function;
|
2021-05-22 21:56:01 +02:00
|
|
|
|
2021-07-19 23:59:40 +02:00
|
|
|
final class BlockImpl implements Block {
|
2021-07-30 17:16:52 +02:00
|
|
|
// Block state -> block object
|
2021-11-06 14:07:42 +01:00
|
|
|
private static final ObjectArray<Block> BLOCK_STATE_MAP = new ObjectArray<>();
|
2021-07-30 17:16:52 +02:00
|
|
|
private static final Registry.Container<Block> CONTAINER = new Registry.Container<>(Registry.Resource.BLOCKS,
|
|
|
|
(container, namespace, object) -> {
|
2021-12-19 00:26:20 +01:00
|
|
|
final var stateObject = (Map<String, Object>) object.get("states");
|
2021-07-30 17:34:39 +02:00
|
|
|
// Loop each state
|
2021-08-04 17:45:17 +02:00
|
|
|
var propertyEntry = new HashMap<Map<String, String>, Block>();
|
2021-12-19 01:05:30 +01:00
|
|
|
AtomicReference<Map<Map<String, String>, Block>> ref = new AtomicReference<>();
|
2021-07-30 17:34:39 +02:00
|
|
|
for (var stateEntry : stateObject.entrySet()) {
|
|
|
|
final String query = stateEntry.getKey();
|
2021-11-07 13:40:54 +01:00
|
|
|
final var stateOverride = (Map<String, Object>) stateEntry.getValue();
|
2021-07-30 17:34:39 +02:00
|
|
|
final var propertyMap = BlockUtils.parseProperties(query);
|
|
|
|
final Block block = new BlockImpl(Registry.block(namespace, object, stateOverride),
|
2021-12-19 01:05:30 +01:00
|
|
|
ref, propertyMap, null, null);
|
2021-11-06 14:07:42 +01:00
|
|
|
BLOCK_STATE_MAP.set(block.stateId(), block);
|
2021-07-30 17:34:39 +02:00
|
|
|
propertyEntry.put(propertyMap, block);
|
|
|
|
}
|
2021-12-19 01:05:30 +01:00
|
|
|
ref.setPlain(Map.copyOf(propertyEntry));
|
2021-07-30 17:34:39 +02:00
|
|
|
// Register default state
|
2021-11-07 13:40:54 +01:00
|
|
|
final int defaultState = ((Number) object.get("defaultStateId")).intValue();
|
2021-07-30 17:16:52 +02:00
|
|
|
container.register(getState(defaultState));
|
|
|
|
});
|
2021-07-30 17:34:39 +02:00
|
|
|
private static final Cache<NBTCompound, NBTCompound> NBT_CACHE = Caffeine.newBuilder()
|
|
|
|
.expireAfterWrite(Duration.ofMinutes(5))
|
|
|
|
.weakValues()
|
|
|
|
.build();
|
2021-07-30 17:16:52 +02:00
|
|
|
|
2021-10-17 14:29:27 +02:00
|
|
|
static {
|
|
|
|
BLOCK_STATE_MAP.trim();
|
|
|
|
}
|
|
|
|
|
2021-07-30 17:16:52 +02:00
|
|
|
static Block get(@NotNull String namespace) {
|
|
|
|
return CONTAINER.get(namespace);
|
|
|
|
}
|
|
|
|
|
|
|
|
static Block getSafe(@NotNull String namespace) {
|
|
|
|
return CONTAINER.getSafe(namespace);
|
|
|
|
}
|
|
|
|
|
|
|
|
static Block getId(int id) {
|
|
|
|
return CONTAINER.getId(id);
|
|
|
|
}
|
|
|
|
|
|
|
|
static Block getState(int stateId) {
|
|
|
|
return BLOCK_STATE_MAP.get(stateId);
|
|
|
|
}
|
|
|
|
|
|
|
|
static Collection<Block> values() {
|
|
|
|
return CONTAINER.values();
|
|
|
|
}
|
|
|
|
|
2021-06-23 17:41:46 +02:00
|
|
|
private final Registry.BlockEntry registry;
|
2021-12-19 01:05:30 +01:00
|
|
|
private final AtomicReference<Map<Map<String, String>, Block>> possibleProperties;
|
2021-06-23 17:41:46 +02:00
|
|
|
private final Map<String, String> properties;
|
|
|
|
private final NBTCompound nbt;
|
|
|
|
private final BlockHandler handler;
|
|
|
|
|
2021-08-16 20:07:57 +02:00
|
|
|
private int hashCode; // Cache
|
|
|
|
|
2021-06-23 17:41:46 +02:00
|
|
|
BlockImpl(@NotNull Registry.BlockEntry registry,
|
2021-12-19 01:05:30 +01:00
|
|
|
@NotNull AtomicReference<Map<Map<String, String>, Block>> possibleProperties,
|
2021-06-23 17:41:46 +02:00
|
|
|
@NotNull Map<String, String> properties,
|
|
|
|
@Nullable NBTCompound nbt,
|
|
|
|
@Nullable BlockHandler handler) {
|
|
|
|
this.registry = registry;
|
2021-12-19 01:05:30 +01:00
|
|
|
this.possibleProperties = possibleProperties;
|
2021-07-23 15:44:53 +02:00
|
|
|
this.properties = properties;
|
2021-06-23 17:41:46 +02:00
|
|
|
this.nbt = nbt;
|
|
|
|
this.handler = handler;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public @NotNull Block withProperty(@NotNull String property, @NotNull String value) {
|
|
|
|
var properties = new HashMap<>(this.properties);
|
2021-07-23 15:44:53 +02:00
|
|
|
properties.replace(property, value);
|
2021-07-23 16:14:42 +02:00
|
|
|
return compute(properties);
|
2021-07-19 23:59:40 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public @NotNull Block withProperties(@NotNull Map<@NotNull String, @NotNull String> properties) {
|
2021-09-20 18:09:16 +02:00
|
|
|
if (properties.isEmpty()) return this;
|
2021-07-19 23:59:40 +02:00
|
|
|
if (this.properties.size() == properties.size()) {
|
2021-07-23 16:14:42 +02:00
|
|
|
return compute(properties); // Map should be complete
|
2021-07-19 23:59:40 +02:00
|
|
|
}
|
2021-07-23 16:14:42 +02:00
|
|
|
var newProperties = new HashMap<>(this.properties);
|
2021-07-24 03:31:03 +02:00
|
|
|
newProperties.putAll(properties);
|
2021-07-23 16:14:42 +02:00
|
|
|
return compute(newProperties);
|
2021-06-23 17:41:46 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2021-06-26 20:23:56 +02:00
|
|
|
public @NotNull <T> Block withTag(@NotNull Tag<T> tag, @Nullable T value) {
|
2021-12-13 16:41:30 +01:00
|
|
|
var temporaryNbt = new MutableNBTCompound(Objects.requireNonNullElse(nbt, NBTCompound.EMPTY));
|
2021-07-24 03:31:03 +02:00
|
|
|
tag.write(temporaryNbt, value);
|
2021-12-13 16:41:30 +01:00
|
|
|
final var finalNbt = temporaryNbt.getSize() > 0 ? NBT_CACHE.get(temporaryNbt.toCompound(), Function.identity()) : null;
|
2021-12-19 01:05:30 +01:00
|
|
|
return new BlockImpl(registry, possibleProperties, properties, finalNbt, handler);
|
2021-06-23 17:41:46 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public @NotNull Block withHandler(@Nullable BlockHandler handler) {
|
2021-12-19 01:05:30 +01:00
|
|
|
return new BlockImpl(registry, possibleProperties, properties, nbt, handler);
|
2021-06-23 17:41:46 +02:00
|
|
|
}
|
|
|
|
|
2021-07-10 20:41:22 +02:00
|
|
|
@Override
|
|
|
|
public boolean hasNbt() {
|
2021-07-23 15:44:53 +02:00
|
|
|
return nbt != null;
|
2021-07-10 20:41:22 +02:00
|
|
|
}
|
|
|
|
|
2021-06-23 17:41:46 +02:00
|
|
|
@Override
|
|
|
|
public @Nullable BlockHandler handler() {
|
|
|
|
return handler;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public @NotNull Map<String, String> properties() {
|
2021-07-23 16:14:42 +02:00
|
|
|
return Collections.unmodifiableMap(properties);
|
2021-06-23 17:41:46 +02:00
|
|
|
}
|
|
|
|
|
2021-10-25 16:17:12 +02:00
|
|
|
@Override
|
|
|
|
public @NotNull Collection<@NotNull Block> possibleStates() {
|
2021-12-19 01:05:30 +01:00
|
|
|
return possibleProperties().values();
|
2021-10-25 16:17:12 +02:00
|
|
|
}
|
|
|
|
|
2021-06-23 17:41:46 +02:00
|
|
|
@Override
|
|
|
|
public @NotNull Registry.BlockEntry registry() {
|
|
|
|
return registry;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public <T> @Nullable T getTag(@NotNull Tag<T> tag) {
|
2021-07-23 15:44:53 +02:00
|
|
|
return nbt != null ? tag.read(nbt) : null;
|
|
|
|
}
|
|
|
|
|
2021-12-19 01:05:30 +01:00
|
|
|
private Map<Map<String, String>, Block> possibleProperties() {
|
|
|
|
return possibleProperties.getPlain();
|
|
|
|
}
|
|
|
|
|
2021-08-11 21:28:13 +02:00
|
|
|
@Override
|
|
|
|
public boolean equals(Object o) {
|
|
|
|
if (this == o) return true;
|
|
|
|
if (o == null || getClass() != o.getClass()) return false;
|
|
|
|
BlockImpl block = (BlockImpl) o;
|
2021-08-13 05:40:29 +02:00
|
|
|
return stateId() == block.stateId() &&
|
2021-08-11 21:28:13 +02:00
|
|
|
Objects.equals(nbt, block.nbt) &&
|
|
|
|
Objects.equals(handler, block.handler);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public int hashCode() {
|
2021-08-16 20:07:57 +02:00
|
|
|
int result = hashCode;
|
|
|
|
if (result == 0) {
|
|
|
|
result = Objects.hash(stateId(), nbt, handler);
|
|
|
|
this.hashCode = result;
|
|
|
|
}
|
|
|
|
return result;
|
2021-08-11 21:28:13 +02:00
|
|
|
}
|
|
|
|
|
2021-08-01 13:01:56 +02:00
|
|
|
@Override
|
|
|
|
public String toString() {
|
|
|
|
return name() + "{" +
|
|
|
|
"properties=" + properties +
|
|
|
|
", nbt=" + nbt +
|
|
|
|
", handler=" + handler +
|
|
|
|
'}';
|
|
|
|
}
|
|
|
|
|
2021-07-23 16:14:42 +02:00
|
|
|
private Block compute(Map<String, String> properties) {
|
2021-09-20 18:09:16 +02:00
|
|
|
if (this.properties.equals(properties)) return this;
|
2021-12-19 01:05:30 +01:00
|
|
|
Block block = possibleProperties().get(properties);
|
2021-07-23 16:14:42 +02:00
|
|
|
if (block == null)
|
2021-08-25 18:26:45 +02:00
|
|
|
throw new IllegalArgumentException("Invalid properties: " + properties + " for block " + this);
|
2021-07-23 16:14:42 +02:00
|
|
|
return nbt == null && handler == null ? block :
|
2021-12-19 01:05:30 +01:00
|
|
|
new BlockImpl(block.registry(), possibleProperties, block.properties(), nbt, handler);
|
2021-05-22 21:56:01 +02:00
|
|
|
}
|
2021-06-23 17:41:46 +02:00
|
|
|
}
|