mirror of
https://github.com/Minestom/Minestom.git
synced 2025-01-08 01:17:47 +01:00
Rename BlockImpl
This commit is contained in:
parent
10ca44bfd8
commit
4bbd89f78d
@ -1,13 +1,93 @@
|
|||||||
package net.minestom.server.instance.block;
|
package net.minestom.server.instance.block;
|
||||||
|
|
||||||
import net.minestom.server.utils.NamespaceID;
|
import com.github.benmanes.caffeine.cache.Cache;
|
||||||
|
import com.github.benmanes.caffeine.cache.Caffeine;
|
||||||
|
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;
|
||||||
|
|
||||||
import java.util.List;
|
import java.util.Collections;
|
||||||
|
import java.util.HashMap;
|
||||||
|
import java.util.Map;
|
||||||
|
import java.util.concurrent.TimeUnit;
|
||||||
|
|
||||||
@Deprecated
|
class BlockImpl implements Block {
|
||||||
class BlockImpl {
|
|
||||||
protected static Block create(NamespaceID namespaceID, short blockId, short minStateId, short maxStateId,
|
private static final Cache<NBTCompound, NBTCompound> NBT_CACHE = Caffeine.newBuilder()
|
||||||
short defaultStateId, List<BlockProperty<?>> properties) {
|
.expireAfterWrite(5, TimeUnit.MINUTES)
|
||||||
return BlockRegistry.get(namespaceID.asString());
|
.weakValues()
|
||||||
|
.build();
|
||||||
|
|
||||||
|
private final Registry.BlockEntry registry;
|
||||||
|
private final Map<String, String> properties;
|
||||||
|
private final NBTCompound nbt;
|
||||||
|
private final BlockHandler handler;
|
||||||
|
|
||||||
|
BlockImpl(@NotNull Registry.BlockEntry registry,
|
||||||
|
@NotNull Map<String, String> properties,
|
||||||
|
@Nullable NBTCompound nbt,
|
||||||
|
@Nullable BlockHandler handler) {
|
||||||
|
this.registry = registry;
|
||||||
|
this.properties = Collections.unmodifiableMap(properties);
|
||||||
|
this.nbt = nbt;
|
||||||
|
this.handler = handler;
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
BlockImpl(@NotNull Registry.BlockEntry registry,
|
||||||
|
@NotNull Map<String, String> properties) {
|
||||||
|
this(registry, properties, null, null);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public @NotNull Block withProperty(@NotNull String property, @NotNull String value) {
|
||||||
|
var properties = new HashMap<>(this.properties);
|
||||||
|
properties.put(property, value);
|
||||||
|
Block block = BlockRegistry.getProperties(name(), properties);
|
||||||
|
if (block == null)
|
||||||
|
throw new IllegalArgumentException("Invalid property: " + property + ":" + value);
|
||||||
|
if (nbt != null || handler != null)
|
||||||
|
return new BlockImpl(block.registry(), block.properties(), nbt, handler);
|
||||||
|
return block;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public @NotNull Block withNbt(@Nullable NBTCompound compound) {
|
||||||
|
final var cachedNbt = NBT_CACHE.get(compound, c -> compound);
|
||||||
|
return new BlockImpl(registry, properties, cachedNbt, handler);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public @NotNull Block withHandler(@Nullable BlockHandler handler) {
|
||||||
|
return new BlockImpl(registry, properties, nbt, handler);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public @Nullable NBTCompound nbt() {
|
||||||
|
// TODO return immutable compound without clone
|
||||||
|
return nbt != null ? nbt.deepClone() : null;
|
||||||
|
}
|
||||||
|
|
||||||
|
@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);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
@ -69,7 +69,7 @@ class BlockRegistry {
|
|||||||
JsonObject stateOverride = stateEntry.getValue().getAsJsonObject();
|
JsonObject stateOverride = stateEntry.getValue().getAsJsonObject();
|
||||||
final int stateId = stateOverride.get("stateId").getAsInt();
|
final int stateId = stateOverride.get("stateId").getAsInt();
|
||||||
final var propertyMap = BlockUtils.parseProperties(query);
|
final var propertyMap = BlockUtils.parseProperties(query);
|
||||||
final Block block = new BlockTest(Registry.block(object, stateOverride), propertyMap);
|
final Block block = new BlockImpl(Registry.block(object, stateOverride), propertyMap);
|
||||||
BLOCK_STATE_MAP.put(stateId, block);
|
BLOCK_STATE_MAP.put(stateId, block);
|
||||||
propertyEntry.propertyMap.put(propertyMap, block);
|
propertyEntry.propertyMap.put(propertyMap, block);
|
||||||
});
|
});
|
||||||
|
@ -1,93 +0,0 @@
|
|||||||
package net.minestom.server.instance.block;
|
|
||||||
|
|
||||||
import com.github.benmanes.caffeine.cache.Cache;
|
|
||||||
import com.github.benmanes.caffeine.cache.Caffeine;
|
|
||||||
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;
|
|
||||||
|
|
||||||
import java.util.Collections;
|
|
||||||
import java.util.HashMap;
|
|
||||||
import java.util.Map;
|
|
||||||
import java.util.concurrent.TimeUnit;
|
|
||||||
|
|
||||||
class BlockTest implements Block {
|
|
||||||
|
|
||||||
private static final Cache<NBTCompound, NBTCompound> NBT_CACHE = Caffeine.newBuilder()
|
|
||||||
.expireAfterWrite(5, TimeUnit.MINUTES)
|
|
||||||
.weakValues()
|
|
||||||
.build();
|
|
||||||
|
|
||||||
private final Registry.BlockEntry registry;
|
|
||||||
private final Map<String, String> properties;
|
|
||||||
private final NBTCompound nbt;
|
|
||||||
private final BlockHandler handler;
|
|
||||||
|
|
||||||
BlockTest(@NotNull Registry.BlockEntry registry,
|
|
||||||
@NotNull Map<String, String> properties,
|
|
||||||
@Nullable NBTCompound nbt,
|
|
||||||
@Nullable BlockHandler handler) {
|
|
||||||
this.registry = registry;
|
|
||||||
this.properties = Collections.unmodifiableMap(properties);
|
|
||||||
this.nbt = nbt;
|
|
||||||
this.handler = handler;
|
|
||||||
}
|
|
||||||
|
|
||||||
BlockTest(@NotNull Registry.BlockEntry registry,
|
|
||||||
@NotNull Map<String, String> properties) {
|
|
||||||
this(registry, properties, null, null);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public @NotNull Block withProperty(@NotNull String property, @NotNull String value) {
|
|
||||||
var properties = new HashMap<>(this.properties);
|
|
||||||
properties.put(property, value);
|
|
||||||
Block block = BlockRegistry.getProperties(name(), properties);
|
|
||||||
if (block == null)
|
|
||||||
throw new IllegalArgumentException("Invalid property: " + property + ":" + value);
|
|
||||||
if (nbt != null || handler != null)
|
|
||||||
return new BlockTest(block.registry(), block.properties(), nbt, handler);
|
|
||||||
return block;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public @NotNull Block withNbt(@Nullable NBTCompound compound) {
|
|
||||||
final var cachedNbt = NBT_CACHE.get(compound, c -> compound);
|
|
||||||
return new BlockTest(registry, properties, cachedNbt, handler);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public @NotNull Block withHandler(@Nullable BlockHandler handler) {
|
|
||||||
return new BlockTest(registry, properties, nbt, handler);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public @Nullable NBTCompound nbt() {
|
|
||||||
// TODO return immutable compound without clone
|
|
||||||
return nbt != null ? nbt.deepClone() : null;
|
|
||||||
}
|
|
||||||
|
|
||||||
@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);
|
|
||||||
}
|
|
||||||
}
|
|
Loading…
Reference in New Issue
Block a user