mirror of
https://github.com/SquidDev-CC/CC-Tweaked
synced 2025-01-10 01:10:30 +00:00
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:
parent
5d8c46c7e6
commit
128ac2f109
@ -11,7 +11,6 @@ import dan200.computercraft.api.lua.MethodResult;
|
|||||||
import dan200.computercraft.api.peripheral.IComputerAccess;
|
import dan200.computercraft.api.peripheral.IComputerAccess;
|
||||||
import dan200.computercraft.api.peripheral.IDynamicPeripheral;
|
import dan200.computercraft.api.peripheral.IDynamicPeripheral;
|
||||||
import dan200.computercraft.api.peripheral.IPeripheral;
|
import dan200.computercraft.api.peripheral.IPeripheral;
|
||||||
import dan200.computercraft.shared.platform.RegistryWrappers;
|
|
||||||
import net.minecraft.core.Direction;
|
import net.minecraft.core.Direction;
|
||||||
import net.minecraft.world.level.block.entity.BlockEntity;
|
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 Set<String> additionalTypes;
|
||||||
private final List<SaturatedMethod> methods;
|
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;
|
this.side = side;
|
||||||
var type = RegistryWrappers.BLOCK_ENTITY_TYPES.getKey(tile.getType());
|
|
||||||
this.tile = tile;
|
this.tile = tile;
|
||||||
this.type = name != null ? name : type.toString();
|
this.type = type;
|
||||||
this.additionalTypes = additionalTypes;
|
this.additionalTypes = additionalTypes;
|
||||||
this.methods = methods;
|
this.methods = methods;
|
||||||
}
|
}
|
||||||
|
@ -8,8 +8,11 @@ import dan200.computercraft.api.peripheral.IPeripheral;
|
|||||||
import dan200.computercraft.api.peripheral.PeripheralType;
|
import dan200.computercraft.api.peripheral.PeripheralType;
|
||||||
import dan200.computercraft.core.methods.NamedMethod;
|
import dan200.computercraft.core.methods.NamedMethod;
|
||||||
import dan200.computercraft.core.methods.PeripheralMethod;
|
import dan200.computercraft.core.methods.PeripheralMethod;
|
||||||
|
import dan200.computercraft.shared.platform.RegistryWrappers;
|
||||||
import net.minecraft.core.Direction;
|
import net.minecraft.core.Direction;
|
||||||
import net.minecraft.world.level.block.entity.BlockEntity;
|
import net.minecraft.world.level.block.entity.BlockEntity;
|
||||||
|
import org.slf4j.Logger;
|
||||||
|
import org.slf4j.LoggerFactory;
|
||||||
|
|
||||||
import javax.annotation.Nullable;
|
import javax.annotation.Nullable;
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
@ -25,6 +28,8 @@ import java.util.Set;
|
|||||||
* See the platform-specific peripheral providers for the usage of this.
|
* See the platform-specific peripheral providers for the usage of this.
|
||||||
*/
|
*/
|
||||||
final class GenericPeripheralBuilder {
|
final class GenericPeripheralBuilder {
|
||||||
|
private static final Logger LOG = LoggerFactory.getLogger(GenericPeripheralBuilder.class);
|
||||||
|
|
||||||
private @Nullable String name;
|
private @Nullable String name;
|
||||||
private final Set<String> additionalTypes = new HashSet<>(0);
|
private final Set<String> additionalTypes = new HashSet<>(0);
|
||||||
private final ArrayList<SaturatedMethod> methods = new ArrayList<>();
|
private final ArrayList<SaturatedMethod> methods = new ArrayList<>();
|
||||||
@ -33,8 +38,24 @@ final class GenericPeripheralBuilder {
|
|||||||
IPeripheral toPeripheral(BlockEntity blockEntity, Direction side) {
|
IPeripheral toPeripheral(BlockEntity blockEntity, Direction side) {
|
||||||
if (methods.isEmpty()) return null;
|
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();
|
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) {
|
void addMethod(Object target, String name, PeripheralMethod method, @Nullable NamedMethod<PeripheralMethod> info) {
|
||||||
|
@ -38,6 +38,8 @@ public final class RegistryWrappers {
|
|||||||
public interface RegistryWrapper<T> extends IdMap<T> {
|
public interface RegistryWrapper<T> extends IdMap<T> {
|
||||||
ResourceLocation getKey(T object);
|
ResourceLocation getKey(T object);
|
||||||
|
|
||||||
|
@Nullable ResourceLocation tryGetKey(T object);
|
||||||
|
|
||||||
T get(ResourceLocation location);
|
T get(ResourceLocation location);
|
||||||
|
|
||||||
@Nullable
|
@Nullable
|
||||||
|
@ -268,6 +268,12 @@ public class TestPlatformHelper extends AbstractComputerCraftAPI implements Plat
|
|||||||
return key;
|
return key;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Nullable
|
||||||
|
@Override
|
||||||
|
public ResourceLocation tryGetKey(T object) {
|
||||||
|
return registry.getKey(object);
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public T get(ResourceLocation location) {
|
public T get(ResourceLocation location) {
|
||||||
var object = registry.get(location);
|
var object = registry.get(location);
|
||||||
|
@ -337,6 +337,12 @@ public class PlatformHelperImpl implements PlatformHelper {
|
|||||||
return key;
|
return key;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Nullable
|
||||||
|
@Override
|
||||||
|
public ResourceLocation tryGetKey(T object) {
|
||||||
|
return registry.getKey(object);
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public T get(ResourceLocation location) {
|
public T get(ResourceLocation location) {
|
||||||
var object = registry.get(location);
|
var object = registry.get(location);
|
||||||
|
@ -360,6 +360,12 @@ public class PlatformHelperImpl implements PlatformHelper {
|
|||||||
return key;
|
return key;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Nullable
|
||||||
|
@Override
|
||||||
|
public ResourceLocation tryGetKey(T object) {
|
||||||
|
return registry.getKey(object);
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public T get(ResourceLocation location) {
|
public T get(ResourceLocation location) {
|
||||||
var object = registry.getValue(location);
|
var object = registry.getValue(location);
|
||||||
|
Loading…
Reference in New Issue
Block a user