From 128ac2f109ff919e4e0d86b55532a3c8c300c657 Mon Sep 17 00:00:00 2001 From: Jonathan Coates Date: Sun, 17 Mar 2024 16:08:31 +0000 Subject: [PATCH] Better handling when a BE type isn't registered This should never happen, but apparently it does!? We now log an error (rather than crashing), and include the original BE (and associated block), as the BE type isn't very useful. See #1750. Technically this fixes it, but want to do some more poking there first. --- .../peripheral/generic/GenericPeripheral.java | 6 ++--- .../generic/GenericPeripheralBuilder.java | 23 ++++++++++++++++++- .../shared/platform/RegistryWrappers.java | 2 ++ .../computercraft/TestPlatformHelper.java | 6 +++++ .../shared/platform/PlatformHelperImpl.java | 6 +++++ .../shared/platform/PlatformHelperImpl.java | 6 +++++ 6 files changed, 44 insertions(+), 5 deletions(-) diff --git a/projects/common/src/main/java/dan200/computercraft/shared/peripheral/generic/GenericPeripheral.java b/projects/common/src/main/java/dan200/computercraft/shared/peripheral/generic/GenericPeripheral.java index 8874a12b9..d57208b8f 100644 --- a/projects/common/src/main/java/dan200/computercraft/shared/peripheral/generic/GenericPeripheral.java +++ b/projects/common/src/main/java/dan200/computercraft/shared/peripheral/generic/GenericPeripheral.java @@ -11,7 +11,6 @@ import dan200.computercraft.api.lua.MethodResult; import dan200.computercraft.api.peripheral.IComputerAccess; import dan200.computercraft.api.peripheral.IDynamicPeripheral; import dan200.computercraft.api.peripheral.IPeripheral; -import dan200.computercraft.shared.platform.RegistryWrappers; import net.minecraft.core.Direction; import net.minecraft.world.level.block.entity.BlockEntity; @@ -27,11 +26,10 @@ public final class GenericPeripheral implements IDynamicPeripheral { private final Set additionalTypes; private final List methods; - GenericPeripheral(BlockEntity tile, Direction side, @Nullable String name, Set additionalTypes, List methods) { + GenericPeripheral(BlockEntity tile, Direction side, String type, Set additionalTypes, List methods) { this.side = side; - var type = RegistryWrappers.BLOCK_ENTITY_TYPES.getKey(tile.getType()); this.tile = tile; - this.type = name != null ? name : type.toString(); + this.type = type; this.additionalTypes = additionalTypes; this.methods = methods; } diff --git a/projects/common/src/main/java/dan200/computercraft/shared/peripheral/generic/GenericPeripheralBuilder.java b/projects/common/src/main/java/dan200/computercraft/shared/peripheral/generic/GenericPeripheralBuilder.java index fc2457905..71102b19e 100644 --- a/projects/common/src/main/java/dan200/computercraft/shared/peripheral/generic/GenericPeripheralBuilder.java +++ b/projects/common/src/main/java/dan200/computercraft/shared/peripheral/generic/GenericPeripheralBuilder.java @@ -8,8 +8,11 @@ import dan200.computercraft.api.peripheral.IPeripheral; import dan200.computercraft.api.peripheral.PeripheralType; import dan200.computercraft.core.methods.NamedMethod; import dan200.computercraft.core.methods.PeripheralMethod; +import dan200.computercraft.shared.platform.RegistryWrappers; import net.minecraft.core.Direction; import net.minecraft.world.level.block.entity.BlockEntity; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; import javax.annotation.Nullable; import java.util.ArrayList; @@ -25,6 +28,8 @@ import java.util.Set; * See the platform-specific peripheral providers for the usage of this. */ final class GenericPeripheralBuilder { + private static final Logger LOG = LoggerFactory.getLogger(GenericPeripheralBuilder.class); + private @Nullable String name; private final Set additionalTypes = new HashSet<>(0); private final ArrayList methods = new ArrayList<>(); @@ -33,8 +38,24 @@ final class GenericPeripheralBuilder { IPeripheral toPeripheral(BlockEntity blockEntity, Direction side) { if (methods.isEmpty()) return null; + String type; + if (name == null) { + var typeId = RegistryWrappers.BLOCK_ENTITY_TYPES.tryGetKey(blockEntity.getType()); + if (typeId == null) { + LOG.error( + "Block entity {} for {} was not registered. Skipping creating a generic peripheral for it.", + blockEntity, blockEntity.getBlockState().getBlock() + ); + return null; + } + + type = typeId.toString(); + } else { + type = name; + } + methods.trimToSize(); - return new GenericPeripheral(blockEntity, side, name, additionalTypes, methods); + return new GenericPeripheral(blockEntity, side, type, additionalTypes, methods); } void addMethod(Object target, String name, PeripheralMethod method, @Nullable NamedMethod info) { diff --git a/projects/common/src/main/java/dan200/computercraft/shared/platform/RegistryWrappers.java b/projects/common/src/main/java/dan200/computercraft/shared/platform/RegistryWrappers.java index 76c289a5b..989183f59 100644 --- a/projects/common/src/main/java/dan200/computercraft/shared/platform/RegistryWrappers.java +++ b/projects/common/src/main/java/dan200/computercraft/shared/platform/RegistryWrappers.java @@ -38,6 +38,8 @@ public final class RegistryWrappers { public interface RegistryWrapper extends IdMap { ResourceLocation getKey(T object); + @Nullable ResourceLocation tryGetKey(T object); + T get(ResourceLocation location); @Nullable diff --git a/projects/common/src/test/java/dan200/computercraft/TestPlatformHelper.java b/projects/common/src/test/java/dan200/computercraft/TestPlatformHelper.java index 815c716db..9ecdaa8e0 100644 --- a/projects/common/src/test/java/dan200/computercraft/TestPlatformHelper.java +++ b/projects/common/src/test/java/dan200/computercraft/TestPlatformHelper.java @@ -268,6 +268,12 @@ public class TestPlatformHelper extends AbstractComputerCraftAPI implements Plat return key; } + @Nullable + @Override + public ResourceLocation tryGetKey(T object) { + return registry.getKey(object); + } + @Override public T get(ResourceLocation location) { var object = registry.get(location); diff --git a/projects/fabric/src/main/java/dan200/computercraft/shared/platform/PlatformHelperImpl.java b/projects/fabric/src/main/java/dan200/computercraft/shared/platform/PlatformHelperImpl.java index 3ed6f52c0..7efb7806c 100644 --- a/projects/fabric/src/main/java/dan200/computercraft/shared/platform/PlatformHelperImpl.java +++ b/projects/fabric/src/main/java/dan200/computercraft/shared/platform/PlatformHelperImpl.java @@ -337,6 +337,12 @@ public class PlatformHelperImpl implements PlatformHelper { return key; } + @Nullable + @Override + public ResourceLocation tryGetKey(T object) { + return registry.getKey(object); + } + @Override public T get(ResourceLocation location) { var object = registry.get(location); diff --git a/projects/forge/src/main/java/dan200/computercraft/shared/platform/PlatformHelperImpl.java b/projects/forge/src/main/java/dan200/computercraft/shared/platform/PlatformHelperImpl.java index 63bc64304..4e2eb27e9 100644 --- a/projects/forge/src/main/java/dan200/computercraft/shared/platform/PlatformHelperImpl.java +++ b/projects/forge/src/main/java/dan200/computercraft/shared/platform/PlatformHelperImpl.java @@ -360,6 +360,12 @@ public class PlatformHelperImpl implements PlatformHelper { return key; } + @Nullable + @Override + public ResourceLocation tryGetKey(T object) { + return registry.getKey(object); + } + @Override public T get(ResourceLocation location) { var object = registry.getValue(location);