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-12-30 12:07:16 +01:00
|
|
|
import net.minestom.server.utils.ArrayUtils;
|
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-12-22 01:30:10 +01:00
|
|
|
import java.util.Collection;
|
|
|
|
import java.util.HashMap;
|
|
|
|
import java.util.Map;
|
|
|
|
import java.util.Objects;
|
2021-07-24 03:31:03 +02:00
|
|
|
import java.util.function.Function;
|
2021-05-22 21:56:01 +02:00
|
|
|
|
2021-12-22 01:30:10 +01:00
|
|
|
record BlockImpl(@NotNull Registry.BlockEntry registry,
|
|
|
|
@NotNull Map<String, String> properties,
|
|
|
|
@Nullable NBTCompound nbt,
|
|
|
|
@Nullable BlockHandler handler) 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-12-22 01:30:10 +01:00
|
|
|
// Block id -> Map<Properties, Block>
|
|
|
|
private static final ObjectArray<Map<Map<String, String>, Block>> POSSIBLE_STATES = 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-12-30 12:07:16 +01:00
|
|
|
// Retrieve the block states
|
|
|
|
{
|
|
|
|
final var stateEntries = stateObject.entrySet();
|
|
|
|
final int propertiesCount = stateEntries.size();
|
|
|
|
Map<String, String>[] propertiesKeys = new Map[propertiesCount];
|
|
|
|
Block[] blocksValues = new Block[propertiesCount];
|
|
|
|
int propertiesOffset = 0;
|
|
|
|
for (var stateEntry : stateEntries) {
|
|
|
|
final String query = stateEntry.getKey();
|
|
|
|
final var stateOverride = (Map<String, Object>) stateEntry.getValue();
|
|
|
|
final var propertyMap = BlockUtils.parseProperties(query);
|
|
|
|
final Block block = new BlockImpl(Registry.block(namespace, object, stateOverride),
|
|
|
|
propertyMap, null, null);
|
|
|
|
BLOCK_STATE_MAP.set(block.stateId(), block);
|
|
|
|
propertiesKeys[propertiesOffset] = propertyMap;
|
|
|
|
blocksValues[propertiesOffset++] = block;
|
|
|
|
}
|
|
|
|
POSSIBLE_STATES.set(((Number) object.get("id")).intValue(),
|
|
|
|
ArrayUtils.toMap(propertiesKeys, blocksValues, propertiesOffset));
|
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-12-22 01:30:10 +01:00
|
|
|
POSSIBLE_STATES.trim();
|
2021-10-17 14:29:27 +02:00
|
|
|
}
|
|
|
|
|
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-12-22 01:30:10 +01:00
|
|
|
public BlockImpl {
|
|
|
|
properties = Map.copyOf(properties);
|
2021-06-23 17:41:46 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public @NotNull Block withProperty(@NotNull String property, @NotNull String value) {
|
|
|
|
var properties = new HashMap<>(this.properties);
|
2021-12-22 01:30:10 +01:00
|
|
|
final String oldProperty = properties.replace(property, value);
|
|
|
|
if (oldProperty == null)
|
|
|
|
throw new IllegalArgumentException("Property " + property + " does not exist");
|
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-22 01:30:10 +01:00
|
|
|
return new BlockImpl(registry, properties, finalNbt, handler);
|
2021-06-23 17:41:46 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public @NotNull Block withHandler(@Nullable BlockHandler handler) {
|
2021-12-22 01:30:10 +01:00
|
|
|
return new BlockImpl(registry, properties, nbt, handler);
|
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 <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() {
|
2021-12-22 01:30:10 +01:00
|
|
|
return POSSIBLE_STATES.get(id());
|
2021-08-11 21:28:13 +02:00
|
|
|
}
|
|
|
|
|
2021-08-01 13:01:56 +02:00
|
|
|
@Override
|
|
|
|
public String toString() {
|
2021-12-22 01:30:10 +01:00
|
|
|
return String.format("%s{properties=%s, nbt=%s, handler=%s}", name(), properties, nbt, handler);
|
2021-08-01 13:01:56 +02:00
|
|
|
}
|
|
|
|
|
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-12-22 01:30:10 +01:00
|
|
|
return nbt == null && handler == null ? block : new BlockImpl(block.registry(), block.properties(), nbt, handler);
|
2021-05-22 21:56:01 +02:00
|
|
|
}
|
2021-06-23 17:41:46 +02:00
|
|
|
}
|