Add dummy block handler

This commit is contained in:
TheMode 2021-06-25 19:10:41 +02:00
parent 74073f13b5
commit 3c04da9ae9
2 changed files with 29 additions and 3 deletions

View File

@ -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);
}

View File

@ -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<String, BlockHandler> 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;
}
}
}