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

120 lines
4.1 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-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-07-10 20:26:30 +02:00
import java.time.Duration;
2021-06-23 17:41:46 +02:00
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
2021-06-26 20:23:56 +02:00
import java.util.Objects;
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;
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,
@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;
this.propertyEntry = propertyEntry;
2021-06-23 17:41:46 +02:00
this.properties = Collections.unmodifiableMap(properties);
this.nbt = nbt;
this.handler = handler;
}
BlockImpl(@NotNull Registry.BlockEntry registry,
@NotNull BlockLoader.PropertyEntry propertyEntry,
2021-06-23 17:41:46 +02:00
@NotNull Map<String, String> properties) {
this(registry, propertyEntry, properties, null, null);
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);
properties.put(property, value);
Block block = propertyEntry.getProperties(properties);
2021-06-23 17:41:46 +02:00
if (block == null)
throw new IllegalArgumentException("Invalid property: " + property + ":" + value);
if (nbt != null || handler != null)
return new BlockImpl(block.registry(), propertyEntry, block.properties(), nbt, handler);
return block;
}
@Override
public @NotNull Block withProperties(@NotNull Map<@NotNull String, @NotNull String> properties) {
if (properties.isEmpty()) {
return this;
}
Block block;
if (this.properties.size() == properties.size()) {
// Map should be complete
block = propertyEntry.getProperties(properties);
} else {
var newProperties = new HashMap<>(this.properties);
newProperties.putAll(properties);
block = propertyEntry.getProperties(newProperties);
}
if (block == null)
throw new IllegalArgumentException("Invalid properties: " + properties);
if (nbt != null || handler != null)
return new BlockImpl(block.registry(), propertyEntry, block.properties(), nbt, handler);
2021-06-23 17:41:46 +02:00
return block;
}
@Override
2021-06-26 20:23:56 +02:00
public @NotNull <T> Block withTag(@NotNull Tag<T> tag, @Nullable T value) {
var compound = Objects.requireNonNullElseGet(nbt(), NBTCompound::new);
tag.write(compound, value);
2021-07-10 20:26:30 +02:00
final var nbt = compound.getSize() > 0 ? NBT_CACHE.get(compound, c -> compound) : null;
return new BlockImpl(registry, propertyEntry, properties, nbt, 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() {
return nbt != null && nbt.getSize() > 0;
}
2021-06-23 17:41:46 +02:00
@Override
public @Nullable BlockHandler handler() {
return handler;
}
@Override
public @NotNull Map<String, String> properties() {
return properties;
}
@Override
public @NotNull Registry.BlockEntry registry() {
return registry;
}
@Override
public <T> @Nullable T getTag(@NotNull Tag<T> tag) {
if (nbt == null)
return null;
return tag.read(nbt);
}
2021-06-23 17:41:46 +02:00
}