Skip to content

Commit

Permalink
feat: storage monitor autocrafting
Browse files Browse the repository at this point in the history
Only if the resource could no longer be extracted,
try trigger autocrafting.
I tried to reuse shift clicking for this
to force autocrafting,
but shift is already used to only extract
one of a resource.

You have to wait 250ms until
the last extract to trigger autocrafting
as attack method from block might be called
very often, and we don't want open
the autocrafting window when it's not needed
(eg right after an extract).
  • Loading branch information
raoulvdberge committed Oct 4, 2024
1 parent 1b3a43e commit 2a2ffc5
Show file tree
Hide file tree
Showing 34 changed files with 372 additions and 100 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
- The crafting amount and crafting preview screens have been merged. Changing the amount will update the live preview.
- The numbers on the crafting preview screen are now compacted with units.
- When requesting autocrafting multiple resources at once, which can happen via a recipe mod, all the crafting requests are now listed on the side of the GUI.
- You can now request autocrafting from the Storage Monitor if the resource count reaches zero.

### Fixed

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package com.refinedmods.refinedstorage.api.autocrafting;

import java.util.List;

public record AutocraftingPreview(AutocraftingPreviewType type, List<AutocraftingPreviewItem> items) {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package com.refinedmods.refinedstorage.api.autocrafting;

import com.refinedmods.refinedstorage.api.resource.ResourceKey;

public record AutocraftingPreviewItem(ResourceKey resource, long available, long missing, long toCraft) {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package com.refinedmods.refinedstorage.api.autocrafting;

import com.refinedmods.refinedstorage.api.resource.ResourceKey;

import java.util.Optional;

public interface AutocraftingPreviewProvider {
Optional<AutocraftingPreview> getPreview(ResourceKey resource, long amount);

boolean start(ResourceKey resource, long amount);
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.refinedmods.refinedstorage.common.autocrafting.preview;
package com.refinedmods.refinedstorage.api.autocrafting;

public enum AutocraftingPreviewType {
SUCCESS,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.refinedmods.refinedstorage.common.api.grid;

import com.refinedmods.refinedstorage.api.autocrafting.AutocraftingPreviewProvider;
import com.refinedmods.refinedstorage.api.grid.operations.GridOperations;
import com.refinedmods.refinedstorage.api.grid.watcher.GridWatcher;
import com.refinedmods.refinedstorage.api.storage.Actor;
Expand All @@ -15,7 +16,7 @@
import org.apiguardian.api.API;

@API(status = API.Status.STABLE, since = "2.0.0-milestone.3.0")
public interface Grid {
public interface Grid extends AutocraftingPreviewProvider {
void addWatcher(GridWatcher watcher, Class<? extends Actor> actorType);

void removeWatcher(GridWatcher watcher);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
import com.refinedmods.refinedstorage.common.api.upgrade.UpgradeMapping;
import com.refinedmods.refinedstorage.common.autocrafting.CrafterScreen;
import com.refinedmods.refinedstorage.common.autocrafting.PatternGridScreen;
import com.refinedmods.refinedstorage.common.autocrafting.preview.AutocraftingPreviewContainerMenu;
import com.refinedmods.refinedstorage.common.autocrafting.preview.AutocraftingPreviewScreen;
import com.refinedmods.refinedstorage.common.constructordestructor.ConstructorScreen;
import com.refinedmods.refinedstorage.common.constructordestructor.DestructorScreen;
import com.refinedmods.refinedstorage.common.content.Items;
Expand Down Expand Up @@ -93,6 +95,15 @@ protected static void registerScreens(final ScreenRegistration registration) {
registration.register(Menus.INSTANCE.getRelay(), RelayScreen::new);
registration.register(Menus.INSTANCE.getDiskInterface(), DiskInterfaceScreen::new);
registration.register(Menus.INSTANCE.getCrafter(), CrafterScreen::new);
registration.register(Menus.INSTANCE.getAutocraftingStorageMonitor(),
new ScreenConstructor<AutocraftingPreviewContainerMenu, AutocraftingPreviewScreen>() {
@Override
public AutocraftingPreviewScreen create(final AutocraftingPreviewContainerMenu menu,
final Inventory inventory,
final Component title) {
return new AutocraftingPreviewScreen(menu, inventory, title);
}
});
}

protected static void registerAlternativeGridHints() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,7 @@
import com.refinedmods.refinedstorage.common.storage.storageblock.StorageBlockLootItemFunction;
import com.refinedmods.refinedstorage.common.storage.storagedisk.FluidStorageDiskItem;
import com.refinedmods.refinedstorage.common.storage.storagedisk.ItemStorageDiskItem;
import com.refinedmods.refinedstorage.common.storagemonitor.AutocraftingStorageMonitorContainerMenu;
import com.refinedmods.refinedstorage.common.storagemonitor.FluidStorageMonitorExtractionStrategy;
import com.refinedmods.refinedstorage.common.storagemonitor.FluidStorageMonitorInsertionStrategy;
import com.refinedmods.refinedstorage.common.storagemonitor.ItemStorageMonitorExtractionStrategy;
Expand All @@ -136,6 +137,7 @@
import com.refinedmods.refinedstorage.common.support.network.component.PlatformStorageNetworkComponent;
import com.refinedmods.refinedstorage.common.support.resource.FluidResourceContainerInsertStrategy;
import com.refinedmods.refinedstorage.common.support.resource.FluidResourceFactory;
import com.refinedmods.refinedstorage.common.support.resource.ResourceCodecs;
import com.refinedmods.refinedstorage.common.support.resource.ResourceContainerData;
import com.refinedmods.refinedstorage.common.support.resource.ResourceTypes;
import com.refinedmods.refinedstorage.common.support.slotreference.InventorySlotReferenceFactory;
Expand Down Expand Up @@ -745,6 +747,11 @@ protected final void registerMenus(final RegistryCallback<MenuType<?>> callback,
ContentIds.STORAGE_MONITOR,
() -> extendedMenuTypeFactory.create(StorageMonitorContainerMenu::new, ResourceContainerData.STREAM_CODEC)
));
Menus.INSTANCE.setAutocraftingStorageMonitor(callback.register(
createIdentifier("autocrafting_storage_monitor"),
() -> extendedMenuTypeFactory.create(AutocraftingStorageMonitorContainerMenu::new,
ResourceCodecs.STREAM_CODEC)
));
Menus.INSTANCE.setNetworkTransmitter(callback.register(
ContentIds.NETWORK_TRANSMITTER,
() -> extendedMenuTypeFactory.create(NetworkTransmitterContainerMenu::new,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -614,7 +614,7 @@ public void openCraftingPreview(final List<ResourceAmount> requests) {
minecraft.setScreen(new AutocraftingPreviewScreen(
minecraft.screen,
inventory,
requests.stream().map(AutocraftingRequest::of).collect(Collectors.toList())
requests.stream().map(AutocraftingRequest::of).toList()
));
}
}

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,17 +1,20 @@
package com.refinedmods.refinedstorage.common.autocrafting.preview;

import com.refinedmods.refinedstorage.api.autocrafting.AutocraftingPreview;
import com.refinedmods.refinedstorage.api.resource.ResourceAmount;
import com.refinedmods.refinedstorage.common.api.support.resource.ResourceContainer;
import com.refinedmods.refinedstorage.common.support.containermenu.AbstractResourceContainerMenu;
import com.refinedmods.refinedstorage.common.support.containermenu.DisabledResourceSlot;
import com.refinedmods.refinedstorage.common.support.containermenu.ResourceSlotType;
import com.refinedmods.refinedstorage.common.support.resource.ResourceContainerImpl;

import java.util.ArrayList;
import java.util.List;
import java.util.UUID;
import javax.annotation.Nullable;

import net.minecraft.network.chat.Component;
import net.minecraft.world.inventory.MenuType;

public class AutocraftingPreviewContainerMenu extends AbstractResourceContainerMenu {
private final List<AutocraftingRequest> requests;
Expand All @@ -21,7 +24,15 @@ public class AutocraftingPreviewContainerMenu extends AbstractResourceContainerM
private AutocraftingPreviewListener listener;

AutocraftingPreviewContainerMenu(final List<AutocraftingRequest> requests) {
super(null, 0);
this(null, 0, requests);
}

public AutocraftingPreviewContainerMenu(@Nullable final MenuType<?> type,
final int syncId,
final List<AutocraftingRequest> requests) {
super(type, syncId);
this.requests = new ArrayList<>(requests);
this.currentRequest = requests.getFirst();
final ResourceContainer resourceContainer = ResourceContainerImpl.createForFilter(1);
resourceContainer.set(0, new ResourceAmount(requests.getFirst().getResource(), 1));
addSlot(new DisabledResourceSlot(
Expand All @@ -32,8 +43,6 @@ public class AutocraftingPreviewContainerMenu extends AbstractResourceContainerM
48,
ResourceSlotType.FILTER
));
this.requests = requests;
this.currentRequest = requests.getFirst();
}

void setListener(final AutocraftingPreviewListener listener) {
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package com.refinedmods.refinedstorage.common.autocrafting.preview;

import com.refinedmods.refinedstorage.api.autocrafting.AutocraftingPreview;

import javax.annotation.Nullable;

interface AutocraftingPreviewListener {
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
package com.refinedmods.refinedstorage.common.autocrafting.preview;

import com.refinedmods.refinedstorage.api.autocrafting.AutocraftingPreview;
import com.refinedmods.refinedstorage.api.autocrafting.AutocraftingPreviewItem;
import com.refinedmods.refinedstorage.api.autocrafting.AutocraftingPreviewType;
import com.refinedmods.refinedstorage.common.api.RefinedStorageApi;
import com.refinedmods.refinedstorage.common.api.support.resource.ResourceRendering;
import com.refinedmods.refinedstorage.common.support.amount.AbstractAmountScreen;
Expand Down Expand Up @@ -72,8 +75,20 @@ public class AutocraftingPreviewScreen extends AbstractAmountScreen<Autocrafting
public AutocraftingPreviewScreen(final Screen parent,
final Inventory playerInventory,
final List<AutocraftingRequest> requests) {
this(new AutocraftingPreviewContainerMenu(requests), parent, playerInventory);
}

public AutocraftingPreviewScreen(final AutocraftingPreviewContainerMenu menu,
final Inventory playerInventory,
final Component title) {
this(menu, null, playerInventory);
}

public AutocraftingPreviewScreen(final AutocraftingPreviewContainerMenu menu,
@Nullable final Screen parent,
final Inventory playerInventory) {
super(
new AutocraftingPreviewContainerMenu(requests),
menu,
parent,
playerInventory,
TITLE,
Expand Down Expand Up @@ -478,7 +493,7 @@ private void updateRequestsScrollbar() {

public void responseReceived(final UUID id, final boolean started) {
if (started && getMenu().requestStarted(id)) {
tryCloseToParent();
close();
}
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.refinedmods.refinedstorage.common.autocrafting.preview;

import com.refinedmods.refinedstorage.api.autocrafting.AutocraftingPreview;
import com.refinedmods.refinedstorage.api.resource.ResourceAmount;
import com.refinedmods.refinedstorage.api.resource.ResourceKey;
import com.refinedmods.refinedstorage.common.api.support.resource.PlatformResourceKey;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import com.refinedmods.refinedstorage.common.autocrafting.CrafterContainerMenu;
import com.refinedmods.refinedstorage.common.autocrafting.PatternGridContainerMenu;
import com.refinedmods.refinedstorage.common.autocrafting.preview.AutocraftingPreviewContainerMenu;
import com.refinedmods.refinedstorage.common.constructordestructor.ConstructorContainerMenu;
import com.refinedmods.refinedstorage.common.constructordestructor.DestructorContainerMenu;
import com.refinedmods.refinedstorage.common.controller.ControllerContainerMenu;
Expand All @@ -25,6 +26,7 @@
import com.refinedmods.refinedstorage.common.storage.portablegrid.PortableGridItemContainerMenu;
import com.refinedmods.refinedstorage.common.storage.storageblock.FluidStorageBlockContainerMenu;
import com.refinedmods.refinedstorage.common.storage.storageblock.ItemStorageBlockContainerMenu;
import com.refinedmods.refinedstorage.common.storagemonitor.AutocraftingStorageMonitorContainerMenu;

Check failure on line 29 in refinedstorage-common/src/main/java/com/refinedmods/refinedstorage/common/content/Menus.java

View workflow job for this annotation

GitHub Actions / build / build

Unused import - com.refinedmods.refinedstorage.common.storagemonitor.AutocraftingStorageMonitorContainerMenu.
import com.refinedmods.refinedstorage.common.storagemonitor.StorageMonitorContainerMenu;
import com.refinedmods.refinedstorage.common.upgrade.RegulatorUpgradeContainerMenu;

Expand Down Expand Up @@ -75,6 +77,8 @@ public final class Menus {
@Nullable
private Supplier<MenuType<StorageMonitorContainerMenu>> storageMonitor;
@Nullable
private Supplier<MenuType<AutocraftingPreviewContainerMenu>> autocraftingStorageMonitor;
@Nullable
private Supplier<MenuType<NetworkTransmitterContainerMenu>> networkTransmitter;
@Nullable
private Supplier<MenuType<PortableGridBlockContainerMenu>> portableGridBlock;
Expand Down Expand Up @@ -240,6 +244,16 @@ public void setStorageMonitor(final Supplier<MenuType<StorageMonitorContainerMen
this.storageMonitor = supplier;
}

public MenuType<AutocraftingPreviewContainerMenu> getAutocraftingStorageMonitor() {
return requireNonNull(autocraftingStorageMonitor).get();
}

public void setAutocraftingStorageMonitor(
final Supplier<MenuType<AutocraftingPreviewContainerMenu>> supplier
) {
this.autocraftingStorageMonitor = supplier;
}

public MenuType<NetworkTransmitterContainerMenu> getNetworkTransmitter() {
return requireNonNull(networkTransmitter).get();
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
package com.refinedmods.refinedstorage.common.grid;

import com.refinedmods.refinedstorage.api.autocrafting.AutocraftingPreview;
import com.refinedmods.refinedstorage.api.grid.operations.GridOperations;
import com.refinedmods.refinedstorage.api.grid.watcher.GridWatcher;
import com.refinedmods.refinedstorage.api.network.Network;
import com.refinedmods.refinedstorage.api.network.autocrafting.AutocraftingNetworkComponent;
import com.refinedmods.refinedstorage.api.network.impl.node.container.NetworkNodeContainerPriorities;
import com.refinedmods.refinedstorage.api.network.impl.node.grid.GridNetworkNode;
import com.refinedmods.refinedstorage.api.network.storage.StorageNetworkComponent;
import com.refinedmods.refinedstorage.api.resource.ResourceKey;
import com.refinedmods.refinedstorage.api.storage.Actor;
import com.refinedmods.refinedstorage.api.storage.Storage;
import com.refinedmods.refinedstorage.api.storage.TrackedResourceAmount;
Expand All @@ -23,6 +25,7 @@
import com.refinedmods.refinedstorage.common.support.network.ColoredConnectionStrategy;

import java.util.List;
import java.util.Optional;
import java.util.Set;
import java.util.stream.Collectors;

Expand Down Expand Up @@ -97,6 +100,22 @@ public void removeWatcher(final GridWatcher watcher) {
mainNetworkNode.removeWatcher(watcher);
}

@Override
public Optional<AutocraftingPreview> getPreview(final ResourceKey resource, final long amount) {
return Optional.ofNullable(mainNetworkNode.getNetwork())
.map(network -> network.getComponent(AutocraftingNetworkComponent.class))
.flatMap(component -> component.getPreview(resource, amount));
}

@Override
public boolean start(final ResourceKey resource, final long amount) {
final Network network = mainNetworkNode.getNetwork();
if (network == null) {
return false;
}
return network.getComponent(AutocraftingNetworkComponent.class).start(resource, amount);
}

@Override
protected boolean doesBlockStateChangeWarrantNetworkNodeUpdate(final BlockState oldBlockState,
final BlockState newBlockState) {
Expand Down
Loading

0 comments on commit 2a2ffc5

Please sign in to comment.