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.
This commit is contained in:
Jonathan Coates 2024-03-17 16:08:31 +00:00
parent 5d8c46c7e6
commit 128ac2f109
No known key found for this signature in database
GPG Key ID: B9E431FF07C98D06
6 changed files with 44 additions and 5 deletions

View File

@ -11,7 +11,6 @@
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<String> additionalTypes;
private final List<SaturatedMethod> methods;
GenericPeripheral(BlockEntity tile, Direction side, @Nullable String name, Set<String> additionalTypes, List<SaturatedMethod> methods) {
GenericPeripheral(BlockEntity tile, Direction side, String type, Set<String> additionalTypes, List<SaturatedMethod> 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;
}

View File

@ -8,8 +8,11 @@
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 @@
* 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<String> additionalTypes = new HashSet<>(0);
private final ArrayList<SaturatedMethod> 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<PeripheralMethod> info) {

View File

@ -38,6 +38,8 @@ public final class RegistryWrappers {
public interface RegistryWrapper<T> extends IdMap<T> {
ResourceLocation getKey(T object);
@Nullable ResourceLocation tryGetKey(T object);
T get(ResourceLocation location);
@Nullable

View File

@ -268,6 +268,12 @@ public ResourceLocation getKey(T object) {
return key;
}
@Nullable
@Override
public ResourceLocation tryGetKey(T object) {
return registry.getKey(object);
}
@Override
public T get(ResourceLocation location) {
var object = registry.get(location);

View File

@ -337,6 +337,12 @@ public ResourceLocation getKey(T object) {
return key;
}
@Nullable
@Override
public ResourceLocation tryGetKey(T object) {
return registry.getKey(object);
}
@Override
public T get(ResourceLocation location) {
var object = registry.get(location);

View File

@ -360,6 +360,12 @@ public ResourceLocation getKey(T object) {
return key;
}
@Nullable
@Override
public ResourceLocation tryGetKey(T object) {
return registry.getKey(object);
}
@Override
public T get(ResourceLocation location) {
var object = registry.getValue(location);