From 3c04da9ae9eabb5fa6e86d8011dff6b4adfc347e Mon Sep 17 00:00:00 2001 From: TheMode Date: Fri, 25 Jun 2021 19:10:41 +0200 Subject: [PATCH] Add dummy block handler --- .../minestom/server/instance/AnvilLoader.java | 6 ++--- .../server/instance/block/BlockHandler.java | 26 +++++++++++++++++++ 2 files changed, 29 insertions(+), 3 deletions(-) diff --git a/src/main/java/net/minestom/server/instance/AnvilLoader.java b/src/main/java/net/minestom/server/instance/AnvilLoader.java index 62822e128..0d0122fe9 100644 --- a/src/main/java/net/minestom/server/instance/AnvilLoader.java +++ b/src/main/java/net/minestom/server/instance/AnvilLoader.java @@ -154,10 +154,10 @@ public class AnvilLoader implements IChunkLoader { final String tileEntityID = te.getString("id"); if (tileEntityID != null) { - final var handler = BLOCK_MANAGER.getHandler(tileEntityID); + var handler = BLOCK_MANAGER.getHandler(tileEntityID); if (handler == null) { - LOGGER.warn("Block {} does not have any corresponding handler, world will load anyway.", tileEntityID); - continue; + LOGGER.warn("Block {} does not have any corresponding handler, default to dummy.", tileEntityID); + handler = BlockHandler.Dummy.get(tileEntityID); } block = block.withHandler(handler); } diff --git a/src/main/java/net/minestom/server/instance/block/BlockHandler.java b/src/main/java/net/minestom/server/instance/block/BlockHandler.java index 3ce43800f..8c80ba674 100644 --- a/src/main/java/net/minestom/server/instance/block/BlockHandler.java +++ b/src/main/java/net/minestom/server/instance/block/BlockHandler.java @@ -11,6 +11,8 @@ import org.jetbrains.annotations.NotNull; import java.util.Collection; import java.util.Collections; +import java.util.Map; +import java.util.concurrent.ConcurrentHashMap; /** * Interface used to provide block behavior. Set with {@link Block#withHandler(BlockHandler)}. @@ -282,4 +284,28 @@ public interface BlockHandler { return blockPosition; } } + + /** + * Handler used for loaded blocks with unknown namespace + * in order to do not lose the information while saving, and for runtime debugging purpose. + */ + class Dummy implements BlockHandler { + private static final Map DUMMY_CACHE = new ConcurrentHashMap<>(); + + @ApiStatus.Internal + public static @NotNull BlockHandler get(@NotNull String namespace) { + return DUMMY_CACHE.computeIfAbsent(namespace, s -> new Dummy(NamespaceID.from(namespace))); + } + + private final NamespaceID namespaceID; + + private Dummy(NamespaceID namespaceID) { + this.namespaceID = namespaceID; + } + + @Override + public @NotNull NamespaceID getNamespaceId() { + return namespaceID; + } + } }