Minestom/src/main/java/net/minestom/server/instance/block/BlockImpl.java

156 lines
5.6 KiB
Java
Raw Normal View History

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-07-10 20:26:30 +02:00
import java.time.Duration;
2021-07-30 17:16:52 +02:00
import java.util.Collection;
2021-07-23 16:14:42 +02:00
import java.util.Collections;
2021-06-23 17:41:46 +02:00
import java.util.HashMap;
import java.util.Map;
2021-07-30 17:16:52 +02:00
import java.util.concurrent.ConcurrentHashMap;
2021-07-24 03:31:03 +02:00
import java.util.function.Function;
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
PropertyEntry propertyEntry = new PropertyEntry();
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),
propertyEntry, propertyMap, null, null);
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-07-30 17:34:39 +02:00
protected static class PropertyEntry extends ConcurrentHashMap<Map<String, String>, Block> {
2021-07-30 17:16:52 +02:00
}
2021-07-10 18:42:02 +02:00
2021-06-23 17:41:46 +02:00
private final Registry.BlockEntry registry;
2021-07-30 17:16:52 +02:00
private final PropertyEntry propertyEntry;
2021-06-23 17:41:46 +02:00
private final Map<String, String> properties;
private final NBTCompound nbt;
private final BlockHandler handler;
BlockImpl(@NotNull Registry.BlockEntry registry,
2021-07-30 17:16:52 +02:00
@NotNull PropertyEntry propertyEntry,
2021-06-23 17:41:46 +02:00
@NotNull Map<String, String> properties,
@Nullable NBTCompound nbt,
@Nullable BlockHandler handler) {
this.registry = registry;
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);
}
@Override
public @NotNull Block withProperties(@NotNull Map<@NotNull String, @NotNull String> properties) {
if (properties.isEmpty()) {
return this;
}
if (this.properties.size() == properties.size()) {
2021-07-23 16:14:42 +02:00
return compute(properties); // Map should be complete
}
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) {
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-07-23 16:14:42 +02:00
private Block compute(Map<String, String> properties) {
2021-07-30 17:34:39 +02:00
Block block = propertyEntry.get(properties);
2021-07-23 16:14:42 +02:00
if (block == null)
throw new IllegalArgumentException("Invalid properties: " + properties);
return nbt == null && handler == null ? block :
new BlockImpl(block.registry(), propertyEntry, block.properties(), nbt, handler);
}
2021-06-23 17:41:46 +02:00
}