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-07-30 17:16:52 +02:00
|
|
|
import com.google.gson.JsonObject;
|
|
|
|
import it.unimi.dsi.fastutil.ints.Int2ObjectMap;
|
|
|
|
import it.unimi.dsi.fastutil.ints.Int2ObjectOpenHashMap;
|
2021-06-23 17:41:46 +02:00
|
|
|
import net.minestom.server.registry.Registry;
|
|
|
|
import net.minestom.server.tag.Tag;
|
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-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-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
|
|
|
|
private static final Int2ObjectMap<Block> BLOCK_STATE_MAP = new Int2ObjectOpenHashMap<>();
|
|
|
|
private static final Registry.Container<Block> CONTAINER = new Registry.Container<>(Registry.Resource.BLOCKS,
|
|
|
|
(container, namespace, object) -> {
|
|
|
|
final JsonObject stateObject = object.remove("states").getAsJsonObject();
|
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>();
|
|
|
|
var unmodifiableEntries = Collections.unmodifiableMap(propertyEntry);
|
2021-07-30 17:34:39 +02:00
|
|
|
for (var stateEntry : stateObject.entrySet()) {
|
|
|
|
final String query = stateEntry.getKey();
|
|
|
|
JsonObject stateOverride = stateEntry.getValue().getAsJsonObject();
|
|
|
|
final var propertyMap = BlockUtils.parseProperties(query);
|
|
|
|
final Block block = new BlockImpl(Registry.block(namespace, object, stateOverride),
|
2021-08-04 17:45:17 +02:00
|
|
|
unmodifiableEntries, propertyMap, null, null);
|
2021-07-30 17:34:39 +02:00
|
|
|
BLOCK_STATE_MAP.put(block.stateId(), block);
|
|
|
|
propertyEntry.put(propertyMap, block);
|
|
|
|
}
|
|
|
|
// Register default state
|
2021-07-30 17:16:52 +02:00
|
|
|
final int defaultState = object.get("defaultStateId").getAsInt();
|
|
|
|
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
|
|
|
|
|
|
|
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-08-04 17:45:17 +02:00
|
|
|
private final Map<Map<String, String>, Block> propertyEntry;
|
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-08-04 17:45:17 +02:00
|
|
|
@NotNull Map<Map<String, String>, Block> propertyEntry,
|
2021-06-23 17:41:46 +02:00
|
|
|
@NotNull Map<String, String> properties,
|
|
|
|
@Nullable NBTCompound nbt,
|
|
|
|
@Nullable BlockHandler handler) {
|
|
|
|
this.registry = registry;
|
2021-07-19 23:59:40 +02:00
|
|
|
this.propertyEntry = propertyEntry;
|
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-07-24 03:31:03 +02:00
|
|
|
var temporaryNbt = nbt != null ? nbt.deepClone() : new NBTCompound();
|
|
|
|
tag.write(temporaryNbt, value);
|
|
|
|
final var finalNbt = temporaryNbt.getSize() > 0 ? NBT_CACHE.get(temporaryNbt, Function.identity()) : null;
|
|
|
|
return new BlockImpl(registry, propertyEntry, properties, finalNbt, handler);
|
2021-06-23 17:41:46 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public @NotNull Block withHandler(@Nullable BlockHandler handler) {
|
2021-07-19 23:59:40 +02:00
|
|
|
return new BlockImpl(registry, propertyEntry, 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
|
|
|
}
|
|
|
|
|
|
|
|
@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-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-07-30 17:34:39 +02:00
|
|
|
Block block = propertyEntry.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 :
|
|
|
|
new BlockImpl(block.registry(), propertyEntry, block.properties(), nbt, handler);
|
2021-05-22 21:56:01 +02:00
|
|
|
}
|
2021-06-23 17:41:46 +02:00
|
|
|
}
|