From eea3eeef15635fcf13b2cc81d1e6394e60ab2a38 Mon Sep 17 00:00:00 2001 From: raoulvdberge Date: Mon, 1 Jan 2024 13:41:26 +0100 Subject: [PATCH] feat: portable grid interactions Generalizes grid watcher registration management code. --- .../grid/operations/NoopGridOperations.java | 20 ++++ .../watcher/GridStorageChannelProvider.java | 18 ++++ .../api/grid/{ => watcher}/GridWatcher.java | 4 +- .../api/grid/watcher/GridWatcherManager.java | 26 +++++ .../grid/watcher/GridWatcherManagerImpl.java | 75 ++++++++------- .../watcher}/GridWatcherRegistration.java | 3 +- .../api/grid/{ => watcher}/package-info.java | 2 +- .../component/StorageNetworkComponent.java | 3 +- .../nodefactory/GridNetworkNodeFactory.java | 3 +- .../StorageNetworkComponentImpl.java | 6 ++ .../impl/node/grid/GridNetworkNode.java | 21 ++-- .../impl/node/storage/StorageNetworkNode.java | 2 +- .../impl/PriorityNetworkBuilderImplTest.java | 8 +- .../impl/node/grid/GridNetworkNodeTest.java | 4 +- .../platform/api/grid/Grid.java | 2 +- .../platform/common/PlatformApiImpl.java | 4 +- .../common/grid/AbstractGridBlockEntity.java | 16 ++-- .../grid/AbstractGridContainerMenu.java | 10 +- ...ronizer.java => NoopGridSynchronizer.java} | 2 +- .../platform/common/grid/WirelessGrid.java | 75 ++++----------- .../common/grid/WirelessGridItem.java | 1 - .../common/grid/WirelessGridOperations.java | 6 +- .../AbstractPortableGridBlockEntity.java | 96 +++++++++++-------- .../PortableGridContainerMenu.java | 20 +++- .../portablegrid/PortableGridOperations.java | 48 ++++++++++ .../portablegrid/PortableGridStorage.java | 31 ++++++ .../fabric/packet/s2c/GridActivePacket.java | 2 +- .../forge/packet/s2c/GridActivePacket.java | 2 +- .../api/storage/NoopStorage.java | 32 +++++++ 29 files changed, 362 insertions(+), 180 deletions(-) create mode 100644 refinedstorage2-grid-api/src/main/java/com/refinedmods/refinedstorage2/api/grid/operations/NoopGridOperations.java create mode 100644 refinedstorage2-grid-api/src/main/java/com/refinedmods/refinedstorage2/api/grid/watcher/GridStorageChannelProvider.java rename refinedstorage2-grid-api/src/main/java/com/refinedmods/refinedstorage2/api/grid/{ => watcher}/GridWatcher.java (93%) create mode 100644 refinedstorage2-grid-api/src/main/java/com/refinedmods/refinedstorage2/api/grid/watcher/GridWatcherManager.java rename refinedstorage2-network/src/main/java/com/refinedmods/refinedstorage2/api/network/impl/node/grid/GridWatchers.java => refinedstorage2-grid-api/src/main/java/com/refinedmods/refinedstorage2/api/grid/watcher/GridWatcherManagerImpl.java (57%) rename {refinedstorage2-network/src/main/java/com/refinedmods/refinedstorage2/api/network/impl/node/grid => refinedstorage2-grid-api/src/main/java/com/refinedmods/refinedstorage2/api/grid/watcher}/GridWatcherRegistration.java (94%) rename refinedstorage2-grid-api/src/main/java/com/refinedmods/refinedstorage2/api/grid/{ => watcher}/package-info.java (78%) rename refinedstorage2-platform-common/src/main/java/com/refinedmods/refinedstorage2/platform/common/grid/{NoOpGridSynchronizer.java => NoopGridSynchronizer.java} (93%) create mode 100644 refinedstorage2-platform-common/src/main/java/com/refinedmods/refinedstorage2/platform/common/storage/portablegrid/PortableGridOperations.java create mode 100644 refinedstorage2-platform-common/src/main/java/com/refinedmods/refinedstorage2/platform/common/storage/portablegrid/PortableGridStorage.java create mode 100644 refinedstorage2-storage-api/src/main/java/com/refinedmods/refinedstorage2/api/storage/NoopStorage.java diff --git a/refinedstorage2-grid-api/src/main/java/com/refinedmods/refinedstorage2/api/grid/operations/NoopGridOperations.java b/refinedstorage2-grid-api/src/main/java/com/refinedmods/refinedstorage2/api/grid/operations/NoopGridOperations.java new file mode 100644 index 000000000..8b090f3c2 --- /dev/null +++ b/refinedstorage2-grid-api/src/main/java/com/refinedmods/refinedstorage2/api/grid/operations/NoopGridOperations.java @@ -0,0 +1,20 @@ +package com.refinedmods.refinedstorage2.api.grid.operations; + +import com.refinedmods.refinedstorage2.api.storage.ExtractableStorage; +import com.refinedmods.refinedstorage2.api.storage.InsertableStorage; + +public class NoopGridOperations implements GridOperations { + @Override + public boolean extract(final T resource, + final GridExtractMode extractMode, + final InsertableStorage destination) { + return false; + } + + @Override + public boolean insert(final T resource, + final GridInsertMode insertMode, + final ExtractableStorage source) { + return false; + } +} diff --git a/refinedstorage2-grid-api/src/main/java/com/refinedmods/refinedstorage2/api/grid/watcher/GridStorageChannelProvider.java b/refinedstorage2-grid-api/src/main/java/com/refinedmods/refinedstorage2/api/grid/watcher/GridStorageChannelProvider.java new file mode 100644 index 000000000..c4e7b6fa0 --- /dev/null +++ b/refinedstorage2-grid-api/src/main/java/com/refinedmods/refinedstorage2/api/grid/watcher/GridStorageChannelProvider.java @@ -0,0 +1,18 @@ +package com.refinedmods.refinedstorage2.api.grid.watcher; + +import com.refinedmods.refinedstorage2.api.storage.channel.StorageChannel; +import com.refinedmods.refinedstorage2.api.storage.channel.StorageChannelType; + +import java.util.Set; + +import org.apiguardian.api.API; + +/** + * Provides the {@link GridWatcherManagerImpl} with {@link StorageChannel}s. + */ +@API(status = API.Status.STABLE, since = "2.0.0-milestone.3.3") +public interface GridStorageChannelProvider { + Set> getStorageChannelTypes(); + + StorageChannel getStorageChannel(StorageChannelType type); +} diff --git a/refinedstorage2-grid-api/src/main/java/com/refinedmods/refinedstorage2/api/grid/GridWatcher.java b/refinedstorage2-grid-api/src/main/java/com/refinedmods/refinedstorage2/api/grid/watcher/GridWatcher.java similarity index 93% rename from refinedstorage2-grid-api/src/main/java/com/refinedmods/refinedstorage2/api/grid/GridWatcher.java rename to refinedstorage2-grid-api/src/main/java/com/refinedmods/refinedstorage2/api/grid/watcher/GridWatcher.java index 16febbd31..f762f0aab 100644 --- a/refinedstorage2-grid-api/src/main/java/com/refinedmods/refinedstorage2/api/grid/GridWatcher.java +++ b/refinedstorage2-grid-api/src/main/java/com/refinedmods/refinedstorage2/api/grid/watcher/GridWatcher.java @@ -1,4 +1,4 @@ -package com.refinedmods.refinedstorage2.api.grid; +package com.refinedmods.refinedstorage2.api.grid.watcher; import com.refinedmods.refinedstorage2.api.storage.channel.StorageChannelType; import com.refinedmods.refinedstorage2.api.storage.tracked.TrackedResource; @@ -38,5 +38,5 @@ void onChanged( /** * Usually called when the grid network has been changed. */ - void clear(); + void invalidate(); } diff --git a/refinedstorage2-grid-api/src/main/java/com/refinedmods/refinedstorage2/api/grid/watcher/GridWatcherManager.java b/refinedstorage2-grid-api/src/main/java/com/refinedmods/refinedstorage2/api/grid/watcher/GridWatcherManager.java new file mode 100644 index 000000000..2e2055c62 --- /dev/null +++ b/refinedstorage2-grid-api/src/main/java/com/refinedmods/refinedstorage2/api/grid/watcher/GridWatcherManager.java @@ -0,0 +1,26 @@ +package com.refinedmods.refinedstorage2.api.grid.watcher; + +import com.refinedmods.refinedstorage2.api.storage.Actor; + +import org.apiguardian.api.API; + +/** + * This manager helps with attaching and detaching listeners to + * {@link com.refinedmods.refinedstorage2.api.storage.channel.StorageChannel}s. + */ +@API(status = API.Status.STABLE, since = "2.0.0-milestone.3.3") +public interface GridWatcherManager { + void addWatcher( + GridWatcher watcher, + Class actorType, + GridStorageChannelProvider storageChannelProvider + ); + + void attachAll(GridStorageChannelProvider storageChannelProvider); + + void removeWatcher(GridWatcher watcher, GridStorageChannelProvider storageChannelProvider); + + void detachAll(GridStorageChannelProvider storageChannelProvider); + + void activeChanged(boolean active); +} diff --git a/refinedstorage2-network/src/main/java/com/refinedmods/refinedstorage2/api/network/impl/node/grid/GridWatchers.java b/refinedstorage2-grid-api/src/main/java/com/refinedmods/refinedstorage2/api/grid/watcher/GridWatcherManagerImpl.java similarity index 57% rename from refinedstorage2-network/src/main/java/com/refinedmods/refinedstorage2/api/network/impl/node/grid/GridWatchers.java rename to refinedstorage2-grid-api/src/main/java/com/refinedmods/refinedstorage2/api/grid/watcher/GridWatcherManagerImpl.java index d9f82e933..93652de30 100644 --- a/refinedstorage2-network/src/main/java/com/refinedmods/refinedstorage2/api/network/impl/node/grid/GridWatchers.java +++ b/refinedstorage2-grid-api/src/main/java/com/refinedmods/refinedstorage2/api/grid/watcher/GridWatcherManagerImpl.java @@ -1,105 +1,110 @@ -package com.refinedmods.refinedstorage2.api.network.impl.node.grid; +package com.refinedmods.refinedstorage2.api.grid.watcher; -import com.refinedmods.refinedstorage2.api.grid.GridWatcher; -import com.refinedmods.refinedstorage2.api.network.Network; -import com.refinedmods.refinedstorage2.api.network.component.StorageNetworkComponent; import com.refinedmods.refinedstorage2.api.storage.Actor; -import com.refinedmods.refinedstorage2.api.storage.channel.StorageChannel; import com.refinedmods.refinedstorage2.api.storage.channel.StorageChannelType; -import java.util.Collection; import java.util.HashMap; import java.util.Map; +import org.apiguardian.api.API; import org.slf4j.Logger; import org.slf4j.LoggerFactory; -public class GridWatchers { - public static final Logger LOGGER = LoggerFactory.getLogger(GridWatchers.class); +// TODO: test. +@API(status = API.Status.STABLE, since = "2.0.0-milestone.3.3") +public class GridWatcherManagerImpl implements GridWatcherManager { + private static final Logger LOGGER = LoggerFactory.getLogger(GridWatcherManagerImpl.class); - private final Collection> storageChannelTypes; private final Map watchers = new HashMap<>(); - public GridWatchers(final Collection> storageChannelTypes) { - this.storageChannelTypes = storageChannelTypes; - } - - private StorageChannel getStorageChannel(final Network network, final StorageChannelType type) { - return network.getComponent(StorageNetworkComponent.class).getStorageChannel(type); - } - + @Override public void addWatcher( final GridWatcher watcher, final Class actorType, - final Network network + final GridStorageChannelProvider storageChannelProvider ) { if (watchers.containsKey(watcher)) { throw new IllegalArgumentException("Watcher is already registered"); } final GridWatcherRegistration registration = new GridWatcherRegistration(watcher, actorType); - attachAll(registration, network, false); + attachAll(registration, storageChannelProvider, false); watchers.put(watcher, registration); LOGGER.info("Added watcher {}, new count is {}", watcher, watchers.size()); } - public void attachAll(final Network network) { + @Override + public void attachAll(final GridStorageChannelProvider storageChannelProvider) { // If we get here we are affected by a network split or network merge. // At this point, all the storages that are affected by the split or merge have not yet been processed // as the grid has the highest priority. watchers.forEach((watcher, registration) -> { // Invalidate all watcher data, the resources that were synced earlier are no longer valid because we have // a brand-new network. - watcher.clear(); + watcher.invalidate(); // Re-attach the watcher to the new network, and send all the resources from the new network. // Resources from the old network are not part of the new network yet, as mentioned above, // but those will be synced when the storages are re-added. - attachAll(registration, network, true); + attachAll(registration, storageChannelProvider, true); }); } - private void attachAll(final GridWatcherRegistration registration, final Network network, final boolean replay) { - storageChannelTypes.forEach(storageChannelType -> attach(registration, storageChannelType, network, replay)); + private void attachAll(final GridWatcherRegistration registration, + final GridStorageChannelProvider storageChannelProvider, + final boolean replay) { + storageChannelProvider.getStorageChannelTypes().forEach(storageChannelType -> attach( + registration, + storageChannelType, + storageChannelProvider, + replay + )); } private void attach( final GridWatcherRegistration registration, final StorageChannelType storageChannelType, - final Network network, + final GridStorageChannelProvider storageChannelProvider, final boolean replay ) { LOGGER.info("Attaching {} to {}", registration, storageChannelType); - final StorageChannel storageChannel = getStorageChannel(network, storageChannelType); - registration.attach(storageChannel, storageChannelType, replay); + registration.attach(storageChannelProvider.getStorageChannel(storageChannelType), storageChannelType, replay); } - public void removeWatcher(final GridWatcher watcher, final Network network) { + @Override + public void removeWatcher(final GridWatcher watcher, final GridStorageChannelProvider storageChannelProvider) { final GridWatcherRegistration registration = watchers.get(watcher); if (registration == null) { throw new IllegalArgumentException("Watcher is not registered"); } - detachAll(registration, network); + detachAll(registration, storageChannelProvider); watchers.remove(watcher); LOGGER.info("Removed watcher {}, remaining {}", watcher, watchers.size()); } - public void detachAll(final Network network) { + @Override + public void detachAll(final GridStorageChannelProvider storageChannelProvider) { LOGGER.info("Detaching {} watchers", watchers.size()); - watchers.values().forEach(w -> detachAll(w, network)); + watchers.values().forEach(w -> detachAll(w, storageChannelProvider)); } - private void detachAll(final GridWatcherRegistration registration, final Network network) { - storageChannelTypes.forEach(storageChannelType -> detach(registration, storageChannelType, network)); + private void detachAll(final GridWatcherRegistration registration, + final GridStorageChannelProvider storageChannelProvider) { + storageChannelProvider.getStorageChannelTypes().forEach(storageChannelType -> detach( + registration, + storageChannelType, + storageChannelProvider + )); } private void detach( final GridWatcherRegistration registration, final StorageChannelType storageChannelType, - final Network network + final GridStorageChannelProvider storageChannelProvider ) { LOGGER.info("Detaching {} from {}", registration, storageChannelType); - registration.detach(getStorageChannel(network, storageChannelType), storageChannelType); + registration.detach(storageChannelProvider.getStorageChannel(storageChannelType), storageChannelType); } + @Override public void activeChanged(final boolean active) { watchers.keySet().forEach(watcher -> watcher.onActiveChanged(active)); } diff --git a/refinedstorage2-network/src/main/java/com/refinedmods/refinedstorage2/api/network/impl/node/grid/GridWatcherRegistration.java b/refinedstorage2-grid-api/src/main/java/com/refinedmods/refinedstorage2/api/grid/watcher/GridWatcherRegistration.java similarity index 94% rename from refinedstorage2-network/src/main/java/com/refinedmods/refinedstorage2/api/network/impl/node/grid/GridWatcherRegistration.java rename to refinedstorage2-grid-api/src/main/java/com/refinedmods/refinedstorage2/api/grid/watcher/GridWatcherRegistration.java index 7d44e1b4b..d3d71e50c 100644 --- a/refinedstorage2-network/src/main/java/com/refinedmods/refinedstorage2/api/network/impl/node/grid/GridWatcherRegistration.java +++ b/refinedstorage2-grid-api/src/main/java/com/refinedmods/refinedstorage2/api/grid/watcher/GridWatcherRegistration.java @@ -1,6 +1,5 @@ -package com.refinedmods.refinedstorage2.api.network.impl.node.grid; +package com.refinedmods.refinedstorage2.api.grid.watcher; -import com.refinedmods.refinedstorage2.api.grid.GridWatcher; import com.refinedmods.refinedstorage2.api.resource.list.listenable.ResourceListListener; import com.refinedmods.refinedstorage2.api.storage.Actor; import com.refinedmods.refinedstorage2.api.storage.channel.StorageChannel; diff --git a/refinedstorage2-grid-api/src/main/java/com/refinedmods/refinedstorage2/api/grid/package-info.java b/refinedstorage2-grid-api/src/main/java/com/refinedmods/refinedstorage2/api/grid/watcher/package-info.java similarity index 78% rename from refinedstorage2-grid-api/src/main/java/com/refinedmods/refinedstorage2/api/grid/package-info.java rename to refinedstorage2-grid-api/src/main/java/com/refinedmods/refinedstorage2/api/grid/watcher/package-info.java index 5276c5241..6b4f2d6b4 100644 --- a/refinedstorage2-grid-api/src/main/java/com/refinedmods/refinedstorage2/api/grid/package-info.java +++ b/refinedstorage2-grid-api/src/main/java/com/refinedmods/refinedstorage2/api/grid/watcher/package-info.java @@ -1,6 +1,6 @@ @ParametersAreNonnullByDefault @FieldsAndMethodsAreNonnullByDefault -package com.refinedmods.refinedstorage2.api.grid; +package com.refinedmods.refinedstorage2.api.grid.watcher; import com.refinedmods.refinedstorage2.api.core.FieldsAndMethodsAreNonnullByDefault; diff --git a/refinedstorage2-network-api/src/main/java/com/refinedmods/refinedstorage2/api/network/component/StorageNetworkComponent.java b/refinedstorage2-network-api/src/main/java/com/refinedmods/refinedstorage2/api/network/component/StorageNetworkComponent.java index e5d67684f..544f6f7d6 100644 --- a/refinedstorage2-network-api/src/main/java/com/refinedmods/refinedstorage2/api/network/component/StorageNetworkComponent.java +++ b/refinedstorage2-network-api/src/main/java/com/refinedmods/refinedstorage2/api/network/component/StorageNetworkComponent.java @@ -1,5 +1,6 @@ package com.refinedmods.refinedstorage2.api.network.component; +import com.refinedmods.refinedstorage2.api.grid.watcher.GridStorageChannelProvider; import com.refinedmods.refinedstorage2.api.storage.Actor; import com.refinedmods.refinedstorage2.api.storage.Storage; import com.refinedmods.refinedstorage2.api.storage.TrackedResourceAmount; @@ -12,7 +13,7 @@ import org.apiguardian.api.API; @API(status = API.Status.STABLE, since = "2.0.0-milestone.1.1") -public interface StorageNetworkComponent extends NetworkComponent { +public interface StorageNetworkComponent extends NetworkComponent, GridStorageChannelProvider { StorageChannel getStorageChannel(StorageChannelType type); boolean hasSource(Predicate> matcher); diff --git a/refinedstorage2-network-test/src/main/java/com/refinedmods/refinedstorage2/network/test/nodefactory/GridNetworkNodeFactory.java b/refinedstorage2-network-test/src/main/java/com/refinedmods/refinedstorage2/network/test/nodefactory/GridNetworkNodeFactory.java index 57ab39ef7..9cb5ca071 100644 --- a/refinedstorage2-network-test/src/main/java/com/refinedmods/refinedstorage2/network/test/nodefactory/GridNetworkNodeFactory.java +++ b/refinedstorage2-network-test/src/main/java/com/refinedmods/refinedstorage2/network/test/nodefactory/GridNetworkNodeFactory.java @@ -2,13 +2,12 @@ import com.refinedmods.refinedstorage2.api.network.impl.node.grid.GridNetworkNode; import com.refinedmods.refinedstorage2.network.test.AddNetworkNode; -import com.refinedmods.refinedstorage2.network.test.NetworkTestFixtures; import java.util.Map; public class GridNetworkNodeFactory extends AbstractNetworkNodeFactory { @Override protected GridNetworkNode innerCreate(final AddNetworkNode ctx, final Map properties) { - return new GridNetworkNode(getEnergyUsage(properties), NetworkTestFixtures.STORAGE_CHANNEL_TYPES); + return new GridNetworkNode(getEnergyUsage(properties)); } } diff --git a/refinedstorage2-network/src/main/java/com/refinedmods/refinedstorage2/api/network/impl/component/StorageNetworkComponentImpl.java b/refinedstorage2-network/src/main/java/com/refinedmods/refinedstorage2/api/network/impl/component/StorageNetworkComponentImpl.java index 13e98a7fd..0b59cc7e6 100644 --- a/refinedstorage2-network/src/main/java/com/refinedmods/refinedstorage2/api/network/impl/component/StorageNetworkComponentImpl.java +++ b/refinedstorage2-network/src/main/java/com/refinedmods/refinedstorage2/api/network/impl/component/StorageNetworkComponentImpl.java @@ -12,6 +12,7 @@ import java.util.Collection; import java.util.List; import java.util.Map; +import java.util.Set; import java.util.function.Predicate; import java.util.stream.Collectors; @@ -76,6 +77,11 @@ public StorageChannel getStorageChannel(final StorageChannelType type) return (StorageChannel) channels.get(type); } + @Override + public Set> getStorageChannelTypes() { + return channels.keySet(); + } + @Override @SuppressWarnings({"unchecked", "rawtypes"}) public boolean hasSource(final Predicate> matcher) { diff --git a/refinedstorage2-network/src/main/java/com/refinedmods/refinedstorage2/api/network/impl/node/grid/GridNetworkNode.java b/refinedstorage2-network/src/main/java/com/refinedmods/refinedstorage2/api/network/impl/node/grid/GridNetworkNode.java index bc97f4261..d57b19d42 100644 --- a/refinedstorage2-network/src/main/java/com/refinedmods/refinedstorage2/api/network/impl/node/grid/GridNetworkNode.java +++ b/refinedstorage2-network/src/main/java/com/refinedmods/refinedstorage2/api/network/impl/node/grid/GridNetworkNode.java @@ -1,24 +1,23 @@ package com.refinedmods.refinedstorage2.api.network.impl.node.grid; -import com.refinedmods.refinedstorage2.api.grid.GridWatcher; +import com.refinedmods.refinedstorage2.api.grid.watcher.GridWatcher; +import com.refinedmods.refinedstorage2.api.grid.watcher.GridWatcherManager; +import com.refinedmods.refinedstorage2.api.grid.watcher.GridWatcherManagerImpl; import com.refinedmods.refinedstorage2.api.network.Network; +import com.refinedmods.refinedstorage2.api.network.component.StorageNetworkComponent; import com.refinedmods.refinedstorage2.api.network.node.AbstractNetworkNode; import com.refinedmods.refinedstorage2.api.storage.Actor; -import com.refinedmods.refinedstorage2.api.storage.channel.StorageChannelType; -import java.util.Collection; import javax.annotation.Nullable; import static java.util.Objects.requireNonNull; public class GridNetworkNode extends AbstractNetworkNode { private final long energyUsage; - private final GridWatchers watchers; + private final GridWatcherManager watchers = new GridWatcherManagerImpl(); - public GridNetworkNode(final long energyUsage, - final Collection> storageChannelTypes) { + public GridNetworkNode(final long energyUsage) { this.energyUsage = energyUsage; - this.watchers = new GridWatchers(storageChannelTypes); } @Override @@ -27,11 +26,11 @@ public long getEnergyUsage() { } public void addWatcher(final GridWatcher watcher, final Class actorType) { - watchers.addWatcher(watcher, actorType, requireNonNull(network)); + watchers.addWatcher(watcher, actorType, requireNonNull(network).getComponent(StorageNetworkComponent.class)); } public void removeWatcher(final GridWatcher watcher) { - watchers.removeWatcher(watcher, requireNonNull(network)); + watchers.removeWatcher(watcher, requireNonNull(network).getComponent(StorageNetworkComponent.class)); } @Override @@ -43,11 +42,11 @@ protected void onActiveChanged(final boolean newActive) { @Override public void setNetwork(@Nullable final Network network) { if (this.network != null) { - watchers.detachAll(this.network); + watchers.detachAll(this.network.getComponent(StorageNetworkComponent.class)); } super.setNetwork(network); if (this.network != null) { - watchers.attachAll(this.network); + watchers.attachAll(this.network.getComponent(StorageNetworkComponent.class)); } } } diff --git a/refinedstorage2-network/src/main/java/com/refinedmods/refinedstorage2/api/network/impl/node/storage/StorageNetworkNode.java b/refinedstorage2-network/src/main/java/com/refinedmods/refinedstorage2/api/network/impl/node/storage/StorageNetworkNode.java index 2a632ab58..59ed41ad7 100644 --- a/refinedstorage2-network/src/main/java/com/refinedmods/refinedstorage2/api/network/impl/node/storage/StorageNetworkNode.java +++ b/refinedstorage2-network/src/main/java/com/refinedmods/refinedstorage2/api/network/impl/node/storage/StorageNetworkNode.java @@ -13,7 +13,7 @@ import org.slf4j.LoggerFactory; public class StorageNetworkNode extends AbstractStorageNetworkNode implements StorageProvider { - public static final Logger LOGGER = LoggerFactory.getLogger(StorageNetworkNode.class); + private static final Logger LOGGER = LoggerFactory.getLogger(StorageNetworkNode.class); private final long energyUsage; private final StorageChannelType type; diff --git a/refinedstorage2-network/src/test/java/com/refinedmods/refinedstorage2/api/network/impl/PriorityNetworkBuilderImplTest.java b/refinedstorage2-network/src/test/java/com/refinedmods/refinedstorage2/api/network/impl/PriorityNetworkBuilderImplTest.java index 491dcf4ec..a2adacb55 100644 --- a/refinedstorage2-network/src/test/java/com/refinedmods/refinedstorage2/api/network/impl/PriorityNetworkBuilderImplTest.java +++ b/refinedstorage2-network/src/test/java/com/refinedmods/refinedstorage2/api/network/impl/PriorityNetworkBuilderImplTest.java @@ -1,7 +1,7 @@ package com.refinedmods.refinedstorage2.api.network.impl; import com.refinedmods.refinedstorage2.api.core.Action; -import com.refinedmods.refinedstorage2.api.grid.GridWatcher; +import com.refinedmods.refinedstorage2.api.grid.watcher.GridWatcher; import com.refinedmods.refinedstorage2.api.network.ConnectionProvider; import com.refinedmods.refinedstorage2.api.network.Network; import com.refinedmods.refinedstorage2.api.network.impl.node.container.NetworkNodeContainerPriorities; @@ -52,7 +52,7 @@ void shouldRespectPriorityWhenSplitting() { assertThat(slave.nodeB.getNetwork()).isNotSameAs(master.nodeA.getNetwork()); final InOrder inOrder = inOrder(slave.watcher); - inOrder.verify(slave.watcher, times(1)).clear(); + inOrder.verify(slave.watcher, times(1)).invalidate(); inOrder.verify(slave.watcher, times(1)).onChanged( NetworkTestFixtures.STORAGE_CHANNEL_TYPE, "slave", @@ -93,7 +93,7 @@ void shouldRespectPriorityWhenMerging() { assertThat(slave.nodeB.getNetwork()).isSameAs(master.nodeA.getNetwork()); final InOrder inOrder = inOrder(slave.watcher); - inOrder.verify(slave.watcher, times(1)).clear(); + inOrder.verify(slave.watcher, times(1)).invalidate(); inOrder.verify(slave.watcher).onChanged( NetworkTestFixtures.STORAGE_CHANNEL_TYPE, "slave", @@ -131,7 +131,7 @@ private NetworkSide createNetworkSide(final String name, final Supplier 0 ); nodeA.setActive(true); - final GridNetworkNode nodeB = new GridNetworkNode(0, NetworkTestFixtures.STORAGE_CHANNEL_TYPES); + final GridNetworkNode nodeB = new GridNetworkNode(0); final NetworkNodeContainer b = createContainerWithNetwork( nodeB, container -> a.getNode().getNetwork(), diff --git a/refinedstorage2-network/src/test/java/com/refinedmods/refinedstorage2/api/network/impl/node/grid/GridNetworkNodeTest.java b/refinedstorage2-network/src/test/java/com/refinedmods/refinedstorage2/api/network/impl/node/grid/GridNetworkNodeTest.java index a1fa5986b..9a90acb90 100644 --- a/refinedstorage2-network/src/test/java/com/refinedmods/refinedstorage2/api/network/impl/node/grid/GridNetworkNodeTest.java +++ b/refinedstorage2-network/src/test/java/com/refinedmods/refinedstorage2/api/network/impl/node/grid/GridNetworkNodeTest.java @@ -1,7 +1,7 @@ package com.refinedmods.refinedstorage2.api.network.impl.node.grid; import com.refinedmods.refinedstorage2.api.core.Action; -import com.refinedmods.refinedstorage2.api.grid.GridWatcher; +import com.refinedmods.refinedstorage2.api.grid.watcher.GridWatcher; import com.refinedmods.refinedstorage2.api.network.Network; import com.refinedmods.refinedstorage2.api.storage.Actor; import com.refinedmods.refinedstorage2.api.storage.EmptyActor; @@ -162,7 +162,7 @@ void shouldDetachWatchersFromOldNetworkAndReattachToNewNetwork( storageChannel.insert("D", 10, Action.EXECUTE, EmptyActor.INSTANCE); // Assert - verify(watcher, times(1)).clear(); + verify(watcher, times(1)).invalidate(); final ArgumentCaptor trackedResources1 = ArgumentCaptor.forClass(TrackedResource.class); verify(watcher, times(1)).onChanged( diff --git a/refinedstorage2-platform-api/src/main/java/com/refinedmods/refinedstorage2/platform/api/grid/Grid.java b/refinedstorage2-platform-api/src/main/java/com/refinedmods/refinedstorage2/platform/api/grid/Grid.java index ac8540118..b0f6e96a4 100644 --- a/refinedstorage2-platform-api/src/main/java/com/refinedmods/refinedstorage2/platform/api/grid/Grid.java +++ b/refinedstorage2-platform-api/src/main/java/com/refinedmods/refinedstorage2/platform/api/grid/Grid.java @@ -1,7 +1,7 @@ package com.refinedmods.refinedstorage2.platform.api.grid; -import com.refinedmods.refinedstorage2.api.grid.GridWatcher; import com.refinedmods.refinedstorage2.api.grid.operations.GridOperations; +import com.refinedmods.refinedstorage2.api.grid.watcher.GridWatcher; import com.refinedmods.refinedstorage2.api.storage.Actor; import com.refinedmods.refinedstorage2.api.storage.Storage; import com.refinedmods.refinedstorage2.api.storage.TrackedResourceAmount; diff --git a/refinedstorage2-platform-common/src/main/java/com/refinedmods/refinedstorage2/platform/common/PlatformApiImpl.java b/refinedstorage2-platform-common/src/main/java/com/refinedmods/refinedstorage2/platform/common/PlatformApiImpl.java index 91cc2fc01..844abecfc 100644 --- a/refinedstorage2-platform-common/src/main/java/com/refinedmods/refinedstorage2/platform/common/PlatformApiImpl.java +++ b/refinedstorage2-platform-common/src/main/java/com/refinedmods/refinedstorage2/platform/common/PlatformApiImpl.java @@ -44,7 +44,7 @@ import com.refinedmods.refinedstorage2.platform.api.upgrade.BuiltinUpgradeDestinations; import com.refinedmods.refinedstorage2.platform.api.upgrade.UpgradeRegistry; import com.refinedmods.refinedstorage2.platform.api.wirelesstransmitter.WirelessTransmitterRangeModifier; -import com.refinedmods.refinedstorage2.platform.common.grid.NoOpGridSynchronizer; +import com.refinedmods.refinedstorage2.platform.common.grid.NoopGridSynchronizer; import com.refinedmods.refinedstorage2.platform.common.grid.screen.AbstractGridScreen; import com.refinedmods.refinedstorage2.platform.common.grid.screen.hint.GridInsertionHintsImpl; import com.refinedmods.refinedstorage2.platform.common.grid.screen.hint.ItemGridInsertionHint; @@ -118,7 +118,7 @@ public class PlatformApiImpl implements PlatformApi { private final PlatformRegistry> storageChannelTypeRegistry = new PlatformRegistryImpl<>(createIdentifier(ITEM_REGISTRY_KEY), StorageChannelTypes.ITEM); private final PlatformRegistry gridSynchronizerRegistry = - new PlatformRegistryImpl<>(createIdentifier("off"), new NoOpGridSynchronizer()); + new PlatformRegistryImpl<>(createIdentifier("off"), new NoopGridSynchronizer()); private final PlatformRegistry importerTransferStrategyRegistry = new PlatformRegistryImpl<>(createIdentifier("noop"), (level, pos, direction, upgradeState, amountOverride) -> (filter, actor, network) -> false); diff --git a/refinedstorage2-platform-common/src/main/java/com/refinedmods/refinedstorage2/platform/common/grid/AbstractGridBlockEntity.java b/refinedstorage2-platform-common/src/main/java/com/refinedmods/refinedstorage2/platform/common/grid/AbstractGridBlockEntity.java index c5a8aa47c..7755daca2 100644 --- a/refinedstorage2-platform-common/src/main/java/com/refinedmods/refinedstorage2/platform/common/grid/AbstractGridBlockEntity.java +++ b/refinedstorage2-platform-common/src/main/java/com/refinedmods/refinedstorage2/platform/common/grid/AbstractGridBlockEntity.java @@ -1,7 +1,7 @@ package com.refinedmods.refinedstorage2.platform.common.grid; -import com.refinedmods.refinedstorage2.api.grid.GridWatcher; import com.refinedmods.refinedstorage2.api.grid.operations.GridOperations; +import com.refinedmods.refinedstorage2.api.grid.watcher.GridWatcher; import com.refinedmods.refinedstorage2.api.network.component.StorageNetworkComponent; import com.refinedmods.refinedstorage2.api.network.impl.node.container.NetworkNodeContainerPriorities; import com.refinedmods.refinedstorage2.api.network.impl.node.grid.GridNetworkNode; @@ -20,7 +20,6 @@ import com.refinedmods.refinedstorage2.platform.common.support.network.AbstractRedstoneModeNetworkNodeContainerBlockEntity; import java.util.List; -import java.util.Objects; import net.minecraft.core.BlockPos; import net.minecraft.network.FriendlyByteBuf; @@ -28,6 +27,8 @@ import net.minecraft.world.level.block.entity.BlockEntityType; import net.minecraft.world.level.block.state.BlockState; +import static java.util.Objects.requireNonNull; + public abstract class AbstractGridBlockEntity extends AbstractRedstoneModeNetworkNodeContainerBlockEntity implements Grid, ExtendedMenuProvider { @@ -35,10 +36,7 @@ protected AbstractGridBlockEntity(final BlockEntityType List> getResources(final StorageChannelType type, final Class actorType) { - return Objects.requireNonNull(getNode().getNetwork()) + return requireNonNull(getNode().getNetwork()) .getComponent(StorageNetworkComponent.class) .getResources(type, actorType); } @@ -57,7 +55,7 @@ public List> getResources(final StorageChannelType< @Override public GridOperations createOperations(final PlatformStorageChannelType storageChannelType, final Actor actor) { - final StorageChannel storageChannel = Objects.requireNonNull(getNode().getNetwork()) + final StorageChannel storageChannel = requireNonNull(getNode().getNetwork()) .getComponent(StorageNetworkComponent.class) .getStorageChannel(storageChannelType); return storageChannelType.createGridOperations(storageChannel, actor); @@ -70,7 +68,7 @@ public boolean isGridActive() { @Override public Storage getItemStorage() { - return Objects.requireNonNull(getNode().getNetwork()) + return requireNonNull(getNode().getNetwork()) .getComponent(StorageNetworkComponent.class) .getStorageChannel(StorageChannelTypes.ITEM); } diff --git a/refinedstorage2-platform-common/src/main/java/com/refinedmods/refinedstorage2/platform/common/grid/AbstractGridContainerMenu.java b/refinedstorage2-platform-common/src/main/java/com/refinedmods/refinedstorage2/platform/common/grid/AbstractGridContainerMenu.java index 37277a5a6..4482e60f7 100644 --- a/refinedstorage2-platform-common/src/main/java/com/refinedmods/refinedstorage2/platform/common/grid/AbstractGridContainerMenu.java +++ b/refinedstorage2-platform-common/src/main/java/com/refinedmods/refinedstorage2/platform/common/grid/AbstractGridContainerMenu.java @@ -1,6 +1,5 @@ package com.refinedmods.refinedstorage2.platform.common.grid; -import com.refinedmods.refinedstorage2.api.grid.GridWatcher; import com.refinedmods.refinedstorage2.api.grid.operations.GridExtractMode; import com.refinedmods.refinedstorage2.api.grid.operations.GridInsertMode; import com.refinedmods.refinedstorage2.api.grid.query.GridQueryParserException; @@ -10,6 +9,7 @@ import com.refinedmods.refinedstorage2.api.grid.view.GridView; import com.refinedmods.refinedstorage2.api.grid.view.GridViewBuilder; import com.refinedmods.refinedstorage2.api.grid.view.GridViewBuilderImpl; +import com.refinedmods.refinedstorage2.api.grid.watcher.GridWatcher; import com.refinedmods.refinedstorage2.api.storage.channel.StorageChannelType; import com.refinedmods.refinedstorage2.api.storage.tracked.TrackedResource; import com.refinedmods.refinedstorage2.platform.api.PlatformApi; @@ -284,7 +284,7 @@ public void onChanged( } @Override - public void clear() { + public void invalidate() { if (playerInventory.player instanceof ServerPlayer serverPlayer) { initStrategies(); Platform.INSTANCE.getServerToClientCommunications().sendGridClear(serverPlayer); @@ -433,13 +433,17 @@ public boolean onTransfer(final int slotIndex) { public ItemStack quickMoveStack(final Player playerEntity, final int slotIndex) { if (!playerEntity.level().isClientSide() && grid != null && grid.isGridActive()) { final Slot slot = getSlot(slotIndex); - if (slot.hasItem() && insertionStrategy != null) { + if (slot.hasItem() && insertionStrategy != null && canTransferSlot(slot)) { insertionStrategy.onTransfer(slot.index); } } return super.quickMoveStack(playerEntity, slotIndex); } + protected boolean canTransferSlot(final Slot slot) { + return true; + } + private static void readStorageChannelFromBuffer(final PlatformStorageChannelType type, final FriendlyByteBuf buf, final GridViewBuilder viewBuilder) { diff --git a/refinedstorage2-platform-common/src/main/java/com/refinedmods/refinedstorage2/platform/common/grid/NoOpGridSynchronizer.java b/refinedstorage2-platform-common/src/main/java/com/refinedmods/refinedstorage2/platform/common/grid/NoopGridSynchronizer.java similarity index 93% rename from refinedstorage2-platform-common/src/main/java/com/refinedmods/refinedstorage2/platform/common/grid/NoOpGridSynchronizer.java rename to refinedstorage2-platform-common/src/main/java/com/refinedmods/refinedstorage2/platform/common/grid/NoopGridSynchronizer.java index e7915548d..825f31698 100644 --- a/refinedstorage2-platform-common/src/main/java/com/refinedmods/refinedstorage2/platform/common/grid/NoOpGridSynchronizer.java +++ b/refinedstorage2-platform-common/src/main/java/com/refinedmods/refinedstorage2/platform/common/grid/NoopGridSynchronizer.java @@ -7,7 +7,7 @@ import static com.refinedmods.refinedstorage2.platform.common.util.IdentifierUtil.createTranslation; -public class NoOpGridSynchronizer extends AbstractGridSynchronizer { +public class NoopGridSynchronizer extends AbstractGridSynchronizer { private static final MutableComponent TITLE = createTranslation("gui", "grid.synchronizer.off"); private static final Component HELP = createTranslation("gui", "grid.synchronizer.off.help"); diff --git a/refinedstorage2-platform-common/src/main/java/com/refinedmods/refinedstorage2/platform/common/grid/WirelessGrid.java b/refinedstorage2-platform-common/src/main/java/com/refinedmods/refinedstorage2/platform/common/grid/WirelessGrid.java index 4a0dde26b..0f129178f 100644 --- a/refinedstorage2-platform-common/src/main/java/com/refinedmods/refinedstorage2/platform/common/grid/WirelessGrid.java +++ b/refinedstorage2-platform-common/src/main/java/com/refinedmods/refinedstorage2/platform/common/grid/WirelessGrid.java @@ -1,39 +1,34 @@ package com.refinedmods.refinedstorage2.platform.common.grid; -import com.refinedmods.refinedstorage2.api.core.Action; -import com.refinedmods.refinedstorage2.api.grid.GridWatcher; -import com.refinedmods.refinedstorage2.api.grid.operations.GridExtractMode; -import com.refinedmods.refinedstorage2.api.grid.operations.GridInsertMode; import com.refinedmods.refinedstorage2.api.grid.operations.GridOperations; +import com.refinedmods.refinedstorage2.api.grid.operations.NoopGridOperations; +import com.refinedmods.refinedstorage2.api.grid.watcher.GridWatcher; +import com.refinedmods.refinedstorage2.api.grid.watcher.GridWatcherManager; +import com.refinedmods.refinedstorage2.api.grid.watcher.GridWatcherManagerImpl; import com.refinedmods.refinedstorage2.api.network.component.EnergyNetworkComponent; import com.refinedmods.refinedstorage2.api.network.component.StorageNetworkComponent; -import com.refinedmods.refinedstorage2.api.network.impl.node.grid.GridWatchers; -import com.refinedmods.refinedstorage2.api.resource.ResourceAmount; import com.refinedmods.refinedstorage2.api.storage.Actor; -import com.refinedmods.refinedstorage2.api.storage.ExtractableStorage; -import com.refinedmods.refinedstorage2.api.storage.InsertableStorage; +import com.refinedmods.refinedstorage2.api.storage.NoopStorage; import com.refinedmods.refinedstorage2.api.storage.Storage; import com.refinedmods.refinedstorage2.api.storage.TrackedResourceAmount; import com.refinedmods.refinedstorage2.api.storage.channel.StorageChannelType; -import com.refinedmods.refinedstorage2.platform.api.PlatformApi; import com.refinedmods.refinedstorage2.platform.api.grid.Grid; import com.refinedmods.refinedstorage2.platform.api.storage.channel.PlatformStorageChannelType; import com.refinedmods.refinedstorage2.platform.api.support.network.bounditem.NetworkBoundItemSession; import com.refinedmods.refinedstorage2.platform.api.support.resource.ItemResource; +import com.refinedmods.refinedstorage2.platform.common.Platform; import com.refinedmods.refinedstorage2.platform.common.storage.channel.StorageChannelTypes; -import java.util.Collection; import java.util.Collections; import java.util.List; import java.util.Optional; class WirelessGrid implements Grid { private final NetworkBoundItemSession session; - private final GridWatchers watchers; + private final GridWatcherManager watchers = new GridWatcherManagerImpl(); WirelessGrid(final NetworkBoundItemSession session) { this.session = session; - this.watchers = new GridWatchers(PlatformApi.INSTANCE.getStorageChannelTypeRegistry().getAll()); } private Optional getStorage() { @@ -42,18 +37,26 @@ private Optional getStorage() { @Override public void addWatcher(final GridWatcher watcher, final Class actorType) { - session.resolveNetwork().ifPresent(network -> watchers.addWatcher(watcher, actorType, network)); + session.drainEnergy(Platform.INSTANCE.getConfig().getWirelessGrid().getOpenEnergyUsage()); + session.resolveNetwork().ifPresent(network -> watchers.addWatcher( + watcher, + actorType, + network.getComponent(StorageNetworkComponent.class) + )); } @Override public void removeWatcher(final GridWatcher watcher) { - session.resolveNetwork().ifPresent(network -> watchers.removeWatcher(watcher, network)); + session.resolveNetwork().ifPresent(network -> watchers.removeWatcher( + watcher, + network.getComponent(StorageNetworkComponent.class) + )); } @Override public Storage getItemStorage() { return getStorage().map(storage -> (Storage) storage.getStorageChannel(StorageChannelTypes.ITEM)) - .orElseGet(NoOpStorage::new); + .orElseGet(NoopStorage::new); } @Override @@ -77,46 +80,6 @@ public GridOperations createOperations(final PlatformStorageChannelType storage.getStorageChannel(storageChannelType)) .map(storageChannel -> storageChannelType.createGridOperations(storageChannel, actor)) .map(gridOperations -> (GridOperations) new WirelessGridOperations<>(gridOperations, session, watchers)) - .orElseGet(this::createNoOpGridOperations); - } - - private GridOperations createNoOpGridOperations() { - return new GridOperations<>() { - @Override - public boolean extract(final T resource, - final GridExtractMode extractMode, - final InsertableStorage destination) { - return false; - } - - @Override - public boolean insert(final T resource, - final GridInsertMode insertMode, - final ExtractableStorage source) { - return false; - } - }; - } - - private static class NoOpStorage implements Storage { - @Override - public long extract(final T resource, final long amount, final Action action, final Actor actor) { - return 0; - } - - @Override - public long insert(final T resource, final long amount, final Action action, final Actor actor) { - return 0; - } - - @Override - public Collection> getAll() { - return Collections.emptyList(); - } - - @Override - public long getStored() { - return 0; - } + .orElseGet(NoopGridOperations::new); } } diff --git a/refinedstorage2-platform-common/src/main/java/com/refinedmods/refinedstorage2/platform/common/grid/WirelessGridItem.java b/refinedstorage2-platform-common/src/main/java/com/refinedmods/refinedstorage2/platform/common/grid/WirelessGridItem.java index 5dc2340d1..e0388c756 100644 --- a/refinedstorage2-platform-common/src/main/java/com/refinedmods/refinedstorage2/platform/common/grid/WirelessGridItem.java +++ b/refinedstorage2-platform-common/src/main/java/com/refinedmods/refinedstorage2/platform/common/grid/WirelessGridItem.java @@ -42,7 +42,6 @@ public Optional createEnergyStorage(final ItemStack stack) { public void use(final ServerPlayer player, final SlotReference slotReference, final NetworkBoundItemSession session) { - session.drainEnergy(Platform.INSTANCE.getConfig().getWirelessGrid().getOpenEnergyUsage()); final Grid grid = new WirelessGrid(session); Platform.INSTANCE.getMenuOpener().openMenu(player, new WirelessGridExtendedMenuProvider(grid, slotReference)); } diff --git a/refinedstorage2-platform-common/src/main/java/com/refinedmods/refinedstorage2/platform/common/grid/WirelessGridOperations.java b/refinedstorage2-platform-common/src/main/java/com/refinedmods/refinedstorage2/platform/common/grid/WirelessGridOperations.java index 791eddd4d..96bab64cd 100644 --- a/refinedstorage2-platform-common/src/main/java/com/refinedmods/refinedstorage2/platform/common/grid/WirelessGridOperations.java +++ b/refinedstorage2-platform-common/src/main/java/com/refinedmods/refinedstorage2/platform/common/grid/WirelessGridOperations.java @@ -3,7 +3,7 @@ import com.refinedmods.refinedstorage2.api.grid.operations.GridExtractMode; import com.refinedmods.refinedstorage2.api.grid.operations.GridInsertMode; import com.refinedmods.refinedstorage2.api.grid.operations.GridOperations; -import com.refinedmods.refinedstorage2.api.network.impl.node.grid.GridWatchers; +import com.refinedmods.refinedstorage2.api.grid.watcher.GridWatcherManager; import com.refinedmods.refinedstorage2.api.storage.ExtractableStorage; import com.refinedmods.refinedstorage2.api.storage.InsertableStorage; import com.refinedmods.refinedstorage2.platform.api.support.network.bounditem.NetworkBoundItemSession; @@ -12,11 +12,11 @@ class WirelessGridOperations implements GridOperations { private final GridOperations delegate; private final NetworkBoundItemSession session; - private final GridWatchers watchers; + private final GridWatcherManager watchers; WirelessGridOperations(final GridOperations delegate, final NetworkBoundItemSession session, - final GridWatchers watchers) { + final GridWatcherManager watchers) { this.delegate = delegate; this.session = session; this.watchers = watchers; diff --git a/refinedstorage2-platform-common/src/main/java/com/refinedmods/refinedstorage2/platform/common/storage/portablegrid/AbstractPortableGridBlockEntity.java b/refinedstorage2-platform-common/src/main/java/com/refinedmods/refinedstorage2/platform/common/storage/portablegrid/AbstractPortableGridBlockEntity.java index da2692490..2797de37a 100644 --- a/refinedstorage2-platform-common/src/main/java/com/refinedmods/refinedstorage2/platform/common/storage/portablegrid/AbstractPortableGridBlockEntity.java +++ b/refinedstorage2-platform-common/src/main/java/com/refinedmods/refinedstorage2/platform/common/storage/portablegrid/AbstractPortableGridBlockEntity.java @@ -1,20 +1,20 @@ package com.refinedmods.refinedstorage2.platform.common.storage.portablegrid; import com.refinedmods.refinedstorage2.api.core.Action; -import com.refinedmods.refinedstorage2.api.grid.GridWatcher; -import com.refinedmods.refinedstorage2.api.grid.operations.GridExtractMode; -import com.refinedmods.refinedstorage2.api.grid.operations.GridInsertMode; import com.refinedmods.refinedstorage2.api.grid.operations.GridOperations; +import com.refinedmods.refinedstorage2.api.grid.operations.NoopGridOperations; +import com.refinedmods.refinedstorage2.api.grid.watcher.GridStorageChannelProvider; +import com.refinedmods.refinedstorage2.api.grid.watcher.GridWatcher; +import com.refinedmods.refinedstorage2.api.grid.watcher.GridWatcherManager; +import com.refinedmods.refinedstorage2.api.grid.watcher.GridWatcherManagerImpl; import com.refinedmods.refinedstorage2.api.network.energy.EnergyStorage; import com.refinedmods.refinedstorage2.api.storage.Actor; -import com.refinedmods.refinedstorage2.api.storage.ExtractableStorage; -import com.refinedmods.refinedstorage2.api.storage.InMemoryStorageImpl; -import com.refinedmods.refinedstorage2.api.storage.InsertableStorage; +import com.refinedmods.refinedstorage2.api.storage.NoopStorage; import com.refinedmods.refinedstorage2.api.storage.StateTrackedStorage; import com.refinedmods.refinedstorage2.api.storage.Storage; import com.refinedmods.refinedstorage2.api.storage.StorageState; import com.refinedmods.refinedstorage2.api.storage.TrackedResourceAmount; -import com.refinedmods.refinedstorage2.api.storage.TypedStorage; +import com.refinedmods.refinedstorage2.api.storage.channel.StorageChannel; import com.refinedmods.refinedstorage2.api.storage.channel.StorageChannelType; import com.refinedmods.refinedstorage2.platform.api.PlatformApi; import com.refinedmods.refinedstorage2.platform.api.configurationcard.ConfigurationCardTarget; @@ -29,6 +29,7 @@ import com.refinedmods.refinedstorage2.platform.common.storage.Disk; import com.refinedmods.refinedstorage2.platform.common.storage.DiskInventory; import com.refinedmods.refinedstorage2.platform.common.storage.DiskStateChangeListener; +import com.refinedmods.refinedstorage2.platform.common.storage.channel.StorageChannelTypes; import com.refinedmods.refinedstorage2.platform.common.support.RedstoneMode; import com.refinedmods.refinedstorage2.platform.common.support.RedstoneModeSettings; import com.refinedmods.refinedstorage2.platform.common.support.containermenu.ExtendedMenuProvider; @@ -38,6 +39,7 @@ import java.util.Collections; import java.util.List; +import java.util.Set; import javax.annotation.Nullable; import com.google.common.util.concurrent.RateLimiter; @@ -61,7 +63,7 @@ import org.slf4j.LoggerFactory; public abstract class AbstractPortableGridBlockEntity extends BlockEntity implements Grid, ExtendedMenuProvider, - EnergyBlockEntity, ConfigurationCardTarget { + EnergyBlockEntity, ConfigurationCardTarget, GridStorageChannelProvider { private static final Logger LOGGER = LoggerFactory.getLogger(AbstractPortableGridBlockEntity.class); private static final String TAG_DISK_INVENTORY = "inv"; @@ -76,10 +78,11 @@ public abstract class AbstractPortableGridBlockEntity extends BlockEntity implem private final DiskStateChangeListener diskStateListener = new DiskStateChangeListener(this); private final EnergyStorage energyStorage; private final RateLimiter activenessChangeRateLimiter = RateLimiter.create(1); + private final GridWatcherManager watchers = new GridWatcherManagerImpl(); private RedstoneMode redstoneMode = RedstoneMode.IGNORE; @Nullable - private TypedStorage> storage; + private PortableGridStorage storage; protected AbstractPortableGridBlockEntity(final PortableGridType type, final BlockPos pos, final BlockState state) { super(getBlockEntityType(type), pos, state); @@ -103,6 +106,7 @@ void update(final BlockState state) { final boolean activenessNeedsUpdate = state.getValue(PortableGridBlock.ACTIVE) != newActive; if (activenessNeedsUpdate && activenessChangeRateLimiter.tryAcquire()) { updateActivenessBlockState(state, newActive); + watchers.activeChanged(newActive); } } @@ -136,24 +140,32 @@ public void setLevel(final Level level) { } } - @Override - public void setChanged() { - super.setChanged(); - if (level != null && !level.isClientSide()) { - initialize(level); - } - } - private void initialize(final Level level) { diskInventory.setStorageRepository(PlatformApi.INSTANCE.getStorageRepository(level)); updateStorage(); } - @SuppressWarnings({"unchecked", "rawtypes"}) private void updateStorage() { + watchers.detachAll(this); this.storage = diskInventory.resolve(0) - .map(resolved -> (TypedStorage) StateTrackedStorage.of(resolved, diskStateListener)) + .map(diskStorage -> StateTrackedStorage.of(diskStorage, diskStateListener)) + .map(PortableGridStorage::new) .orElse(null); + watchers.attachAll(this); + } + + @Override + public Set> getStorageChannelTypes() { + return storage == null ? Collections.emptySet() : Set.of(storage.getStorageChannelType()); + } + + @Override + @SuppressWarnings("unchecked") + public StorageChannel getStorageChannel(final StorageChannelType type) { + if (storage == null || type != storage.getStorageChannelType()) { + throw new IllegalArgumentException(); + } + return (StorageChannel) storage.getStorageChannel(); } @Override @@ -229,23 +241,27 @@ private StorageState getStorageState() { if (!isGridActive()) { return StorageState.INACTIVE; } - return storage.storage().getState(); + return storage.getState(); } @Override public void addWatcher(final GridWatcher watcher, final Class actorType) { - // TODO + energyStorage.extract(Platform.INSTANCE.getConfig().getPortableGrid().getOpenEnergyUsage(), Action.EXECUTE); + watchers.addWatcher(watcher, actorType, this); } @Override public void removeWatcher(final GridWatcher watcher) { - // TODO + watchers.removeWatcher(watcher, this); } @Override + @SuppressWarnings("unchecked") public Storage getItemStorage() { - // TODO - return new InMemoryStorageImpl<>(); + if (storage == null || storage.getStorageChannelType() != StorageChannelTypes.ITEM) { + return new NoopStorage<>(); + } + return (Storage) storage.getStorageChannel(); } @Override @@ -256,29 +272,29 @@ public boolean isGridActive() { } @Override + @SuppressWarnings("unchecked") public List> getResources(final StorageChannelType type, final Class actorType) { - // TODO - return Collections.emptyList(); + if (storage == null || storage.getStorageChannelType() != type) { + return Collections.emptyList(); + } + final StorageChannel casted = (StorageChannel) storage.getStorageChannel(); + return casted.getAll().stream().map(resource -> new TrackedResourceAmount<>( + resource, + casted.findTrackedResourceByActorType(resource.getResource(), actorType).orElse(null) + )).toList(); } @Override + @SuppressWarnings("unchecked") public GridOperations createOperations(final PlatformStorageChannelType storageChannelType, final Actor actor) { - // TODO - return new GridOperations<>() { - @Override - public boolean extract(final T resource, final GridExtractMode extractMode, - final InsertableStorage destination) { - return false; - } - - @Override - public boolean insert(final T resource, final GridInsertMode insertMode, - final ExtractableStorage source) { - return false; - } - }; + if (storage == null || storage.getStorageChannelType() != storageChannelType) { + return new NoopGridOperations<>(); + } + final StorageChannel casted = (StorageChannel) storage.getStorageChannel(); + final GridOperations operations = storageChannelType.createGridOperations(casted, actor); + return new PortableGridOperations<>(operations, energyStorage); } @Override diff --git a/refinedstorage2-platform-common/src/main/java/com/refinedmods/refinedstorage2/platform/common/storage/portablegrid/PortableGridContainerMenu.java b/refinedstorage2-platform-common/src/main/java/com/refinedmods/refinedstorage2/platform/common/storage/portablegrid/PortableGridContainerMenu.java index 4ba36f4d6..66ad3a56d 100644 --- a/refinedstorage2-platform-common/src/main/java/com/refinedmods/refinedstorage2/platform/common/storage/portablegrid/PortableGridContainerMenu.java +++ b/refinedstorage2-platform-common/src/main/java/com/refinedmods/refinedstorage2/platform/common/storage/portablegrid/PortableGridContainerMenu.java @@ -11,14 +11,20 @@ import com.refinedmods.refinedstorage2.platform.common.support.energy.EnergyContainerMenu; import com.refinedmods.refinedstorage2.platform.common.support.energy.EnergyInfo; +import javax.annotation.Nullable; + import net.minecraft.network.FriendlyByteBuf; import net.minecraft.world.SimpleContainer; import net.minecraft.world.entity.player.Inventory; +import net.minecraft.world.inventory.Slot; public class PortableGridContainerMenu extends AbstractGridContainerMenu implements EnergyContainerMenu { private final SimpleContainer diskInventory; private final EnergyInfo energyInfo; + @Nullable + private Slot diskSlot; + public PortableGridContainerMenu(final int syncId, final Inventory playerInventory, final FriendlyByteBuf buf) { super(Menus.INSTANCE.getPortableGrid(), syncId, playerInventory, buf); this.diskInventory = new SimpleContainer(1); @@ -51,10 +57,22 @@ public void broadcastChanges() { energyInfo.detectChanges(); } + @Override + protected boolean canTransferSlot(final Slot slot) { + return slot != diskSlot; + } + @Override public void addSlots(final int playerInventoryY) { super.addSlots(playerInventoryY); - addSlot(new ValidatedSlot(diskInventory, 0, -19, 8, stack -> stack.getItem() instanceof StorageContainerItem)); + diskSlot = new ValidatedSlot( + diskInventory, + 0, + -19, + 8, + stack -> stack.getItem() instanceof StorageContainerItem + ); + addSlot(diskSlot); transferManager.addBiTransfer(playerInventory, diskInventory); } diff --git a/refinedstorage2-platform-common/src/main/java/com/refinedmods/refinedstorage2/platform/common/storage/portablegrid/PortableGridOperations.java b/refinedstorage2-platform-common/src/main/java/com/refinedmods/refinedstorage2/platform/common/storage/portablegrid/PortableGridOperations.java new file mode 100644 index 000000000..7b41fb300 --- /dev/null +++ b/refinedstorage2-platform-common/src/main/java/com/refinedmods/refinedstorage2/platform/common/storage/portablegrid/PortableGridOperations.java @@ -0,0 +1,48 @@ +package com.refinedmods.refinedstorage2.platform.common.storage.portablegrid; + +import com.refinedmods.refinedstorage2.api.core.Action; +import com.refinedmods.refinedstorage2.api.grid.operations.GridExtractMode; +import com.refinedmods.refinedstorage2.api.grid.operations.GridInsertMode; +import com.refinedmods.refinedstorage2.api.grid.operations.GridOperations; +import com.refinedmods.refinedstorage2.api.network.energy.EnergyStorage; +import com.refinedmods.refinedstorage2.api.storage.ExtractableStorage; +import com.refinedmods.refinedstorage2.api.storage.InsertableStorage; +import com.refinedmods.refinedstorage2.platform.common.Platform; + +public class PortableGridOperations implements GridOperations { + private final GridOperations delegate; + private final EnergyStorage energyStorage; + + public PortableGridOperations(final GridOperations delegate, final EnergyStorage energyStorage) { + this.delegate = delegate; + this.energyStorage = energyStorage; + } + + @Override + public boolean extract(final T resource, + final GridExtractMode extractMode, + final InsertableStorage destination) { + if (delegate.extract(resource, extractMode, destination)) { + energyStorage.extract( + Platform.INSTANCE.getConfig().getPortableGrid().getExtractEnergyUsage(), + Action.EXECUTE + ); + return true; + } + return false; + } + + @Override + public boolean insert(final T resource, + final GridInsertMode insertMode, + final ExtractableStorage source) { + if (delegate.insert(resource, insertMode, source)) { + energyStorage.extract( + Platform.INSTANCE.getConfig().getPortableGrid().getInsertEnergyUsage(), + Action.EXECUTE + ); + return true; + } + return false; + } +} diff --git a/refinedstorage2-platform-common/src/main/java/com/refinedmods/refinedstorage2/platform/common/storage/portablegrid/PortableGridStorage.java b/refinedstorage2-platform-common/src/main/java/com/refinedmods/refinedstorage2/platform/common/storage/portablegrid/PortableGridStorage.java new file mode 100644 index 000000000..e2bb1c486 --- /dev/null +++ b/refinedstorage2-platform-common/src/main/java/com/refinedmods/refinedstorage2/platform/common/storage/portablegrid/PortableGridStorage.java @@ -0,0 +1,31 @@ +package com.refinedmods.refinedstorage2.platform.common.storage.portablegrid; + +import com.refinedmods.refinedstorage2.api.storage.StateTrackedStorage; +import com.refinedmods.refinedstorage2.api.storage.StorageState; +import com.refinedmods.refinedstorage2.api.storage.TypedStorage; +import com.refinedmods.refinedstorage2.api.storage.channel.StorageChannel; +import com.refinedmods.refinedstorage2.api.storage.channel.StorageChannelImpl; +import com.refinedmods.refinedstorage2.api.storage.channel.StorageChannelType; + +class PortableGridStorage { + private final StorageChannel storageChannel; + private final TypedStorage> diskStorage; + + PortableGridStorage(final TypedStorage> diskStorage) { + this.storageChannel = new StorageChannelImpl<>(); + this.diskStorage = diskStorage; + this.storageChannel.addSource(diskStorage.storage()); + } + + StorageChannelType getStorageChannelType() { + return diskStorage.storageChannelType(); + } + + StorageState getState() { + return diskStorage.storage().getState(); + } + + StorageChannel getStorageChannel() { + return storageChannel; + } +} diff --git a/refinedstorage2-platform-fabric/src/main/java/com/refinedmods/refinedstorage2/platform/fabric/packet/s2c/GridActivePacket.java b/refinedstorage2-platform-fabric/src/main/java/com/refinedmods/refinedstorage2/platform/fabric/packet/s2c/GridActivePacket.java index 0250612d1..650fe03fd 100644 --- a/refinedstorage2-platform-fabric/src/main/java/com/refinedmods/refinedstorage2/platform/fabric/packet/s2c/GridActivePacket.java +++ b/refinedstorage2-platform-fabric/src/main/java/com/refinedmods/refinedstorage2/platform/fabric/packet/s2c/GridActivePacket.java @@ -1,6 +1,6 @@ package com.refinedmods.refinedstorage2.platform.fabric.packet.s2c; -import com.refinedmods.refinedstorage2.api.grid.GridWatcher; +import com.refinedmods.refinedstorage2.api.grid.watcher.GridWatcher; import net.fabricmc.fabric.api.client.networking.v1.ClientPlayNetworking; import net.fabricmc.fabric.api.networking.v1.PacketSender; diff --git a/refinedstorage2-platform-forge/src/main/java/com/refinedmods/refinedstorage2/platform/forge/packet/s2c/GridActivePacket.java b/refinedstorage2-platform-forge/src/main/java/com/refinedmods/refinedstorage2/platform/forge/packet/s2c/GridActivePacket.java index e0b5a0cd6..09296201c 100644 --- a/refinedstorage2-platform-forge/src/main/java/com/refinedmods/refinedstorage2/platform/forge/packet/s2c/GridActivePacket.java +++ b/refinedstorage2-platform-forge/src/main/java/com/refinedmods/refinedstorage2/platform/forge/packet/s2c/GridActivePacket.java @@ -1,6 +1,6 @@ package com.refinedmods.refinedstorage2.platform.forge.packet.s2c; -import com.refinedmods.refinedstorage2.api.grid.GridWatcher; +import com.refinedmods.refinedstorage2.api.grid.watcher.GridWatcher; import java.util.function.Supplier; diff --git a/refinedstorage2-storage-api/src/main/java/com/refinedmods/refinedstorage2/api/storage/NoopStorage.java b/refinedstorage2-storage-api/src/main/java/com/refinedmods/refinedstorage2/api/storage/NoopStorage.java new file mode 100644 index 000000000..91236852c --- /dev/null +++ b/refinedstorage2-storage-api/src/main/java/com/refinedmods/refinedstorage2/api/storage/NoopStorage.java @@ -0,0 +1,32 @@ +package com.refinedmods.refinedstorage2.api.storage; + +import com.refinedmods.refinedstorage2.api.core.Action; +import com.refinedmods.refinedstorage2.api.resource.ResourceAmount; + +import java.util.Collection; +import java.util.Collections; + +import org.apiguardian.api.API; + +@API(status = API.Status.STABLE, since = "2.0.0-milestone.3.3") +public class NoopStorage implements Storage { + @Override + public long extract(final T resource, final long amount, final Action action, final Actor actor) { + return 0; + } + + @Override + public long insert(final T resource, final long amount, final Action action, final Actor actor) { + return 0; + } + + @Override + public Collection> getAll() { + return Collections.emptyList(); + } + + @Override + public long getStored() { + return 0; + } +}