1
0
mirror of https://github.com/SquidDev-CC/CC-Tweaked synced 2025-12-07 06:48:05 +00:00

Small refactoring to generic peripherals

- Remove SidedGenericPeripheral (we never used this!), adding the
   functionality to GenericPeripheral directly. This is just used on the
   Fabric side for now, but might make sense with Forge too.

 - Move GenericPeripheralBuilder into the common project - this is
   identical between the two projects!

 - GenericPeripheralBuilder now generates a list of methods internally,
   rather than being passed the methods.

 - Add a tiny bit of documentation.
This commit is contained in:
Jonathan Coates
2023-06-26 19:11:59 +01:00
parent 4a5e03c11a
commit a29a516a3f
7 changed files with 95 additions and 131 deletions

View File

@@ -5,8 +5,6 @@
package dan200.computercraft.shared.peripheral.generic;
import dan200.computercraft.api.peripheral.IPeripheral;
import dan200.computercraft.core.asm.NamedMethod;
import dan200.computercraft.core.asm.PeripheralMethod;
import dan200.computercraft.shared.peripheral.generic.methods.InventoryMethods;
import net.minecraft.core.BlockPos;
import net.minecraft.core.Direction;
@@ -15,10 +13,7 @@ import net.minecraft.world.level.block.entity.BlockEntity;
import net.minecraft.world.level.block.state.BlockState;
import javax.annotation.Nullable;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
public class GenericPeripheralProvider {
interface Lookup<T> {
@@ -31,53 +26,17 @@ public class GenericPeripheralProvider {
);
@Nullable
public static IPeripheral getPeripheral(Level world, BlockPos pos, Direction side, @Nullable BlockEntity blockEntity) {
public static IPeripheral getPeripheral(Level level, BlockPos pos, Direction side, @Nullable BlockEntity blockEntity) {
if (blockEntity == null) return null;
var saturated = new GenericPeripheralBuilder();
var tileMethods = PeripheralMethod.GENERATOR.getMethods(blockEntity.getClass());
if (!tileMethods.isEmpty()) saturated.addMethods(blockEntity, tileMethods);
var builder = new GenericPeripheralBuilder();
builder.addMethods(blockEntity);
for (var lookup : lookups) {
var contents = lookup.find(world, pos, blockEntity.getBlockState(), blockEntity, side);
if (contents == null) continue;
var methods = PeripheralMethod.GENERATOR.getMethods(contents.getClass());
if (!methods.isEmpty()) saturated.addMethods(contents, methods);
var contents = lookup.find(level, pos, blockEntity.getBlockState(), blockEntity, side);
if (contents != null) builder.addMethods(contents);
}
return saturated.toPeripheral(blockEntity);
}
private static class GenericPeripheralBuilder {
private @Nullable String name;
private final Set<String> additionalTypes = new HashSet<>(0);
private final ArrayList<SaturatedMethod> methods = new ArrayList<>(0);
@Nullable
IPeripheral toPeripheral(BlockEntity tile) {
if (methods.isEmpty()) return null;
methods.trimToSize();
return new GenericPeripheral(tile, name, additionalTypes, methods);
}
void addMethods(Object target, List<NamedMethod<PeripheralMethod>> methods) {
var saturatedMethods = this.methods;
saturatedMethods.ensureCapacity(saturatedMethods.size() + methods.size());
for (var method : methods) {
saturatedMethods.add(new SaturatedMethod(target, method));
// If we have a peripheral type, use it. Always pick the smallest one, so it's consistent (assuming mods
// don't change).
var type = method.genericType();
if (type != null && type.getPrimaryType() != null) {
var name = type.getPrimaryType();
if (this.name == null || this.name.compareTo(name) > 0) this.name = name;
}
if (type != null) additionalTypes.addAll(type.getAdditionalTypes());
}
}
return builder.toPeripheral(blockEntity, side);
}
}

View File

@@ -1,25 +0,0 @@
// SPDX-FileCopyrightText: 2022 The CC: Tweaked Developers
//
// SPDX-License-Identifier: MPL-2.0
package dan200.computercraft.shared.peripheral.generic;
import net.minecraft.core.Direction;
import net.minecraft.world.level.block.entity.BlockEntity;
import javax.annotation.Nullable;
import java.util.List;
import java.util.Set;
public class SidedGenericPeripheral extends GenericPeripheral {
private final Direction direction;
SidedGenericPeripheral(BlockEntity tile, Direction direction, @Nullable String name, Set<String> additionalTypes, List<SaturatedMethod> methods) {
super(tile, name, additionalTypes, methods);
this.direction = direction;
}
public Direction direction() {
return direction;
}
}

View File

@@ -12,7 +12,6 @@ import dan200.computercraft.api.peripheral.GenericPeripheral;
import dan200.computercraft.api.peripheral.IComputerAccess;
import dan200.computercraft.api.peripheral.IPeripheral;
import dan200.computercraft.api.peripheral.PeripheralType;
import dan200.computercraft.shared.peripheral.generic.SidedGenericPeripheral;
import dan200.computercraft.shared.platform.FabricContainerTransfer;
import net.fabricmc.fabric.api.transfer.v1.item.ItemStorage;
import net.fabricmc.fabric.api.transfer.v1.item.ItemVariant;
@@ -157,7 +156,7 @@ public class InventoryMethods implements GenericPeripheral {
@Nullable
private static SlottedStorage<ItemVariant> extractHandler(IPeripheral peripheral) {
var object = peripheral.getTarget();
var direction = peripheral instanceof SidedGenericPeripheral sided ? sided.direction() : null;
var direction = peripheral instanceof dan200.computercraft.shared.peripheral.generic.GenericPeripheral sided ? sided.side() : null;
if (object instanceof BlockEntity blockEntity) {
if (blockEntity.isRemoved()) return null;