Code review

This commit is contained in:
Шандуренко Константин Владимирович 2021-09-10 19:08:39 +03:00
parent 1e5dc8d870
commit 6c76baa940
3 changed files with 14 additions and 20 deletions

View File

@ -296,15 +296,22 @@ public interface BlockHandler {
* in order to do not lose the information while saving, and for runtime debugging purpose.
*/
@ApiStatus.Internal
final class Dummy extends BlockHandlerBase {
final class Dummy implements BlockHandler {
private static final Map<String, BlockHandler> DUMMY_CACHE = new ConcurrentHashMap<>();
public static @NotNull BlockHandler get(@NotNull String namespace) {
return DUMMY_CACHE.computeIfAbsent(namespace, Dummy::new);
}
private final NamespaceID namespace;
private Dummy(String name) {
super(name);
namespace = NamespaceID.from(name);
}
@Override
public @NotNull NamespaceID getNamespaceId() {
return namespace;
}
}
}

View File

@ -1,18 +0,0 @@
package net.minestom.server.instance.block;
import net.minestom.server.utils.NamespaceID;
import org.jetbrains.annotations.NotNull;
public class BlockHandlerBase implements BlockHandler {
private final NamespaceID namespace;
public BlockHandlerBase(String name) {
namespace = NamespaceID.from(name);
}
@Override
public @NotNull NamespaceID getNamespaceId() {
return namespace;
}
}

View File

@ -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;
@ -27,6 +28,10 @@ public class BlockManager {
blockHandlerMap.put(namespace, handlerSupplier);
}
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;