From 681830813b7272ca9b0c2fbcc10376d9d515fd8f Mon Sep 17 00:00:00 2001 From: raoulvdberge Date: Sun, 17 Nov 2024 09:49:24 +0100 Subject: [PATCH] refactor: autocrafting monitor now delegates to network --- .../status/TaskStatusProvider.java | 2 + .../common/AbstractModInitializer.java | 3 +- ...tractAutocraftingMonitorContainerMenu.java | 41 +++++------ .../monitor/AutocraftingMonitor.java | 9 +++ .../AutocraftingMonitorBlockEntity.java | 70 ++++++++++++++++--- .../AutocraftingMonitorContainerMenu.java | 20 +----- .../monitor/AutocraftingMonitorScreen.java | 2 +- .../monitor/AutocraftingMonitorWatcher.java | 2 +- .../monitor/TaskStatusProviderImpl.java | 7 +- .../grid/AbstractGridContainerMenu.java | 20 ++---- .../AutocraftingNetworkComponent.java | 3 +- .../test/fixtures/FakeTaskStatusProvider.java | 40 +++++++++++ .../test/fixtures/NetworkTestFixtures.java | 2 +- .../AutocraftingNetworkComponentImpl.java | 39 +++++++++++ .../AutocraftingNetworkComponentImplTest.java | 3 +- 15 files changed, 190 insertions(+), 73 deletions(-) create mode 100644 refinedstorage-common/src/main/java/com/refinedmods/refinedstorage/common/autocrafting/monitor/AutocraftingMonitor.java create mode 100644 refinedstorage-network-test/src/main/java/com/refinedmods/refinedstorage/network/test/fixtures/FakeTaskStatusProvider.java diff --git a/refinedstorage-autocrafting-api/src/main/java/com/refinedmods/refinedstorage/api/autocrafting/status/TaskStatusProvider.java b/refinedstorage-autocrafting-api/src/main/java/com/refinedmods/refinedstorage/api/autocrafting/status/TaskStatusProvider.java index a74424707..8fbc08337 100644 --- a/refinedstorage-autocrafting-api/src/main/java/com/refinedmods/refinedstorage/api/autocrafting/status/TaskStatusProvider.java +++ b/refinedstorage-autocrafting-api/src/main/java/com/refinedmods/refinedstorage/api/autocrafting/status/TaskStatusProvider.java @@ -17,4 +17,6 @@ public interface TaskStatusProvider { void cancel(TaskId taskId); void cancelAll(); + + void testUpdate(); } diff --git a/refinedstorage-common/src/main/java/com/refinedmods/refinedstorage/common/AbstractModInitializer.java b/refinedstorage-common/src/main/java/com/refinedmods/refinedstorage/common/AbstractModInitializer.java index 454a34aa4..5f7f8d8ee 100644 --- a/refinedstorage-common/src/main/java/com/refinedmods/refinedstorage/common/AbstractModInitializer.java +++ b/refinedstorage-common/src/main/java/com/refinedmods/refinedstorage/common/AbstractModInitializer.java @@ -28,6 +28,7 @@ import com.refinedmods.refinedstorage.common.autocrafting.monitor.AutocraftingMonitorBlockEntity; import com.refinedmods.refinedstorage.common.autocrafting.monitor.AutocraftingMonitorContainerMenu; import com.refinedmods.refinedstorage.common.autocrafting.monitor.AutocraftingMonitorData; +import com.refinedmods.refinedstorage.common.autocrafting.monitor.TaskStatusProviderImpl; import com.refinedmods.refinedstorage.common.autocrafting.patterngrid.PatternGridBlockEntity; import com.refinedmods.refinedstorage.common.autocrafting.patterngrid.PatternGridContainerMenu; import com.refinedmods.refinedstorage.common.autocrafting.patterngrid.PatternGridData; @@ -276,7 +277,7 @@ private void registerNetworkComponents() { ); RefinedStorageApi.INSTANCE.getNetworkComponentMapFactory().addFactory( AutocraftingNetworkComponent.class, - network -> new AutocraftingNetworkComponentImpl() + network -> new AutocraftingNetworkComponentImpl(new TaskStatusProviderImpl()) ); } diff --git a/refinedstorage-common/src/main/java/com/refinedmods/refinedstorage/common/autocrafting/monitor/AbstractAutocraftingMonitorContainerMenu.java b/refinedstorage-common/src/main/java/com/refinedmods/refinedstorage/common/autocrafting/monitor/AbstractAutocraftingMonitorContainerMenu.java index cf639c9d5..88fc8fe48 100644 --- a/refinedstorage-common/src/main/java/com/refinedmods/refinedstorage/common/autocrafting/monitor/AbstractAutocraftingMonitorContainerMenu.java +++ b/refinedstorage-common/src/main/java/com/refinedmods/refinedstorage/common/autocrafting/monitor/AbstractAutocraftingMonitorContainerMenu.java @@ -3,7 +3,6 @@ import com.refinedmods.refinedstorage.api.autocrafting.TaskId; import com.refinedmods.refinedstorage.api.autocrafting.status.TaskStatus; import com.refinedmods.refinedstorage.api.autocrafting.status.TaskStatusListener; -import com.refinedmods.refinedstorage.api.autocrafting.status.TaskStatusProvider; import com.refinedmods.refinedstorage.common.support.AbstractBaseContainerMenu; import com.refinedmods.refinedstorage.common.support.packet.c2s.C2SPackets; import com.refinedmods.refinedstorage.common.support.packet.s2c.S2CPackets; @@ -25,7 +24,7 @@ public abstract class AbstractAutocraftingMonitorContainerMenu extends AbstractB private final List tasks; private final List tasksView; @Nullable - private final TaskStatusProvider taskStatusProvider; + private final AutocraftingMonitor autocraftingMonitor; private final Player player; @Nullable @@ -46,38 +45,40 @@ protected AbstractAutocraftingMonitorContainerMenu(final MenuType menuType, this.tasks = data.statuses().stream().map(TaskStatus::info).collect(Collectors.toList()); this.tasksView = Collections.unmodifiableList(tasks); this.currentTaskId = data.statuses().isEmpty() ? null : data.statuses().getFirst().info().id(); - this.taskStatusProvider = null; + this.autocraftingMonitor = null; this.active = data.active(); this.player = playerInventory.player; } - protected AbstractAutocraftingMonitorContainerMenu(final MenuType menuType, - final int syncId, - final Player player, - final TaskStatusProvider taskStatusProvider) { + AbstractAutocraftingMonitorContainerMenu(final MenuType menuType, + final int syncId, + final Player player, + final AutocraftingMonitor autocraftingMonitor) { super(menuType, syncId); this.statusByTaskId = Collections.emptyMap(); this.tasks = Collections.emptyList(); this.tasksView = Collections.emptyList(); this.currentTaskId = null; - this.taskStatusProvider = taskStatusProvider; + this.autocraftingMonitor = autocraftingMonitor; this.player = player; - taskStatusProvider.addListener(this); + this.autocraftingMonitor.addListener(this); + this.autocraftingMonitor.addWatcher(this); } @Override public void removed(final Player removedPlayer) { super.removed(removedPlayer); - if (taskStatusProvider != null) { - taskStatusProvider.removeListener(this); + if (autocraftingMonitor != null) { + autocraftingMonitor.removeListener(this); + autocraftingMonitor.removeWatcher(this); } } @Override public void broadcastChanges() { super.broadcastChanges(); - if (taskStatusProvider instanceof TaskStatusProviderImpl taskStatusProviderImpl) { - taskStatusProviderImpl.testTick(); + if (autocraftingMonitor != null) { + autocraftingMonitor.testUpdate(); } } @@ -115,7 +116,7 @@ void loadCurrentTask() { @Override public void taskStatusChanged(final TaskStatus status) { - if (taskStatusProvider != null && player instanceof ServerPlayer serverPlayer) { + if (autocraftingMonitor != null && player instanceof ServerPlayer serverPlayer) { S2CPackets.sendAutocraftingMonitorTaskStatusChanged(serverPlayer, status); return; } @@ -124,7 +125,7 @@ public void taskStatusChanged(final TaskStatus status) { @Override public void taskRemoved(final TaskId id) { - if (taskStatusProvider != null && player instanceof ServerPlayer serverPlayer) { + if (autocraftingMonitor != null && player instanceof ServerPlayer serverPlayer) { S2CPackets.sendAutocraftingMonitorTaskRemoved(serverPlayer, id); return; } @@ -141,7 +142,7 @@ public void taskRemoved(final TaskId id) { @Override public void taskAdded(final TaskStatus status) { - if (taskStatusProvider != null && player instanceof ServerPlayer serverPlayer) { + if (autocraftingMonitor != null && player instanceof ServerPlayer serverPlayer) { S2CPackets.sendAutocraftingMonitorTaskAdded(serverPlayer, status); return; } @@ -157,8 +158,8 @@ public void taskAdded(final TaskStatus status) { } public void cancelTask(final TaskId taskId) { - if (taskStatusProvider != null) { - taskStatusProvider.cancel(taskId); + if (autocraftingMonitor != null) { + autocraftingMonitor.cancel(taskId); } } @@ -169,8 +170,8 @@ void cancelCurrentTask() { } public void cancelAllTasks() { - if (taskStatusProvider != null) { - taskStatusProvider.cancelAll(); + if (autocraftingMonitor != null) { + autocraftingMonitor.cancelAll(); } else { C2SPackets.sendAutocraftingMonitorCancelAll(); } diff --git a/refinedstorage-common/src/main/java/com/refinedmods/refinedstorage/common/autocrafting/monitor/AutocraftingMonitor.java b/refinedstorage-common/src/main/java/com/refinedmods/refinedstorage/common/autocrafting/monitor/AutocraftingMonitor.java new file mode 100644 index 000000000..3bb5b3259 --- /dev/null +++ b/refinedstorage-common/src/main/java/com/refinedmods/refinedstorage/common/autocrafting/monitor/AutocraftingMonitor.java @@ -0,0 +1,9 @@ +package com.refinedmods.refinedstorage.common.autocrafting.monitor; + +import com.refinedmods.refinedstorage.api.autocrafting.status.TaskStatusProvider; + +interface AutocraftingMonitor extends TaskStatusProvider { + void addWatcher(AutocraftingMonitorWatcher watcher); + + void removeWatcher(AutocraftingMonitorWatcher watcher); +} diff --git a/refinedstorage-common/src/main/java/com/refinedmods/refinedstorage/common/autocrafting/monitor/AutocraftingMonitorBlockEntity.java b/refinedstorage-common/src/main/java/com/refinedmods/refinedstorage/common/autocrafting/monitor/AutocraftingMonitorBlockEntity.java index b9759b6f6..ddd57f10d 100644 --- a/refinedstorage-common/src/main/java/com/refinedmods/refinedstorage/common/autocrafting/monitor/AutocraftingMonitorBlockEntity.java +++ b/refinedstorage-common/src/main/java/com/refinedmods/refinedstorage/common/autocrafting/monitor/AutocraftingMonitorBlockEntity.java @@ -1,5 +1,10 @@ package com.refinedmods.refinedstorage.common.autocrafting.monitor; +import com.refinedmods.refinedstorage.api.autocrafting.TaskId; +import com.refinedmods.refinedstorage.api.autocrafting.status.TaskStatus; +import com.refinedmods.refinedstorage.api.autocrafting.status.TaskStatusListener; +import com.refinedmods.refinedstorage.api.network.Network; +import com.refinedmods.refinedstorage.api.network.autocrafting.AutocraftingNetworkComponent; import com.refinedmods.refinedstorage.api.network.impl.node.SimpleNetworkNode; import com.refinedmods.refinedstorage.common.Platform; import com.refinedmods.refinedstorage.common.content.BlockEntities; @@ -8,7 +13,10 @@ import com.refinedmods.refinedstorage.common.support.containermenu.NetworkNodeExtendedMenuProvider; import com.refinedmods.refinedstorage.common.support.network.AbstractBaseNetworkNodeContainerBlockEntity; +import java.util.Collections; import java.util.HashSet; +import java.util.List; +import java.util.Optional; import java.util.Set; import net.minecraft.core.BlockPos; @@ -21,7 +29,7 @@ import net.minecraft.world.level.block.state.BlockState; public class AutocraftingMonitorBlockEntity extends AbstractBaseNetworkNodeContainerBlockEntity - implements NetworkNodeExtendedMenuProvider { + implements NetworkNodeExtendedMenuProvider, AutocraftingMonitor { private final Set watchers = new HashSet<>(); public AutocraftingMonitorBlockEntity(final BlockPos pos, final BlockState state) { @@ -30,14 +38,6 @@ public AutocraftingMonitorBlockEntity(final BlockPos pos, final BlockState state )); } - void addWatcher(final AutocraftingMonitorWatcher watcher) { - watchers.add(watcher); - } - - void removeWatcher(final AutocraftingMonitorWatcher watcher) { - watchers.remove(watcher); - } - @Override protected void activenessChanged(final boolean newActive) { super.activenessChanged(newActive); @@ -57,7 +57,7 @@ protected boolean doesBlockStateChangeWarrantNetworkNodeUpdate(final BlockState @Override public AutocraftingMonitorData getMenuData() { - return new AutocraftingMonitorData(new TaskStatusProviderImpl().getStatuses(), mainNetworkNode.isActive()); + return new AutocraftingMonitorData(getStatuses(), mainNetworkNode.isActive()); } @Override @@ -67,6 +67,54 @@ public StreamEncoder getMenuCo @Override public AbstractContainerMenu createMenu(final int syncId, final Inventory inventory, final Player player) { - return new AutocraftingMonitorContainerMenu(syncId, player, new TaskStatusProviderImpl(), this); + return new AutocraftingMonitorContainerMenu(syncId, player, this); + } + + private Optional getAutocrafting() { + final Network network = mainNetworkNode.getNetwork(); + if (network == null) { + return Optional.empty(); + } + return Optional.of(network.getComponent(AutocraftingNetworkComponent.class)); + } + + @Override + public List getStatuses() { + return getAutocrafting().map(AutocraftingNetworkComponent::getStatuses).orElse(Collections.emptyList()); + } + + @Override + public void addListener(final TaskStatusListener listener) { + getAutocrafting().ifPresent(autocrafting -> autocrafting.addListener(listener)); + } + + @Override + public void removeListener(final TaskStatusListener listener) { + getAutocrafting().ifPresent(autocrafting -> autocrafting.removeListener(listener)); + } + + @Override + public void cancel(final TaskId taskId) { + getAutocrafting().ifPresent(autocrafting -> autocrafting.cancel(taskId)); + } + + @Override + public void cancelAll() { + getAutocrafting().ifPresent(AutocraftingNetworkComponent::cancelAll); + } + + @Override + public void testUpdate() { + getAutocrafting().ifPresent(AutocraftingNetworkComponent::testUpdate); + } + + @Override + public void addWatcher(final AutocraftingMonitorWatcher watcher) { + watchers.add(watcher); + } + + @Override + public void removeWatcher(final AutocraftingMonitorWatcher watcher) { + watchers.remove(watcher); } } diff --git a/refinedstorage-common/src/main/java/com/refinedmods/refinedstorage/common/autocrafting/monitor/AutocraftingMonitorContainerMenu.java b/refinedstorage-common/src/main/java/com/refinedmods/refinedstorage/common/autocrafting/monitor/AutocraftingMonitorContainerMenu.java index e6d29160f..46a08b825 100644 --- a/refinedstorage-common/src/main/java/com/refinedmods/refinedstorage/common/autocrafting/monitor/AutocraftingMonitorContainerMenu.java +++ b/refinedstorage-common/src/main/java/com/refinedmods/refinedstorage/common/autocrafting/monitor/AutocraftingMonitorContainerMenu.java @@ -1,48 +1,30 @@ package com.refinedmods.refinedstorage.common.autocrafting.monitor; -import com.refinedmods.refinedstorage.api.autocrafting.status.TaskStatusProvider; import com.refinedmods.refinedstorage.common.content.Menus; import com.refinedmods.refinedstorage.common.support.RedstoneMode; import com.refinedmods.refinedstorage.common.support.containermenu.ClientProperty; import com.refinedmods.refinedstorage.common.support.containermenu.PropertyTypes; import com.refinedmods.refinedstorage.common.support.containermenu.ServerProperty; -import javax.annotation.Nullable; - import net.minecraft.world.entity.player.Inventory; import net.minecraft.world.entity.player.Player; public class AutocraftingMonitorContainerMenu extends AbstractAutocraftingMonitorContainerMenu { - @Nullable - private final AutocraftingMonitorBlockEntity autocraftingMonitor; - public AutocraftingMonitorContainerMenu(final int syncId, final Inventory playerInventory, final AutocraftingMonitorData data) { super(Menus.INSTANCE.getAutocraftingMonitor(), syncId, playerInventory, data); registerProperty(new ClientProperty<>(PropertyTypes.REDSTONE_MODE, RedstoneMode.IGNORE)); - this.autocraftingMonitor = null; } AutocraftingMonitorContainerMenu(final int syncId, final Player player, - final TaskStatusProvider taskStatusProvider, final AutocraftingMonitorBlockEntity autocraftingMonitor) { - super(Menus.INSTANCE.getAutocraftingMonitor(), syncId, player, taskStatusProvider); + super(Menus.INSTANCE.getAutocraftingMonitor(), syncId, player, autocraftingMonitor); registerProperty(new ServerProperty<>( PropertyTypes.REDSTONE_MODE, autocraftingMonitor::getRedstoneMode, autocraftingMonitor::setRedstoneMode )); - this.autocraftingMonitor = autocraftingMonitor; - autocraftingMonitor.addWatcher(this); - } - - @Override - public void removed(final Player removedPlayer) { - super.removed(removedPlayer); - if (autocraftingMonitor != null) { - autocraftingMonitor.removeWatcher(this); - } } } diff --git a/refinedstorage-common/src/main/java/com/refinedmods/refinedstorage/common/autocrafting/monitor/AutocraftingMonitorScreen.java b/refinedstorage-common/src/main/java/com/refinedmods/refinedstorage/common/autocrafting/monitor/AutocraftingMonitorScreen.java index d7341a5cc..9ac2e2d80 100644 --- a/refinedstorage-common/src/main/java/com/refinedmods/refinedstorage/common/autocrafting/monitor/AutocraftingMonitorScreen.java +++ b/refinedstorage-common/src/main/java/com/refinedmods/refinedstorage/common/autocrafting/monitor/AutocraftingMonitorScreen.java @@ -326,7 +326,7 @@ private void renderItemText(final GuiGraphics graphics, final int y) { int yy = y; if (item.stored() > 0) { - renderItemText(graphics, "stored", rendering, x, yy, item.missing()); + renderItemText(graphics, "stored", rendering, x, yy, item.stored()); yy += 7; } if (item.missing() > 0) { diff --git a/refinedstorage-common/src/main/java/com/refinedmods/refinedstorage/common/autocrafting/monitor/AutocraftingMonitorWatcher.java b/refinedstorage-common/src/main/java/com/refinedmods/refinedstorage/common/autocrafting/monitor/AutocraftingMonitorWatcher.java index 7ee80632c..56c3c0dca 100644 --- a/refinedstorage-common/src/main/java/com/refinedmods/refinedstorage/common/autocrafting/monitor/AutocraftingMonitorWatcher.java +++ b/refinedstorage-common/src/main/java/com/refinedmods/refinedstorage/common/autocrafting/monitor/AutocraftingMonitorWatcher.java @@ -1,6 +1,6 @@ package com.refinedmods.refinedstorage.common.autocrafting.monitor; @FunctionalInterface -interface AutocraftingMonitorWatcher { +public interface AutocraftingMonitorWatcher { void activeChanged(boolean active); } diff --git a/refinedstorage-common/src/main/java/com/refinedmods/refinedstorage/common/autocrafting/monitor/TaskStatusProviderImpl.java b/refinedstorage-common/src/main/java/com/refinedmods/refinedstorage/common/autocrafting/monitor/TaskStatusProviderImpl.java index fc96c9b99..76aeb33fc 100644 --- a/refinedstorage-common/src/main/java/com/refinedmods/refinedstorage/common/autocrafting/monitor/TaskStatusProviderImpl.java +++ b/refinedstorage-common/src/main/java/com/refinedmods/refinedstorage/common/autocrafting/monitor/TaskStatusProviderImpl.java @@ -18,7 +18,7 @@ import net.minecraft.world.item.ItemStack; import net.minecraft.world.item.Items; -class TaskStatusProviderImpl implements TaskStatusProvider { +public class TaskStatusProviderImpl implements TaskStatusProvider { private static final Item[] ITEM_SET = new Item[] { Items.DIRT, Items.DIAMOND, @@ -66,12 +66,13 @@ public void cancelAll() { copy.forEach(s -> listeners.forEach(l -> l.taskRemoved(s.info().id()))); } - void testTick() { + @Override + public void testUpdate() { if (ticks++ % 10 != 0) { return; } final int chance = r.nextInt(100); - if (chance < 50) { + if (chance < 50 && statuses.size() < 100) { final TaskStatus status = generateTaskStatus(); statuses.add(status); listeners.forEach(l -> l.taskAdded(status)); diff --git a/refinedstorage-common/src/main/java/com/refinedmods/refinedstorage/common/grid/AbstractGridContainerMenu.java b/refinedstorage-common/src/main/java/com/refinedmods/refinedstorage/common/grid/AbstractGridContainerMenu.java index 47ea13859..64ca79739 100644 --- a/refinedstorage-common/src/main/java/com/refinedmods/refinedstorage/common/grid/AbstractGridContainerMenu.java +++ b/refinedstorage-common/src/main/java/com/refinedmods/refinedstorage/common/grid/AbstractGridContainerMenu.java @@ -207,15 +207,14 @@ public void setViewType(final GridViewType viewType) { } public void setSearchBox(final GridSearchBox searchBox) { - registerViewUpdatingListener(searchBox); - configureSearchBox(searchBox); - } - - private void registerViewUpdatingListener(final GridSearchBox theSearchBox) { - theSearchBox.addListener(text -> { + searchBox.addListener(text -> { final boolean valid = onSearchTextChanged(text); - theSearchBox.setValid(valid); + searchBox.setValid(valid); }); + if (Platform.INSTANCE.getConfig().getGrid().isRememberSearchQuery()) { + searchBox.setValue(lastSearchQuery); + searchBox.addListener(AbstractGridContainerMenu::updateLastSearchQuery); + } } private boolean onSearchTextChanged(final String text) { @@ -228,13 +227,6 @@ private boolean onSearchTextChanged(final String text) { } } - private void configureSearchBox(final GridSearchBox theSearchBox) { - if (Platform.INSTANCE.getConfig().getGrid().isRememberSearchQuery()) { - theSearchBox.setValue(lastSearchQuery); - theSearchBox.addListener(AbstractGridContainerMenu::updateLastSearchQuery); - } - } - private static void updateLastSearchQuery(final String text) { lastSearchQuery = text; } diff --git a/refinedstorage-network-api/src/main/java/com/refinedmods/refinedstorage/api/network/autocrafting/AutocraftingNetworkComponent.java b/refinedstorage-network-api/src/main/java/com/refinedmods/refinedstorage/api/network/autocrafting/AutocraftingNetworkComponent.java index 072ba8151..d7b34c58f 100644 --- a/refinedstorage-network-api/src/main/java/com/refinedmods/refinedstorage/api/network/autocrafting/AutocraftingNetworkComponent.java +++ b/refinedstorage-network-api/src/main/java/com/refinedmods/refinedstorage/api/network/autocrafting/AutocraftingNetworkComponent.java @@ -2,6 +2,7 @@ import com.refinedmods.refinedstorage.api.autocrafting.Pattern; import com.refinedmods.refinedstorage.api.autocrafting.preview.PreviewProvider; +import com.refinedmods.refinedstorage.api.autocrafting.status.TaskStatusProvider; import com.refinedmods.refinedstorage.api.network.NetworkComponent; import com.refinedmods.refinedstorage.api.resource.ResourceKey; @@ -10,7 +11,7 @@ import org.apiguardian.api.API; @API(status = API.Status.STABLE, since = "2.0.0-milestone.4.8") -public interface AutocraftingNetworkComponent extends NetworkComponent, PreviewProvider { +public interface AutocraftingNetworkComponent extends NetworkComponent, PreviewProvider, TaskStatusProvider { void addListener(PatternListener listener); void removeListener(PatternListener listener); diff --git a/refinedstorage-network-test/src/main/java/com/refinedmods/refinedstorage/network/test/fixtures/FakeTaskStatusProvider.java b/refinedstorage-network-test/src/main/java/com/refinedmods/refinedstorage/network/test/fixtures/FakeTaskStatusProvider.java new file mode 100644 index 000000000..9a180e45e --- /dev/null +++ b/refinedstorage-network-test/src/main/java/com/refinedmods/refinedstorage/network/test/fixtures/FakeTaskStatusProvider.java @@ -0,0 +1,40 @@ +package com.refinedmods.refinedstorage.network.test.fixtures; + +import com.refinedmods.refinedstorage.api.autocrafting.TaskId; +import com.refinedmods.refinedstorage.api.autocrafting.status.TaskStatus; +import com.refinedmods.refinedstorage.api.autocrafting.status.TaskStatusListener; +import com.refinedmods.refinedstorage.api.autocrafting.status.TaskStatusProvider; + +import java.util.List; + +public class FakeTaskStatusProvider implements TaskStatusProvider { + @Override + public List getStatuses() { + return List.of(); + } + + @Override + public void addListener(final TaskStatusListener listener) { + // no op + } + + @Override + public void removeListener(final TaskStatusListener listener) { + // no op + } + + @Override + public void cancel(final TaskId taskId) { + // no op + } + + @Override + public void cancelAll() { + // no op + } + + @Override + public void testUpdate() { + // no op + } +} diff --git a/refinedstorage-network-test/src/main/java/com/refinedmods/refinedstorage/network/test/fixtures/NetworkTestFixtures.java b/refinedstorage-network-test/src/main/java/com/refinedmods/refinedstorage/network/test/fixtures/NetworkTestFixtures.java index 7be2b7176..86c0261f5 100644 --- a/refinedstorage-network-test/src/main/java/com/refinedmods/refinedstorage/network/test/fixtures/NetworkTestFixtures.java +++ b/refinedstorage-network-test/src/main/java/com/refinedmods/refinedstorage/network/test/fixtures/NetworkTestFixtures.java @@ -39,7 +39,7 @@ public final class NetworkTestFixtures { ); NETWORK_COMPONENT_MAP_FACTORY.addFactory( AutocraftingNetworkComponent.class, - network -> new AutocraftingNetworkComponentImpl() + network -> new AutocraftingNetworkComponentImpl(new FakeTaskStatusProvider()) ); } diff --git a/refinedstorage-network/src/main/java/com/refinedmods/refinedstorage/api/network/impl/autocrafting/AutocraftingNetworkComponentImpl.java b/refinedstorage-network/src/main/java/com/refinedmods/refinedstorage/api/network/impl/autocrafting/AutocraftingNetworkComponentImpl.java index e3407c8f3..ed4e2741c 100644 --- a/refinedstorage-network/src/main/java/com/refinedmods/refinedstorage/api/network/impl/autocrafting/AutocraftingNetworkComponentImpl.java +++ b/refinedstorage-network/src/main/java/com/refinedmods/refinedstorage/api/network/impl/autocrafting/AutocraftingNetworkComponentImpl.java @@ -2,9 +2,13 @@ import com.refinedmods.refinedstorage.api.autocrafting.Pattern; import com.refinedmods.refinedstorage.api.autocrafting.PatternRepositoryImpl; +import com.refinedmods.refinedstorage.api.autocrafting.TaskId; import com.refinedmods.refinedstorage.api.autocrafting.preview.Preview; import com.refinedmods.refinedstorage.api.autocrafting.preview.PreviewItem; import com.refinedmods.refinedstorage.api.autocrafting.preview.PreviewType; +import com.refinedmods.refinedstorage.api.autocrafting.status.TaskStatus; +import com.refinedmods.refinedstorage.api.autocrafting.status.TaskStatusListener; +import com.refinedmods.refinedstorage.api.autocrafting.status.TaskStatusProvider; import com.refinedmods.refinedstorage.api.network.autocrafting.AutocraftingNetworkComponent; import com.refinedmods.refinedstorage.api.network.autocrafting.ParentContainer; import com.refinedmods.refinedstorage.api.network.autocrafting.PatternListener; @@ -22,6 +26,11 @@ public class AutocraftingNetworkComponentImpl implements AutocraftingNetworkComp private final Set providers = new HashSet<>(); private final Set listeners = new HashSet<>(); private final PatternRepositoryImpl patternRepository = new PatternRepositoryImpl(); + private final TaskStatusProvider taskStatusProvider; + + public AutocraftingNetworkComponentImpl(final TaskStatusProvider taskStatusProvider) { + this.taskStatusProvider = taskStatusProvider; + } @Override public void onContainerAdded(final NetworkNodeContainer container) { @@ -88,13 +97,43 @@ public void addListener(final PatternListener listener) { listeners.add(listener); } + @Override + public void addListener(final TaskStatusListener listener) { + taskStatusProvider.addListener(listener); + } + @Override public void removeListener(final PatternListener listener) { listeners.remove(listener); } + @Override + public void removeListener(final TaskStatusListener listener) { + taskStatusProvider.removeListener(listener); + } + @Override public Set getPatterns() { return patternRepository.getAll(); } + + @Override + public List getStatuses() { + return taskStatusProvider.getStatuses(); + } + + @Override + public void cancel(final TaskId taskId) { + taskStatusProvider.cancel(taskId); + } + + @Override + public void cancelAll() { + taskStatusProvider.cancelAll(); + } + + @Override + public void testUpdate() { + taskStatusProvider.testUpdate(); + } } diff --git a/refinedstorage-network/src/test/java/com/refinedmods/refinedstorage/api/network/impl/autocrafting/AutocraftingNetworkComponentImplTest.java b/refinedstorage-network/src/test/java/com/refinedmods/refinedstorage/api/network/impl/autocrafting/AutocraftingNetworkComponentImplTest.java index cebdb63ee..4200f04d6 100644 --- a/refinedstorage-network/src/test/java/com/refinedmods/refinedstorage/api/network/impl/autocrafting/AutocraftingNetworkComponentImplTest.java +++ b/refinedstorage-network/src/test/java/com/refinedmods/refinedstorage/api/network/impl/autocrafting/AutocraftingNetworkComponentImplTest.java @@ -2,6 +2,7 @@ import com.refinedmods.refinedstorage.api.network.impl.node.patternprovider.PatternProviderNetworkNode; import com.refinedmods.refinedstorage.api.network.node.container.NetworkNodeContainer; +import com.refinedmods.refinedstorage.network.test.fixtures.FakeTaskStatusProvider; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; @@ -14,7 +15,7 @@ class AutocraftingNetworkComponentImplTest { @BeforeEach void setUp() { - sut = new AutocraftingNetworkComponentImpl(); + sut = new AutocraftingNetworkComponentImpl(new FakeTaskStatusProvider()); } @Test