mirror of
				https://github.com/SquidDev-CC/CC-Tweaked
				synced 2025-10-31 05:33:00 +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:
		| @@ -12,19 +12,23 @@ 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; | ||||
| 
 | ||||
| import javax.annotation.Nullable; | ||||
| import java.util.List; | ||||
| import java.util.Set; | ||||
| 
 | ||||
| class GenericPeripheral implements IDynamicPeripheral { | ||||
| public final class GenericPeripheral implements IDynamicPeripheral { | ||||
|     private final BlockEntity tile; | ||||
|     private final Direction side; | ||||
| 
 | ||||
|     private final String type; | ||||
|     private final Set<String> additionalTypes; | ||||
|     private final BlockEntity tile; | ||||
|     private final List<SaturatedMethod> methods; | ||||
| 
 | ||||
|     GenericPeripheral(BlockEntity tile, @Nullable String name, Set<String> additionalTypes, List<SaturatedMethod> methods) { | ||||
|     GenericPeripheral(BlockEntity tile, Direction side, @Nullable String name, 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(); | ||||
| @@ -32,6 +36,10 @@ class GenericPeripheral implements IDynamicPeripheral { | ||||
|         this.methods = methods; | ||||
|     } | ||||
| 
 | ||||
|     public Direction side() { | ||||
|         return side; | ||||
|     } | ||||
| 
 | ||||
|     @Override | ||||
|     public String[] getMethodNames() { | ||||
|         var names = new String[methods.size()]; | ||||
| @@ -54,7 +62,6 @@ class GenericPeripheral implements IDynamicPeripheral { | ||||
|         return additionalTypes; | ||||
|     } | ||||
| 
 | ||||
|     @Nullable | ||||
|     @Override | ||||
|     public Object getTarget() { | ||||
|         return tile; | ||||
|   | ||||
| @@ -0,0 +1,61 @@ | ||||
| // SPDX-FileCopyrightText: 2020 The CC: Tweaked Developers | ||||
| // | ||||
| // SPDX-License-Identifier: MPL-2.0 | ||||
| 
 | ||||
| package dan200.computercraft.shared.peripheral.generic; | ||||
| 
 | ||||
| import dan200.computercraft.api.peripheral.IPeripheral; | ||||
| import dan200.computercraft.api.peripheral.PeripheralType; | ||||
| import dan200.computercraft.core.asm.NamedMethod; | ||||
| import dan200.computercraft.core.asm.PeripheralMethod; | ||||
| import net.minecraft.core.Direction; | ||||
| import net.minecraft.world.level.block.entity.BlockEntity; | ||||
| 
 | ||||
| import javax.annotation.Nullable; | ||||
| import java.util.ArrayList; | ||||
| import java.util.HashSet; | ||||
| import java.util.Set; | ||||
| 
 | ||||
| /** | ||||
|  * A builder for a {@link GenericPeripheral}. | ||||
|  * <p> | ||||
|  * This handles building a list of {@linkplain SaturatedMethod methods} and computing the appropriate | ||||
|  * {@link PeripheralType} from the {@linkplain NamedMethod#genericType() methods' peripheral types}. | ||||
|  * <p> | ||||
|  * See the platform-specific peripheral providers for the usage of this. | ||||
|  */ | ||||
| final 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, Direction side) { | ||||
|         if (methods.isEmpty()) return null; | ||||
| 
 | ||||
|         methods.trimToSize(); | ||||
|         return new GenericPeripheral(tile, side, name, additionalTypes, methods); | ||||
|     } | ||||
| 
 | ||||
|     boolean addMethods(Object target) { | ||||
|         var methods = PeripheralMethod.GENERATOR.getMethods(target.getClass()); | ||||
|         if (methods.isEmpty()) return false; | ||||
| 
 | ||||
|         var saturatedMethods = this.methods; | ||||
|         saturatedMethods.ensureCapacity(saturatedMethods.size() + methods.size()); | ||||
|         for (var method : methods) { | ||||
|             saturatedMethods.add(new SaturatedMethod(target, method.name(), method.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 true; | ||||
|     } | ||||
| } | ||||
| @@ -9,18 +9,20 @@ import dan200.computercraft.api.lua.ILuaContext; | ||||
| import dan200.computercraft.api.lua.LuaException; | ||||
| import dan200.computercraft.api.lua.MethodResult; | ||||
| import dan200.computercraft.api.peripheral.IComputerAccess; | ||||
| import dan200.computercraft.core.asm.NamedMethod; | ||||
| import dan200.computercraft.core.asm.PeripheralMethod; | ||||
| 
 | ||||
| /** | ||||
|  * A {@link PeripheralMethod} along with the method's target. | ||||
|  */ | ||||
| final class SaturatedMethod { | ||||
|     private final Object target; | ||||
|     private final String name; | ||||
|     private final PeripheralMethod method; | ||||
| 
 | ||||
|     SaturatedMethod(Object target, NamedMethod<PeripheralMethod> method) { | ||||
|     SaturatedMethod(Object target, String name, PeripheralMethod method) { | ||||
|         this.target = target; | ||||
|         name = method.name(); | ||||
|         this.method = method.method(); | ||||
|         this.name = name; | ||||
|         this.method = method; | ||||
|     } | ||||
| 
 | ||||
|     MethodResult apply(ILuaContext context, IComputerAccess computer, IArguments args) throws LuaException { | ||||
|   | ||||
		Reference in New Issue
	
	Block a user
	 Jonathan Coates
					Jonathan Coates