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;
|
|
|
|
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-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-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-10 18:42:02 +02:00
|
|
|
private static final Cache<NBTCompound, NBTCompound> NBT_CACHE = Caffeine.newBuilder()
|
2021-07-10 20:26:30 +02:00
|
|
|
.expireAfterWrite(Duration.ofMinutes(5))
|
2021-07-10 18:42:02 +02:00
|
|
|
.weakValues()
|
|
|
|
.build();
|
|
|
|
|
2021-06-23 17:41:46 +02:00
|
|
|
private final Registry.BlockEntry registry;
|
2021-07-19 23:59:40 +02:00
|
|
|
private final BlockLoader.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-19 23:59:40 +02:00
|
|
|
@NotNull BlockLoader.PropertyEntry 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-07-20 23:48:13 +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-07-23 16:14:42 +02:00
|
|
|
private Block compute(Map<String, String> properties) {
|
|
|
|
Block block = propertyEntry.getProperties(properties);
|
|
|
|
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-05-22 21:56:01 +02:00
|
|
|
}
|
2021-06-23 17:41:46 +02:00
|
|
|
}
|