diff --git a/refinedstorage-autocrafting-api/src/main/java/com/refinedmods/refinedstorage/api/autocrafting/TaskId.java b/refinedstorage-autocrafting-api/src/main/java/com/refinedmods/refinedstorage/api/autocrafting/TaskId.java new file mode 100644 index 000000000..878278358 --- /dev/null +++ b/refinedstorage-autocrafting-api/src/main/java/com/refinedmods/refinedstorage/api/autocrafting/TaskId.java @@ -0,0 +1,9 @@ +package com.refinedmods.refinedstorage.api.autocrafting; + +import java.util.UUID; + +import org.apiguardian.api.API; + +@API(status = API.Status.STABLE, since = "2.0.0-milestone.4.10") +public record TaskId(UUID id) { +} diff --git a/refinedstorage-autocrafting-api/src/main/java/com/refinedmods/refinedstorage/api/autocrafting/preview/AutocraftingPreview.java b/refinedstorage-autocrafting-api/src/main/java/com/refinedmods/refinedstorage/api/autocrafting/preview/Preview.java similarity index 64% rename from refinedstorage-autocrafting-api/src/main/java/com/refinedmods/refinedstorage/api/autocrafting/preview/AutocraftingPreview.java rename to refinedstorage-autocrafting-api/src/main/java/com/refinedmods/refinedstorage/api/autocrafting/preview/Preview.java index ad5576ef7..1ca73b92b 100644 --- a/refinedstorage-autocrafting-api/src/main/java/com/refinedmods/refinedstorage/api/autocrafting/preview/AutocraftingPreview.java +++ b/refinedstorage-autocrafting-api/src/main/java/com/refinedmods/refinedstorage/api/autocrafting/preview/Preview.java @@ -5,5 +5,5 @@ import org.apiguardian.api.API; @API(status = API.Status.STABLE, since = "2.0.0-milestone.4.9") -public record AutocraftingPreview(AutocraftingPreviewType type, List items) { +public record Preview(PreviewType type, List items) { } diff --git a/refinedstorage-autocrafting-api/src/main/java/com/refinedmods/refinedstorage/api/autocrafting/preview/AutocraftingPreviewItem.java b/refinedstorage-autocrafting-api/src/main/java/com/refinedmods/refinedstorage/api/autocrafting/preview/PreviewItem.java similarity index 68% rename from refinedstorage-autocrafting-api/src/main/java/com/refinedmods/refinedstorage/api/autocrafting/preview/AutocraftingPreviewItem.java rename to refinedstorage-autocrafting-api/src/main/java/com/refinedmods/refinedstorage/api/autocrafting/preview/PreviewItem.java index f1c71ada5..11891557d 100644 --- a/refinedstorage-autocrafting-api/src/main/java/com/refinedmods/refinedstorage/api/autocrafting/preview/AutocraftingPreviewItem.java +++ b/refinedstorage-autocrafting-api/src/main/java/com/refinedmods/refinedstorage/api/autocrafting/preview/PreviewItem.java @@ -5,5 +5,5 @@ import org.apiguardian.api.API; @API(status = API.Status.STABLE, since = "2.0.0-milestone.4.9") -public record AutocraftingPreviewItem(ResourceKey resource, long available, long missing, long toCraft) { +public record PreviewItem(ResourceKey resource, long available, long missing, long toCraft) { } diff --git a/refinedstorage-autocrafting-api/src/main/java/com/refinedmods/refinedstorage/api/autocrafting/preview/AutocraftingPreviewProvider.java b/refinedstorage-autocrafting-api/src/main/java/com/refinedmods/refinedstorage/api/autocrafting/preview/PreviewProvider.java similarity index 71% rename from refinedstorage-autocrafting-api/src/main/java/com/refinedmods/refinedstorage/api/autocrafting/preview/AutocraftingPreviewProvider.java rename to refinedstorage-autocrafting-api/src/main/java/com/refinedmods/refinedstorage/api/autocrafting/preview/PreviewProvider.java index 7cb97463c..fd69071c9 100644 --- a/refinedstorage-autocrafting-api/src/main/java/com/refinedmods/refinedstorage/api/autocrafting/preview/AutocraftingPreviewProvider.java +++ b/refinedstorage-autocrafting-api/src/main/java/com/refinedmods/refinedstorage/api/autocrafting/preview/PreviewProvider.java @@ -7,8 +7,8 @@ import org.apiguardian.api.API; @API(status = API.Status.STABLE, since = "2.0.0-milestone.4.9") -public interface AutocraftingPreviewProvider { - Optional getPreview(ResourceKey resource, long amount); +public interface PreviewProvider { + Optional getPreview(ResourceKey resource, long amount); boolean startTask(ResourceKey resource, long amount); } diff --git a/refinedstorage-autocrafting-api/src/main/java/com/refinedmods/refinedstorage/api/autocrafting/preview/AutocraftingPreviewType.java b/refinedstorage-autocrafting-api/src/main/java/com/refinedmods/refinedstorage/api/autocrafting/preview/PreviewType.java similarity index 84% rename from refinedstorage-autocrafting-api/src/main/java/com/refinedmods/refinedstorage/api/autocrafting/preview/AutocraftingPreviewType.java rename to refinedstorage-autocrafting-api/src/main/java/com/refinedmods/refinedstorage/api/autocrafting/preview/PreviewType.java index 47af2f527..d69ea48d5 100644 --- a/refinedstorage-autocrafting-api/src/main/java/com/refinedmods/refinedstorage/api/autocrafting/preview/AutocraftingPreviewType.java +++ b/refinedstorage-autocrafting-api/src/main/java/com/refinedmods/refinedstorage/api/autocrafting/preview/PreviewType.java @@ -3,7 +3,7 @@ import org.apiguardian.api.API; @API(status = API.Status.STABLE, since = "2.0.0-milestone.4.9") -public enum AutocraftingPreviewType { +public enum PreviewType { SUCCESS, MISSING_RESOURCES } diff --git a/refinedstorage-autocrafting-api/src/main/java/com/refinedmods/refinedstorage/api/autocrafting/status/AutocraftingTaskStatus.java b/refinedstorage-autocrafting-api/src/main/java/com/refinedmods/refinedstorage/api/autocrafting/status/TaskStatus.java similarity index 63% rename from refinedstorage-autocrafting-api/src/main/java/com/refinedmods/refinedstorage/api/autocrafting/status/AutocraftingTaskStatus.java rename to refinedstorage-autocrafting-api/src/main/java/com/refinedmods/refinedstorage/api/autocrafting/status/TaskStatus.java index 540094c4c..1761d92eb 100644 --- a/refinedstorage-autocrafting-api/src/main/java/com/refinedmods/refinedstorage/api/autocrafting/status/AutocraftingTaskStatus.java +++ b/refinedstorage-autocrafting-api/src/main/java/com/refinedmods/refinedstorage/api/autocrafting/status/TaskStatus.java @@ -1,19 +1,19 @@ package com.refinedmods.refinedstorage.api.autocrafting.status; +import com.refinedmods.refinedstorage.api.autocrafting.TaskId; import com.refinedmods.refinedstorage.api.resource.ResourceKey; import java.util.List; -import java.util.UUID; import org.apiguardian.api.API; @API(status = API.Status.STABLE, since = "2.0.0-milestone.4.10") -public record AutocraftingTaskStatus(Id id, List elements) { - public record Id(UUID id, ResourceKey resource, long amount, long startTime) { +public record TaskStatus(TaskInfo info, float percentageCompleted, List items) { + public record TaskInfo(TaskId id, ResourceKey resource, long amount, long startTime) { } - public record Element( - ElementType type, + public record Item( + ItemType type, ResourceKey resource, long stored, long missing, @@ -23,7 +23,7 @@ public record Element( ) { } - public enum ElementType { + public enum ItemType { NORMAL, MACHINE_DOES_NOT_ACCEPT_RESOURCE, NO_MACHINE_FOUND, diff --git a/refinedstorage-autocrafting-api/src/main/java/com/refinedmods/refinedstorage/api/autocrafting/status/TaskStatusListener.java b/refinedstorage-autocrafting-api/src/main/java/com/refinedmods/refinedstorage/api/autocrafting/status/TaskStatusListener.java new file mode 100644 index 000000000..666cf1fa8 --- /dev/null +++ b/refinedstorage-autocrafting-api/src/main/java/com/refinedmods/refinedstorage/api/autocrafting/status/TaskStatusListener.java @@ -0,0 +1,14 @@ +package com.refinedmods.refinedstorage.api.autocrafting.status; + +import com.refinedmods.refinedstorage.api.autocrafting.TaskId; + +import org.apiguardian.api.API; + +@API(status = API.Status.STABLE, since = "2.0.0-milestone.4.10") +public interface TaskStatusListener { + void taskStatusChanged(TaskStatus status); + + void taskRemoved(TaskId id); + + void taskAdded(TaskStatus status); +} 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 new file mode 100644 index 000000000..a74424707 --- /dev/null +++ b/refinedstorage-autocrafting-api/src/main/java/com/refinedmods/refinedstorage/api/autocrafting/status/TaskStatusProvider.java @@ -0,0 +1,20 @@ +package com.refinedmods.refinedstorage.api.autocrafting.status; + +import com.refinedmods.refinedstorage.api.autocrafting.TaskId; + +import java.util.List; + +import org.apiguardian.api.API; + +@API(status = API.Status.STABLE, since = "2.0.0-milestone.4.10") +public interface TaskStatusProvider { + List getStatuses(); + + void addListener(TaskStatusListener listener); + + void removeListener(TaskStatusListener listener); + + void cancel(TaskId taskId); + + void cancelAll(); +} diff --git a/refinedstorage-common-api/src/main/java/com/refinedmods/refinedstorage/common/api/grid/Grid.java b/refinedstorage-common-api/src/main/java/com/refinedmods/refinedstorage/common/api/grid/Grid.java index 1e6a710e7..e7147ac36 100644 --- a/refinedstorage-common-api/src/main/java/com/refinedmods/refinedstorage/common/api/grid/Grid.java +++ b/refinedstorage-common-api/src/main/java/com/refinedmods/refinedstorage/common/api/grid/Grid.java @@ -1,6 +1,6 @@ package com.refinedmods.refinedstorage.common.api.grid; -import com.refinedmods.refinedstorage.api.autocrafting.preview.AutocraftingPreviewProvider; +import com.refinedmods.refinedstorage.api.autocrafting.preview.PreviewProvider; import com.refinedmods.refinedstorage.api.grid.operations.GridOperations; import com.refinedmods.refinedstorage.api.grid.watcher.GridWatcher; import com.refinedmods.refinedstorage.api.storage.Actor; @@ -16,7 +16,7 @@ import org.apiguardian.api.API; @API(status = API.Status.STABLE, since = "2.0.0-milestone.3.0") -public interface Grid extends AutocraftingPreviewProvider { +public interface Grid extends PreviewProvider { void addWatcher(GridWatcher watcher, Class actorType); void removeWatcher(GridWatcher watcher); 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 20bcd3e82..454a34aa4 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 @@ -821,7 +821,7 @@ protected final void registerMenus(final RegistryCallback> callback, Menus.INSTANCE.setAutocraftingMonitor(callback.register( ContentIds.AUTOCRAFTING_MONITOR, () -> extendedMenuTypeFactory.create( - (syncId, player, data) -> new AutocraftingMonitorContainerMenu(syncId, data), + AutocraftingMonitorContainerMenu::new, AutocraftingMonitorData.STREAM_CODEC ) )); 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 815c36c32..4a7273adb 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,6 +1,5 @@ package com.refinedmods.refinedstorage.common.autocrafting.monitor; -import com.refinedmods.refinedstorage.api.autocrafting.status.AutocraftingTaskStatus; import com.refinedmods.refinedstorage.api.network.impl.node.SimpleNetworkNode; import com.refinedmods.refinedstorage.common.Platform; import com.refinedmods.refinedstorage.common.content.BlockEntities; @@ -8,24 +7,15 @@ import com.refinedmods.refinedstorage.common.support.AbstractDirectionalBlock; import com.refinedmods.refinedstorage.common.support.containermenu.NetworkNodeExtendedMenuProvider; import com.refinedmods.refinedstorage.common.support.network.AbstractBaseNetworkNodeContainerBlockEntity; -import com.refinedmods.refinedstorage.common.support.resource.FluidResource; -import com.refinedmods.refinedstorage.common.support.resource.ItemResource; - -import java.util.List; -import java.util.UUID; import net.minecraft.core.BlockPos; -import net.minecraft.core.component.DataComponentPatch; import net.minecraft.network.RegistryFriendlyByteBuf; import net.minecraft.network.chat.Component; import net.minecraft.network.codec.StreamEncoder; import net.minecraft.world.entity.player.Inventory; import net.minecraft.world.entity.player.Player; import net.minecraft.world.inventory.AbstractContainerMenu; -import net.minecraft.world.item.ItemStack; -import net.minecraft.world.item.Items; import net.minecraft.world.level.block.state.BlockState; -import net.minecraft.world.level.material.Fluids; public class AutocraftingMonitorBlockEntity extends AbstractBaseNetworkNodeContainerBlockEntity implements NetworkNodeExtendedMenuProvider { @@ -48,289 +38,7 @@ protected boolean doesBlockStateChangeWarrantNetworkNodeUpdate(final BlockState @Override public AutocraftingMonitorData getMenuData() { - return new AutocraftingMonitorData(List.of( - new AutocraftingTaskStatus( - new AutocraftingTaskStatus.Id( - UUID.randomUUID(), - ItemResource.ofItemStack(new ItemStack(Items.DIAMOND)), - 64, - System.currentTimeMillis() - ), - List.of( - new AutocraftingTaskStatus.Element( - AutocraftingTaskStatus.ElementType.NORMAL, - ItemResource.ofItemStack(new ItemStack(Items.DIAMOND)), - 1, - 0, - 0, - 0, - 0 - ), - new AutocraftingTaskStatus.Element( - AutocraftingTaskStatus.ElementType.NORMAL, - ItemResource.ofItemStack(new ItemStack(Items.DIAMOND)), - 1, - 1, - 0, - 0, - 0 - ), - new AutocraftingTaskStatus.Element( - AutocraftingTaskStatus.ElementType.NORMAL, - ItemResource.ofItemStack(new ItemStack(Items.DIAMOND)), - 1, - 0, - 1, - 0, - 0 - ), - new AutocraftingTaskStatus.Element( - AutocraftingTaskStatus.ElementType.NORMAL, - ItemResource.ofItemStack(new ItemStack(Items.DIAMOND)), - 1, - 0, - 0, - 1, - 0 - ), - new AutocraftingTaskStatus.Element( - AutocraftingTaskStatus.ElementType.NORMAL, - ItemResource.ofItemStack(new ItemStack(Items.DIAMOND)), - 1, - 0, - 0, - 0, - 1 - ), - new AutocraftingTaskStatus.Element( - AutocraftingTaskStatus.ElementType.AUTOCRAFTER_IS_LOCKED, - ItemResource.ofItemStack(new ItemStack(Items.DIAMOND)), - 1, - 0, - 0, - 0, - 0 - ), - new AutocraftingTaskStatus.Element( - AutocraftingTaskStatus.ElementType.NORMAL, - ItemResource.ofItemStack(new ItemStack(Items.DIAMOND)), - 1, - 0, - 0, - 0, - 0 - ), - new AutocraftingTaskStatus.Element( - AutocraftingTaskStatus.ElementType.NORMAL, - ItemResource.ofItemStack(new ItemStack(Items.DIAMOND)), - 1, - 1, - 0, - 0, - 0 - ), - new AutocraftingTaskStatus.Element( - AutocraftingTaskStatus.ElementType.NORMAL, - ItemResource.ofItemStack(new ItemStack(Items.DIAMOND)), - 1, - 0, - 1, - 0, - 0 - ), - new AutocraftingTaskStatus.Element( - AutocraftingTaskStatus.ElementType.NORMAL, - ItemResource.ofItemStack(new ItemStack(Items.DIAMOND)), - 1, - 0, - 0, - 1, - 0 - ), - new AutocraftingTaskStatus.Element( - AutocraftingTaskStatus.ElementType.NORMAL, - ItemResource.ofItemStack(new ItemStack(Items.DIAMOND)), - 1, - 0, - 0, - 0, - 1 - ), - new AutocraftingTaskStatus.Element( - AutocraftingTaskStatus.ElementType.AUTOCRAFTER_IS_LOCKED, - ItemResource.ofItemStack(new ItemStack(Items.DIAMOND)), - 1, - 0, - 0, - 0, - 0 - ), - new AutocraftingTaskStatus.Element( - AutocraftingTaskStatus.ElementType.NORMAL, - ItemResource.ofItemStack(new ItemStack(Items.DIAMOND)), - 1, - 0, - 0, - 0, - 0 - ), - new AutocraftingTaskStatus.Element( - AutocraftingTaskStatus.ElementType.NORMAL, - ItemResource.ofItemStack(new ItemStack(Items.DIAMOND)), - 5448748, - 1, - 0, - 0, - 0 - ), - new AutocraftingTaskStatus.Element( - AutocraftingTaskStatus.ElementType.NORMAL, - ItemResource.ofItemStack(new ItemStack(Items.DIAMOND)), - 1, - 0, - 1, - 0, - 0 - ), - new AutocraftingTaskStatus.Element( - AutocraftingTaskStatus.ElementType.NORMAL, - ItemResource.ofItemStack(new ItemStack(Items.DIAMOND)), - 1, - 0, - 0, - 1, - 0 - ), - new AutocraftingTaskStatus.Element( - AutocraftingTaskStatus.ElementType.NORMAL, - ItemResource.ofItemStack(new ItemStack(Items.DIAMOND)), - 1, - 0, - 0, - 0, - 1 - ), - new AutocraftingTaskStatus.Element( - AutocraftingTaskStatus.ElementType.AUTOCRAFTER_IS_LOCKED, - ItemResource.ofItemStack(new ItemStack(Items.DIAMOND)), - 10000, - 0, - 0, - 0, - 0 - ), - new AutocraftingTaskStatus.Element( - AutocraftingTaskStatus.ElementType.NORMAL, - ItemResource.ofItemStack(new ItemStack(Items.DIAMOND)), - 1, - 0, - 1, - 0, - 0 - ), - new AutocraftingTaskStatus.Element( - AutocraftingTaskStatus.ElementType.NORMAL, - ItemResource.ofItemStack(new ItemStack(Items.DIAMOND)), - 1, - 0, - 0, - 1, - 0 - ), - new AutocraftingTaskStatus.Element( - AutocraftingTaskStatus.ElementType.NORMAL, - ItemResource.ofItemStack(new ItemStack(Items.DIAMOND)), - 1, - 0, - 0, - 0, - 1 - ), - new AutocraftingTaskStatus.Element( - AutocraftingTaskStatus.ElementType.AUTOCRAFTER_IS_LOCKED, - ItemResource.ofItemStack(new ItemStack(Items.DIAMOND)), - 1, - 0, - 0, - 0, - 0 - ), - new AutocraftingTaskStatus.Element( - AutocraftingTaskStatus.ElementType.NORMAL, - ItemResource.ofItemStack(new ItemStack(Items.DIAMOND)), - 1, - 0, - 0, - 0, - 0 - ), - new AutocraftingTaskStatus.Element( - AutocraftingTaskStatus.ElementType.NORMAL, - ItemResource.ofItemStack(new ItemStack(Items.DIAMOND)), - 5448748, - 1, - 0, - 0, - 0 - ), - new AutocraftingTaskStatus.Element( - AutocraftingTaskStatus.ElementType.NORMAL, - ItemResource.ofItemStack(new ItemStack(Items.DIAMOND)), - 1, - 0, - 1, - 0, - 0 - ), - new AutocraftingTaskStatus.Element( - AutocraftingTaskStatus.ElementType.NORMAL, - ItemResource.ofItemStack(new ItemStack(Items.DIAMOND)), - 1, - 0, - 0, - 1, - 0 - ), - new AutocraftingTaskStatus.Element( - AutocraftingTaskStatus.ElementType.NORMAL, - ItemResource.ofItemStack(new ItemStack(Items.DIAMOND)), - 1, - 0, - 0, - 0, - 1 - ), - new AutocraftingTaskStatus.Element( - AutocraftingTaskStatus.ElementType.AUTOCRAFTER_IS_LOCKED, - ItemResource.ofItemStack(new ItemStack(Items.DIAMOND)), - 10000, - 0, - 0, - 0, - 0 - ) - ) - ), - new AutocraftingTaskStatus( - new AutocraftingTaskStatus.Id( - UUID.randomUUID(), - new FluidResource(Fluids.WATER, DataComponentPatch.EMPTY), - Platform.INSTANCE.getBucketAmount() * 2, - System.currentTimeMillis() - 3000 - ), - List.of( - new AutocraftingTaskStatus.Element( - AutocraftingTaskStatus.ElementType.NORMAL, - ItemResource.ofItemStack(new ItemStack(Items.DIRT)), - 6, - 7, - 8, - 9, - 10 - ) - ) - ) - )); + return new AutocraftingMonitorData(new TaskStatusProviderImpl().getStatuses()); } @Override @@ -340,6 +48,6 @@ public StreamEncoder getMenuCo @Override public AbstractContainerMenu createMenu(final int syncId, final Inventory inventory, final Player player) { - return new AutocraftingMonitorContainerMenu(syncId, this); + return new AutocraftingMonitorContainerMenu(syncId, player, new TaskStatusProviderImpl()); } } 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 287f74a17..15cc28f8d 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,8 +1,13 @@ package com.refinedmods.refinedstorage.common.autocrafting.monitor; -import com.refinedmods.refinedstorage.api.autocrafting.status.AutocraftingTaskStatus; +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.content.Menus; 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; import java.util.Collections; import java.util.List; @@ -10,67 +15,160 @@ import java.util.stream.Collectors; import javax.annotation.Nullable; -public class AutocraftingMonitorContainerMenu extends AbstractBaseContainerMenu { - private final Map> elementsByTaskId; - private final List tasks; - private final List tasksView; +import net.minecraft.server.level.ServerPlayer; +import net.minecraft.world.entity.player.Inventory; +import net.minecraft.world.entity.player.Player; + +public class AutocraftingMonitorContainerMenu extends AbstractBaseContainerMenu implements TaskStatusListener { + private final Map statusByTaskId; + private final List tasks; + private final List tasksView; + @Nullable + private final TaskStatusProvider taskStatusProvider; + private final Player player; @Nullable private AutocraftingMonitorListener listener; @Nullable - private AutocraftingTaskStatus.Id currentTaskId; + private TaskId currentTaskId; - public AutocraftingMonitorContainerMenu(final int syncId, final AutocraftingMonitorData data) { + public AutocraftingMonitorContainerMenu(final int syncId, + final Inventory playerInventory, + final AutocraftingMonitorData data) { super(Menus.INSTANCE.getAutocraftingMonitor(), syncId); - this.elementsByTaskId = data.statuses().stream().collect(Collectors.toMap( - AutocraftingTaskStatus::id, - AutocraftingTaskStatus::elements + this.statusByTaskId = data.statuses().stream().collect(Collectors.toMap( + s -> s.info().id(), + s -> s )); - this.tasks = data.statuses().stream().map(AutocraftingTaskStatus::id).collect(Collectors.toList()); + 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().id(); + this.currentTaskId = data.statuses().isEmpty() ? null : data.statuses().getFirst().info().id(); + this.taskStatusProvider = null; + this.player = playerInventory.player; } - AutocraftingMonitorContainerMenu(final int syncId, final AutocraftingMonitorBlockEntity autocraftingMonitor) { + AutocraftingMonitorContainerMenu(final int syncId, + final Player player, + final TaskStatusProvider taskStatusProvider) { super(Menus.INSTANCE.getAutocraftingMonitor(), syncId); - this.elementsByTaskId = Collections.emptyMap(); + this.statusByTaskId = Collections.emptyMap(); this.tasks = Collections.emptyList(); this.tasksView = Collections.emptyList(); this.currentTaskId = null; + this.taskStatusProvider = taskStatusProvider; + this.player = player; + taskStatusProvider.addListener(this); + } + + @Override + public void removed(final Player removedPlayer) { + super.removed(removedPlayer); + if (taskStatusProvider != null) { + taskStatusProvider.removeListener(this); + } + } + + @Override + public void broadcastChanges() { + super.broadcastChanges(); + if (taskStatusProvider instanceof TaskStatusProviderImpl taskStatusProviderImpl) { + taskStatusProviderImpl.testTick(); + } } void setListener(@Nullable final AutocraftingMonitorListener listener) { this.listener = listener; } - List getCurrentElements() { - return elementsByTaskId.getOrDefault(currentTaskId, Collections.emptyList()); + List getCurrentItems() { + final TaskStatus status = statusByTaskId.get(currentTaskId); + if (status == null) { + return Collections.emptyList(); + } + return status.items(); } - List getTasks() { + List getTasks() { return tasksView; } - void setCurrentTaskId(@Nullable final AutocraftingTaskStatus.Id taskId) { + float getPercentageCompleted(final TaskId taskId) { + final TaskStatus status = statusByTaskId.get(taskId); + return status == null ? 0 : status.percentageCompleted(); + } + + void setCurrentTaskId(@Nullable final TaskId taskId) { this.currentTaskId = taskId; loadCurrentTask(); } void loadCurrentTask() { if (listener != null) { - listener.taskChanged( - currentTaskId, - elementsByTaskId.getOrDefault(currentTaskId, Collections.emptyList()) - ); + listener.currentTaskChanged(currentTaskId == null ? null : statusByTaskId.get(currentTaskId)); + } + } + + @Override + public void taskStatusChanged(final TaskStatus status) { + if (taskStatusProvider != null && player instanceof ServerPlayer serverPlayer) { + S2CPackets.sendAutocraftingMonitorTaskStatusChanged(serverPlayer, status); + return; + } + statusByTaskId.put(status.info().id(), status); + } + + @Override + public void taskRemoved(final TaskId id) { + if (taskStatusProvider != null && player instanceof ServerPlayer serverPlayer) { + S2CPackets.sendAutocraftingMonitorTaskRemoved(serverPlayer, id); + return; + } + statusByTaskId.remove(id); + tasks.removeIf(task -> task.id().equals(id)); + if (listener != null) { + listener.taskRemoved(id); + } + if (id.equals(currentTaskId)) { + currentTaskId = tasks.isEmpty() ? null : tasks.getFirst().id(); + loadCurrentTask(); + } + } + + @Override + public void taskAdded(final TaskStatus status) { + if (taskStatusProvider != null && player instanceof ServerPlayer serverPlayer) { + S2CPackets.sendAutocraftingMonitorTaskAdded(serverPlayer, status); + return; + } + statusByTaskId.put(status.info().id(), status); + tasks.add(status.info()); + if (listener != null) { + listener.taskAdded(status); + } + if (currentTaskId == null) { + currentTaskId = status.info().id(); + loadCurrentTask(); + } + } + + public void cancelTask(final TaskId taskId) { + if (taskStatusProvider != null) { + taskStatusProvider.cancel(taskId); } } void cancelCurrentTask() { - // todo + if (currentTaskId != null) { + C2SPackets.sendAutocraftingMonitorCancel(currentTaskId); + } } - void cancelAllTasks() { - // todo + public void cancelAllTasks() { + if (taskStatusProvider != null) { + taskStatusProvider.cancelAll(); + } else { + C2SPackets.sendAutocraftingMonitorCancelAll(); + } } } diff --git a/refinedstorage-common/src/main/java/com/refinedmods/refinedstorage/common/autocrafting/monitor/AutocraftingMonitorData.java b/refinedstorage-common/src/main/java/com/refinedmods/refinedstorage/common/autocrafting/monitor/AutocraftingMonitorData.java index fecfa5793..24df0d6a0 100644 --- a/refinedstorage-common/src/main/java/com/refinedmods/refinedstorage/common/autocrafting/monitor/AutocraftingMonitorData.java +++ b/refinedstorage-common/src/main/java/com/refinedmods/refinedstorage/common/autocrafting/monitor/AutocraftingMonitorData.java @@ -1,69 +1,19 @@ package com.refinedmods.refinedstorage.common.autocrafting.monitor; -import com.refinedmods.refinedstorage.api.autocrafting.status.AutocraftingTaskStatus; -import com.refinedmods.refinedstorage.common.api.support.resource.PlatformResourceKey; -import com.refinedmods.refinedstorage.common.support.resource.ResourceCodecs; -import com.refinedmods.refinedstorage.common.util.PlatformUtil; +import com.refinedmods.refinedstorage.api.autocrafting.status.TaskStatus; import java.util.ArrayList; import java.util.List; -import io.netty.buffer.ByteBuf; -import net.minecraft.core.UUIDUtil; import net.minecraft.network.RegistryFriendlyByteBuf; import net.minecraft.network.codec.ByteBufCodecs; import net.minecraft.network.codec.StreamCodec; -public record AutocraftingMonitorData(List statuses) { - private static final StreamCodec - STATUS_ELEMENT_STREAM_CODEC = new StatusElementStreamCodec(); - private static final StreamCodec ID_STREAM_CODEC = - StreamCodec.composite( - UUIDUtil.STREAM_CODEC, AutocraftingTaskStatus.Id::id, - ResourceCodecs.STREAM_CODEC, s -> (PlatformResourceKey) s.resource(), - ByteBufCodecs.VAR_LONG, AutocraftingTaskStatus.Id::amount, - ByteBufCodecs.VAR_LONG, AutocraftingTaskStatus.Id::startTime, - AutocraftingTaskStatus.Id::new - ); - private static final StreamCodec STATUS_STREAM_CODEC = - StreamCodec.composite( - ID_STREAM_CODEC, AutocraftingTaskStatus::id, - ByteBufCodecs.collection(ArrayList::new, STATUS_ELEMENT_STREAM_CODEC), AutocraftingTaskStatus::elements, - AutocraftingTaskStatus::new - ); +public record AutocraftingMonitorData(List statuses) { public static final StreamCodec STREAM_CODEC = StreamCodec.composite( - ByteBufCodecs.collection(ArrayList::new, STATUS_STREAM_CODEC), AutocraftingMonitorData::statuses, + ByteBufCodecs.collection(ArrayList::new, AutocraftingMonitorStreamCodecs.STATUS_STREAM_CODEC), + AutocraftingMonitorData::statuses, AutocraftingMonitorData::new ); - - private static class StatusElementStreamCodec - implements StreamCodec { - private static final StreamCodec ELEMENT_TYPE_STREAM_CODEC - = PlatformUtil.enumStreamCodec(AutocraftingTaskStatus.ElementType.values()); - - @Override - public AutocraftingTaskStatus.Element decode(final RegistryFriendlyByteBuf buf) { - return new AutocraftingTaskStatus.Element( - ELEMENT_TYPE_STREAM_CODEC.decode(buf), - ResourceCodecs.STREAM_CODEC.decode(buf), - buf.readLong(), - buf.readLong(), - buf.readLong(), - buf.readLong(), - buf.readLong() - ); - } - - @Override - public void encode(final RegistryFriendlyByteBuf buf, final AutocraftingTaskStatus.Element element) { - ELEMENT_TYPE_STREAM_CODEC.encode(buf, element.type()); - ResourceCodecs.STREAM_CODEC.encode(buf, (PlatformResourceKey) element.resource()); - buf.writeLong(element.stored()); - buf.writeLong(element.missing()); - buf.writeLong(element.processing()); - buf.writeLong(element.scheduled()); - buf.writeLong(element.crafting()); - } - } } diff --git a/refinedstorage-common/src/main/java/com/refinedmods/refinedstorage/common/autocrafting/monitor/AutocraftingMonitorListener.java b/refinedstorage-common/src/main/java/com/refinedmods/refinedstorage/common/autocrafting/monitor/AutocraftingMonitorListener.java index ff7800eac..e5fc4f557 100644 --- a/refinedstorage-common/src/main/java/com/refinedmods/refinedstorage/common/autocrafting/monitor/AutocraftingMonitorListener.java +++ b/refinedstorage-common/src/main/java/com/refinedmods/refinedstorage/common/autocrafting/monitor/AutocraftingMonitorListener.java @@ -1,12 +1,14 @@ package com.refinedmods.refinedstorage.common.autocrafting.monitor; -import com.refinedmods.refinedstorage.api.autocrafting.status.AutocraftingTaskStatus; +import com.refinedmods.refinedstorage.api.autocrafting.TaskId; +import com.refinedmods.refinedstorage.api.autocrafting.status.TaskStatus; -import java.util.List; import javax.annotation.Nullable; -@FunctionalInterface interface AutocraftingMonitorListener { - void taskChanged(@Nullable AutocraftingTaskStatus.Id id, - List elements); + void currentTaskChanged(@Nullable TaskStatus taskStatus); + + void taskAdded(TaskStatus taskStatus); + + void taskRemoved(TaskId taskId); } 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 fb1713441..5af4f1cb5 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 @@ -1,6 +1,7 @@ package com.refinedmods.refinedstorage.common.autocrafting.monitor; -import com.refinedmods.refinedstorage.api.autocrafting.status.AutocraftingTaskStatus; +import com.refinedmods.refinedstorage.api.autocrafting.TaskId; +import com.refinedmods.refinedstorage.api.autocrafting.status.TaskStatus; import com.refinedmods.refinedstorage.common.api.RefinedStorageApi; import com.refinedmods.refinedstorage.common.api.support.resource.ResourceRendering; import com.refinedmods.refinedstorage.common.support.AbstractBaseScreen; @@ -34,7 +35,7 @@ public class AutocraftingMonitorScreen extends AbstractBaseScreen { final int scrollOffset = taskButtonsScrollbar.isSmoothScrolling() @@ -141,13 +142,14 @@ private void initTaskButtons() { }); updateTaskButtonsScrollbar(); for (int i = 0; i < getMenu().getTasks().size(); ++i) { - final AutocraftingTaskStatus.Id taskId = getMenu().getTasks().get(i); + final TaskStatus.TaskInfo taskId = getMenu().getTasks().get(i); final int buttonY = getTaskButtonY(i); final AutocraftingTaskButton button = new AutocraftingTaskButton( getTaskButtonsInnerX(), buttonY, taskId, - menu::setCurrentTaskId + menu::setCurrentTaskId, + menu::getPercentageCompleted ); button.visible = isTaskButtonVisible(buttonY); taskButtons.add(addWidget(button)); @@ -166,8 +168,8 @@ private int getTaskButtonY(final int i) { @Override public void render(final GuiGraphics graphics, final int mouseX, final int mouseY, final float partialTicks) { super.render(graphics, mouseX, mouseY, partialTicks); - if (taskElementsScrollbar != null) { - taskElementsScrollbar.render(graphics, mouseX, mouseY, partialTicks); + if (taskItemsScrollbar != null) { + taskItemsScrollbar.render(graphics, mouseX, mouseY, partialTicks); } if (taskButtonsScrollbar != null) { taskButtonsScrollbar.render(graphics, mouseX, mouseY, partialTicks); @@ -191,20 +193,20 @@ public void render(final GuiGraphics graphics, final int mouseX, final int mouse protected void renderBg(final GuiGraphics graphics, final float delta, final int mouseX, final int mouseY) { super.renderBg(graphics, delta, mouseX, mouseY); graphics.blitSprite(TASKS, leftPos - TASKS_WIDTH + 4, topPos, TASKS_WIDTH, TASKS_HEIGHT); - final List elements = getMenu().getCurrentElements(); - if (elements.isEmpty() || taskElementsScrollbar == null) { + final List items = getMenu().getCurrentItems(); + if (items.isEmpty() || taskItemsScrollbar == null) { return; } final int x = leftPos + 8; final int y = topPos + 20; - graphics.enableScissor(x, y, x + 221, y + ELEMENTS_AREA_HEIGHT); - final int rows = Math.ceilDiv(elements.size(), COLUMNS); + graphics.enableScissor(x, y, x + 221, y + ITEMS_AREA_HEIGHT); + final int rows = Math.ceilDiv(items.size(), COLUMNS); for (int i = 0; i < rows; ++i) { - final int scrollOffset = taskElementsScrollbar.isSmoothScrolling() - ? (int) taskElementsScrollbar.getOffset() - : (int) taskElementsScrollbar.getOffset() * ROW_HEIGHT; + final int scrollOffset = taskItemsScrollbar.isSmoothScrolling() + ? (int) taskItemsScrollbar.getOffset() + : (int) taskItemsScrollbar.getOffset() * ROW_HEIGHT; final int yy = y + (i * ROW_HEIGHT) - scrollOffset; - renderRow(graphics, x, yy, i, elements, mouseX, mouseY); + renderRow(graphics, x, yy, i, items, mouseX, mouseY); } graphics.disableScissor(); } @@ -213,69 +215,69 @@ private void renderRow(final GuiGraphics graphics, final int x, final int y, final int i, - final List elements, + final List items, final double mouseX, final double mouseY) { - if (y <= topPos + 20 - ROW_HEIGHT || y > topPos + 20 + ELEMENTS_AREA_HEIGHT) { + if (y <= topPos + 20 - ROW_HEIGHT || y > topPos + 20 + ITEMS_AREA_HEIGHT) { return; } graphics.blitSprite(ROW, x, y, ROW_WIDTH, ROW_HEIGHT); - for (int column = i * COLUMNS; column < Math.min(i * COLUMNS + COLUMNS, elements.size()); ++column) { - final AutocraftingTaskStatus.Element element = elements.get(column); + for (int column = i * COLUMNS; column < Math.min(i * COLUMNS + COLUMNS, items.size()); ++column) { + final TaskStatus.Item item = items.get(column); final int xx = x + (column % COLUMNS) * 74; - renderElement(graphics, xx, y, element, mouseX, mouseY); + renderItem(graphics, xx, y, item, mouseX, mouseY); } } - private static int getElementColor(final AutocraftingTaskStatus.Element element) { - if (element.missing() > 0) { + private static int getItemColor(final TaskStatus.Item item) { + if (item.missing() > 0) { return MISSING_COLOR; } - return getElementColor2(element); + return getItemColor2(item); } - private static int getElementColor2(final AutocraftingTaskStatus.Element element) { - if (element.processing() > 0) { + private static int getItemColor2(final TaskStatus.Item item) { + if (item.processing() > 0) { return PROCESSING_COLOR; } - if (element.scheduled() > 0) { + if (item.scheduled() > 0) { return SCHEDULED_COLOR; } - if (element.crafting() > 0) { + if (item.crafting() > 0) { return CRAFTING_COLOR; } return 0; } - private void renderElement(final GuiGraphics graphics, - final int x, - final int y, - final AutocraftingTaskStatus.Element elem, - final double mouseX, - final double mouseY) { - final int color = getElementColor(elem); + private void renderItem(final GuiGraphics graphics, + final int x, + final int y, + final TaskStatus.Item item, + final double mouseX, + final double mouseY) { + final int color = getItemColor(item); if (color != 0) { graphics.fill(x, y, x + 73, y + 29, color); } - if (elem.type() != AutocraftingTaskStatus.ElementType.NORMAL) { - renderElementErrorIcon(graphics, x, y); + if (item.type() != TaskStatus.ItemType.NORMAL) { + renderItemErrorIcon(graphics, x, y); } int xx = x + 2; - final ResourceRendering rendering = RefinedStorageApi.INSTANCE.getResourceRendering(elem.resource().getClass()); + final ResourceRendering rendering = RefinedStorageApi.INSTANCE.getResourceRendering(item.resource().getClass()); int yy = y + 7; - rendering.render(elem.resource(), graphics, xx, yy); + rendering.render(item.resource(), graphics, xx, yy); if (isHovering(x - leftPos, y - topPos, 73, 29, mouseX, mouseY) - && isHoveringOverTaskElements(mouseX, mouseY)) { - setTooltipForNextRenderPass(getElementTooltip(elem, rendering)); + && isHoveringOverItems(mouseX, mouseY)) { + setTooltipForNextRenderPass(getItemTooltip(item, rendering)); } if (!SmallText.isSmall()) { yy -= 2; } xx += 16 + 3; - renderElementText(graphics, elem, rendering, xx, yy); + renderItemText(graphics, item, rendering, xx, yy); } - private static void renderElementErrorIcon(final GuiGraphics graphics, final int x, final int y) { + private static void renderItemErrorIcon(final GuiGraphics graphics, final int x, final int y) { graphics.blitSprite( ERROR, x + 73 - ERROR_SIZE - 3, @@ -285,18 +287,17 @@ private static void renderElementErrorIcon(final GuiGraphics graphics, final int ); } - private List getElementTooltip(final AutocraftingTaskStatus.Element element, - final ResourceRendering rendering) { - final List tooltip = rendering.getTooltip(element.resource()).stream() + private List getItemTooltip(final TaskStatus.Item item, final ResourceRendering rendering) { + final List tooltip = rendering.getTooltip(item.resource()).stream() .map(Component::getVisualOrderText) .collect(Collectors.toList()); - if (element.type() != AutocraftingTaskStatus.ElementType.NORMAL) { - tooltip.add(getErrorTooltip(element.type()).getVisualOrderText()); + if (item.type() != TaskStatus.ItemType.NORMAL) { + tooltip.add(getErrorTooltip(item.type()).getVisualOrderText()); } return tooltip; } - private Component getErrorTooltip(final AutocraftingTaskStatus.ElementType type) { + private Component getErrorTooltip(final TaskStatus.ItemType type) { return switch (type) { case MACHINE_DOES_NOT_ACCEPT_RESOURCE -> MACHINE_DOES_NOT_ACCEPT_RESOURCE; case NO_MACHINE_FOUND -> NO_MACHINE_FOUND; @@ -305,39 +306,39 @@ private Component getErrorTooltip(final AutocraftingTaskStatus.ElementType type) }; } - private void renderElementText(final GuiGraphics graphics, - final AutocraftingTaskStatus.Element element, - final ResourceRendering rendering, - final int x, - final int y) { + private void renderItemText(final GuiGraphics graphics, + final TaskStatus.Item item, + final ResourceRendering rendering, + final int x, + final int y) { int yy = y; - if (element.stored() > 0) { - renderElementText(graphics, "stored", rendering, x, yy, element.missing()); + if (item.stored() > 0) { + renderItemText(graphics, "stored", rendering, x, yy, item.missing()); yy += 7; } - if (element.missing() > 0) { - renderElementText(graphics, "missing", rendering, x, yy, element.missing()); + if (item.missing() > 0) { + renderItemText(graphics, "missing", rendering, x, yy, item.missing()); yy += 7; } - if (element.processing() > 0) { - renderElementText(graphics, "processing", rendering, x, yy, element.processing()); + if (item.processing() > 0) { + renderItemText(graphics, "processing", rendering, x, yy, item.processing()); yy += 7; } - if (element.scheduled() > 0) { - renderElementText(graphics, "scheduled", rendering, x, yy, element.scheduled()); + if (item.scheduled() > 0) { + renderItemText(graphics, "scheduled", rendering, x, yy, item.scheduled()); yy += 7; } - if (element.crafting() > 0) { - renderElementText(graphics, "crafting", rendering, x, yy, element.crafting()); + if (item.crafting() > 0) { + renderItemText(graphics, "crafting", rendering, x, yy, item.crafting()); } } - private void renderElementText(final GuiGraphics graphics, - final String type, - final ResourceRendering rendering, - final int x, - final int y, - final long amount) { + private void renderItemText(final GuiGraphics graphics, + final String type, + final ResourceRendering rendering, + final int x, + final int y, + final long amount) { SmallText.render( graphics, font, @@ -352,8 +353,8 @@ private void renderElementText(final GuiGraphics graphics, @Override public boolean mouseClicked(final double mouseX, final double mouseY, final int clickedButton) { - if (taskElementsScrollbar != null - && taskElementsScrollbar.mouseClicked(mouseX, mouseY, clickedButton)) { + if (taskItemsScrollbar != null + && taskItemsScrollbar.mouseClicked(mouseX, mouseY, clickedButton)) { return true; } if (taskButtonsScrollbar != null && taskButtonsScrollbar.mouseClicked(mouseX, mouseY, clickedButton)) { @@ -364,8 +365,8 @@ public boolean mouseClicked(final double mouseX, final double mouseY, final int @Override public void mouseMoved(final double mx, final double my) { - if (taskElementsScrollbar != null) { - taskElementsScrollbar.mouseMoved(mx, my); + if (taskItemsScrollbar != null) { + taskItemsScrollbar.mouseMoved(mx, my); } if (taskButtonsScrollbar != null) { taskButtonsScrollbar.mouseMoved(mx, my); @@ -375,7 +376,7 @@ public void mouseMoved(final double mx, final double my) { @Override public boolean mouseReleased(final double mx, final double my, final int button) { - if (taskElementsScrollbar != null && taskElementsScrollbar.mouseReleased(mx, my, button)) { + if (taskItemsScrollbar != null && taskItemsScrollbar.mouseReleased(mx, my, button)) { return true; } if (taskButtonsScrollbar != null && taskButtonsScrollbar.mouseReleased(mx, my, button)) { @@ -386,24 +387,24 @@ public boolean mouseReleased(final double mx, final double my, final int button) @Override public boolean mouseScrolled(final double x, final double y, final double z, final double delta) { - final boolean didTaskElementsScrollbar = taskElementsScrollbar != null - && isHoveringOverTaskElements(x, y) - && taskElementsScrollbar.mouseScrolled(x, y, z, delta); - final boolean didTaskButtonsScrollbar = !didTaskElementsScrollbar + final boolean didTaskItemsScrollbar = taskItemsScrollbar != null + && isHoveringOverItems(x, y) + && taskItemsScrollbar.mouseScrolled(x, y, z, delta); + final boolean didTaskButtonsScrollbar = !didTaskItemsScrollbar && taskButtonsScrollbar != null && isHoveringOverTaskButtons(x, y) && taskButtonsScrollbar.mouseScrolled(x, y, z, delta); - return didTaskElementsScrollbar || didTaskButtonsScrollbar || super.mouseScrolled(x, y, z, delta); + return didTaskItemsScrollbar || didTaskButtonsScrollbar || super.mouseScrolled(x, y, z, delta); } - private boolean isHoveringOverTaskElements(final double x, final double y) { - return isHovering(8, 20, 221, ELEMENTS_AREA_HEIGHT, x, y); + private boolean isHoveringOverItems(final double x, final double y) { + return isHovering(8, 20, 221, ITEMS_AREA_HEIGHT, x, y); } private boolean isHoveringOverTaskButtons(final double x, final double y) { final int tasksInnerX = getTaskButtonsInnerX() - 1; final int tasksInnerY = getTaskButtonsInnerY() - 1; - return isHovering(tasksInnerX - leftPos, tasksInnerY - topPos, 80, 98, x, y); + return isHovering(tasksInnerX - leftPos, tasksInnerY - topPos, 80, 170, x, y); } private int getTaskButtonsInnerY() { @@ -437,28 +438,56 @@ protected ResourceLocation getTexture() { } @Override - public void taskChanged(@Nullable final AutocraftingTaskStatus.Id taskId, - final List elements) { + public void currentTaskChanged(@Nullable final TaskStatus taskStatus) { if (cancelButton != null) { - cancelButton.active = taskId != null; + cancelButton.active = taskStatus != null; } if (cancelAllButton != null) { - cancelAllButton.active = taskId != null; + cancelAllButton.active = !menu.getTasks().isEmpty(); } for (final AutocraftingTaskButton taskButton : taskButtons) { - taskButton.active = taskButton.getTaskId() != taskId; + taskButton.active = taskStatus == null || !taskButton.getTaskId().equals(taskStatus.info().id()); } - if (taskElementsScrollbar == null) { + if (taskItemsScrollbar == null) { return; } - if (taskId == null) { - taskElementsScrollbar.setEnabled(false); - taskElementsScrollbar.setMaxOffset(0); + if (taskStatus == null) { + taskItemsScrollbar.setEnabled(false); + taskItemsScrollbar.setMaxOffset(0); return; } - final int items = elements.size(); + final int items = taskStatus.items().size(); final int rows = Math.ceilDiv(items, COLUMNS) - ROWS_VISIBLE; - taskElementsScrollbar.setMaxOffset(taskElementsScrollbar.isSmoothScrolling() ? rows * ROW_HEIGHT : rows); - taskElementsScrollbar.setEnabled(rows > 0); + taskItemsScrollbar.setMaxOffset(taskItemsScrollbar.isSmoothScrolling() ? rows * ROW_HEIGHT : rows); + taskItemsScrollbar.setEnabled(rows > 0); + } + + @Override + public void taskAdded(final TaskStatus taskStatus) { + updateTaskButtonsScrollbar(); + final int buttonY = getTaskButtonY(getMenu().getTasks().size() - 1); + final AutocraftingTaskButton button = new AutocraftingTaskButton( + getTaskButtonsInnerX(), + buttonY, + taskStatus.info(), + menu::setCurrentTaskId, + menu::getPercentageCompleted + ); + button.visible = isTaskButtonVisible(buttonY); + taskButtons.add(addWidget(button)); + } + + @Override + public void taskRemoved(final TaskId taskId) { + updateTaskButtonsScrollbar(); + taskButtons.stream().filter(b -> b.getTaskId().equals(taskId)).findFirst().ifPresent(button -> { + removeWidget(button); + taskButtons.remove(button); + }); + for (int i = 0; i < taskButtons.size(); i++) { + final AutocraftingTaskButton button = taskButtons.get(i); + button.setY(getTaskButtonY(i)); + button.visible = isTaskButtonVisible(button.getY()); + } } } diff --git a/refinedstorage-common/src/main/java/com/refinedmods/refinedstorage/common/autocrafting/monitor/AutocraftingMonitorStreamCodecs.java b/refinedstorage-common/src/main/java/com/refinedmods/refinedstorage/common/autocrafting/monitor/AutocraftingMonitorStreamCodecs.java new file mode 100644 index 000000000..24f2d8ce9 --- /dev/null +++ b/refinedstorage-common/src/main/java/com/refinedmods/refinedstorage/common/autocrafting/monitor/AutocraftingMonitorStreamCodecs.java @@ -0,0 +1,72 @@ +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.common.api.support.resource.PlatformResourceKey; +import com.refinedmods.refinedstorage.common.support.resource.ResourceCodecs; +import com.refinedmods.refinedstorage.common.util.PlatformUtil; + +import java.util.ArrayList; + +import io.netty.buffer.ByteBuf; +import net.minecraft.core.UUIDUtil; +import net.minecraft.network.RegistryFriendlyByteBuf; +import net.minecraft.network.codec.ByteBufCodecs; +import net.minecraft.network.codec.StreamCodec; + +public final class AutocraftingMonitorStreamCodecs { + public static final StreamCodec TASK_ID_STREAM_CODEC = StreamCodec.composite( + UUIDUtil.STREAM_CODEC, TaskId::id, + TaskId::new + ); + private static final StreamCodec STATUS_ITEM_STREAM_CODEC = + new StatusItemStreamCodec(); + private static final StreamCodec INFO_STREAM_CODEC = + StreamCodec.composite( + TASK_ID_STREAM_CODEC, TaskStatus.TaskInfo::id, + ResourceCodecs.STREAM_CODEC, s -> (PlatformResourceKey) s.resource(), + ByteBufCodecs.VAR_LONG, TaskStatus.TaskInfo::amount, + ByteBufCodecs.VAR_LONG, TaskStatus.TaskInfo::startTime, + TaskStatus.TaskInfo::new + ); + public static final StreamCodec STATUS_STREAM_CODEC = + StreamCodec.composite( + INFO_STREAM_CODEC, TaskStatus::info, + ByteBufCodecs.FLOAT, TaskStatus::percentageCompleted, + ByteBufCodecs.collection(ArrayList::new, STATUS_ITEM_STREAM_CODEC), TaskStatus::items, + TaskStatus::new + ); + + private AutocraftingMonitorStreamCodecs() { + } + + private static class StatusItemStreamCodec implements StreamCodec { + private static final StreamCodec TYPE_STREAM_CODEC = PlatformUtil.enumStreamCodec( + TaskStatus.ItemType.values() + ); + + @Override + public TaskStatus.Item decode(final RegistryFriendlyByteBuf buf) { + return new TaskStatus.Item( + TYPE_STREAM_CODEC.decode(buf), + ResourceCodecs.STREAM_CODEC.decode(buf), + buf.readLong(), + buf.readLong(), + buf.readLong(), + buf.readLong(), + buf.readLong() + ); + } + + @Override + public void encode(final RegistryFriendlyByteBuf buf, final TaskStatus.Item item) { + TYPE_STREAM_CODEC.encode(buf, item.type()); + ResourceCodecs.STREAM_CODEC.encode(buf, (PlatformResourceKey) item.resource()); + buf.writeLong(item.stored()); + buf.writeLong(item.missing()); + buf.writeLong(item.processing()); + buf.writeLong(item.scheduled()); + buf.writeLong(item.crafting()); + } + } +} diff --git a/refinedstorage-common/src/main/java/com/refinedmods/refinedstorage/common/autocrafting/monitor/AutocraftingTaskButton.java b/refinedstorage-common/src/main/java/com/refinedmods/refinedstorage/common/autocrafting/monitor/AutocraftingTaskButton.java index 14574ff70..cf6036ada 100644 --- a/refinedstorage-common/src/main/java/com/refinedmods/refinedstorage/common/autocrafting/monitor/AutocraftingTaskButton.java +++ b/refinedstorage-common/src/main/java/com/refinedmods/refinedstorage/common/autocrafting/monitor/AutocraftingTaskButton.java @@ -1,9 +1,11 @@ package com.refinedmods.refinedstorage.common.autocrafting.monitor; -import com.refinedmods.refinedstorage.api.autocrafting.status.AutocraftingTaskStatus; +import com.refinedmods.refinedstorage.api.autocrafting.TaskId; +import com.refinedmods.refinedstorage.api.autocrafting.status.TaskStatus; import com.refinedmods.refinedstorage.api.resource.ResourceKey; import com.refinedmods.refinedstorage.common.api.RefinedStorageApi; import com.refinedmods.refinedstorage.common.api.support.resource.ResourceRendering; +import com.refinedmods.refinedstorage.common.support.ResourceSlotRendering; import com.refinedmods.refinedstorage.common.support.tooltip.SmallText; import com.refinedmods.refinedstorage.common.support.widget.TextMarquee; @@ -21,30 +23,33 @@ import static com.refinedmods.refinedstorage.common.util.IdentifierUtil.createTranslation; class AutocraftingTaskButton extends AbstractButton { - private final AutocraftingTaskStatus.Id taskId; + private final TaskStatus.TaskInfo task; private final TextMarquee text; - private final Consumer onPress; + private final Consumer onPress; + private final PercentageCompletedProvider percentageCompletedProvider; AutocraftingTaskButton(final int x, final int y, - final AutocraftingTaskStatus.Id taskId, - final Consumer onPress) { + final TaskStatus.TaskInfo task, + final Consumer onPress, + final PercentageCompletedProvider percentageCompletedProvider) { super(x, y, TASK_BUTTON_WIDTH, TASK_BUTTON_HEIGHT, Component.empty()); - this.taskId = taskId; - final ResourceKey resource = taskId.resource(); + this.task = task; + final ResourceKey resource = task.resource(); final ResourceRendering rendering = RefinedStorageApi.INSTANCE.getResourceRendering(resource.getClass()); - this.text = new TextMarquee(Component.literal(rendering.formatAmount(taskId.amount(), true)) - .append(" ") - .append(rendering.getDisplayName(resource)), + this.text = new TextMarquee( + rendering.getDisplayName(resource), TASK_BUTTON_WIDTH - 16 - 4 - 4 - 4, 0xFFFFFF, true, - true); + true + ); this.onPress = onPress; + this.percentageCompletedProvider = percentageCompletedProvider; } - AutocraftingTaskStatus.Id getTaskId() { - return taskId; + TaskId getTaskId() { + return task.id(); } @Override @@ -53,18 +58,27 @@ protected void renderWidget(final GuiGraphics graphics, final int mouseY, final float partialTick) { super.renderWidget(graphics, mouseX, mouseY, partialTick); - final ResourceKey resource = taskId.resource(); - final ResourceRendering rendering = RefinedStorageApi.INSTANCE.getResourceRendering(resource.getClass()); - rendering.render(resource, graphics, getX() + 3, getY() + 4); + renderResourceIcon(graphics); final int yOffset = SmallText.isSmall() ? 5 : 3; final int textX = getX() + 3 + 16 + 3; final int textY = getY() + yOffset; text.render(graphics, textX, textY, Minecraft.getInstance().font, isHovered); final int ySpacing = SmallText.isSmall() ? 7 : 8; - SmallText.render(graphics, Minecraft.getInstance().font, "69%", textX, textY + ySpacing, 0xFFFFFF, true); + final int percentageCompleted = Math.round(percentageCompletedProvider.getPercentageCompleted(task.id()) * 100); + SmallText.render(graphics, Minecraft.getInstance().font, percentageCompleted + "%", textX, textY + ySpacing, + 0xFFFFFF, true); updateTooltip(); } + private void renderResourceIcon(final GuiGraphics graphics) { + final ResourceKey resource = task.resource(); + final ResourceRendering rendering = RefinedStorageApi.INSTANCE.getResourceRendering(resource.getClass()); + final int resourceX = getX() + 3; + final int resourceY = getY() + 4; + rendering.render(resource, graphics, resourceX, resourceY); + ResourceSlotRendering.renderAmount(graphics, resourceX, resourceY, task.amount(), rendering); + } + private void updateTooltip() { if (isHovered) { final String runningTime = getRunningTimeText(); @@ -77,7 +91,7 @@ private void updateTooltip() { } private String getRunningTimeText() { - final int totalSecs = (int) (System.currentTimeMillis() - taskId.startTime()) / 1000; + final int totalSecs = (int) (System.currentTimeMillis() - task.startTime()) / 1000; final int hours = totalSecs / 3600; final int minutes = (totalSecs % 3600) / 60; final int seconds = totalSecs % 60; @@ -92,11 +106,15 @@ private String getRunningTimeText() { @Override public void onPress() { - onPress.accept(taskId); + onPress.accept(task.id()); } @Override protected void updateWidgetNarration(final NarrationElementOutput narrationElementOutput) { // no op } + + interface PercentageCompletedProvider { + float getPercentageCompleted(TaskId taskId); + } } 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 new file mode 100644 index 000000000..23ff57450 --- /dev/null +++ b/refinedstorage-common/src/main/java/com/refinedmods/refinedstorage/common/autocrafting/monitor/TaskStatusProviderImpl.java @@ -0,0 +1,121 @@ +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.autocrafting.status.TaskStatusProvider; +import com.refinedmods.refinedstorage.common.support.resource.ItemResource; + +import java.util.ArrayList; +import java.util.Collections; +import java.util.HashSet; +import java.util.List; +import java.util.Random; +import java.util.Set; +import java.util.UUID; + +import net.minecraft.world.item.Item; +import net.minecraft.world.item.ItemStack; +import net.minecraft.world.item.Items; + +class TaskStatusProviderImpl implements TaskStatusProvider { + private static final Item[] ITEM_SET = new Item[] { + Items.DIRT, + Items.DIAMOND, + Items.GOLD_INGOT, + Items.ACACIA_WOOD, + Items.BEEHIVE + }; + + private final List statuses = new ArrayList<>(); + private final Set listeners = new HashSet<>(); + private final Random r = new Random(); + private int ticks; + + @Override + public List getStatuses() { + return Collections.unmodifiableList(statuses); + } + + @Override + public void addListener(final TaskStatusListener listener) { + listeners.add(listener); + } + + @Override + public void removeListener(final TaskStatusListener listener) { + listeners.remove(listener); + } + + @Override + public void cancel(final TaskId taskId) { + final TaskStatus status = statuses.stream() + .filter(s -> s.info().id().equals(taskId)) + .findFirst() + .orElse(null); + if (status != null) { + statuses.remove(status); + listeners.forEach(l -> l.taskRemoved(taskId)); + } + } + + @Override + public void cancelAll() { + final List copy = new ArrayList<>(statuses); + statuses.clear(); + copy.forEach(s -> listeners.forEach(l -> l.taskRemoved(s.info().id()))); + } + + void testTick() { + if (ticks++ % 10 != 0) { + return; + } + final int chance = r.nextInt(100); + if (chance < 50) { + final TaskStatus status = generateTaskStatus(); + statuses.add(status); + listeners.forEach(l -> l.taskAdded(status)); + } else if (chance < 85 && !statuses.isEmpty()) { + final int idx = r.nextInt(statuses.size()); + final TaskStatus status = statuses.get(idx); + final TaskStatus updated = new TaskStatus( + status.info(), + r.nextFloat(), + generateTaskStatus().items() + ); + statuses.set(idx, updated); + listeners.forEach(l -> l.taskStatusChanged(updated)); + } else if (chance > 85 && !statuses.isEmpty()) { + final int idx = r.nextInt(statuses.size()); + final TaskStatus status = statuses.remove(idx); + listeners.forEach(l -> l.taskRemoved(status.info().id())); + } + } + + private TaskStatus generateTaskStatus() { + final TaskStatus.TaskInfo taskInfo = new TaskStatus.TaskInfo( + new TaskId(UUID.randomUUID()), + ItemResource.ofItemStack(new ItemStack(ITEM_SET[r.nextInt(ITEM_SET.length)])), + 5 + r.nextInt(32), + System.currentTimeMillis() + ); + final float pct = r.nextFloat(); + final List items = new ArrayList<>(); + for (int i = 0; i < 3 + r.nextInt(30); i++) { + final int typeIdx = r.nextInt(TaskStatus.ItemType.values().length); + final TaskStatus.ItemType type = TaskStatus.ItemType.values()[typeIdx]; + final int idx = r.nextInt(5); + final int amount = 2 + r.nextInt(20); + items.add(new TaskStatus.Item( + type, + ItemResource.ofItemStack(new ItemStack(ITEM_SET[r.nextInt(ITEM_SET.length)])), + idx == 0 ? amount : 0, + idx == 1 ? amount : 0, + idx == 2 ? amount : 0, + idx == 3 ? amount : 0, + idx == 4 ? amount : 0 + )); + } + return new TaskStatus(taskInfo, pct, items); + } +} diff --git a/refinedstorage-common/src/main/java/com/refinedmods/refinedstorage/common/autocrafting/preview/AutocraftingPreviewContainerMenu.java b/refinedstorage-common/src/main/java/com/refinedmods/refinedstorage/common/autocrafting/preview/AutocraftingPreviewContainerMenu.java index 761ef1ae1..5bc763e4b 100644 --- a/refinedstorage-common/src/main/java/com/refinedmods/refinedstorage/common/autocrafting/preview/AutocraftingPreviewContainerMenu.java +++ b/refinedstorage-common/src/main/java/com/refinedmods/refinedstorage/common/autocrafting/preview/AutocraftingPreviewContainerMenu.java @@ -1,6 +1,6 @@ package com.refinedmods.refinedstorage.common.autocrafting.preview; -import com.refinedmods.refinedstorage.api.autocrafting.preview.AutocraftingPreview; +import com.refinedmods.refinedstorage.api.autocrafting.preview.Preview; import com.refinedmods.refinedstorage.api.resource.ResourceAmount; import com.refinedmods.refinedstorage.common.api.support.resource.ResourceContainer; import com.refinedmods.refinedstorage.common.support.containermenu.AbstractResourceContainerMenu; @@ -70,7 +70,7 @@ void amountChanged(final double amount) { } } - public void previewResponseReceived(final UUID id, final AutocraftingPreview preview) { + public void previewResponseReceived(final UUID id, final Preview preview) { if (!currentRequest.getId().equals(id)) { return; } diff --git a/refinedstorage-common/src/main/java/com/refinedmods/refinedstorage/common/autocrafting/preview/AutocraftingPreviewListener.java b/refinedstorage-common/src/main/java/com/refinedmods/refinedstorage/common/autocrafting/preview/AutocraftingPreviewListener.java index 1d3fb99f7..f2c528fbb 100644 --- a/refinedstorage-common/src/main/java/com/refinedmods/refinedstorage/common/autocrafting/preview/AutocraftingPreviewListener.java +++ b/refinedstorage-common/src/main/java/com/refinedmods/refinedstorage/common/autocrafting/preview/AutocraftingPreviewListener.java @@ -1,13 +1,13 @@ package com.refinedmods.refinedstorage.common.autocrafting.preview; -import com.refinedmods.refinedstorage.api.autocrafting.preview.AutocraftingPreview; +import com.refinedmods.refinedstorage.api.autocrafting.preview.Preview; import javax.annotation.Nullable; interface AutocraftingPreviewListener { void requestChanged(AutocraftingRequest request); - void previewChanged(@Nullable AutocraftingPreview preview); + void previewChanged(@Nullable Preview preview); void requestRemoved(AutocraftingRequest request, boolean last); } diff --git a/refinedstorage-common/src/main/java/com/refinedmods/refinedstorage/common/autocrafting/preview/AutocraftingPreviewScreen.java b/refinedstorage-common/src/main/java/com/refinedmods/refinedstorage/common/autocrafting/preview/AutocraftingPreviewScreen.java index de474f0b6..80e1b91db 100644 --- a/refinedstorage-common/src/main/java/com/refinedmods/refinedstorage/common/autocrafting/preview/AutocraftingPreviewScreen.java +++ b/refinedstorage-common/src/main/java/com/refinedmods/refinedstorage/common/autocrafting/preview/AutocraftingPreviewScreen.java @@ -1,8 +1,8 @@ package com.refinedmods.refinedstorage.common.autocrafting.preview; -import com.refinedmods.refinedstorage.api.autocrafting.preview.AutocraftingPreview; -import com.refinedmods.refinedstorage.api.autocrafting.preview.AutocraftingPreviewItem; -import com.refinedmods.refinedstorage.api.autocrafting.preview.AutocraftingPreviewType; +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.common.api.RefinedStorageApi; import com.refinedmods.refinedstorage.common.api.support.resource.ResourceRendering; import com.refinedmods.refinedstorage.common.support.amount.AbstractAmountScreen; @@ -192,7 +192,7 @@ private void setRequest(final AutocraftingRequest request) { setPreview(request.getPreview()); } - private void setPreview(@Nullable final AutocraftingPreview preview) { + private void setPreview(@Nullable final Preview preview) { if (previewItemsScrollbar == null || confirmButton == null) { return; } @@ -207,9 +207,9 @@ private void setPreview(@Nullable final AutocraftingPreview preview) { previewItemsScrollbar.setMaxOffset(previewItemsScrollbar.isSmoothScrolling() ? rows * ROW_HEIGHT : rows); previewItemsScrollbar.setEnabled(rows > 0); confirmButton.setMessage(START); - confirmButton.active = preview.type() == AutocraftingPreviewType.SUCCESS; - confirmButton.setError(preview.type() != AutocraftingPreviewType.SUCCESS); - confirmButton.setTooltip(preview.type() == AutocraftingPreviewType.MISSING_RESOURCES + confirmButton.active = preview.type() == PreviewType.SUCCESS; + confirmButton.setError(preview.type() != PreviewType.SUCCESS); + confirmButton.setTooltip(preview.type() == PreviewType.MISSING_RESOURCES ? Tooltip.create(MISSING_RESOURCES) : null); } @@ -247,14 +247,14 @@ protected void renderBg(final GuiGraphics graphics, final float delta, final int REQUESTS_HEIGHT); } final AutocraftingRequest request = getMenu().getCurrentRequest(); - final AutocraftingPreview preview = request.getPreview(); + final Preview preview = request.getPreview(); if (preview == null || previewItemsScrollbar == null) { return; } final int x = leftPos + 8; final int y = topPos + 98; graphics.enableScissor(x, y, x + 221, y + PREVIEW_AREA_HEIGHT); - final List items = preview.items(); + final List items = preview.items(); final int rows = Math.ceilDiv(items.size(), COLUMNS); for (int i = 0; i < rows; ++i) { final int scrollOffset = previewItemsScrollbar.isSmoothScrolling() @@ -270,7 +270,7 @@ private void renderRow(final GuiGraphics graphics, final int x, final int y, final int i, - final List items, + final List items, final double mouseX, final double mouseY) { if (y <= topPos + 98 - ROW_HEIGHT || y > topPos + 98 + PREVIEW_AREA_HEIGHT) { @@ -278,7 +278,7 @@ private void renderRow(final GuiGraphics graphics, } graphics.blitSprite(ROW, x, y, ROW_WIDTH, ROW_HEIGHT); for (int column = i * COLUMNS; column < Math.min(i * COLUMNS + COLUMNS, items.size()); ++column) { - final AutocraftingPreviewItem item = items.get(column); + final PreviewItem item = items.get(column); final int xx = x + (column % COLUMNS) * 74; renderCell(graphics, xx, y, item, mouseX, mouseY); } @@ -287,7 +287,7 @@ private void renderRow(final GuiGraphics graphics, private void renderCell(final GuiGraphics graphics, final int x, final int y, - final AutocraftingPreviewItem item, + final PreviewItem item, final double mouseX, final double mouseY) { if (item.missing() > 0) { @@ -462,7 +462,7 @@ public void requestChanged(final AutocraftingRequest request) { } @Override - public void previewChanged(@Nullable final AutocraftingPreview preview) { + public void previewChanged(@Nullable final Preview preview) { setPreview(preview); } @@ -472,7 +472,10 @@ public void requestRemoved(final AutocraftingRequest request, final boolean last close(); return; } - requestButtons.removeIf(requestButton -> requestButton.getRequest() == request); + requestButtons.stream().filter(btn -> btn.getRequest() == request).findFirst().ifPresent(btn -> { + removeWidget(btn); + requestButtons.remove(btn); + }); updateRequestsScrollbar(); for (int i = 0; i < requestButtons.size(); ++i) { final AutocraftingRequestButton requestButton = requestButtons.get(i); diff --git a/refinedstorage-common/src/main/java/com/refinedmods/refinedstorage/common/autocrafting/preview/AutocraftingRequest.java b/refinedstorage-common/src/main/java/com/refinedmods/refinedstorage/common/autocrafting/preview/AutocraftingRequest.java index 14c0db7d6..49cd311c1 100644 --- a/refinedstorage-common/src/main/java/com/refinedmods/refinedstorage/common/autocrafting/preview/AutocraftingRequest.java +++ b/refinedstorage-common/src/main/java/com/refinedmods/refinedstorage/common/autocrafting/preview/AutocraftingRequest.java @@ -1,6 +1,6 @@ package com.refinedmods.refinedstorage.common.autocrafting.preview; -import com.refinedmods.refinedstorage.api.autocrafting.preview.AutocraftingPreview; +import com.refinedmods.refinedstorage.api.autocrafting.preview.Preview; import com.refinedmods.refinedstorage.api.resource.ResourceAmount; import com.refinedmods.refinedstorage.api.resource.ResourceKey; import com.refinedmods.refinedstorage.common.api.support.resource.PlatformResourceKey; @@ -14,7 +14,7 @@ public class AutocraftingRequest { private final ResourceKey resource; private final double amount; @Nullable - private AutocraftingPreview preview; + private Preview preview; private long pendingPreviewAmount; private AutocraftingRequest(final UUID id, final ResourceKey resource, final double amount) { @@ -44,7 +44,7 @@ boolean sendPreviewRequest(final double previewAmount) { return true; } - void previewResponseReceived(final AutocraftingPreview previewReceived) { + void previewResponseReceived(final Preview previewReceived) { this.pendingPreviewAmount = 0; this.preview = previewReceived; } @@ -70,7 +70,7 @@ ResourceKey getResource() { } @Nullable - AutocraftingPreview getPreview() { + Preview getPreview() { return preview; } diff --git a/refinedstorage-common/src/main/java/com/refinedmods/refinedstorage/common/autocrafting/preview/AutocraftingRequestButton.java b/refinedstorage-common/src/main/java/com/refinedmods/refinedstorage/common/autocrafting/preview/AutocraftingRequestButton.java index 050bdc89a..dadb6d171 100644 --- a/refinedstorage-common/src/main/java/com/refinedmods/refinedstorage/common/autocrafting/preview/AutocraftingRequestButton.java +++ b/refinedstorage-common/src/main/java/com/refinedmods/refinedstorage/common/autocrafting/preview/AutocraftingRequestButton.java @@ -4,6 +4,7 @@ import com.refinedmods.refinedstorage.common.api.RefinedStorageApi; import com.refinedmods.refinedstorage.common.api.support.resource.PlatformResourceKey; import com.refinedmods.refinedstorage.common.api.support.resource.ResourceRendering; +import com.refinedmods.refinedstorage.common.support.ResourceSlotRendering; import com.refinedmods.refinedstorage.common.support.tooltip.SmallText; import com.refinedmods.refinedstorage.common.support.widget.TextMarquee; @@ -30,17 +31,14 @@ class AutocraftingRequestButton extends AbstractButton { super(x, y, REQUEST_BUTTON_WIDTH, REQUEST_BUTTON_HEIGHT, Component.empty()); this.request = request; final ResourceKey resource = request.getResource(); - final long normalizedAmount = resource instanceof PlatformResourceKey platformResourceKey - ? platformResourceKey.getResourceType().normalizeAmount(request.getAmount()) - : 0; final ResourceRendering rendering = RefinedStorageApi.INSTANCE.getResourceRendering(resource.getClass()); - this.text = new TextMarquee(Component.literal(rendering.formatAmount(normalizedAmount, true)) - .append(" ") - .append(rendering.getDisplayName(resource)), + this.text = new TextMarquee( + rendering.getDisplayName(resource), REQUEST_BUTTON_WIDTH - 16 - 4 - 4 - 4, 0xFFFFFF, true, - true); + true + ); this.onPress = onPress; } @@ -54,13 +52,23 @@ protected void renderWidget(final GuiGraphics graphics, final int mouseY, final float partialTick) { super.renderWidget(graphics, mouseX, mouseY, partialTick); - final ResourceKey resource = request.getResource(); - final ResourceRendering rendering = RefinedStorageApi.INSTANCE.getResourceRendering(resource.getClass()); - rendering.render(resource, graphics, getX() + 3, getY() + 4); + renderResourceIcon(graphics); final int yOffset = SmallText.isSmall() ? 8 : 5; text.render(graphics, getX() + 3 + 16 + 3, getY() + yOffset, Minecraft.getInstance().font, isHovered); } + private void renderResourceIcon(final GuiGraphics graphics) { + final ResourceKey resource = request.getResource(); + final ResourceRendering rendering = RefinedStorageApi.INSTANCE.getResourceRendering(resource.getClass()); + final int resourceX = getX() + 3; + final int resourceY = getY() + 4; + rendering.render(resource, graphics, resourceX, resourceY); + final long normalizedAmount = resource instanceof PlatformResourceKey platformResourceKey + ? platformResourceKey.getResourceType().normalizeAmount(request.getAmount()) + : 0; + ResourceSlotRendering.renderAmount(graphics, resourceX, resourceY, normalizedAmount, rendering); + } + @Override public void onPress() { onPress.accept(request); diff --git a/refinedstorage-common/src/main/java/com/refinedmods/refinedstorage/common/grid/AbstractGridBlockEntity.java b/refinedstorage-common/src/main/java/com/refinedmods/refinedstorage/common/grid/AbstractGridBlockEntity.java index 534a09739..bb1180725 100644 --- a/refinedstorage-common/src/main/java/com/refinedmods/refinedstorage/common/grid/AbstractGridBlockEntity.java +++ b/refinedstorage-common/src/main/java/com/refinedmods/refinedstorage/common/grid/AbstractGridBlockEntity.java @@ -1,6 +1,6 @@ package com.refinedmods.refinedstorage.common.grid; -import com.refinedmods.refinedstorage.api.autocrafting.preview.AutocraftingPreview; +import com.refinedmods.refinedstorage.api.autocrafting.preview.Preview; import com.refinedmods.refinedstorage.api.grid.operations.GridOperations; import com.refinedmods.refinedstorage.api.grid.watcher.GridWatcher; import com.refinedmods.refinedstorage.api.network.Network; @@ -101,7 +101,7 @@ public void removeWatcher(final GridWatcher watcher) { } @Override - public Optional getPreview(final ResourceKey resource, final long amount) { + public Optional getPreview(final ResourceKey resource, final long amount) { return Optional.ofNullable(mainNetworkNode.getNetwork()) .map(network -> network.getComponent(AutocraftingNetworkComponent.class)) .flatMap(component -> component.getPreview(resource, amount)); 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 63b35dcf8..2f9c52dfa 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 @@ -1,7 +1,7 @@ package com.refinedmods.refinedstorage.common.grid; -import com.refinedmods.refinedstorage.api.autocrafting.preview.AutocraftingPreview; -import com.refinedmods.refinedstorage.api.autocrafting.preview.AutocraftingPreviewProvider; +import com.refinedmods.refinedstorage.api.autocrafting.preview.Preview; +import com.refinedmods.refinedstorage.api.autocrafting.preview.PreviewProvider; import com.refinedmods.refinedstorage.api.autocrafting.Pattern; import com.refinedmods.refinedstorage.api.autocrafting.PatternRepository; import com.refinedmods.refinedstorage.api.autocrafting.PatternRepositoryImpl; @@ -62,7 +62,7 @@ public abstract class AbstractGridContainerMenu extends AbstractResourceContainerMenu implements GridWatcher, GridInsertionStrategy, GridExtractionStrategy, GridScrollingStrategy, ScreenSizeListener, - AutocraftingPreviewProvider { + PreviewProvider { private static final Logger LOGGER = LoggerFactory.getLogger(AbstractGridContainerMenu.class); private static final GridQueryParserImpl QUERY_PARSER = new GridQueryParserImpl( LexerTokenMappings.DEFAULT_MAPPINGS, @@ -489,7 +489,7 @@ protected ResourceKey getResourceForAutocraftableHint(final Slot slot) { } @Override - public Optional getPreview(final ResourceKey resource, final long amount) { + public Optional getPreview(final ResourceKey resource, final long amount) { return requireNonNull(grid).getPreview(resource, amount); } diff --git a/refinedstorage-common/src/main/java/com/refinedmods/refinedstorage/common/grid/WirelessGrid.java b/refinedstorage-common/src/main/java/com/refinedmods/refinedstorage/common/grid/WirelessGrid.java index 37389c5a3..e824e73f2 100644 --- a/refinedstorage-common/src/main/java/com/refinedmods/refinedstorage/common/grid/WirelessGrid.java +++ b/refinedstorage-common/src/main/java/com/refinedmods/refinedstorage/common/grid/WirelessGrid.java @@ -1,6 +1,6 @@ package com.refinedmods.refinedstorage.common.grid; -import com.refinedmods.refinedstorage.api.autocrafting.preview.AutocraftingPreview; +import com.refinedmods.refinedstorage.api.autocrafting.preview.Preview; import com.refinedmods.refinedstorage.api.grid.operations.GridOperations; import com.refinedmods.refinedstorage.api.grid.operations.NoopGridOperations; import com.refinedmods.refinedstorage.api.grid.watcher.GridWatcher; @@ -105,7 +105,7 @@ private GridOperations createGridOperations(final ResourceType resourceType, } @Override - public Optional getPreview(final ResourceKey resource, final long amount) { + public Optional getPreview(final ResourceKey resource, final long amount) { return context.resolveNetwork() .map(network -> network.getComponent(AutocraftingNetworkComponent.class)) .flatMap(component -> component.getPreview(resource, amount)); diff --git a/refinedstorage-common/src/main/java/com/refinedmods/refinedstorage/common/storage/portablegrid/PortableGrid.java b/refinedstorage-common/src/main/java/com/refinedmods/refinedstorage/common/storage/portablegrid/PortableGrid.java index b9757d581..87df2e7ae 100644 --- a/refinedstorage-common/src/main/java/com/refinedmods/refinedstorage/common/storage/portablegrid/PortableGrid.java +++ b/refinedstorage-common/src/main/java/com/refinedmods/refinedstorage/common/storage/portablegrid/PortableGrid.java @@ -1,6 +1,6 @@ package com.refinedmods.refinedstorage.common.storage.portablegrid; -import com.refinedmods.refinedstorage.api.autocrafting.preview.AutocraftingPreview; +import com.refinedmods.refinedstorage.api.autocrafting.preview.Preview; import com.refinedmods.refinedstorage.api.core.Action; import com.refinedmods.refinedstorage.api.grid.operations.GridOperations; import com.refinedmods.refinedstorage.api.grid.operations.NoopGridOperations; @@ -131,7 +131,7 @@ public GridOperations createOperations(final ResourceType resourceType, final Se } @Override - public Optional getPreview(final ResourceKey resource, final long amount) { + public Optional getPreview(final ResourceKey resource, final long amount) { return Optional.empty(); } diff --git a/refinedstorage-common/src/main/java/com/refinedmods/refinedstorage/common/storagemonitor/AutocraftingStorageMonitorContainerMenu.java b/refinedstorage-common/src/main/java/com/refinedmods/refinedstorage/common/storagemonitor/AutocraftingStorageMonitorContainerMenu.java index 976400998..edee1c301 100644 --- a/refinedstorage-common/src/main/java/com/refinedmods/refinedstorage/common/storagemonitor/AutocraftingStorageMonitorContainerMenu.java +++ b/refinedstorage-common/src/main/java/com/refinedmods/refinedstorage/common/storagemonitor/AutocraftingStorageMonitorContainerMenu.java @@ -1,7 +1,7 @@ package com.refinedmods.refinedstorage.common.storagemonitor; -import com.refinedmods.refinedstorage.api.autocrafting.preview.AutocraftingPreview; -import com.refinedmods.refinedstorage.api.autocrafting.preview.AutocraftingPreviewProvider; +import com.refinedmods.refinedstorage.api.autocrafting.preview.Preview; +import com.refinedmods.refinedstorage.api.autocrafting.preview.PreviewProvider; import com.refinedmods.refinedstorage.api.resource.ResourceAmount; import com.refinedmods.refinedstorage.api.resource.ResourceKey; import com.refinedmods.refinedstorage.common.api.support.resource.PlatformResourceKey; @@ -16,7 +16,7 @@ import static java.util.Objects.requireNonNull; public class AutocraftingStorageMonitorContainerMenu extends AutocraftingPreviewContainerMenu - implements AutocraftingPreviewProvider { + implements PreviewProvider { @Nullable private final StorageMonitorBlockEntity storageMonitor; @@ -39,7 +39,7 @@ private static List getRequests(final PlatformResourceKey r } @Override - public Optional getPreview(final ResourceKey resource, final long amount) { + public Optional getPreview(final ResourceKey resource, final long amount) { return requireNonNull(storageMonitor).getPreview(resource, amount); } diff --git a/refinedstorage-common/src/main/java/com/refinedmods/refinedstorage/common/storagemonitor/StorageMonitorBlockEntity.java b/refinedstorage-common/src/main/java/com/refinedmods/refinedstorage/common/storagemonitor/StorageMonitorBlockEntity.java index a13e72655..d3b078ab7 100644 --- a/refinedstorage-common/src/main/java/com/refinedmods/refinedstorage/common/storagemonitor/StorageMonitorBlockEntity.java +++ b/refinedstorage-common/src/main/java/com/refinedmods/refinedstorage/common/storagemonitor/StorageMonitorBlockEntity.java @@ -1,7 +1,7 @@ package com.refinedmods.refinedstorage.common.storagemonitor; -import com.refinedmods.refinedstorage.api.autocrafting.preview.AutocraftingPreview; -import com.refinedmods.refinedstorage.api.autocrafting.preview.AutocraftingPreviewProvider; +import com.refinedmods.refinedstorage.api.autocrafting.preview.Preview; +import com.refinedmods.refinedstorage.api.autocrafting.preview.PreviewProvider; import com.refinedmods.refinedstorage.api.network.Network; import com.refinedmods.refinedstorage.api.network.autocrafting.AutocraftingNetworkComponent; import com.refinedmods.refinedstorage.api.network.impl.node.SimpleNetworkNode; @@ -50,7 +50,7 @@ import org.slf4j.LoggerFactory; public class StorageMonitorBlockEntity extends AbstractBaseNetworkNodeContainerBlockEntity - implements NetworkNodeExtendedMenuProvider, AutocraftingPreviewProvider { + implements NetworkNodeExtendedMenuProvider, PreviewProvider { private static final Logger LOGGER = LoggerFactory.getLogger(StorageMonitorBlockEntity.class); private static final String TAG_CLIENT_FILTER = "cf"; @@ -367,7 +367,7 @@ protected boolean doesBlockStateChangeWarrantNetworkNodeUpdate(final BlockState } @Override - public Optional getPreview(final ResourceKey resource, final long amount) { + public Optional getPreview(final ResourceKey resource, final long amount) { return Optional.ofNullable(mainNetworkNode.getNetwork()) .map(network -> network.getComponent(AutocraftingNetworkComponent.class)) .flatMap(component -> component.getPreview(resource, amount)); diff --git a/refinedstorage-common/src/main/java/com/refinedmods/refinedstorage/common/support/packet/c2s/AutocraftingMonitorCancelAllPacket.java b/refinedstorage-common/src/main/java/com/refinedmods/refinedstorage/common/support/packet/c2s/AutocraftingMonitorCancelAllPacket.java new file mode 100644 index 000000000..94b07741e --- /dev/null +++ b/refinedstorage-common/src/main/java/com/refinedmods/refinedstorage/common/support/packet/c2s/AutocraftingMonitorCancelAllPacket.java @@ -0,0 +1,30 @@ +package com.refinedmods.refinedstorage.common.support.packet.c2s; + +import com.refinedmods.refinedstorage.common.autocrafting.monitor.AutocraftingMonitorContainerMenu; +import com.refinedmods.refinedstorage.common.support.packet.PacketContext; + +import net.minecraft.network.RegistryFriendlyByteBuf; +import net.minecraft.network.codec.StreamCodec; +import net.minecraft.network.protocol.common.custom.CustomPacketPayload; + +import static com.refinedmods.refinedstorage.common.util.IdentifierUtil.createIdentifier; + +public record AutocraftingMonitorCancelAllPacket() implements CustomPacketPayload { + public static final Type PACKET_TYPE = new Type<>( + createIdentifier("autocrafting_monitor_cancel_all") + ); + public static final AutocraftingMonitorCancelAllPacket INSTANCE = new AutocraftingMonitorCancelAllPacket(); + public static final StreamCodec STREAM_CODEC = + StreamCodec.unit(INSTANCE); + + public static void handle(final PacketContext ctx) { + if (ctx.getPlayer().containerMenu instanceof AutocraftingMonitorContainerMenu containerMenu) { + containerMenu.cancelAllTasks(); + } + } + + @Override + public Type type() { + return PACKET_TYPE; + } +} diff --git a/refinedstorage-common/src/main/java/com/refinedmods/refinedstorage/common/support/packet/c2s/AutocraftingMonitorCancelPacket.java b/refinedstorage-common/src/main/java/com/refinedmods/refinedstorage/common/support/packet/c2s/AutocraftingMonitorCancelPacket.java new file mode 100644 index 000000000..27f5dd090 --- /dev/null +++ b/refinedstorage-common/src/main/java/com/refinedmods/refinedstorage/common/support/packet/c2s/AutocraftingMonitorCancelPacket.java @@ -0,0 +1,34 @@ +package com.refinedmods.refinedstorage.common.support.packet.c2s; + +import com.refinedmods.refinedstorage.api.autocrafting.TaskId; +import com.refinedmods.refinedstorage.common.autocrafting.monitor.AutocraftingMonitorContainerMenu; +import com.refinedmods.refinedstorage.common.autocrafting.monitor.AutocraftingMonitorStreamCodecs; +import com.refinedmods.refinedstorage.common.support.packet.PacketContext; + +import net.minecraft.network.RegistryFriendlyByteBuf; +import net.minecraft.network.codec.StreamCodec; +import net.minecraft.network.protocol.common.custom.CustomPacketPayload; + +import static com.refinedmods.refinedstorage.common.util.IdentifierUtil.createIdentifier; + +public record AutocraftingMonitorCancelPacket(TaskId taskId) implements CustomPacketPayload { + public static final Type PACKET_TYPE = new Type<>( + createIdentifier("autocrafting_monitor_cancel") + ); + public static final StreamCodec STREAM_CODEC = + StreamCodec.composite( + AutocraftingMonitorStreamCodecs.TASK_ID_STREAM_CODEC, AutocraftingMonitorCancelPacket::taskId, + AutocraftingMonitorCancelPacket::new + ); + + public static void handle(final AutocraftingMonitorCancelPacket packet, final PacketContext ctx) { + if (ctx.getPlayer().containerMenu instanceof AutocraftingMonitorContainerMenu containerMenu) { + containerMenu.cancelTask(packet.taskId()); + } + } + + @Override + public Type type() { + return PACKET_TYPE; + } +} diff --git a/refinedstorage-common/src/main/java/com/refinedmods/refinedstorage/common/support/packet/c2s/AutocraftingPreviewRequestPacket.java b/refinedstorage-common/src/main/java/com/refinedmods/refinedstorage/common/support/packet/c2s/AutocraftingPreviewRequestPacket.java index b08a6c3b6..2da3a407e 100644 --- a/refinedstorage-common/src/main/java/com/refinedmods/refinedstorage/common/support/packet/c2s/AutocraftingPreviewRequestPacket.java +++ b/refinedstorage-common/src/main/java/com/refinedmods/refinedstorage/common/support/packet/c2s/AutocraftingPreviewRequestPacket.java @@ -1,6 +1,6 @@ package com.refinedmods.refinedstorage.common.support.packet.c2s; -import com.refinedmods.refinedstorage.api.autocrafting.preview.AutocraftingPreviewProvider; +import com.refinedmods.refinedstorage.api.autocrafting.preview.PreviewProvider; import com.refinedmods.refinedstorage.common.api.support.resource.PlatformResourceKey; import com.refinedmods.refinedstorage.common.support.packet.PacketContext; import com.refinedmods.refinedstorage.common.support.packet.s2c.S2CPackets; @@ -34,7 +34,7 @@ public record AutocraftingPreviewRequestPacket(UUID id, ); public static void handle(final AutocraftingPreviewRequestPacket packet, final PacketContext ctx) { - if (ctx.getPlayer().containerMenu instanceof AutocraftingPreviewProvider provider) { + if (ctx.getPlayer().containerMenu instanceof PreviewProvider provider) { final ServerPlayer player = (ServerPlayer) ctx.getPlayer(); provider.getPreview(packet.resource(), packet.amount()).ifPresent( preview -> S2CPackets.sendAutocraftingPreviewResponse(player, packet.id, preview) diff --git a/refinedstorage-common/src/main/java/com/refinedmods/refinedstorage/common/support/packet/c2s/AutocraftingRequestPacket.java b/refinedstorage-common/src/main/java/com/refinedmods/refinedstorage/common/support/packet/c2s/AutocraftingRequestPacket.java index 095a430ae..b8383f997 100644 --- a/refinedstorage-common/src/main/java/com/refinedmods/refinedstorage/common/support/packet/c2s/AutocraftingRequestPacket.java +++ b/refinedstorage-common/src/main/java/com/refinedmods/refinedstorage/common/support/packet/c2s/AutocraftingRequestPacket.java @@ -1,6 +1,6 @@ package com.refinedmods.refinedstorage.common.support.packet.c2s; -import com.refinedmods.refinedstorage.api.autocrafting.preview.AutocraftingPreviewProvider; +import com.refinedmods.refinedstorage.api.autocrafting.preview.PreviewProvider; import com.refinedmods.refinedstorage.common.api.support.resource.PlatformResourceKey; import com.refinedmods.refinedstorage.common.support.packet.PacketContext; import com.refinedmods.refinedstorage.common.support.packet.s2c.S2CPackets; @@ -24,16 +24,15 @@ public record AutocraftingRequestPacket(UUID id, createIdentifier("autocrafting_request") ); public static final StreamCodec STREAM_CODEC = - StreamCodec - .composite( - UUIDUtil.STREAM_CODEC, AutocraftingRequestPacket::id, - ResourceCodecs.STREAM_CODEC, AutocraftingRequestPacket::resource, - ByteBufCodecs.VAR_LONG, AutocraftingRequestPacket::amount, - AutocraftingRequestPacket::new - ); + StreamCodec.composite( + UUIDUtil.STREAM_CODEC, AutocraftingRequestPacket::id, + ResourceCodecs.STREAM_CODEC, AutocraftingRequestPacket::resource, + ByteBufCodecs.VAR_LONG, AutocraftingRequestPacket::amount, + AutocraftingRequestPacket::new + ); public static void handle(final AutocraftingRequestPacket packet, final PacketContext ctx) { - if (ctx.getPlayer().containerMenu instanceof AutocraftingPreviewProvider provider) { + if (ctx.getPlayer().containerMenu instanceof PreviewProvider provider) { final boolean started = provider.startTask(packet.resource(), packet.amount()); S2CPackets.sendAutocraftingResponse((ServerPlayer) ctx.getPlayer(), packet.id, started); } diff --git a/refinedstorage-common/src/main/java/com/refinedmods/refinedstorage/common/support/packet/c2s/C2SPackets.java b/refinedstorage-common/src/main/java/com/refinedmods/refinedstorage/common/support/packet/c2s/C2SPackets.java index 9737e798b..96bb861b9 100644 --- a/refinedstorage-common/src/main/java/com/refinedmods/refinedstorage/common/support/packet/c2s/C2SPackets.java +++ b/refinedstorage-common/src/main/java/com/refinedmods/refinedstorage/common/support/packet/c2s/C2SPackets.java @@ -1,5 +1,6 @@ package com.refinedmods.refinedstorage.common.support.packet.c2s; +import com.refinedmods.refinedstorage.api.autocrafting.TaskId; import com.refinedmods.refinedstorage.api.grid.operations.GridExtractMode; import com.refinedmods.refinedstorage.api.grid.operations.GridInsertMode; import com.refinedmods.refinedstorage.api.resource.ResourceAmount; @@ -143,4 +144,12 @@ public static void sendAutocraftingRequest(final UUID id, final long amount) { Platform.INSTANCE.sendPacketToServer(new AutocraftingRequestPacket(id, resource, amount)); } + + public static void sendAutocraftingMonitorCancel(final TaskId taskId) { + Platform.INSTANCE.sendPacketToServer(new AutocraftingMonitorCancelPacket(taskId)); + } + + public static void sendAutocraftingMonitorCancelAll() { + Platform.INSTANCE.sendPacketToServer(AutocraftingMonitorCancelAllPacket.INSTANCE); + } } diff --git a/refinedstorage-common/src/main/java/com/refinedmods/refinedstorage/common/support/packet/s2c/AutocraftingMonitorTaskAddedPacket.java b/refinedstorage-common/src/main/java/com/refinedmods/refinedstorage/common/support/packet/s2c/AutocraftingMonitorTaskAddedPacket.java new file mode 100644 index 000000000..508538305 --- /dev/null +++ b/refinedstorage-common/src/main/java/com/refinedmods/refinedstorage/common/support/packet/s2c/AutocraftingMonitorTaskAddedPacket.java @@ -0,0 +1,36 @@ +package com.refinedmods.refinedstorage.common.support.packet.s2c; + +import com.refinedmods.refinedstorage.api.autocrafting.status.TaskStatus; +import com.refinedmods.refinedstorage.common.autocrafting.monitor.AutocraftingMonitorContainerMenu; +import com.refinedmods.refinedstorage.common.autocrafting.monitor.AutocraftingMonitorStreamCodecs; +import com.refinedmods.refinedstorage.common.support.packet.PacketContext; + +import net.minecraft.network.RegistryFriendlyByteBuf; +import net.minecraft.network.codec.StreamCodec; +import net.minecraft.network.protocol.common.custom.CustomPacketPayload; + +import static com.refinedmods.refinedstorage.common.util.IdentifierUtil.createIdentifier; + +public record AutocraftingMonitorTaskAddedPacket(TaskStatus taskStatus) implements CustomPacketPayload { + public static final Type PACKET_TYPE = new Type<>( + createIdentifier("autocrafting_monitor_task_added") + ); + + public static final StreamCodec STREAM_CODEC = + StreamCodec.composite( + AutocraftingMonitorStreamCodecs.STATUS_STREAM_CODEC, AutocraftingMonitorTaskAddedPacket::taskStatus, + AutocraftingMonitorTaskAddedPacket::new + ); + + public static void handle(final AutocraftingMonitorTaskAddedPacket packet, final PacketContext ctx) { + if (ctx.getPlayer().containerMenu instanceof AutocraftingMonitorContainerMenu containerMenu) { + containerMenu.taskAdded(packet.taskStatus()); + } + } + + @Override + public Type type() { + return PACKET_TYPE; + } +} + diff --git a/refinedstorage-common/src/main/java/com/refinedmods/refinedstorage/common/support/packet/s2c/AutocraftingMonitorTaskRemovedPacket.java b/refinedstorage-common/src/main/java/com/refinedmods/refinedstorage/common/support/packet/s2c/AutocraftingMonitorTaskRemovedPacket.java new file mode 100644 index 000000000..2363eecea --- /dev/null +++ b/refinedstorage-common/src/main/java/com/refinedmods/refinedstorage/common/support/packet/s2c/AutocraftingMonitorTaskRemovedPacket.java @@ -0,0 +1,36 @@ +package com.refinedmods.refinedstorage.common.support.packet.s2c; + +import com.refinedmods.refinedstorage.api.autocrafting.TaskId; +import com.refinedmods.refinedstorage.common.autocrafting.monitor.AutocraftingMonitorContainerMenu; +import com.refinedmods.refinedstorage.common.autocrafting.monitor.AutocraftingMonitorStreamCodecs; +import com.refinedmods.refinedstorage.common.support.packet.PacketContext; + +import net.minecraft.network.RegistryFriendlyByteBuf; +import net.minecraft.network.codec.StreamCodec; +import net.minecraft.network.protocol.common.custom.CustomPacketPayload; + +import static com.refinedmods.refinedstorage.common.util.IdentifierUtil.createIdentifier; + +public record AutocraftingMonitorTaskRemovedPacket(TaskId taskId) implements CustomPacketPayload { + public static final Type PACKET_TYPE = new Type<>( + createIdentifier("autocrafting_monitor_task_removed") + ); + + public static final StreamCodec STREAM_CODEC = + StreamCodec.composite( + AutocraftingMonitorStreamCodecs.TASK_ID_STREAM_CODEC, AutocraftingMonitorTaskRemovedPacket::taskId, + AutocraftingMonitorTaskRemovedPacket::new + ); + + public static void handle(final AutocraftingMonitorTaskRemovedPacket packet, final PacketContext ctx) { + if (ctx.getPlayer().containerMenu instanceof AutocraftingMonitorContainerMenu containerMenu) { + containerMenu.taskRemoved(packet.taskId()); + } + } + + @Override + public Type type() { + return PACKET_TYPE; + } +} + diff --git a/refinedstorage-common/src/main/java/com/refinedmods/refinedstorage/common/support/packet/s2c/AutocraftingMonitorTaskStatusChangedPacket.java b/refinedstorage-common/src/main/java/com/refinedmods/refinedstorage/common/support/packet/s2c/AutocraftingMonitorTaskStatusChangedPacket.java new file mode 100644 index 000000000..90e6684b3 --- /dev/null +++ b/refinedstorage-common/src/main/java/com/refinedmods/refinedstorage/common/support/packet/s2c/AutocraftingMonitorTaskStatusChangedPacket.java @@ -0,0 +1,36 @@ +package com.refinedmods.refinedstorage.common.support.packet.s2c; + +import com.refinedmods.refinedstorage.api.autocrafting.status.TaskStatus; +import com.refinedmods.refinedstorage.common.autocrafting.monitor.AutocraftingMonitorContainerMenu; +import com.refinedmods.refinedstorage.common.autocrafting.monitor.AutocraftingMonitorStreamCodecs; +import com.refinedmods.refinedstorage.common.support.packet.PacketContext; + +import net.minecraft.network.RegistryFriendlyByteBuf; +import net.minecraft.network.codec.StreamCodec; +import net.minecraft.network.protocol.common.custom.CustomPacketPayload; + +import static com.refinedmods.refinedstorage.common.util.IdentifierUtil.createIdentifier; + +public record AutocraftingMonitorTaskStatusChangedPacket(TaskStatus taskStatus) implements CustomPacketPayload { + public static final Type PACKET_TYPE = new Type<>( + createIdentifier("autocrafting_monitor_task_status_changed") + ); + + public static final StreamCodec STREAM_CODEC = + StreamCodec.composite( + AutocraftingMonitorStreamCodecs.STATUS_STREAM_CODEC, AutocraftingMonitorTaskStatusChangedPacket::taskStatus, + AutocraftingMonitorTaskStatusChangedPacket::new + ); + + public static void handle(final AutocraftingMonitorTaskStatusChangedPacket packet, final PacketContext ctx) { + if (ctx.getPlayer().containerMenu instanceof AutocraftingMonitorContainerMenu containerMenu) { + containerMenu.taskStatusChanged(packet.taskStatus()); + } + } + + @Override + public Type type() { + return PACKET_TYPE; + } +} + diff --git a/refinedstorage-common/src/main/java/com/refinedmods/refinedstorage/common/support/packet/s2c/AutocraftingPreviewResponsePacket.java b/refinedstorage-common/src/main/java/com/refinedmods/refinedstorage/common/support/packet/s2c/AutocraftingPreviewResponsePacket.java index f61c4df2d..d46a880d1 100644 --- a/refinedstorage-common/src/main/java/com/refinedmods/refinedstorage/common/support/packet/s2c/AutocraftingPreviewResponsePacket.java +++ b/refinedstorage-common/src/main/java/com/refinedmods/refinedstorage/common/support/packet/s2c/AutocraftingPreviewResponsePacket.java @@ -1,8 +1,8 @@ package com.refinedmods.refinedstorage.common.support.packet.s2c; -import com.refinedmods.refinedstorage.api.autocrafting.preview.AutocraftingPreview; -import com.refinedmods.refinedstorage.api.autocrafting.preview.AutocraftingPreviewItem; -import com.refinedmods.refinedstorage.api.autocrafting.preview.AutocraftingPreviewType; +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.common.api.support.resource.PlatformResourceKey; import com.refinedmods.refinedstorage.common.support.resource.ResourceCodecs; import com.refinedmods.refinedstorage.common.util.ClientPlatformUtil; @@ -19,23 +19,23 @@ import static com.refinedmods.refinedstorage.common.util.IdentifierUtil.createIdentifier; import static com.refinedmods.refinedstorage.common.util.PlatformUtil.enumStreamCodec; -public record AutocraftingPreviewResponsePacket(UUID id, AutocraftingPreview preview) implements CustomPacketPayload { +public record AutocraftingPreviewResponsePacket(UUID id, Preview preview) implements CustomPacketPayload { public static final Type PACKET_TYPE = new Type<>( createIdentifier("autocrafting_preview_response") ); - private static final StreamCodec PREVIEW_ITEM_STREAM_CODEC = + private static final StreamCodec PREVIEW_ITEM_STREAM_CODEC = StreamCodec.composite( ResourceCodecs.STREAM_CODEC, item -> (PlatformResourceKey) item.resource(), - ByteBufCodecs.VAR_LONG, AutocraftingPreviewItem::available, - ByteBufCodecs.VAR_LONG, AutocraftingPreviewItem::missing, - ByteBufCodecs.VAR_LONG, AutocraftingPreviewItem::toCraft, - AutocraftingPreviewItem::new + ByteBufCodecs.VAR_LONG, PreviewItem::available, + ByteBufCodecs.VAR_LONG, PreviewItem::missing, + ByteBufCodecs.VAR_LONG, PreviewItem::toCraft, + PreviewItem::new ); - private static final StreamCodec PREVIEW_STREAM_CODEC = + private static final StreamCodec PREVIEW_STREAM_CODEC = StreamCodec.composite( - enumStreamCodec(AutocraftingPreviewType.values()), AutocraftingPreview::type, - ByteBufCodecs.collection(ArrayList::new, PREVIEW_ITEM_STREAM_CODEC), AutocraftingPreview::items, - AutocraftingPreview::new + enumStreamCodec(PreviewType.values()), Preview::type, + ByteBufCodecs.collection(ArrayList::new, PREVIEW_ITEM_STREAM_CODEC), Preview::items, + Preview::new ); public static final StreamCodec STREAM_CODEC = StreamCodec.composite( diff --git a/refinedstorage-common/src/main/java/com/refinedmods/refinedstorage/common/support/packet/s2c/S2CPackets.java b/refinedstorage-common/src/main/java/com/refinedmods/refinedstorage/common/support/packet/s2c/S2CPackets.java index d25475029..cc985ac1d 100644 --- a/refinedstorage-common/src/main/java/com/refinedmods/refinedstorage/common/support/packet/s2c/S2CPackets.java +++ b/refinedstorage-common/src/main/java/com/refinedmods/refinedstorage/common/support/packet/s2c/S2CPackets.java @@ -1,6 +1,8 @@ package com.refinedmods.refinedstorage.common.support.packet.s2c; -import com.refinedmods.refinedstorage.api.autocrafting.preview.AutocraftingPreview; +import com.refinedmods.refinedstorage.api.autocrafting.TaskId; +import com.refinedmods.refinedstorage.api.autocrafting.preview.Preview; +import com.refinedmods.refinedstorage.api.autocrafting.status.TaskStatus; import com.refinedmods.refinedstorage.api.resource.ResourceAmount; import com.refinedmods.refinedstorage.api.storage.tracked.TrackedResource; import com.refinedmods.refinedstorage.common.Platform; @@ -93,7 +95,7 @@ public static void sendAutocrafterNameUpdate(final ServerPlayer player, final Co public static void sendAutocraftingPreviewResponse(final ServerPlayer player, final UUID id, - final AutocraftingPreview preview) { + final Preview preview) { Platform.INSTANCE.sendPacketToClient(player, new AutocraftingPreviewResponsePacket(id, preview)); } @@ -102,4 +104,17 @@ public static void sendAutocraftingResponse(final ServerPlayer player, final boolean started) { Platform.INSTANCE.sendPacketToClient(player, new AutocraftingResponsePacket(id, started)); } + + public static void sendAutocraftingMonitorTaskAdded(final ServerPlayer player, final TaskStatus taskStatus) { + Platform.INSTANCE.sendPacketToClient(player, new AutocraftingMonitorTaskAddedPacket(taskStatus)); + } + + public static void sendAutocraftingMonitorTaskRemoved(final ServerPlayer player, final TaskId taskId) { + Platform.INSTANCE.sendPacketToClient(player, new AutocraftingMonitorTaskRemovedPacket(taskId)); + } + + public static void sendAutocraftingMonitorTaskStatusChanged(final ServerPlayer player, + final TaskStatus taskStatus) { + Platform.INSTANCE.sendPacketToClient(player, new AutocraftingMonitorTaskStatusChangedPacket(taskStatus)); + } } diff --git a/refinedstorage-common/src/main/java/com/refinedmods/refinedstorage/common/util/ClientPlatformUtil.java b/refinedstorage-common/src/main/java/com/refinedmods/refinedstorage/common/util/ClientPlatformUtil.java index 879bae875..12b37c339 100644 --- a/refinedstorage-common/src/main/java/com/refinedmods/refinedstorage/common/util/ClientPlatformUtil.java +++ b/refinedstorage-common/src/main/java/com/refinedmods/refinedstorage/common/util/ClientPlatformUtil.java @@ -1,6 +1,6 @@ package com.refinedmods.refinedstorage.common.util; -import com.refinedmods.refinedstorage.api.autocrafting.preview.AutocraftingPreview; +import com.refinedmods.refinedstorage.api.autocrafting.preview.Preview; import com.refinedmods.refinedstorage.api.resource.ResourceAmount; import com.refinedmods.refinedstorage.common.autocrafting.preview.AutocraftingPreviewScreen; import com.refinedmods.refinedstorage.common.autocrafting.preview.AutocraftingRequest; @@ -40,7 +40,7 @@ public static void addNoPermissionToast(final Component message) { ); } - public static void autocraftingPreviewResponseReceived(final UUID id, final AutocraftingPreview preview) { + public static void autocraftingPreviewResponseReceived(final UUID id, final Preview preview) { if (Minecraft.getInstance().screen instanceof AutocraftingPreviewScreen screen) { screen.getMenu().previewResponseReceived(id, preview); } diff --git a/refinedstorage-fabric/src/main/java/com/refinedmods/refinedstorage/fabric/ClientModInitializerImpl.java b/refinedstorage-fabric/src/main/java/com/refinedmods/refinedstorage/fabric/ClientModInitializerImpl.java index 10d49971e..0eb7ab4c9 100644 --- a/refinedstorage-fabric/src/main/java/com/refinedmods/refinedstorage/fabric/ClientModInitializerImpl.java +++ b/refinedstorage-fabric/src/main/java/com/refinedmods/refinedstorage/fabric/ClientModInitializerImpl.java @@ -21,6 +21,9 @@ import com.refinedmods.refinedstorage.common.support.packet.PacketHandler; import com.refinedmods.refinedstorage.common.support.packet.s2c.AutocrafterManagerActivePacket; import com.refinedmods.refinedstorage.common.support.packet.s2c.AutocrafterNameUpdatePacket; +import com.refinedmods.refinedstorage.common.support.packet.s2c.AutocraftingMonitorTaskAddedPacket; +import com.refinedmods.refinedstorage.common.support.packet.s2c.AutocraftingMonitorTaskRemovedPacket; +import com.refinedmods.refinedstorage.common.support.packet.s2c.AutocraftingMonitorTaskStatusChangedPacket; import com.refinedmods.refinedstorage.common.support.packet.s2c.AutocraftingPreviewResponsePacket; import com.refinedmods.refinedstorage.common.support.packet.s2c.AutocraftingResponsePacket; import com.refinedmods.refinedstorage.common.support.packet.s2c.EnergyInfoPacket; @@ -309,6 +312,18 @@ private void registerPacketHandlers() { AutocraftingResponsePacket.PACKET_TYPE, wrapHandler((packet, ctx) -> AutocraftingResponsePacket.handle(packet)) ); + ClientPlayNetworking.registerGlobalReceiver( + AutocraftingMonitorTaskAddedPacket.PACKET_TYPE, + wrapHandler(AutocraftingMonitorTaskAddedPacket::handle) + ); + ClientPlayNetworking.registerGlobalReceiver( + AutocraftingMonitorTaskRemovedPacket.PACKET_TYPE, + wrapHandler(AutocraftingMonitorTaskRemovedPacket::handle) + ); + ClientPlayNetworking.registerGlobalReceiver( + AutocraftingMonitorTaskStatusChangedPacket.PACKET_TYPE, + wrapHandler(AutocraftingMonitorTaskStatusChangedPacket::handle) + ); } private static ClientPlayNetworking.PlayPayloadHandler wrapHandler( diff --git a/refinedstorage-fabric/src/main/java/com/refinedmods/refinedstorage/fabric/ModInitializerImpl.java b/refinedstorage-fabric/src/main/java/com/refinedmods/refinedstorage/fabric/ModInitializerImpl.java index f04a78317..d085b482f 100644 --- a/refinedstorage-fabric/src/main/java/com/refinedmods/refinedstorage/fabric/ModInitializerImpl.java +++ b/refinedstorage-fabric/src/main/java/com/refinedmods/refinedstorage/fabric/ModInitializerImpl.java @@ -30,6 +30,8 @@ import com.refinedmods.refinedstorage.common.support.AbstractBaseBlock; import com.refinedmods.refinedstorage.common.support.packet.PacketHandler; import com.refinedmods.refinedstorage.common.support.packet.c2s.AutocrafterNameChangePacket; +import com.refinedmods.refinedstorage.common.support.packet.c2s.AutocraftingMonitorCancelAllPacket; +import com.refinedmods.refinedstorage.common.support.packet.c2s.AutocraftingMonitorCancelPacket; import com.refinedmods.refinedstorage.common.support.packet.c2s.AutocraftingPreviewRequestPacket; import com.refinedmods.refinedstorage.common.support.packet.c2s.AutocraftingRequestPacket; import com.refinedmods.refinedstorage.common.support.packet.c2s.CraftingGridClearPacket; @@ -57,6 +59,9 @@ import com.refinedmods.refinedstorage.common.support.packet.c2s.UseSlotReferencedItemPacket; import com.refinedmods.refinedstorage.common.support.packet.s2c.AutocrafterManagerActivePacket; import com.refinedmods.refinedstorage.common.support.packet.s2c.AutocrafterNameUpdatePacket; +import com.refinedmods.refinedstorage.common.support.packet.s2c.AutocraftingMonitorTaskAddedPacket; +import com.refinedmods.refinedstorage.common.support.packet.s2c.AutocraftingMonitorTaskRemovedPacket; +import com.refinedmods.refinedstorage.common.support.packet.s2c.AutocraftingMonitorTaskStatusChangedPacket; import com.refinedmods.refinedstorage.common.support.packet.s2c.AutocraftingPreviewResponsePacket; import com.refinedmods.refinedstorage.common.support.packet.s2c.AutocraftingResponsePacket; import com.refinedmods.refinedstorage.common.support.packet.s2c.EnergyInfoPacket; @@ -460,6 +465,18 @@ private void registerServerToClientPackets() { AutocraftingResponsePacket.PACKET_TYPE, AutocraftingResponsePacket.STREAM_CODEC ); + PayloadTypeRegistry.playS2C().register( + AutocraftingMonitorTaskAddedPacket.PACKET_TYPE, + AutocraftingMonitorTaskAddedPacket.STREAM_CODEC + ); + PayloadTypeRegistry.playS2C().register( + AutocraftingMonitorTaskRemovedPacket.PACKET_TYPE, + AutocraftingMonitorTaskRemovedPacket.STREAM_CODEC + ); + PayloadTypeRegistry.playS2C().register( + AutocraftingMonitorTaskStatusChangedPacket.PACKET_TYPE, + AutocraftingMonitorTaskStatusChangedPacket.STREAM_CODEC + ); } private void registerClientToServerPackets() { @@ -555,6 +572,14 @@ private void registerClientToServerPackets() { AutocraftingRequestPacket.PACKET_TYPE, AutocraftingRequestPacket.STREAM_CODEC ); + PayloadTypeRegistry.playC2S().register( + AutocraftingMonitorCancelPacket.PACKET_TYPE, + AutocraftingMonitorCancelPacket.STREAM_CODEC + ); + PayloadTypeRegistry.playC2S().register( + AutocraftingMonitorCancelAllPacket.PACKET_TYPE, + AutocraftingMonitorCancelAllPacket.STREAM_CODEC + ); } private void registerPacketHandlers() { @@ -662,6 +687,14 @@ private void registerPacketHandlers() { AutocraftingRequestPacket.PACKET_TYPE, wrapHandler(AutocraftingRequestPacket::handle) ); + ServerPlayNetworking.registerGlobalReceiver( + AutocraftingMonitorCancelPacket.PACKET_TYPE, + wrapHandler(AutocraftingMonitorCancelPacket::handle) + ); + ServerPlayNetworking.registerGlobalReceiver( + AutocraftingMonitorCancelAllPacket.PACKET_TYPE, + wrapHandler((packet, ctx) -> AutocraftingMonitorCancelAllPacket.handle(ctx)) + ); } private static ServerPlayNetworking.PlayPayloadHandler wrapHandler( diff --git a/refinedstorage-neoforge/src/main/java/com/refinedmods/refinedstorage/neoforge/ModInitializer.java b/refinedstorage-neoforge/src/main/java/com/refinedmods/refinedstorage/neoforge/ModInitializer.java index e2f7d174e..ac3431d8e 100644 --- a/refinedstorage-neoforge/src/main/java/com/refinedmods/refinedstorage/neoforge/ModInitializer.java +++ b/refinedstorage-neoforge/src/main/java/com/refinedmods/refinedstorage/neoforge/ModInitializer.java @@ -30,6 +30,8 @@ import com.refinedmods.refinedstorage.common.support.AbstractBaseBlock; import com.refinedmods.refinedstorage.common.support.packet.PacketHandler; import com.refinedmods.refinedstorage.common.support.packet.c2s.AutocrafterNameChangePacket; +import com.refinedmods.refinedstorage.common.support.packet.c2s.AutocraftingMonitorCancelAllPacket; +import com.refinedmods.refinedstorage.common.support.packet.c2s.AutocraftingMonitorCancelPacket; import com.refinedmods.refinedstorage.common.support.packet.c2s.AutocraftingPreviewRequestPacket; import com.refinedmods.refinedstorage.common.support.packet.c2s.AutocraftingRequestPacket; import com.refinedmods.refinedstorage.common.support.packet.c2s.CraftingGridClearPacket; @@ -57,6 +59,9 @@ import com.refinedmods.refinedstorage.common.support.packet.c2s.UseSlotReferencedItemPacket; import com.refinedmods.refinedstorage.common.support.packet.s2c.AutocrafterManagerActivePacket; import com.refinedmods.refinedstorage.common.support.packet.s2c.AutocrafterNameUpdatePacket; +import com.refinedmods.refinedstorage.common.support.packet.s2c.AutocraftingMonitorTaskAddedPacket; +import com.refinedmods.refinedstorage.common.support.packet.s2c.AutocraftingMonitorTaskRemovedPacket; +import com.refinedmods.refinedstorage.common.support.packet.s2c.AutocraftingMonitorTaskStatusChangedPacket; import com.refinedmods.refinedstorage.common.support.packet.s2c.AutocraftingPreviewResponsePacket; import com.refinedmods.refinedstorage.common.support.packet.s2c.AutocraftingResponsePacket; import com.refinedmods.refinedstorage.common.support.packet.s2c.EnergyInfoPacket; @@ -653,6 +658,21 @@ private static void registerServerToClientPackets(final PayloadRegistrar registr AutocraftingResponsePacket.STREAM_CODEC, wrapHandler((packet, ctx) -> AutocraftingResponsePacket.handle(packet)) ); + registrar.playToClient( + AutocraftingMonitorTaskAddedPacket.PACKET_TYPE, + AutocraftingMonitorTaskAddedPacket.STREAM_CODEC, + wrapHandler(AutocraftingMonitorTaskAddedPacket::handle) + ); + registrar.playToClient( + AutocraftingMonitorTaskRemovedPacket.PACKET_TYPE, + AutocraftingMonitorTaskRemovedPacket.STREAM_CODEC, + wrapHandler(AutocraftingMonitorTaskRemovedPacket::handle) + ); + registrar.playToClient( + AutocraftingMonitorTaskStatusChangedPacket.PACKET_TYPE, + AutocraftingMonitorTaskStatusChangedPacket.STREAM_CODEC, + wrapHandler(AutocraftingMonitorTaskStatusChangedPacket::handle) + ); } private static void registerClientToServerPackets(final PayloadRegistrar registrar) { @@ -786,6 +806,16 @@ private static void registerClientToServerPackets(final PayloadRegistrar registr AutocraftingRequestPacket.STREAM_CODEC, wrapHandler(AutocraftingRequestPacket::handle) ); + registrar.playToServer( + AutocraftingMonitorCancelPacket.PACKET_TYPE, + AutocraftingMonitorCancelPacket.STREAM_CODEC, + wrapHandler(AutocraftingMonitorCancelPacket::handle) + ); + registrar.playToServer( + AutocraftingMonitorCancelAllPacket.PACKET_TYPE, + AutocraftingMonitorCancelAllPacket.STREAM_CODEC, + wrapHandler((packet, ctx) -> AutocraftingMonitorCancelAllPacket.handle(ctx)) + ); } private static IPayloadHandler wrapHandler(final PacketHandler handler) { 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 3d90a97da..072ba8151 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 @@ -1,7 +1,7 @@ package com.refinedmods.refinedstorage.api.network.autocrafting; -import com.refinedmods.refinedstorage.api.autocrafting.preview.AutocraftingPreviewProvider; import com.refinedmods.refinedstorage.api.autocrafting.Pattern; +import com.refinedmods.refinedstorage.api.autocrafting.preview.PreviewProvider; import com.refinedmods.refinedstorage.api.network.NetworkComponent; import com.refinedmods.refinedstorage.api.resource.ResourceKey; @@ -10,7 +10,7 @@ import org.apiguardian.api.API; @API(status = API.Status.STABLE, since = "2.0.0-milestone.4.8") -public interface AutocraftingNetworkComponent extends NetworkComponent, AutocraftingPreviewProvider { +public interface AutocraftingNetworkComponent extends NetworkComponent, PreviewProvider { void addListener(PatternListener listener); void removeListener(PatternListener listener); 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 08017d748..c52adb449 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 @@ -1,10 +1,11 @@ package com.refinedmods.refinedstorage.api.network.impl.autocrafting; -import com.refinedmods.refinedstorage.api.autocrafting.preview.AutocraftingPreview; -import com.refinedmods.refinedstorage.api.autocrafting.preview.AutocraftingPreviewItem; -import com.refinedmods.refinedstorage.api.autocrafting.preview.AutocraftingPreviewType; import com.refinedmods.refinedstorage.api.autocrafting.Pattern; import com.refinedmods.refinedstorage.api.autocrafting.PatternRepositoryImpl; +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.network.autocrafting.AutocraftingNetworkComponent; import com.refinedmods.refinedstorage.api.network.autocrafting.ParentContainer; import com.refinedmods.refinedstorage.api.network.autocrafting.PatternListener; @@ -62,20 +63,20 @@ public boolean contains(final AutocraftingNetworkComponent component) { } @Override - public Optional getPreview(final ResourceKey resource, final long amount) { - final List items = new ArrayList<>(); + public Optional getPreview(final ResourceKey resource, final long amount) { + final List items = new ArrayList<>(); final boolean missing = amount == 404; for (int i = 0; i < 31; ++i) { - items.add(new AutocraftingPreviewItem( + items.add(new PreviewItem( resource, (i + 1), (i % 2 == 0 && missing) ? amount : 0, i % 2 == 0 ? 0 : amount )); } - return Optional.of(new AutocraftingPreview(missing - ? AutocraftingPreviewType.MISSING_RESOURCES - : AutocraftingPreviewType.SUCCESS, items)); + return Optional.of(new Preview(missing + ? PreviewType.MISSING_RESOURCES + : PreviewType.SUCCESS, items)); } @Override