diff --git a/src/main/java/net/minestom/server/instance/AnvilLoader.java b/src/main/java/net/minestom/server/instance/AnvilLoader.java index 2282cf57e..1ff8ee3a4 100644 --- a/src/main/java/net/minestom/server/instance/AnvilLoader.java +++ b/src/main/java/net/minestom/server/instance/AnvilLoader.java @@ -139,8 +139,12 @@ public class AnvilLoader implements IChunkLoader { for (int y = 0; y < Chunk.CHUNK_SECTION_SIZE; y++) { try { final BlockState blockState = section.get(x, y, z); - final Block block = Objects.requireNonNull(Block.fromNamespaceId(blockState.getName())) + Block block = Objects.requireNonNull(Block.fromNamespaceId(blockState.getName())) .withProperties(blockState.getProperties()); + BlockHandler handler = MinecraftServer.getBlockManager().getHandler(block.name()); + if (handler != null) { + block = block.withHandler(handler); + } chunk.setBlock(x, y + yOffset, z, block); } catch (Exception e) { EXCEPTION_MANAGER.handleException(e); 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 acd794dc1..dd40ae834 100644 --- a/src/main/java/net/minestom/server/instance/block/BlockHandler.java +++ b/src/main/java/net/minestom/server/instance/block/BlockHandler.java @@ -295,23 +295,23 @@ public interface BlockHandler { * 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 { + @ApiStatus.Internal + final 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))); + return DUMMY_CACHE.computeIfAbsent(namespace, Dummy::new); } - private final NamespaceID namespaceID; + private final NamespaceID namespace; - private Dummy(NamespaceID namespaceID) { - this.namespaceID = namespaceID; + private Dummy(String name) { + namespace = NamespaceID.from(name); } @Override public @NotNull NamespaceID getNamespaceId() { - return namespaceID; + return namespace; } } } diff --git a/src/main/java/net/minestom/server/instance/block/BlockManager.java b/src/main/java/net/minestom/server/instance/block/BlockManager.java index 446fe6dea..ed7c29799 100644 --- a/src/main/java/net/minestom/server/instance/block/BlockManager.java +++ b/src/main/java/net/minestom/server/instance/block/BlockManager.java @@ -3,6 +3,7 @@ package net.minestom.server.instance.block; import it.unimi.dsi.fastutil.ints.Int2ObjectMap; import it.unimi.dsi.fastutil.ints.Int2ObjectOpenHashMap; import net.minestom.server.instance.block.rule.BlockPlacementRule; +import net.minestom.server.utils.NamespaceID; import net.minestom.server.utils.validate.Check; import org.jetbrains.annotations.ApiStatus; import org.jetbrains.annotations.NotNull; @@ -23,17 +24,21 @@ public class BlockManager { // block id -> block placement rule private final Int2ObjectMap placementRuleMap = new Int2ObjectOpenHashMap<>(); - public synchronized void registerHandler(@NotNull String namespace, @NotNull Supplier<@NotNull BlockHandler> handlerSupplier) { - this.blockHandlerMap.put(namespace, handlerSupplier); + public void registerHandler(@NotNull String namespace, @NotNull Supplier<@NotNull BlockHandler> handlerSupplier) { + blockHandlerMap.put(namespace, handlerSupplier); } - public synchronized @Nullable BlockHandler getHandler(@NotNull String namespace) { + public void registerHandler(@NotNull NamespaceID namespace, @NotNull Supplier<@NotNull BlockHandler> handlerSupplier) { + registerHandler(namespace.toString(), handlerSupplier); + } + + public @Nullable BlockHandler getHandler(@NotNull String namespace) { final var handler = blockHandlerMap.get(namespace); return handler != null ? handler.get() : null; } @ApiStatus.Internal - public synchronized @Nullable BlockHandler getHandlerOrDummy(@NotNull String namespace) { + public @NotNull BlockHandler getHandlerOrDummy(@NotNull String namespace) { BlockHandler handler = getHandler(namespace); if (handler == null) { LOGGER.warn("Block {} does not have any corresponding handler, default to dummy.", namespace); @@ -51,7 +56,7 @@ public class BlockManager { public synchronized void registerBlockPlacementRule(@NotNull BlockPlacementRule blockPlacementRule) { final int id = blockPlacementRule.getBlock().id(); Check.argCondition(id < 0, "Block ID must be >= 0, got: " + id); - this.placementRuleMap.put(id, blockPlacementRule); + placementRuleMap.put(id, blockPlacementRule); } /**