From 1c43ff1a6a2e66d6831a5c338ae11edb1d2bddb2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Florian=20N=C3=BCcke?= Date: Tue, 1 Dec 2020 20:30:38 +0100 Subject: [PATCH] Renamed Device -> DeviceInterface, IdentifiableDevice -> Device because that's more accurately what they are/represent. --- src/main/java/li/cil/oc2/api/API.java | 3 +- .../java/li/cil/oc2/api/bus/DeviceBus.java | 12 ++-- .../cil/oc2/api/bus/DeviceBusController.java | 10 +-- .../li/cil/oc2/api/bus/DeviceBusElement.java | 4 +- .../java/li/cil/oc2/api/device/Device.java | 49 ++++++------- .../cil/oc2/api/device/DeviceInterface.java | 43 ++++++++++++ .../li/cil/oc2/api/device/DeviceMethod.java | 8 +-- .../oc2/api/device/IdentifiableDevice.java | 38 ----------- .../cil/oc2/api/device/object/Callback.java | 3 +- .../cil/oc2/api/device/object/Callbacks.java | 2 +- .../oc2/api/device/object/NamedDevice.java | 4 +- ...Device.java => ObjectDeviceInterface.java} | 18 ++--- .../provider/DeviceInterfaceProvider.java | 47 +++++++++++++ .../api/device/provider/DeviceProvider.java | 49 ------------- .../oc2/api/device/provider/DeviceQuery.java | 4 +- .../java/li/cil/oc2/common/CommonSetup.java | 2 +- src/main/java/li/cil/oc2/common/IMC.java | 6 +- .../common/bus/DeviceBusControllerImpl.java | 28 ++++---- .../oc2/common/bus/DeviceBusElementImpl.java | 12 ++-- .../bus/TileEntityDeviceBusElement.java | 14 ++-- ...ifiableDeviceImpl.java => DeviceImpl.java} | 30 ++++---- ...ce.java => DeviceInterfaceCollection.java} | 24 +++---- .../li/cil/oc2/common/device/Providers.java | 66 ------------------ ...yAnyTileEntityDeviceInterfaceProvider.java | 12 ++++ ...CapabilityAnyTileEntityDeviceProvider.java | 12 ---- ...ityTileEntityDeviceInterfaceProvider.java} | 12 ++-- ...actTileEntityDeviceInterfaceProvider.java} | 14 ++-- ...java => BlockDeviceInterfaceProvider.java} | 12 ++-- ...EnergyStorageDeviceInterfaceProvider.java} | 12 ++-- ... FluidHandlerDeviceInterfaceProvider.java} | 12 ++-- ...> ItemHandlerDeviceInterfaceProvider.java} | 12 ++-- .../oc2/common/device/provider/Providers.java | 68 +++++++++++++++++++ ...=> TileEntityDeviceInterfaceProvider.java} | 12 ++-- .../serializers/DeviceJsonSerializer.java | 6 +- .../java/li/cil/oc2/bus/DeviceBusTests.java | 4 +- .../cil/oc2/vm/ObjectDeviceProtocolTests.java | 22 +++--- 36 files changed, 347 insertions(+), 339 deletions(-) create mode 100644 src/main/java/li/cil/oc2/api/device/DeviceInterface.java delete mode 100644 src/main/java/li/cil/oc2/api/device/IdentifiableDevice.java rename src/main/java/li/cil/oc2/api/device/object/{ObjectDevice.java => ObjectDeviceInterface.java} (79%) create mode 100644 src/main/java/li/cil/oc2/api/device/provider/DeviceInterfaceProvider.java delete mode 100644 src/main/java/li/cil/oc2/api/device/provider/DeviceProvider.java rename src/main/java/li/cil/oc2/common/device/{IdentifiableDeviceImpl.java => DeviceImpl.java} (54%) rename src/main/java/li/cil/oc2/common/device/{CompoundDevice.java => DeviceInterfaceCollection.java} (52%) delete mode 100644 src/main/java/li/cil/oc2/common/device/Providers.java create mode 100644 src/main/java/li/cil/oc2/common/device/provider/AbstractCapabilityAnyTileEntityDeviceInterfaceProvider.java delete mode 100644 src/main/java/li/cil/oc2/common/device/provider/AbstractCapabilityAnyTileEntityDeviceProvider.java rename src/main/java/li/cil/oc2/common/device/provider/{AbstractCapabilityTileEntityDeviceProvider.java => AbstractCapabilityTileEntityDeviceInterfaceProvider.java} (56%) rename src/main/java/li/cil/oc2/common/device/provider/{AbstractTileEntityDeviceProvider.java => AbstractTileEntityDeviceInterfaceProvider.java} (57%) rename src/main/java/li/cil/oc2/common/device/provider/{BlockDeviceProvider.java => BlockDeviceInterfaceProvider.java} (71%) rename src/main/java/li/cil/oc2/common/device/provider/{EnergyStorageDeviceProvider.java => EnergyStorageDeviceInterfaceProvider.java} (67%) rename src/main/java/li/cil/oc2/common/device/provider/{FluidHandlerDeviceProvider.java => FluidHandlerDeviceInterfaceProvider.java} (67%) rename src/main/java/li/cil/oc2/common/device/provider/{ItemHandlerDeviceProvider.java => ItemHandlerDeviceInterfaceProvider.java} (66%) create mode 100644 src/main/java/li/cil/oc2/common/device/provider/Providers.java rename src/main/java/li/cil/oc2/common/device/provider/{TileEntityDeviceProvider.java => TileEntityDeviceInterfaceProvider.java} (57%) diff --git a/src/main/java/li/cil/oc2/api/API.java b/src/main/java/li/cil/oc2/api/API.java index ccdc819c..b23c0be3 100644 --- a/src/main/java/li/cil/oc2/api/API.java +++ b/src/main/java/li/cil/oc2/api/API.java @@ -3,6 +3,7 @@ package li.cil.oc2.api; import com.google.gson.GsonBuilder; import li.cil.oc2.api.device.DeviceMethod; import li.cil.oc2.api.device.object.Callback; +import li.cil.oc2.api.device.provider.DeviceInterfaceProvider; import li.cil.oc2.api.imc.DeviceMethodParameterTypeAdapter; import java.lang.reflect.Type; @@ -11,7 +12,7 @@ public final class API { public static final String MOD_ID = "oc2"; /** - * IMC message for registering a {@link li.cil.oc2.api.device.provider.DeviceProvider}. + * IMC message for registering a {@link DeviceInterfaceProvider}. *

* Example: *

diff --git a/src/main/java/li/cil/oc2/api/bus/DeviceBus.java b/src/main/java/li/cil/oc2/api/bus/DeviceBus.java
index f99da661..a183eb85 100644
--- a/src/main/java/li/cil/oc2/api/bus/DeviceBus.java
+++ b/src/main/java/li/cil/oc2/api/bus/DeviceBus.java
@@ -1,12 +1,12 @@
 package li.cil.oc2.api.bus;
 
+import li.cil.oc2.api.device.DeviceInterface;
 import li.cil.oc2.api.device.Device;
-import li.cil.oc2.api.device.IdentifiableDevice;
 
 import java.util.Collection;
 
 /**
- * A device bus provides the interface by which {@link Device} can be made available
+ * A device bus provides the interface by which {@link DeviceInterface} can be made available
  * to a {@link DeviceBusController}, which is usually used by VMs to access devices.
  */
 public interface DeviceBus {
@@ -22,24 +22,24 @@ public interface DeviceBus {
      *
      * @param device the device to add to the bus.
      */
-    void addDevice(IdentifiableDevice device);
+    void addDevice(Device device);
 
     /**
      * Removes a device from this device bus.
      * 

- * If the device has not been added with {@link #addDevice(IdentifiableDevice)} before calling + * If the device has not been added with {@link #addDevice(Device)} before calling * this method, this method is a no-op. * * @param device the device to remove from the bus. */ - void removeDevice(IdentifiableDevice device); + void removeDevice(Device device); /** * The list of all devices currently registered with this device bus. * * @return the list of all devices that are currently on this bus. */ - Collection getDevices(); + Collection getDevices(); /** * Schedules a rescan of the device bus. diff --git a/src/main/java/li/cil/oc2/api/bus/DeviceBusController.java b/src/main/java/li/cil/oc2/api/bus/DeviceBusController.java index 6714d377..8c312911 100644 --- a/src/main/java/li/cil/oc2/api/bus/DeviceBusController.java +++ b/src/main/java/li/cil/oc2/api/bus/DeviceBusController.java @@ -1,7 +1,7 @@ package li.cil.oc2.api.bus; +import li.cil.oc2.api.device.DeviceInterface; import li.cil.oc2.api.device.Device; -import li.cil.oc2.api.device.IdentifiableDevice; import java.util.Collection; @@ -11,8 +11,8 @@ import java.util.Collection; * {@link DeviceBusElement#setController(DeviceBusController)}. *

* This interface is usually provided by VM containers and used to collect connected - * {@link Device}s by aggregating the devices that were added to the device bus elements - * via {@link DeviceBus#addDevice(IdentifiableDevice)}. + * {@link DeviceInterface}s by aggregating the devices that were added to the device bus elements + * via {@link DeviceBus#addDevice(Device)}. *

* The only way for {@link DeviceBusElement}s to be added to a bus is for a * {@link DeviceBusController} to detect them during a scan. @@ -50,10 +50,10 @@ public interface DeviceBusController { /** * The list of all devices currently known to this controller. *

- * This is the aggregation of all {@link Device} added to all {@link DeviceBusElement}s known + * This is the aggregation of all {@link DeviceInterface} added to all {@link DeviceBusElement}s known * to the controller as found during the last scan scheduled via {@link #scheduleBusScan()}. * * @return the list of all devices on the bus managed by this controller. */ - Collection getDevices(); + Collection getDevices(); } diff --git a/src/main/java/li/cil/oc2/api/bus/DeviceBusElement.java b/src/main/java/li/cil/oc2/api/bus/DeviceBusElement.java index d8d90668..c496372e 100644 --- a/src/main/java/li/cil/oc2/api/bus/DeviceBusElement.java +++ b/src/main/java/li/cil/oc2/api/bus/DeviceBusElement.java @@ -1,6 +1,6 @@ package li.cil.oc2.api.bus; -import li.cil.oc2.api.device.IdentifiableDevice; +import li.cil.oc2.api.device.Device; import javax.annotation.Nullable; import java.util.Collection; @@ -48,5 +48,5 @@ public interface DeviceBusElement extends DeviceBus { * * @return the devices that have been added to this element. */ - Collection getLocalDevices(); + Collection getLocalDevices(); } diff --git a/src/main/java/li/cil/oc2/api/device/Device.java b/src/main/java/li/cil/oc2/api/device/Device.java index e51b8d80..664f2d3f 100644 --- a/src/main/java/li/cil/oc2/api/device/Device.java +++ b/src/main/java/li/cil/oc2/api/device/Device.java @@ -1,39 +1,42 @@ package li.cil.oc2.api.device; import li.cil.oc2.api.bus.DeviceBus; -import li.cil.oc2.api.device.object.ObjectDevice; -import li.cil.oc2.api.device.provider.DeviceProvider; +import li.cil.oc2.api.device.provider.DeviceInterfaceProvider; -import java.util.List; +import java.util.UUID; /** - * Defines a device that may be added to a {@link DeviceBus}. + * Specialization of a device interface that can be referenced by a {@link UUID} and + * represents a device as a whole. *

- * The easiest and hence recommended way of implementing this interface is to use - * the {@link ObjectDevice} class. + * Implementations will typically act as a top-level wrapper for one singular device + * as viewed by a list of {@link DeviceInterface}s. *

- * Note that it is strongly encouraged for implementations to provide an overloaded - * {@link #equals(Object)} and {@link #hashCode()} so that identical devices can be - * detected. - * - * @see ObjectDevice - * @see DeviceProvider + * A unique ID is required when adding devices to a {@link DeviceBus} such that they + * may then be referenced. + *

+ * Note that {@link DeviceInterfaceProvider}s are not expected to return instances + * implementing this interface, but return regular {@link DeviceInterface}s instead. */ -public interface Device { +public interface Device extends DeviceInterface { /** - * A list of device type names for this device. + * An id unique to this device. *

- * Devices may be identified by multiple type names. Although every atomic - * implementation will usually only have one, when compounding such modular - * devices all the underlying type names can thus be retained. - *

- * In a more general sense, these can be considered tags the device can be - * referenced by inside a VM. + * This id must persist over save/load to prevent code in a running VM losing + * track of the device. */ - List getTypeNames(); + UUID getUniqueIdentifier(); /** - * The list of methods implemented by this device. + * Returns a possible underlying instance of a device. + *

+ * Frequently some {@link DeviceInterface} obtained from a {@link DeviceInterfaceProvider} will be + * wrapped by an instance of this interface. To prevent this leading to duplicated + * device listings this allows accessing the device proper for equality checks. + * + * @return the underlying device. May be this device itself. */ - List getMethods(); + default DeviceInterface getIdentifiedDevice() { + return this; + } } diff --git a/src/main/java/li/cil/oc2/api/device/DeviceInterface.java b/src/main/java/li/cil/oc2/api/device/DeviceInterface.java new file mode 100644 index 00000000..3019f8c6 --- /dev/null +++ b/src/main/java/li/cil/oc2/api/device/DeviceInterface.java @@ -0,0 +1,43 @@ +package li.cil.oc2.api.device; + +import li.cil.oc2.api.device.object.ObjectDeviceInterface; +import li.cil.oc2.api.device.provider.DeviceInterfaceProvider; + +import java.util.List; + +/** + * Implementations act as an interface for a device. + *

+ * A {@link DeviceInterface} represents a single view onto some device. One device + * may have multiple {@code DeviceInterfaces} providing different methods for the + * device. This allows specifying general purpose interfaces which provide logic + * for some aspect of an underlying device which may be shared with other devices. + *

+ * The easiest and hence recommended way of implementing this interface is to use + * the {@link ObjectDeviceInterface} class. + *

+ * Note that it is strongly encouraged for implementations to provide an overloaded + * {@link #equals(Object)} and {@link #hashCode()} so that identical devices can be + * detected. + * + * @see ObjectDeviceInterface + * @see DeviceInterfaceProvider + */ +public interface DeviceInterface { + /** + * A list of type names identifying this interface. + *

+ * Device interfaces may be identified by multiple type names. Although every + * atomic implementation will usually only have one, when compounding interfaces + * all the underlying type names can thus be retained. + *

+ * In a more general sense, these can be considered tags the device can be + * referenced by inside a VM. + */ + List getTypeNames(); + + /** + * The list of methods provided by this interface. + */ + List getMethods(); +} diff --git a/src/main/java/li/cil/oc2/api/device/DeviceMethod.java b/src/main/java/li/cil/oc2/api/device/DeviceMethod.java index 3396b947..3141e3a8 100644 --- a/src/main/java/li/cil/oc2/api/device/DeviceMethod.java +++ b/src/main/java/li/cil/oc2/api/device/DeviceMethod.java @@ -1,22 +1,22 @@ package li.cil.oc2.api.device; import li.cil.oc2.api.bus.DeviceBusController; -import li.cil.oc2.api.device.object.ObjectDevice; +import li.cil.oc2.api.device.object.ObjectDeviceInterface; import javax.annotation.Nullable; import java.util.Optional; /** - * Represents a single method that can be exposed by a {@link Device}. + * Represents a single method that can be exposed by a {@link DeviceInterface}. *

* The easiest and hence recommended way of implementing this interface is to use - * the {@link ObjectDevice} class. + * the {@link ObjectDeviceInterface} class. *

* Method parameters are serialized and deserialized using Gson. When using custom * parameter types it may be necessary to register a custom type adapter for them * via {@link li.cil.oc2.api.API#IMC_ADD_DEVICE_METHOD_PARAMETER_TYPE_ADAPTER}. * - * @see ObjectDevice + * @see ObjectDeviceInterface */ public interface DeviceMethod { /** diff --git a/src/main/java/li/cil/oc2/api/device/IdentifiableDevice.java b/src/main/java/li/cil/oc2/api/device/IdentifiableDevice.java deleted file mode 100644 index 3ae3dcbc..00000000 --- a/src/main/java/li/cil/oc2/api/device/IdentifiableDevice.java +++ /dev/null @@ -1,38 +0,0 @@ -package li.cil.oc2.api.device; - -import li.cil.oc2.api.device.provider.DeviceProvider; - -import java.util.UUID; - -/** - * Specialization of devices that allows referencing the device by a {@link UUID}. - *

- * This type is required when adding devices to a {@link li.cil.oc2.api.bus.DeviceBus} - * or referencing devices on a bus. Some {@link li.cil.oc2.api.bus.DeviceBusElement}s - * may take care of wrapping connected devices automatically. - *

- * Note that {@link DeviceProvider}s are not - * required to return identifiable devices. - */ -public interface IdentifiableDevice extends Device { - /** - * An id unique to this device. - *

- * This id must persist over save/load to prevent code in a running VM losing - * track of the device. - */ - UUID getUniqueIdentifier(); - - /** - * Returns a possible underlying instance of a device. - *

- * Frequently some {@link Device} obtained from a {@link DeviceProvider} will be - * wrapped by an instance of this interface. To prevent this leading to duplicated - * device listings this allows accessing the device proper for equality checks. - * - * @return the underlying device. May be this device itself. - */ - default Device getIdentifiedDevice() { - return this; - } -} diff --git a/src/main/java/li/cil/oc2/api/device/object/Callback.java b/src/main/java/li/cil/oc2/api/device/object/Callback.java index 06152dca..a34c2a7e 100644 --- a/src/main/java/li/cil/oc2/api/device/object/Callback.java +++ b/src/main/java/li/cil/oc2/api/device/object/Callback.java @@ -11,8 +11,7 @@ import java.lang.annotation.Target; * Utility annotation to allow generating lists of {@link DeviceMethod}s using * {@link Callbacks#collectMethods(Object)}. *

- * Intended to be used in classes instances of which are used in combination with - * {@link ObjectDevice} and subclasses of {@link ObjectDevice}. + * Intended to be used in classes instances of which are used as a target of {@link ObjectDeviceInterface}. *

* Method parameters are serialized and deserialized using Gson. When using custom * parameter types it may be necessary to register a custom type adapter for them diff --git a/src/main/java/li/cil/oc2/api/device/object/Callbacks.java b/src/main/java/li/cil/oc2/api/device/object/Callbacks.java index f33cc39f..79362b28 100644 --- a/src/main/java/li/cil/oc2/api/device/object/Callbacks.java +++ b/src/main/java/li/cil/oc2/api/device/object/Callbacks.java @@ -15,7 +15,7 @@ import java.util.stream.Collectors; * Provides automated extraction of {@link DeviceMethod}s from instances of * class with methods annotated with the {@link Callback} annotation. *

- * Prefer using {@link ObjectDevice} instead of using this class directly. + * Prefer using {@link ObjectDeviceInterface} instead of using this class directly. */ public final class Callbacks { private static final Logger LOGGER = LogManager.getLogger(); diff --git a/src/main/java/li/cil/oc2/api/device/object/NamedDevice.java b/src/main/java/li/cil/oc2/api/device/object/NamedDevice.java index 76bf9bea..c09cfce2 100644 --- a/src/main/java/li/cil/oc2/api/device/object/NamedDevice.java +++ b/src/main/java/li/cil/oc2/api/device/object/NamedDevice.java @@ -6,14 +6,14 @@ import net.minecraft.tileentity.TileEntity; import java.util.Collection; /** - * This interface is used to declare additional type names for a device on targets of an {@link ObjectDevice}. + * This interface is used to declare additional type names for a device on targets of an {@link ObjectDeviceInterface}. *

* In particular {@link Block}s and {@link TileEntity}s that contain {@link Callback} methods may implement * this interface to provide additional type names. */ public interface NamedDevice { /** - * A list of additional type names to associate with any {@link ObjectDevice} used + * A list of additional type names to associate with any {@link ObjectDeviceInterface} used * to make available methods in an instance of a class implementing this interface. * * @return the list of additional type names. diff --git a/src/main/java/li/cil/oc2/api/device/object/ObjectDevice.java b/src/main/java/li/cil/oc2/api/device/object/ObjectDeviceInterface.java similarity index 79% rename from src/main/java/li/cil/oc2/api/device/object/ObjectDevice.java rename to src/main/java/li/cil/oc2/api/device/object/ObjectDeviceInterface.java index c1889025..a809bf6f 100644 --- a/src/main/java/li/cil/oc2/api/device/object/ObjectDevice.java +++ b/src/main/java/li/cil/oc2/api/device/object/ObjectDeviceInterface.java @@ -1,6 +1,6 @@ package li.cil.oc2.api.device.object; -import li.cil.oc2.api.device.Device; +import li.cil.oc2.api.device.DeviceInterface; import li.cil.oc2.api.device.DeviceMethod; import javax.annotation.Nullable; @@ -9,11 +9,11 @@ import java.util.Collections; import java.util.List; /** - * A reflection based implementation of {@link Device} using the {@link Callback} - * annotation to discover {@link DeviceMethod} in a target object via + * A reflection based implementation of {@link DeviceInterface} using the {@link Callback} + * annotation to discover {@link DeviceMethod}s in a target object via * {@link Callbacks#collectMethods(Object)}. */ -public final class ObjectDevice implements Device { +public final class ObjectDeviceInterface implements DeviceInterface { private final Object object; private final ArrayList typeNames; private final List methods; @@ -26,7 +26,7 @@ public final class ObjectDevice implements Device { * @param object the object containing methods provided by this device. * @param typeNames the type names of the device. */ - public ObjectDevice(final Object object, final List typeNames) { + public ObjectDeviceInterface(final Object object, final List typeNames) { this.object = object; this.typeNames = new ArrayList<>(typeNames); this.methods = Callbacks.collectMethods(object); @@ -40,12 +40,12 @@ public final class ObjectDevice implements Device { /** * Creates a new object device with methods in the specified object and the specified * type name. For convenience, the type name may be {@code null}, in which case using - * this constructor is equivalent to using {@link #ObjectDevice(Object)}. + * this constructor is equivalent to using {@link #ObjectDeviceInterface(Object)}. * * @param object the object containing methods provided by this device. * @param typeName the type name of the device. */ - public ObjectDevice(final Object object, @Nullable final String typeName) { + public ObjectDeviceInterface(final Object object, @Nullable final String typeName) { this(object, typeName != null ? Collections.singletonList(typeName) : Collections.emptyList()); } @@ -54,7 +54,7 @@ public final class ObjectDevice implements Device { * * @param object the object containing the methods provided by this device. */ - public ObjectDevice(final Object object) { + public ObjectDeviceInterface(final Object object) { this(object, Collections.emptyList()); } @@ -72,7 +72,7 @@ public final class ObjectDevice implements Device { public boolean equals(@Nullable final Object o) { if (this == o) return true; if (o == null || getClass() != o.getClass()) return false; - final ObjectDevice that = (ObjectDevice) o; + final ObjectDeviceInterface that = (ObjectDeviceInterface) o; return object.equals(that.object); } diff --git a/src/main/java/li/cil/oc2/api/device/provider/DeviceInterfaceProvider.java b/src/main/java/li/cil/oc2/api/device/provider/DeviceInterfaceProvider.java new file mode 100644 index 00000000..8ea40f83 --- /dev/null +++ b/src/main/java/li/cil/oc2/api/device/provider/DeviceInterfaceProvider.java @@ -0,0 +1,47 @@ +package li.cil.oc2.api.device.provider; + +import li.cil.oc2.api.device.DeviceInterface; +import li.cil.oc2.api.device.object.ObjectDeviceInterface; +import net.minecraftforge.common.util.LazyOptional; + +/** + * Allows querying for device interfaces given some context. + *

+ * See the specializations of {@link DeviceQuery} for possible queries. + *

+ * Returning a device interface does not transfer ownership of the device in terms + * of responsibility for persistence. Callers of this method will not attempt + * to persist objects returned by this method. It is the responsibility of the provider + * to ensure persistence where required. + *

+ *

+ * Providers can be registered with the IMC message {@link li.cil.oc2.api.API#IMC_ADD_DEVICE_PROVIDER}. + * + * @see DeviceInterface + * @see ObjectDeviceInterface + * @see DeviceQuery + * @see BlockDeviceQuery + */ +@FunctionalInterface +public interface DeviceInterfaceProvider { + /** + * Get a device for the specified query. + * + * @param query the query describing the object to get a {@link DeviceInterface} for. + * @return a device for the specified query, if available. + */ + LazyOptional getDeviceInterface(DeviceQuery query); +} diff --git a/src/main/java/li/cil/oc2/api/device/provider/DeviceProvider.java b/src/main/java/li/cil/oc2/api/device/provider/DeviceProvider.java deleted file mode 100644 index 17390bfe..00000000 --- a/src/main/java/li/cil/oc2/api/device/provider/DeviceProvider.java +++ /dev/null @@ -1,49 +0,0 @@ -package li.cil.oc2.api.device.provider; - -import li.cil.oc2.api.device.Device; -import li.cil.oc2.api.device.object.ObjectDevice; -import net.minecraftforge.common.util.LazyOptional; - -/** - * Allows querying for devices given some context. - *

- * See the specializations of {@link DeviceQuery} for possible queries. - *

- * Returning a device does not transfer ownership of the device in terms of - * responsibility for persistence. Callers of this method will not attempt - * to persist devices returned by this method. It is the responsibility of the provider - * to ensure persistence where required. Typically by returning devices that are - * themselves persisted objects such as {@link net.minecraft.tileentity.TileEntity}s - * or storing data in a related persisted object. - *

- *

- * Providers can be registered with the IMC message {@link li.cil.oc2.api.API#IMC_ADD_DEVICE_PROVIDER}. - * - * @see DeviceQuery - * @see BlockDeviceQuery - * @see ObjectDevice - */ -@FunctionalInterface -public interface DeviceProvider { - /** - * Get a device for the specified query. - * - * @param query the query describing the object to get a {@link Device} for. - * @return a device for the specified query, if available. - */ - LazyOptional getDevice(DeviceQuery query); -} diff --git a/src/main/java/li/cil/oc2/api/device/provider/DeviceQuery.java b/src/main/java/li/cil/oc2/api/device/provider/DeviceQuery.java index f53d4b68..ec618696 100644 --- a/src/main/java/li/cil/oc2/api/device/provider/DeviceQuery.java +++ b/src/main/java/li/cil/oc2/api/device/provider/DeviceQuery.java @@ -1,9 +1,9 @@ package li.cil.oc2.api.device.provider; /** - * Base interface for all queries to {@link DeviceProvider}s. + * Base interface for all queries to {@link DeviceInterfaceProvider}s. * - * @see DeviceProvider + * @see DeviceInterfaceProvider * @see BlockDeviceQuery */ public interface DeviceQuery { diff --git a/src/main/java/li/cil/oc2/common/CommonSetup.java b/src/main/java/li/cil/oc2/common/CommonSetup.java index b9afe7f1..2f03803f 100644 --- a/src/main/java/li/cil/oc2/common/CommonSetup.java +++ b/src/main/java/li/cil/oc2/common/CommonSetup.java @@ -2,7 +2,7 @@ package li.cil.oc2.common; import li.cil.oc2.common.capabilities.DeviceBusElementCapability; import li.cil.oc2.common.device.DeviceMethodParameterTypeAdapters; -import li.cil.oc2.common.device.Providers; +import li.cil.oc2.common.device.provider.Providers; import li.cil.oc2.common.network.Network; import li.cil.oc2.common.vm.Allocator; import li.cil.oc2.serialization.BlobStorage; diff --git a/src/main/java/li/cil/oc2/common/IMC.java b/src/main/java/li/cil/oc2/common/IMC.java index b91a6f90..9900bde1 100644 --- a/src/main/java/li/cil/oc2/common/IMC.java +++ b/src/main/java/li/cil/oc2/common/IMC.java @@ -1,10 +1,10 @@ package li.cil.oc2.common; import li.cil.oc2.api.API; -import li.cil.oc2.api.device.provider.DeviceProvider; +import li.cil.oc2.api.device.provider.DeviceInterfaceProvider; import li.cil.oc2.api.imc.DeviceMethodParameterTypeAdapter; import li.cil.oc2.common.device.DeviceMethodParameterTypeAdapters; -import li.cil.oc2.common.device.Providers; +import li.cil.oc2.common.device.provider.Providers; import net.minecraft.util.Util; import net.minecraftforge.fml.InterModComms; import net.minecraftforge.fml.event.lifecycle.InterModProcessEvent; @@ -39,7 +39,7 @@ public final class IMC { } private static void addDeviceProvider(final InterModComms.IMCMessage message) { - getMessageParameter(message, DeviceProvider.class).ifPresent(Providers::addProvider); + getMessageParameter(message, DeviceInterfaceProvider.class).ifPresent(Providers::addProvider); } private static void addDeviceMethodParameterTypeAdapter(final InterModComms.IMCMessage message) { diff --git a/src/main/java/li/cil/oc2/common/bus/DeviceBusControllerImpl.java b/src/main/java/li/cil/oc2/common/bus/DeviceBusControllerImpl.java index 12b17112..8f2ce688 100644 --- a/src/main/java/li/cil/oc2/common/bus/DeviceBusControllerImpl.java +++ b/src/main/java/li/cil/oc2/common/bus/DeviceBusControllerImpl.java @@ -5,10 +5,10 @@ import com.google.gson.JsonArray; import li.cil.ceres.api.Serialized; import li.cil.oc2.api.bus.DeviceBusController; import li.cil.oc2.api.bus.DeviceBusElement; -import li.cil.oc2.api.device.Device; +import li.cil.oc2.api.device.DeviceInterface; import li.cil.oc2.api.device.DeviceMethod; import li.cil.oc2.api.device.DeviceMethodParameter; -import li.cil.oc2.api.device.IdentifiableDevice; +import li.cil.oc2.api.device.Device; import li.cil.oc2.common.capabilities.Capabilities; import li.cil.oc2.common.device.DeviceMethodParameterTypeAdapters; import li.cil.oc2.serialization.serializers.DeviceJsonSerializer; @@ -49,7 +49,7 @@ public class DeviceBusControllerImpl implements DeviceBusController, Steppable { public static final String ERROR_INVALID_PARAMETER_SIGNATURE = "invalid parameter signature"; private final Set elements = new HashSet<>(); - private final ConcurrentHashMap devices = new ConcurrentHashMap<>(); + private final ConcurrentHashMap devices = new ConcurrentHashMap<>(); private final SerialDevice serialDevice; private final Gson gson; @@ -69,7 +69,7 @@ public class DeviceBusControllerImpl implements DeviceBusController, Steppable { this.gson = DeviceMethodParameterTypeAdapters.beginBuildGson() .registerTypeAdapter(MethodInvocation.class, new MethodInvocationJsonDeserializer()) .registerTypeAdapter(Message.class, new MessageJsonDeserializer()) - .registerTypeAdapter(Device.class, new DeviceJsonSerializer()) + .registerTypeAdapter(DeviceInterface.class, new DeviceJsonSerializer()) .registerTypeAdapter(DeviceMethod.class, new DeviceMethodJsonSerializer()) .create(); } @@ -91,22 +91,22 @@ public class DeviceBusControllerImpl implements DeviceBusController, Steppable { public void scanDevices() { devices.clear(); - final HashMap> groupedDevices = new HashMap<>(); + final HashMap> groupedDevices = new HashMap<>(); for (final DeviceBusElement element : elements) { - for (final IdentifiableDevice device : element.getLocalDevices()) { + for (final Device device : element.getLocalDevices()) { groupedDevices.computeIfAbsent(device.getIdentifiedDevice(), d -> new ArrayList<>()).add(device); } } - for (final ArrayList group : groupedDevices.values()) { - final IdentifiableDevice device = selectDeviceDeterministically(group); + for (final ArrayList group : groupedDevices.values()) { + final Device device = selectDeviceDeterministically(group); devices.putIfAbsent(device.getUniqueIdentifier(), device); } } @Override - public Collection getDevices() { + public Collection getDevices() { return devices.values(); } @@ -208,10 +208,10 @@ public class DeviceBusControllerImpl implements DeviceBusController, Steppable { writeToDevice(); } - private static IdentifiableDevice selectDeviceDeterministically(final ArrayList devices) { - IdentifiableDevice deviceWithLowestUuid = devices.get(0); + private static Device selectDeviceDeterministically(final ArrayList devices) { + Device deviceWithLowestUuid = devices.get(0); for (int i = 1; i < devices.size(); i++) { - final IdentifiableDevice device = devices.get(i); + final Device device = devices.get(i); if (device.getUniqueIdentifier().compareTo(deviceWithLowestUuid.getUniqueIdentifier()) < 0) { deviceWithLowestUuid = device; } @@ -293,7 +293,7 @@ public class DeviceBusControllerImpl implements DeviceBusController, Steppable { } private void processMethodInvocation(final MethodInvocation methodInvocation, final boolean isMainThread) { - final Device device = devices.get(methodInvocation.deviceId); + final DeviceInterface device = devices.get(methodInvocation.deviceId); if (device == null) { writeError(ERROR_UNKNOWN_DEVICE); return; @@ -346,7 +346,7 @@ public class DeviceBusControllerImpl implements DeviceBusController, Steppable { } private void writeStatus() { - writeMessage(Message.MESSAGE_TYPE_STATUS, devices.values().toArray(new Device[0])); + writeMessage(Message.MESSAGE_TYPE_STATUS, devices.values().toArray(new DeviceInterface[0])); } private void writeError(final String message) { diff --git a/src/main/java/li/cil/oc2/common/bus/DeviceBusElementImpl.java b/src/main/java/li/cil/oc2/common/bus/DeviceBusElementImpl.java index 20dd783a..cc4faba8 100644 --- a/src/main/java/li/cil/oc2/common/bus/DeviceBusElementImpl.java +++ b/src/main/java/li/cil/oc2/common/bus/DeviceBusElementImpl.java @@ -2,7 +2,7 @@ package li.cil.oc2.common.bus; import li.cil.oc2.api.bus.DeviceBusController; import li.cil.oc2.api.bus.DeviceBusElement; -import li.cil.oc2.api.device.IdentifiableDevice; +import li.cil.oc2.api.device.Device; import javax.annotation.Nullable; import java.util.ArrayList; @@ -11,7 +11,7 @@ import java.util.List; import java.util.Optional; public final class DeviceBusElementImpl implements DeviceBusElement { - private final List devices = new ArrayList<>(); + private final List devices = new ArrayList<>(); @Nullable private DeviceBusController controller; @Override @@ -24,12 +24,12 @@ public final class DeviceBusElementImpl implements DeviceBusElement { } @Override - public Collection getLocalDevices() { + public Collection getLocalDevices() { return devices; } @Override - public void addDevice(final IdentifiableDevice device) { + public void addDevice(final Device device) { devices.add(device); if (controller != null) { controller.scanDevices(); @@ -37,7 +37,7 @@ public final class DeviceBusElementImpl implements DeviceBusElement { } @Override - public void removeDevice(final IdentifiableDevice device) { + public void removeDevice(final Device device) { devices.remove(device); if (controller != null) { controller.scanDevices(); @@ -45,7 +45,7 @@ public final class DeviceBusElementImpl implements DeviceBusElement { } @Override - public Collection getDevices() { + public Collection getDevices() { if (controller != null) { return controller.getDevices(); } else { diff --git a/src/main/java/li/cil/oc2/common/bus/TileEntityDeviceBusElement.java b/src/main/java/li/cil/oc2/common/bus/TileEntityDeviceBusElement.java index 822c3cdb..017e8444 100644 --- a/src/main/java/li/cil/oc2/common/bus/TileEntityDeviceBusElement.java +++ b/src/main/java/li/cil/oc2/common/bus/TileEntityDeviceBusElement.java @@ -4,9 +4,9 @@ import li.cil.oc2.api.bus.DeviceBus; import li.cil.oc2.api.bus.DeviceBusElement; import li.cil.oc2.common.ServerScheduler; import li.cil.oc2.common.capabilities.Capabilities; -import li.cil.oc2.common.device.CompoundDevice; -import li.cil.oc2.common.device.IdentifiableDeviceImpl; -import li.cil.oc2.common.device.Providers; +import li.cil.oc2.common.device.DeviceInterfaceCollection; +import li.cil.oc2.common.device.DeviceImpl; +import li.cil.oc2.common.device.provider.Providers; import li.cil.oc2.common.util.NBTTagIds; import li.cil.oc2.common.util.WorldUtils; import net.minecraft.nbt.CompoundNBT; @@ -31,7 +31,7 @@ public final class TileEntityDeviceBusElement implements INBTSerializable device = Providers.getDevice(world, pos, direction); - final IdentifiableDeviceImpl identifiableDevice; + final LazyOptional device = Providers.getDevice(world, pos, direction); + final DeviceImpl identifiableDevice; if (device.isPresent()) { final String typeName = WorldUtils.getBlockName(world, pos); - identifiableDevice = new IdentifiableDeviceImpl(device, deviceIds[index], typeName); + identifiableDevice = new DeviceImpl(device, deviceIds[index], typeName); device.addListener((ignored) -> handleNeighborChanged(pos)); } else { identifiableDevice = null; diff --git a/src/main/java/li/cil/oc2/common/device/IdentifiableDeviceImpl.java b/src/main/java/li/cil/oc2/common/device/DeviceImpl.java similarity index 54% rename from src/main/java/li/cil/oc2/common/device/IdentifiableDeviceImpl.java rename to src/main/java/li/cil/oc2/common/device/DeviceImpl.java index a12649c4..e9337260 100644 --- a/src/main/java/li/cil/oc2/common/device/IdentifiableDeviceImpl.java +++ b/src/main/java/li/cil/oc2/common/device/DeviceImpl.java @@ -1,25 +1,25 @@ package li.cil.oc2.common.device; import li.cil.oc2.api.device.Device; +import li.cil.oc2.api.device.DeviceInterface; import li.cil.oc2.api.device.DeviceMethod; -import li.cil.oc2.api.device.IdentifiableDevice; import li.cil.oc2.common.util.LazyOptionalUtils; import net.minecraftforge.common.util.LazyOptional; import javax.annotation.Nullable; import java.util.*; -public final class IdentifiableDeviceImpl implements IdentifiableDevice { - private final LazyOptional device; +public final class DeviceImpl implements Device { + private final LazyOptional deviceInterface; private final UUID uuid; @Nullable private final String typeName; - public IdentifiableDeviceImpl(final LazyOptional device, final UUID uuid) { - this(device, uuid, null); + public DeviceImpl(final LazyOptional deviceInterface, final UUID uuid) { + this(deviceInterface, uuid, null); } - public IdentifiableDeviceImpl(final LazyOptional device, final UUID uuid, @Nullable final String typeName) { - this.device = device; + public DeviceImpl(final LazyOptional deviceInterface, final UUID uuid, @Nullable final String typeName) { + this.deviceInterface = deviceInterface.cast(); this.uuid = uuid; this.typeName = typeName; } @@ -30,38 +30,38 @@ public final class IdentifiableDeviceImpl implements IdentifiableDevice { } @Override - public Device getIdentifiedDevice() { - return device.cast().orElse(this); + public DeviceInterface getIdentifiedDevice() { + return deviceInterface.orElse(this); } @Override public List getTypeNames() { if (typeName != null) { - final List typeNames = new ArrayList<>(device.map(Device::getTypeNames).orElse(Collections.emptyList())); + final List typeNames = new ArrayList<>(deviceInterface.map(DeviceInterface::getTypeNames).orElse(Collections.emptyList())); typeNames.add(typeName); return typeNames; } else { - return device.map(Device::getTypeNames).orElse(Collections.emptyList()); + return deviceInterface.map(DeviceInterface::getTypeNames).orElse(Collections.emptyList()); } } @Override public List getMethods() { - return device.map(Device::getMethods).orElse(Collections.emptyList()); + return deviceInterface.map(DeviceInterface::getMethods).orElse(Collections.emptyList()); } @Override public boolean equals(final Object o) { if (this == o) return true; if (o == null || getClass() != o.getClass()) return false; - final IdentifiableDeviceImpl that = (IdentifiableDeviceImpl) o; + final DeviceImpl that = (DeviceImpl) o; return uuid.equals(that.uuid) && - LazyOptionalUtils.equals(device, that.device) && + LazyOptionalUtils.equals(deviceInterface, that.deviceInterface) && Objects.equals(typeName, that.typeName); } @Override public int hashCode() { - return Objects.hash(uuid, LazyOptionalUtils.hashCode(device), typeName); + return Objects.hash(uuid, LazyOptionalUtils.hashCode(deviceInterface), typeName); } } diff --git a/src/main/java/li/cil/oc2/common/device/CompoundDevice.java b/src/main/java/li/cil/oc2/common/device/DeviceInterfaceCollection.java similarity index 52% rename from src/main/java/li/cil/oc2/common/device/CompoundDevice.java rename to src/main/java/li/cil/oc2/common/device/DeviceInterfaceCollection.java index 1727b0b3..9948402c 100644 --- a/src/main/java/li/cil/oc2/common/device/CompoundDevice.java +++ b/src/main/java/li/cil/oc2/common/device/DeviceInterfaceCollection.java @@ -1,6 +1,6 @@ package li.cil.oc2.common.device; -import li.cil.oc2.api.device.Device; +import li.cil.oc2.api.device.DeviceInterface; import li.cil.oc2.api.device.DeviceMethod; import java.util.ArrayList; @@ -9,25 +9,25 @@ import java.util.List; import java.util.Objects; import java.util.stream.Collectors; -public final class CompoundDevice implements Device { - private final ArrayList devices; +public final class DeviceInterfaceCollection implements DeviceInterface { + private final ArrayList deviceInterfaces; - public CompoundDevice(final ArrayList devices) { - this.devices = devices; + public DeviceInterfaceCollection(final ArrayList deviceInterfaces) { + this.deviceInterfaces = deviceInterfaces; } @Override public List getTypeNames() { - return devices.stream() - .map(Device::getTypeNames) + return deviceInterfaces.stream() + .map(DeviceInterface::getTypeNames) .flatMap(Collection::stream) .collect(Collectors.toList()); } @Override public List getMethods() { - return devices.stream() - .map(Device::getMethods) + return deviceInterfaces.stream() + .map(DeviceInterface::getMethods) .flatMap(Collection::stream) .collect(Collectors.toList()); } @@ -36,12 +36,12 @@ public final class CompoundDevice implements Device { public boolean equals(final Object o) { if (this == o) return true; if (o == null || getClass() != o.getClass()) return false; - final CompoundDevice that = (CompoundDevice) o; - return devices.equals(that.devices); + final DeviceInterfaceCollection that = (DeviceInterfaceCollection) o; + return deviceInterfaces.equals(that.deviceInterfaces); } @Override public int hashCode() { - return Objects.hash(devices); + return Objects.hash(deviceInterfaces); } } diff --git a/src/main/java/li/cil/oc2/common/device/Providers.java b/src/main/java/li/cil/oc2/common/device/Providers.java deleted file mode 100644 index 583d1bce..00000000 --- a/src/main/java/li/cil/oc2/common/device/Providers.java +++ /dev/null @@ -1,66 +0,0 @@ -package li.cil.oc2.common.device; - -import li.cil.oc2.api.device.Device; -import li.cil.oc2.api.device.provider.DeviceProvider; -import li.cil.oc2.api.device.provider.DeviceQuery; -import li.cil.oc2.common.device.provider.*; -import net.minecraft.tileentity.TileEntity; -import net.minecraft.util.Direction; -import net.minecraft.util.math.BlockPos; -import net.minecraft.world.World; -import net.minecraftforge.common.util.LazyOptional; - -import java.util.ArrayList; - -public final class Providers { - private static final ArrayList DEVICE_PROVIDERS = new ArrayList<>(); - - public static void initialize() { - addProvider(new EnergyStorageDeviceProvider()); - addProvider(new FluidHandlerDeviceProvider()); - addProvider(new ItemHandlerDeviceProvider()); - addProvider(new TileEntityDeviceProvider()); - addProvider(new BlockDeviceProvider()); - } - - public static void addProvider(final DeviceProvider provider) { - if (!DEVICE_PROVIDERS.contains(provider)) { - DEVICE_PROVIDERS.add(provider); - } - } - - public static LazyOptional getDevice(final TileEntity tileEntity, final Direction side) { - final World world = tileEntity.getWorld(); - final BlockPos pos = tileEntity.getPos(); - - if (world == null) throw new IllegalArgumentException(); - - return getDevice(world, pos, side); - } - - public static LazyOptional getDevice(final World world, final BlockPos pos, final Direction side) { - return getDevice(new BlockDeviceQueryImpl(world, pos, side)); - } - - public static LazyOptional getDevice(final DeviceQuery query) { - final ArrayList devices = new ArrayList<>(); - final ArrayList> optionals = new ArrayList<>(); - for (final DeviceProvider provider : DEVICE_PROVIDERS) { - final LazyOptional optional = provider.getDevice(query); - optional.ifPresent((device) -> { - devices.add(device); - optionals.add(optional); - }); - } - - if (devices.isEmpty()) { - return LazyOptional.empty(); - } else { - final LazyOptional compoundOptional = LazyOptional.of(() -> new CompoundDevice(devices)); - for (final LazyOptional optional : optionals) { - optional.addListener((ignored) -> compoundOptional.invalidate()); - } - return compoundOptional; - } - } -} diff --git a/src/main/java/li/cil/oc2/common/device/provider/AbstractCapabilityAnyTileEntityDeviceInterfaceProvider.java b/src/main/java/li/cil/oc2/common/device/provider/AbstractCapabilityAnyTileEntityDeviceInterfaceProvider.java new file mode 100644 index 00000000..82f5721f --- /dev/null +++ b/src/main/java/li/cil/oc2/common/device/provider/AbstractCapabilityAnyTileEntityDeviceInterfaceProvider.java @@ -0,0 +1,12 @@ +package li.cil.oc2.common.device.provider; + +import net.minecraft.tileentity.TileEntity; +import net.minecraftforge.common.capabilities.Capability; + +import java.util.function.Supplier; + +public abstract class AbstractCapabilityAnyTileEntityDeviceInterfaceProvider extends AbstractCapabilityTileEntityDeviceInterfaceProvider { + public AbstractCapabilityAnyTileEntityDeviceInterfaceProvider(final Supplier> capabilitySupplier) { + super(TileEntity.class, capabilitySupplier); + } +} diff --git a/src/main/java/li/cil/oc2/common/device/provider/AbstractCapabilityAnyTileEntityDeviceProvider.java b/src/main/java/li/cil/oc2/common/device/provider/AbstractCapabilityAnyTileEntityDeviceProvider.java deleted file mode 100644 index f8ca6daa..00000000 --- a/src/main/java/li/cil/oc2/common/device/provider/AbstractCapabilityAnyTileEntityDeviceProvider.java +++ /dev/null @@ -1,12 +0,0 @@ -package li.cil.oc2.common.device.provider; - -import net.minecraft.tileentity.TileEntity; -import net.minecraftforge.common.capabilities.Capability; - -import java.util.function.Supplier; - -public abstract class AbstractCapabilityAnyTileEntityDeviceProvider extends AbstractCapabilityTileEntityDeviceProvider { - public AbstractCapabilityAnyTileEntityDeviceProvider(final Supplier> capabilitySupplier) { - super(TileEntity.class, capabilitySupplier); - } -} diff --git a/src/main/java/li/cil/oc2/common/device/provider/AbstractCapabilityTileEntityDeviceProvider.java b/src/main/java/li/cil/oc2/common/device/provider/AbstractCapabilityTileEntityDeviceInterfaceProvider.java similarity index 56% rename from src/main/java/li/cil/oc2/common/device/provider/AbstractCapabilityTileEntityDeviceProvider.java rename to src/main/java/li/cil/oc2/common/device/provider/AbstractCapabilityTileEntityDeviceInterfaceProvider.java index f37b2fa8..88e9b695 100644 --- a/src/main/java/li/cil/oc2/common/device/provider/AbstractCapabilityTileEntityDeviceProvider.java +++ b/src/main/java/li/cil/oc2/common/device/provider/AbstractCapabilityTileEntityDeviceInterfaceProvider.java @@ -1,6 +1,6 @@ package li.cil.oc2.common.device.provider; -import li.cil.oc2.api.device.Device; +import li.cil.oc2.api.device.DeviceInterface; import li.cil.oc2.api.device.provider.BlockDeviceQuery; import net.minecraft.tileentity.TileEntity; import net.minecraftforge.common.capabilities.Capability; @@ -8,16 +8,16 @@ import net.minecraftforge.common.util.LazyOptional; import java.util.function.Supplier; -public abstract class AbstractCapabilityTileEntityDeviceProvider extends AbstractTileEntityDeviceProvider { +public abstract class AbstractCapabilityTileEntityDeviceInterfaceProvider extends AbstractTileEntityDeviceInterfaceProvider { private final Supplier> capabilitySupplier; - protected AbstractCapabilityTileEntityDeviceProvider(final Class tileEntityType, final Supplier> capabilitySupplier) { + protected AbstractCapabilityTileEntityDeviceInterfaceProvider(final Class tileEntityType, final Supplier> capabilitySupplier) { super(tileEntityType); this.capabilitySupplier = capabilitySupplier; } @Override - protected final LazyOptional getDevice(final BlockDeviceQuery blockQuery, final TileEntity tileEntity) { + protected final LazyOptional getDeviceInterface(final BlockDeviceQuery blockQuery, final TileEntity tileEntity) { final Capability capability = capabilitySupplier.get(); if (capability == null) throw new IllegalStateException(); final LazyOptional optional = tileEntity.getCapability(capability, blockQuery.getQuerySide()); @@ -26,10 +26,10 @@ public abstract class AbstractCapabilityTileEntityDeviceProvider device = getDevice(blockQuery, value); + final LazyOptional device = getDeviceInterface(blockQuery, value); optional.addListener(ignored -> device.invalidate()); return device; } - protected abstract LazyOptional getDevice(final BlockDeviceQuery query, final TCapability value); + protected abstract LazyOptional getDeviceInterface(final BlockDeviceQuery query, final TCapability value); } diff --git a/src/main/java/li/cil/oc2/common/device/provider/AbstractTileEntityDeviceProvider.java b/src/main/java/li/cil/oc2/common/device/provider/AbstractTileEntityDeviceInterfaceProvider.java similarity index 57% rename from src/main/java/li/cil/oc2/common/device/provider/AbstractTileEntityDeviceProvider.java rename to src/main/java/li/cil/oc2/common/device/provider/AbstractTileEntityDeviceInterfaceProvider.java index dda21918..0428ef32 100644 --- a/src/main/java/li/cil/oc2/common/device/provider/AbstractTileEntityDeviceProvider.java +++ b/src/main/java/li/cil/oc2/common/device/provider/AbstractTileEntityDeviceInterfaceProvider.java @@ -1,22 +1,22 @@ package li.cil.oc2.common.device.provider; -import li.cil.oc2.api.device.Device; +import li.cil.oc2.api.device.DeviceInterface; import li.cil.oc2.api.device.provider.BlockDeviceQuery; -import li.cil.oc2.api.device.provider.DeviceProvider; +import li.cil.oc2.api.device.provider.DeviceInterfaceProvider; import li.cil.oc2.api.device.provider.DeviceQuery; import net.minecraft.tileentity.TileEntity; import net.minecraftforge.common.util.LazyOptional; -public abstract class AbstractTileEntityDeviceProvider implements DeviceProvider { +public abstract class AbstractTileEntityDeviceInterfaceProvider implements DeviceInterfaceProvider { private final Class tileEntityType; - protected AbstractTileEntityDeviceProvider(final Class tileEntityType) { + protected AbstractTileEntityDeviceInterfaceProvider(final Class tileEntityType) { this.tileEntityType = tileEntityType; } @SuppressWarnings("unchecked") @Override - public LazyOptional getDevice(final DeviceQuery query) { + public LazyOptional getDeviceInterface(final DeviceQuery query) { if (!(query instanceof BlockDeviceQuery)) { return LazyOptional.empty(); } @@ -27,8 +27,8 @@ public abstract class AbstractTileEntityDeviceProvider imp return LazyOptional.empty(); } - return getDevice(blockQuery, (T) tileEntity); + return getDeviceInterface(blockQuery, (T) tileEntity); } - protected abstract LazyOptional getDevice(final BlockDeviceQuery query, final T tileEntity); + protected abstract LazyOptional getDeviceInterface(final BlockDeviceQuery query, final T tileEntity); } diff --git a/src/main/java/li/cil/oc2/common/device/provider/BlockDeviceProvider.java b/src/main/java/li/cil/oc2/common/device/provider/BlockDeviceInterfaceProvider.java similarity index 71% rename from src/main/java/li/cil/oc2/common/device/provider/BlockDeviceProvider.java rename to src/main/java/li/cil/oc2/common/device/provider/BlockDeviceInterfaceProvider.java index 9a281bf1..e2e8afc6 100644 --- a/src/main/java/li/cil/oc2/common/device/provider/BlockDeviceProvider.java +++ b/src/main/java/li/cil/oc2/common/device/provider/BlockDeviceInterfaceProvider.java @@ -1,19 +1,19 @@ package li.cil.oc2.common.device.provider; -import li.cil.oc2.api.device.Device; +import li.cil.oc2.api.device.DeviceInterface; import li.cil.oc2.api.device.object.Callbacks; -import li.cil.oc2.api.device.object.ObjectDevice; +import li.cil.oc2.api.device.object.ObjectDeviceInterface; import li.cil.oc2.api.device.provider.BlockDeviceQuery; -import li.cil.oc2.api.device.provider.DeviceProvider; +import li.cil.oc2.api.device.provider.DeviceInterfaceProvider; import li.cil.oc2.api.device.provider.DeviceQuery; import li.cil.oc2.common.util.WorldUtils; import net.minecraft.block.Block; import net.minecraft.block.BlockState; import net.minecraftforge.common.util.LazyOptional; -public class BlockDeviceProvider implements DeviceProvider { +public class BlockDeviceInterfaceProvider implements DeviceInterfaceProvider { @Override - public LazyOptional getDevice(final DeviceQuery query) { + public LazyOptional getDeviceInterface(final DeviceQuery query) { if (!(query instanceof BlockDeviceQuery)) { return LazyOptional.empty(); } @@ -30,6 +30,6 @@ public class BlockDeviceProvider implements DeviceProvider { } final String typeName = WorldUtils.getBlockName(blockQuery.getWorld(), blockQuery.getQueryPosition()); - return LazyOptional.of(() -> new ObjectDevice(block, typeName)); + return LazyOptional.of(() -> new ObjectDeviceInterface(block, typeName)); } } diff --git a/src/main/java/li/cil/oc2/common/device/provider/EnergyStorageDeviceProvider.java b/src/main/java/li/cil/oc2/common/device/provider/EnergyStorageDeviceInterfaceProvider.java similarity index 67% rename from src/main/java/li/cil/oc2/common/device/provider/EnergyStorageDeviceProvider.java rename to src/main/java/li/cil/oc2/common/device/provider/EnergyStorageDeviceInterfaceProvider.java index 819ed146..773b64ad 100644 --- a/src/main/java/li/cil/oc2/common/device/provider/EnergyStorageDeviceProvider.java +++ b/src/main/java/li/cil/oc2/common/device/provider/EnergyStorageDeviceInterfaceProvider.java @@ -1,23 +1,23 @@ package li.cil.oc2.common.device.provider; -import li.cil.oc2.api.device.Device; +import li.cil.oc2.api.device.DeviceInterface; import li.cil.oc2.api.device.object.Callback; -import li.cil.oc2.api.device.object.ObjectDevice; +import li.cil.oc2.api.device.object.ObjectDeviceInterface; import li.cil.oc2.api.device.provider.BlockDeviceQuery; import li.cil.oc2.common.capabilities.Capabilities; import net.minecraftforge.common.util.LazyOptional; import net.minecraftforge.energy.IEnergyStorage; -public class EnergyStorageDeviceProvider extends AbstractCapabilityAnyTileEntityDeviceProvider { +public class EnergyStorageDeviceInterfaceProvider extends AbstractCapabilityAnyTileEntityDeviceInterfaceProvider { private static final String ENERGY_STORAGE_TYPE_NAME = "energyStorage"; - public EnergyStorageDeviceProvider() { + public EnergyStorageDeviceInterfaceProvider() { super(() -> Capabilities.ENERGY_STORAGE_CAPABILITY); } @Override - protected LazyOptional getDevice(final BlockDeviceQuery query, final IEnergyStorage value) { - return LazyOptional.of(() -> new ObjectDevice(new EnergyStorageDevice(value), ENERGY_STORAGE_TYPE_NAME)); + protected LazyOptional getDeviceInterface(final BlockDeviceQuery query, final IEnergyStorage value) { + return LazyOptional.of(() -> new ObjectDeviceInterface(new EnergyStorageDevice(value), ENERGY_STORAGE_TYPE_NAME)); } public static final class EnergyStorageDevice extends AbstractObjectProxy { diff --git a/src/main/java/li/cil/oc2/common/device/provider/FluidHandlerDeviceProvider.java b/src/main/java/li/cil/oc2/common/device/provider/FluidHandlerDeviceInterfaceProvider.java similarity index 67% rename from src/main/java/li/cil/oc2/common/device/provider/FluidHandlerDeviceProvider.java rename to src/main/java/li/cil/oc2/common/device/provider/FluidHandlerDeviceInterfaceProvider.java index e0a9c26a..cdcddeda 100644 --- a/src/main/java/li/cil/oc2/common/device/provider/FluidHandlerDeviceProvider.java +++ b/src/main/java/li/cil/oc2/common/device/provider/FluidHandlerDeviceInterfaceProvider.java @@ -1,24 +1,24 @@ package li.cil.oc2.common.device.provider; -import li.cil.oc2.api.device.Device; +import li.cil.oc2.api.device.DeviceInterface; import li.cil.oc2.api.device.object.Callback; -import li.cil.oc2.api.device.object.ObjectDevice; +import li.cil.oc2.api.device.object.ObjectDeviceInterface; import li.cil.oc2.api.device.provider.BlockDeviceQuery; import li.cil.oc2.common.capabilities.Capabilities; import net.minecraftforge.common.util.LazyOptional; import net.minecraftforge.fluids.FluidStack; import net.minecraftforge.fluids.capability.IFluidHandler; -public class FluidHandlerDeviceProvider extends AbstractCapabilityAnyTileEntityDeviceProvider { +public class FluidHandlerDeviceInterfaceProvider extends AbstractCapabilityAnyTileEntityDeviceInterfaceProvider { private static final String FLUID_HANDLER_TYPE_NAME = "fluidHandler"; - public FluidHandlerDeviceProvider() { + public FluidHandlerDeviceInterfaceProvider() { super(() -> Capabilities.FLUID_HANDLER_CAPABILITY); } @Override - protected LazyOptional getDevice(final BlockDeviceQuery query, final IFluidHandler value) { - return LazyOptional.of(() -> new ObjectDevice(new FluidHandlerDevice(value), FLUID_HANDLER_TYPE_NAME)); + protected LazyOptional getDeviceInterface(final BlockDeviceQuery query, final IFluidHandler value) { + return LazyOptional.of(() -> new ObjectDeviceInterface(new FluidHandlerDevice(value), FLUID_HANDLER_TYPE_NAME)); } public static final class FluidHandlerDevice extends AbstractObjectProxy { diff --git a/src/main/java/li/cil/oc2/common/device/provider/ItemHandlerDeviceProvider.java b/src/main/java/li/cil/oc2/common/device/provider/ItemHandlerDeviceInterfaceProvider.java similarity index 66% rename from src/main/java/li/cil/oc2/common/device/provider/ItemHandlerDeviceProvider.java rename to src/main/java/li/cil/oc2/common/device/provider/ItemHandlerDeviceInterfaceProvider.java index 9f41f095..cd0993ec 100644 --- a/src/main/java/li/cil/oc2/common/device/provider/ItemHandlerDeviceProvider.java +++ b/src/main/java/li/cil/oc2/common/device/provider/ItemHandlerDeviceInterfaceProvider.java @@ -1,24 +1,24 @@ package li.cil.oc2.common.device.provider; -import li.cil.oc2.api.device.Device; +import li.cil.oc2.api.device.DeviceInterface; import li.cil.oc2.api.device.object.Callback; -import li.cil.oc2.api.device.object.ObjectDevice; +import li.cil.oc2.api.device.object.ObjectDeviceInterface; import li.cil.oc2.api.device.provider.BlockDeviceQuery; import li.cil.oc2.common.capabilities.Capabilities; import net.minecraft.item.ItemStack; import net.minecraftforge.common.util.LazyOptional; import net.minecraftforge.items.IItemHandler; -public class ItemHandlerDeviceProvider extends AbstractCapabilityAnyTileEntityDeviceProvider { +public class ItemHandlerDeviceInterfaceProvider extends AbstractCapabilityAnyTileEntityDeviceInterfaceProvider { private static final String ITEM_HANDLER_TYPE_NAME = "itemHandler"; - public ItemHandlerDeviceProvider() { + public ItemHandlerDeviceInterfaceProvider() { super(() -> Capabilities.ITEM_HANDLER_CAPABILITY); } @Override - protected LazyOptional getDevice(final BlockDeviceQuery query, final IItemHandler value) { - return LazyOptional.of(() -> new ObjectDevice(new ItemHandlerDevice(value), ITEM_HANDLER_TYPE_NAME)); + protected LazyOptional getDeviceInterface(final BlockDeviceQuery query, final IItemHandler value) { + return LazyOptional.of(() -> new ObjectDeviceInterface(new ItemHandlerDevice(value), ITEM_HANDLER_TYPE_NAME)); } public static final class ItemHandlerDevice extends AbstractObjectProxy { diff --git a/src/main/java/li/cil/oc2/common/device/provider/Providers.java b/src/main/java/li/cil/oc2/common/device/provider/Providers.java new file mode 100644 index 00000000..48aaf67d --- /dev/null +++ b/src/main/java/li/cil/oc2/common/device/provider/Providers.java @@ -0,0 +1,68 @@ +package li.cil.oc2.common.device.provider; + +import li.cil.oc2.api.device.DeviceInterface; +import li.cil.oc2.api.device.provider.DeviceInterfaceProvider; +import li.cil.oc2.api.device.provider.DeviceQuery; +import li.cil.oc2.common.device.BlockDeviceQueryImpl; +import li.cil.oc2.common.device.DeviceInterfaceCollection; +import li.cil.oc2.common.device.provider.*; +import net.minecraft.tileentity.TileEntity; +import net.minecraft.util.Direction; +import net.minecraft.util.math.BlockPos; +import net.minecraft.world.World; +import net.minecraftforge.common.util.LazyOptional; + +import java.util.ArrayList; + +public final class Providers { + private static final ArrayList DEVICE_PROVIDERS = new ArrayList<>(); + + public static void initialize() { + addProvider(new EnergyStorageDeviceInterfaceProvider()); + addProvider(new FluidHandlerDeviceInterfaceProvider()); + addProvider(new ItemHandlerDeviceInterfaceProvider()); + addProvider(new TileEntityDeviceInterfaceProvider()); + addProvider(new BlockDeviceInterfaceProvider()); + } + + public static void addProvider(final DeviceInterfaceProvider provider) { + if (!DEVICE_PROVIDERS.contains(provider)) { + DEVICE_PROVIDERS.add(provider); + } + } + + public static LazyOptional getDevice(final TileEntity tileEntity, final Direction side) { + final World world = tileEntity.getWorld(); + final BlockPos pos = tileEntity.getPos(); + + if (world == null) throw new IllegalArgumentException(); + + return getDevice(world, pos, side); + } + + public static LazyOptional getDevice(final World world, final BlockPos pos, final Direction side) { + return getDevice(new BlockDeviceQueryImpl(world, pos, side)); + } + + public static LazyOptional getDevice(final DeviceQuery query) { + final ArrayList deviceInterfaces = new ArrayList<>(); + final ArrayList> optionals = new ArrayList<>(); + for (final DeviceInterfaceProvider provider : DEVICE_PROVIDERS) { + final LazyOptional optional = provider.getDeviceInterface(query); + optional.ifPresent((device) -> { + deviceInterfaces.add(device); + optionals.add(optional); + }); + } + + if (deviceInterfaces.isEmpty()) { + return LazyOptional.empty(); + } else { + final LazyOptional compoundOptional = LazyOptional.of(() -> new DeviceInterfaceCollection(deviceInterfaces)); + for (final LazyOptional optional : optionals) { + optional.addListener((ignored) -> compoundOptional.invalidate()); + } + return compoundOptional; + } + } +} diff --git a/src/main/java/li/cil/oc2/common/device/provider/TileEntityDeviceProvider.java b/src/main/java/li/cil/oc2/common/device/provider/TileEntityDeviceInterfaceProvider.java similarity index 57% rename from src/main/java/li/cil/oc2/common/device/provider/TileEntityDeviceProvider.java rename to src/main/java/li/cil/oc2/common/device/provider/TileEntityDeviceInterfaceProvider.java index 604e277a..dcb4e754 100644 --- a/src/main/java/li/cil/oc2/common/device/provider/TileEntityDeviceProvider.java +++ b/src/main/java/li/cil/oc2/common/device/provider/TileEntityDeviceInterfaceProvider.java @@ -1,24 +1,24 @@ package li.cil.oc2.common.device.provider; -import li.cil.oc2.api.device.Device; +import li.cil.oc2.api.device.DeviceInterface; import li.cil.oc2.api.device.object.Callbacks; -import li.cil.oc2.api.device.object.ObjectDevice; +import li.cil.oc2.api.device.object.ObjectDeviceInterface; import li.cil.oc2.api.device.provider.BlockDeviceQuery; import li.cil.oc2.common.util.WorldUtils; import net.minecraft.tileentity.TileEntity; import net.minecraftforge.common.util.LazyOptional; -public final class TileEntityDeviceProvider extends AbstractTileEntityDeviceProvider { - public TileEntityDeviceProvider() { +public final class TileEntityDeviceInterfaceProvider extends AbstractTileEntityDeviceInterfaceProvider { + public TileEntityDeviceInterfaceProvider() { super(TileEntity.class); } @Override - public LazyOptional getDevice(final BlockDeviceQuery query, final TileEntity tileEntity) { + public LazyOptional getDeviceInterface(final BlockDeviceQuery query, final TileEntity tileEntity) { if (Callbacks.hasMethods(tileEntity)) { return LazyOptional.of(() -> { final String typeName = WorldUtils.getBlockName(query.getWorld(), query.getQueryPosition()); - return new ObjectDevice(tileEntity, typeName); + return new ObjectDeviceInterface(tileEntity, typeName); }); } else { return LazyOptional.empty(); diff --git a/src/main/java/li/cil/oc2/serialization/serializers/DeviceJsonSerializer.java b/src/main/java/li/cil/oc2/serialization/serializers/DeviceJsonSerializer.java index 3c9c5fbb..ee37ec6a 100644 --- a/src/main/java/li/cil/oc2/serialization/serializers/DeviceJsonSerializer.java +++ b/src/main/java/li/cil/oc2/serialization/serializers/DeviceJsonSerializer.java @@ -2,13 +2,13 @@ package li.cil.oc2.serialization.serializers; import com.google.gson.*; import li.cil.oc2.api.device.DeviceMethod; -import li.cil.oc2.api.device.IdentifiableDevice; +import li.cil.oc2.api.device.Device; import java.lang.reflect.Type; -public final class DeviceJsonSerializer implements JsonSerializer { +public final class DeviceJsonSerializer implements JsonSerializer { @Override - public JsonElement serialize(final IdentifiableDevice src, final Type typeOfSrc, final JsonSerializationContext context) { + public JsonElement serialize(final Device src, final Type typeOfSrc, final JsonSerializationContext context) { if (src == null) { return JsonNull.INSTANCE; } diff --git a/src/test/java/li/cil/oc2/bus/DeviceBusTests.java b/src/test/java/li/cil/oc2/bus/DeviceBusTests.java index 480c821b..1902f0f5 100644 --- a/src/test/java/li/cil/oc2/bus/DeviceBusTests.java +++ b/src/test/java/li/cil/oc2/bus/DeviceBusTests.java @@ -1,7 +1,7 @@ package li.cil.oc2.bus; import li.cil.oc2.api.bus.DeviceBusElement; -import li.cil.oc2.api.device.IdentifiableDevice; +import li.cil.oc2.api.device.Device; import li.cil.oc2.common.bus.DeviceBusControllerImpl; import li.cil.oc2.common.capabilities.Capabilities; import li.cil.sedna.api.device.serial.SerialDevice; @@ -64,7 +64,7 @@ public class DeviceBusTests { final DeviceBusElement busElement = mock(DeviceBusElement.class); when(tileEntity.getCapability(eq(busElementCapability), any())).thenReturn(LazyOptional.of(() -> busElement)); - final IdentifiableDevice device = mock(IdentifiableDevice.class); + final Device device = mock(Device.class); when(busElement.getLocalDevices()).thenReturn(Collections.singletonList(device)); when(device.getUniqueIdentifier()).thenReturn(UUID.randomUUID()); diff --git a/src/test/java/li/cil/oc2/vm/ObjectDeviceProtocolTests.java b/src/test/java/li/cil/oc2/vm/ObjectDeviceProtocolTests.java index f2047b06..3bcdd0e6 100644 --- a/src/test/java/li/cil/oc2/vm/ObjectDeviceProtocolTests.java +++ b/src/test/java/li/cil/oc2/vm/ObjectDeviceProtocolTests.java @@ -4,14 +4,14 @@ import com.google.gson.*; import it.unimi.dsi.fastutil.bytes.ByteArrayFIFOQueue; import li.cil.oc2.api.bus.DeviceBusElement; import li.cil.oc2.api.device.DeviceMethod; -import li.cil.oc2.api.device.IdentifiableDevice; +import li.cil.oc2.api.device.Device; import li.cil.oc2.api.device.object.Callback; -import li.cil.oc2.api.device.object.ObjectDevice; +import li.cil.oc2.api.device.object.ObjectDeviceInterface; import li.cil.oc2.api.device.object.Parameter; import li.cil.oc2.common.bus.DeviceBusControllerImpl; import li.cil.oc2.common.bus.DeviceBusElementImpl; import li.cil.oc2.common.capabilities.Capabilities; -import li.cil.oc2.common.device.IdentifiableDeviceImpl; +import li.cil.oc2.common.device.DeviceImpl; import li.cil.sedna.api.device.serial.SerialDevice; import net.minecraft.tileentity.TileEntity; import net.minecraft.util.math.BlockPos; @@ -65,7 +65,7 @@ public class ObjectDeviceProtocolTests { public void resetAndReadDescriptor() { final VoidIntMethod method = new VoidIntMethod(); - busElement.addDevice(new TestDevice(method)); + busElement.addDevice(new TestDeviceInterface(method)); controller.scan(world, CONTROLLER_POS); final JsonObject request = new JsonObject(); @@ -90,7 +90,7 @@ public class ObjectDeviceProtocolTests { @Test public void simpleMethod() { final VoidIntMethod method = new VoidIntMethod(); - final TestDevice device = new TestDevice(method); + final TestDeviceInterface device = new TestDeviceInterface(method); busElement.addDevice(device); controller.scan(world, CONTROLLER_POS); @@ -103,7 +103,7 @@ public class ObjectDeviceProtocolTests { @Test public void returningMethod() { final IntLongMethod method = new IntLongMethod(); - final TestDevice device = new TestDevice(method); + final TestDeviceInterface device = new TestDeviceInterface(method); busElement.addDevice(device); controller.scan(world, CONTROLLER_POS); @@ -117,8 +117,8 @@ public class ObjectDeviceProtocolTests { @Test public void annotatedObject() { final SimpleObject object = new SimpleObject(); - final ObjectDevice device = new ObjectDevice(object); - final IdentifiableDeviceImpl identifiableDevice = new IdentifiableDeviceImpl(LazyOptional.of(() -> device), UUID.randomUUID()); + final ObjectDeviceInterface device = new ObjectDeviceInterface(object); + final DeviceImpl identifiableDevice = new DeviceImpl(LazyOptional.of(() -> device), UUID.randomUUID()); busElement.addDevice(identifiableDevice); controller.scan(world, CONTROLLER_POS); @@ -126,7 +126,7 @@ public class ObjectDeviceProtocolTests { Assertions.assertEquals(42 + 23, invokeMethod(identifiableDevice, "add", 42, 23).getAsInt()); } - private JsonElement invokeMethod(final IdentifiableDevice device, final String name, final Object... parameters) { + private JsonElement invokeMethod(final Device device, final String name, final Object... parameters) { final JsonObject request = new JsonObject(); request.addProperty("type", "invoke"); final JsonObject methodInvocation = new JsonObject(); @@ -243,12 +243,12 @@ public class ObjectDeviceProtocolTests { } } - private static final class TestDevice implements IdentifiableDevice { + private static final class TestDeviceInterface implements Device { private static final UUID UUID = java.util.UUID.randomUUID(); private final DeviceMethod method; - public TestDevice(final DeviceMethod method) { + public TestDeviceInterface(final DeviceMethod method) { this.method = method; }