From 4bbd89f78d9b82dcb82226aea9d8e2a134b22e66 Mon Sep 17 00:00:00 2001 From: TheMode Date: Wed, 23 Jun 2021 17:41:46 +0200 Subject: [PATCH] Rename BlockImpl --- .../server/instance/block/BlockImpl.java | 96 +++++++++++++++++-- .../server/instance/block/BlockRegistry.java | 2 +- .../server/instance/block/BlockTest.java | 93 ------------------ 3 files changed, 89 insertions(+), 102 deletions(-) delete mode 100644 src/main/java/net/minestom/server/instance/block/BlockTest.java diff --git a/src/main/java/net/minestom/server/instance/block/BlockImpl.java b/src/main/java/net/minestom/server/instance/block/BlockImpl.java index 2c8397880..4db72a2f0 100644 --- a/src/main/java/net/minestom/server/instance/block/BlockImpl.java +++ b/src/main/java/net/minestom/server/instance/block/BlockImpl.java @@ -1,13 +1,93 @@ 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 { - protected static Block create(NamespaceID namespaceID, short blockId, short minStateId, short maxStateId, - short defaultStateId, List> properties) { - return BlockRegistry.get(namespaceID.asString()); +class BlockImpl implements Block { + + private static final Cache NBT_CACHE = Caffeine.newBuilder() + .expireAfterWrite(5, TimeUnit.MINUTES) + .weakValues() + .build(); + + private final Registry.BlockEntry registry; + private final Map properties; + private final NBTCompound nbt; + private final BlockHandler handler; + + BlockImpl(@NotNull Registry.BlockEntry registry, + @NotNull Map properties, + @Nullable NBTCompound nbt, + @Nullable BlockHandler handler) { + this.registry = registry; + this.properties = Collections.unmodifiableMap(properties); + this.nbt = nbt; + this.handler = handler; } -} \ No newline at end of file + + BlockImpl(@NotNull Registry.BlockEntry registry, + @NotNull Map 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 properties() { + return properties; + } + + @Override + public @NotNull Registry.BlockEntry registry() { + return registry; + } + + @Override + public @Nullable T getTag(@NotNull Tag tag) { + if (nbt == null) + return null; + return tag.read(nbt); + } +} diff --git a/src/main/java/net/minestom/server/instance/block/BlockRegistry.java b/src/main/java/net/minestom/server/instance/block/BlockRegistry.java index 4e463d591..cf94188c6 100644 --- a/src/main/java/net/minestom/server/instance/block/BlockRegistry.java +++ b/src/main/java/net/minestom/server/instance/block/BlockRegistry.java @@ -69,7 +69,7 @@ class BlockRegistry { JsonObject stateOverride = stateEntry.getValue().getAsJsonObject(); final int stateId = stateOverride.get("stateId").getAsInt(); 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); propertyEntry.propertyMap.put(propertyMap, block); }); diff --git a/src/main/java/net/minestom/server/instance/block/BlockTest.java b/src/main/java/net/minestom/server/instance/block/BlockTest.java deleted file mode 100644 index 56c7c312f..000000000 --- a/src/main/java/net/minestom/server/instance/block/BlockTest.java +++ /dev/null @@ -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 NBT_CACHE = Caffeine.newBuilder() - .expireAfterWrite(5, TimeUnit.MINUTES) - .weakValues() - .build(); - - private final Registry.BlockEntry registry; - private final Map properties; - private final NBTCompound nbt; - private final BlockHandler handler; - - BlockTest(@NotNull Registry.BlockEntry registry, - @NotNull Map 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 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 properties() { - return properties; - } - - @Override - public @NotNull Registry.BlockEntry registry() { - return registry; - } - - @Override - public @Nullable T getTag(@NotNull Tag tag) { - if (nbt == null) - return null; - return tag.read(nbt); - } -}