diff --git a/build.gradle.kts b/build.gradle.kts index e7891180d..b854b533d 100644 --- a/build.gradle.kts +++ b/build.gradle.kts @@ -13,6 +13,17 @@ subprojects { group = "com.refinedmods.refinedstorage" } +/* publish all subprojects to mavenLocal */ +subprojects { + apply(plugin = "maven-publish") + + publishing { + repositories { + mavenLocal() + } + } +} + project.extensions.getByType().apply { properties { property( diff --git a/refinedstorage-common/src/main/java/com/refinedmods/refinedstorage/common/autocrafting/patterngrid/PatternGridBlockEntity.java b/refinedstorage-common/src/main/java/com/refinedmods/refinedstorage/common/autocrafting/patterngrid/PatternGridBlockEntity.java index b687d4b77..a978db09e 100644 --- a/refinedstorage-common/src/main/java/com/refinedmods/refinedstorage/common/autocrafting/patterngrid/PatternGridBlockEntity.java +++ b/refinedstorage-common/src/main/java/com/refinedmods/refinedstorage/common/autocrafting/patterngrid/PatternGridBlockEntity.java @@ -1,5 +1,6 @@ package com.refinedmods.refinedstorage.common.autocrafting.patterngrid; +import com.refinedmods.refinedstorage.api.network.storage.StorageNetworkComponent; import com.refinedmods.refinedstorage.api.resource.ResourceAmount; import com.refinedmods.refinedstorage.api.resource.ResourceKey; import com.refinedmods.refinedstorage.common.Platform; @@ -496,7 +497,9 @@ private boolean isPatternAvailable() { void transferCraftingRecipe(final Player player, final List> recipe) { final Comparator sorter = ResourceSorters.create( - mainNetworkNode.getNetwork(), + mainNetworkNode.getNetwork() != null + ? mainNetworkNode.getNetwork().getComponent(StorageNetworkComponent.class) + : null, player.getInventory() ); getCraftingMatrix().clearContent(); @@ -517,7 +520,9 @@ void transferProcessingRecipe(final Player player, final List> inputs, final List> outputs) { final Comparator sorter = ResourceSorters.create( - mainNetworkNode.getNetwork(), + mainNetworkNode.getNetwork() != null + ? mainNetworkNode.getNetwork().getComponent(StorageNetworkComponent.class) + : null, player.getInventory(), ResourceAmount::resource ); @@ -556,7 +561,9 @@ void transferSmithingTableRecipe(final Player player, return; } final Comparator sorter = ResourceSorters.create( - mainNetworkNode.getNetwork(), + mainNetworkNode.getNetwork() != null + ? mainNetworkNode.getNetwork().getComponent(StorageNetworkComponent.class) + : null, player.getInventory(), r -> r ); diff --git a/refinedstorage-common/src/main/java/com/refinedmods/refinedstorage/common/grid/AbstractCraftingGridContainerMenu.java b/refinedstorage-common/src/main/java/com/refinedmods/refinedstorage/common/grid/AbstractCraftingGridContainerMenu.java new file mode 100644 index 000000000..0e8545c03 --- /dev/null +++ b/refinedstorage-common/src/main/java/com/refinedmods/refinedstorage/common/grid/AbstractCraftingGridContainerMenu.java @@ -0,0 +1,182 @@ +package com.refinedmods.refinedstorage.common.grid; + +import com.refinedmods.refinedstorage.api.grid.view.GridResource; +import com.refinedmods.refinedstorage.api.grid.view.GridView; +import com.refinedmods.refinedstorage.api.resource.ResourceKey; +import com.refinedmods.refinedstorage.api.resource.list.MutableResourceList; +import com.refinedmods.refinedstorage.common.grid.view.ItemGridResource; +import com.refinedmods.refinedstorage.common.support.resource.ItemResource; + +import java.util.ArrayList; +import java.util.HashSet; +import java.util.List; +import java.util.Set; +import java.util.function.BiPredicate; +import java.util.function.Consumer; +import javax.annotation.Nullable; + +import net.minecraft.world.Container; +import net.minecraft.world.entity.player.Inventory; +import net.minecraft.world.entity.player.Player; +import net.minecraft.world.inventory.MenuType; +import net.minecraft.world.inventory.Slot; +import net.minecraft.world.item.ItemStack; +import org.apiguardian.api.API; + +public abstract class AbstractCraftingGridContainerMenu extends AbstractGridContainerMenu { + private static final int Y_OFFSET_BETWEEN_PLAYER_INVENTORY_AND_FIRST_CRAFTING_MATRIX_SLOT = 69; + + private final Player gridPlayer; + private final CraftingGrid craftingGrid; + private final List craftingMatrixSlots = new ArrayList<>(); + + @Nullable + private Consumer activenessListener; + @Nullable + private BiPredicate filterBeforeFilteringBasedOnCraftingMatrixItems; + + protected AbstractCraftingGridContainerMenu(final MenuType menuType, + final int syncId, + final Inventory playerInventory, + final GridData gridData) { + super(menuType, syncId, playerInventory, gridData); + this.craftingGrid = new ClientCraftingGrid(); + this.gridPlayer = playerInventory.player; + } + + protected AbstractCraftingGridContainerMenu(final MenuType menuType, + final int syncId, + final Inventory playerInventory, + final CraftingGrid craftingGrid) { + super(menuType, syncId, playerInventory, craftingGrid); + this.craftingGrid = craftingGrid; + this.gridPlayer = playerInventory.player; + } + + public void setActivenessListener(@Nullable final Consumer activenessListener) { + this.activenessListener = activenessListener; + } + + @Override + public void onActiveChanged(final boolean newActive) { + super.onActiveChanged(newActive); + if (activenessListener != null) { + activenessListener.accept(newActive); + } + } + + @Override + public boolean canTakeItemForPickAll(final ItemStack stack, final Slot slot) { + if (slot instanceof CraftingGridResultSlot) { + return false; + } + return super.canTakeItemForPickAll(stack, slot); + } + + @Override + @SuppressWarnings("resource") + public ItemStack quickMoveStack(final Player actor, final int slotIndex) { + final Slot slot = getSlot(slotIndex); + if (!actor.level().isClientSide() + && slot instanceof CraftingGridResultSlot resultSlot + && resultSlot.hasItem()) { + final ItemStack craftedStack = resultSlot.onQuickCraft(actor); + craftingGrid.acceptQuickCraft(actor, craftedStack); + return ItemStack.EMPTY; + } + return super.quickMoveStack(actor, slotIndex); + } + + @Override + public void resized(final int playerInventoryY, final int topYStart, final int topYEnd) { + super.resized(playerInventoryY, topYStart, topYEnd); + craftingMatrixSlots.clear(); + for (int y = 0; y < 3; ++y) { + for (int x = 0; x < 3; ++x) { + final int slotX = 26 + ((x % 3) * 18); + final int slotY = playerInventoryY + - Y_OFFSET_BETWEEN_PLAYER_INVENTORY_AND_FIRST_CRAFTING_MATRIX_SLOT + + ((y % 3) * 18); + craftingMatrixSlots.add(addSlot(new Slot(craftingGrid.getCraftingMatrix(), x + y * 3, slotX, slotY))); + } + } + addSlot(new CraftingGridResultSlot( + gridPlayer, + craftingGrid, + 130 + 4, + playerInventoryY - Y_OFFSET_BETWEEN_PLAYER_INVENTORY_AND_FIRST_CRAFTING_MATRIX_SLOT + 18 + )); + } + + public List getCraftingMatrixSlots() { + return craftingMatrixSlots; + } + + public void clear(final boolean toPlayerInventory) { + craftingGrid.clearMatrix(gridPlayer, toPlayerInventory); + } + + @API(status = API.Status.INTERNAL) + public MutableResourceList getAvailableListForRecipeTransfer() { + final MutableResourceList available = getView().copyBackingList(); + addContainerToList(craftingGrid.getCraftingMatrix(), available); + addContainerToList(gridPlayer.getInventory(), available); + return available; + } + + private void addContainerToList(final Container container, final MutableResourceList available) { + for (int i = 0; i < container.getContainerSize(); ++i) { + final ItemStack stack = container.getItem(i); + if (stack.isEmpty()) { + continue; + } + available.add(ItemResource.ofItemStack(stack), stack.getCount()); + } + } + + public void transferRecipe(final List> recipe) { + craftingGrid.transferRecipe(gridPlayer, recipe); + } + + public void filterBasedOnCraftingMatrixItems() { + final Set craftingMatrixItems = getCraftingMatrixItems(); + filterBeforeFilteringBasedOnCraftingMatrixItems = getView().setFilterAndSort( + (view, resource) -> resource instanceof ItemGridResource itemResource + && craftingMatrixItems.contains(itemResource.getItemResource()) + ); + } + + private Set getCraftingMatrixItems() { + final Set craftingMatrixItems = new HashSet<>(); + for (int i = 0; i < craftingGrid.getCraftingMatrix().getContainerSize(); ++i) { + final ItemStack craftingMatrixStack = craftingGrid.getCraftingMatrix().getItem(i); + if (craftingMatrixStack.isEmpty()) { + continue; + } + craftingMatrixItems.add(ItemResource.ofItemStack(craftingMatrixStack)); + } + return craftingMatrixItems; + } + + public void stopFilteringBasedOnCraftingMatrixItems() { + if (filterBeforeFilteringBasedOnCraftingMatrixItems == null) { + return; + } + getView().setFilterAndSort(filterBeforeFilteringBasedOnCraftingMatrixItems); + filterBeforeFilteringBasedOnCraftingMatrixItems = null; + } + + @Nullable + @Override + protected ResourceKey getResourceForAutocraftableHint(final Slot slot) { + if (slot.container == craftingGrid.getCraftingMatrix() || slot.container == craftingGrid.getCraftingResult()) { + return ItemResource.ofItemStack(slot.getItem()); + } + return super.getResourceForAutocraftableHint(slot); + } + + @Override + public boolean isLargeSlot(final Slot slot) { + return slot.container == craftingGrid.getCraftingResult() || super.isLargeSlot(slot); + } +} diff --git a/refinedstorage-common/src/main/java/com/refinedmods/refinedstorage/common/grid/ClientCraftingGrid.java b/refinedstorage-common/src/main/java/com/refinedmods/refinedstorage/common/grid/ClientCraftingGrid.java index 7a3473d39..d4510fbe0 100644 --- a/refinedstorage-common/src/main/java/com/refinedmods/refinedstorage/common/grid/ClientCraftingGrid.java +++ b/refinedstorage-common/src/main/java/com/refinedmods/refinedstorage/common/grid/ClientCraftingGrid.java @@ -1,12 +1,24 @@ package com.refinedmods.refinedstorage.common.grid; +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.resource.ResourceKey; +import com.refinedmods.refinedstorage.api.storage.Actor; +import com.refinedmods.refinedstorage.api.storage.Storage; +import com.refinedmods.refinedstorage.api.storage.TrackedResourceAmount; +import com.refinedmods.refinedstorage.common.api.support.resource.PlatformResourceKey; +import com.refinedmods.refinedstorage.common.api.support.resource.ResourceType; import com.refinedmods.refinedstorage.common.support.RecipeMatrixContainer; import com.refinedmods.refinedstorage.common.support.packet.c2s.C2SPackets; import com.refinedmods.refinedstorage.common.support.resource.ItemResource; import java.util.List; +import java.util.Optional; +import java.util.Set; import net.minecraft.core.NonNullList; +import net.minecraft.server.level.ServerPlayer; import net.minecraft.world.entity.player.Player; import net.minecraft.world.inventory.ResultContainer; import net.minecraft.world.item.ItemStack; @@ -37,13 +49,8 @@ public NonNullList getRemainingItems(final Player player, final Craft } @Override - public CraftingGridRefillContext openRefillContext() { - throw new UnsupportedOperationException(); - } - - @Override - public CraftingGridRefillContext openSnapshotRefillContext(final Player player) { - throw new UnsupportedOperationException(); + public ExtractTransaction startExtractTransaction(final Player player, final boolean directCommit) { + return ExtractTransaction.NOOP; } @Override @@ -61,4 +68,49 @@ public void transferRecipe(final Player player, final List> r public void acceptQuickCraft(final Player player, final ItemStack craftedStack) { throw new UnsupportedOperationException(); } + + @Override + public void addWatcher(final GridWatcher watcher, final Class actorType) { + throw new UnsupportedOperationException(); + } + + @Override + public void removeWatcher(final GridWatcher watcher) { + throw new UnsupportedOperationException(); + } + + @Override + public Storage getItemStorage() { + throw new UnsupportedOperationException(); + } + + @Override + public boolean isGridActive() { + throw new UnsupportedOperationException(); + } + + @Override + public List getResources(final Class actorType) { + throw new UnsupportedOperationException(); + } + + @Override + public Set getAutocraftableResources() { + throw new UnsupportedOperationException(); + } + + @Override + public GridOperations createOperations(final ResourceType resourceType, final ServerPlayer player) { + throw new UnsupportedOperationException(); + } + + @Override + public Optional getPreview(final ResourceKey resource, final long amount) { + throw new UnsupportedOperationException(); + } + + @Override + public boolean startTask(final ResourceKey resource, final long amount) { + throw new UnsupportedOperationException(); + } } diff --git a/refinedstorage-common/src/main/java/com/refinedmods/refinedstorage/common/grid/CraftingGrid.java b/refinedstorage-common/src/main/java/com/refinedmods/refinedstorage/common/grid/CraftingGrid.java index 0674963f0..8efa78239 100644 --- a/refinedstorage-common/src/main/java/com/refinedmods/refinedstorage/common/grid/CraftingGrid.java +++ b/refinedstorage-common/src/main/java/com/refinedmods/refinedstorage/common/grid/CraftingGrid.java @@ -1,5 +1,6 @@ package com.refinedmods.refinedstorage.common.grid; +import com.refinedmods.refinedstorage.common.api.grid.Grid; import com.refinedmods.refinedstorage.common.support.RecipeMatrixContainer; import com.refinedmods.refinedstorage.common.support.resource.ItemResource; @@ -11,16 +12,14 @@ import net.minecraft.world.item.ItemStack; import net.minecraft.world.item.crafting.CraftingInput; -interface CraftingGrid { +public interface CraftingGrid extends Grid { RecipeMatrixContainer getCraftingMatrix(); ResultContainer getCraftingResult(); NonNullList getRemainingItems(Player player, CraftingInput input); - CraftingGridRefillContext openRefillContext(); - - CraftingGridRefillContext openSnapshotRefillContext(Player player); + ExtractTransaction startExtractTransaction(Player player, boolean directCommit); boolean clearMatrix(Player player, boolean toPlayerInventory); diff --git a/refinedstorage-common/src/main/java/com/refinedmods/refinedstorage/common/grid/CraftingGridBlockEntity.java b/refinedstorage-common/src/main/java/com/refinedmods/refinedstorage/common/grid/CraftingGridBlockEntity.java index 6b83eae94..411fa2ce8 100644 --- a/refinedstorage-common/src/main/java/com/refinedmods/refinedstorage/common/grid/CraftingGridBlockEntity.java +++ b/refinedstorage-common/src/main/java/com/refinedmods/refinedstorage/common/grid/CraftingGridBlockEntity.java @@ -3,7 +3,6 @@ import com.refinedmods.refinedstorage.api.core.Action; import com.refinedmods.refinedstorage.api.network.Network; import com.refinedmods.refinedstorage.api.network.storage.StorageNetworkComponent; -import com.refinedmods.refinedstorage.api.storage.root.RootStorage; import com.refinedmods.refinedstorage.common.Platform; import com.refinedmods.refinedstorage.common.api.storage.PlayerActor; import com.refinedmods.refinedstorage.common.content.BlockEntities; @@ -14,6 +13,7 @@ import com.refinedmods.refinedstorage.common.support.containermenu.NetworkNodeExtendedMenuProvider; import com.refinedmods.refinedstorage.common.support.resource.ItemResource; +import java.util.List; import java.util.Optional; import javax.annotation.Nullable; @@ -34,7 +34,7 @@ import net.minecraft.world.level.block.state.BlockState; public class CraftingGridBlockEntity extends AbstractGridBlockEntity implements BlockEntityWithDrops, - NetworkNodeExtendedMenuProvider { + NetworkNodeExtendedMenuProvider, CraftingGrid { private static final String TAG_MATRIX = "matrix"; private final RecipeMatrix craftingRecipe = RecipeMatrix.crafting( @@ -51,18 +51,75 @@ public CraftingGridBlockEntity(final BlockPos pos, final BlockState state) { ); } - RecipeMatrixContainer getCraftingMatrix() { + @Override + public RecipeMatrixContainer getCraftingMatrix() { return craftingRecipe.getMatrix(); } - ResultContainer getCraftingResult() { + @Override + public ResultContainer getCraftingResult() { return craftingRecipe.getResult(); } - NonNullList getRemainingItems(final Player player, final CraftingInput input) { + @Override + public NonNullList getRemainingItems(final Player player, final CraftingInput input) { return craftingRecipe.getRemainingItems(level, player, input); } + @Override + public ExtractTransaction startExtractTransaction(final Player player, final boolean directCommit) { + return getNetwork() + .map(network -> network.getComponent(StorageNetworkComponent.class)) + .map(storage -> directCommit + ? new DirectCommitExtractTransaction(storage) + : new SnapshotExtractTransaction(player, storage, getCraftingMatrix())) + .orElse(ExtractTransaction.NOOP); + } + + @Override + public boolean clearMatrix(final Player player, final boolean toPlayerInventory) { + return toPlayerInventory + ? getCraftingMatrix().clearToPlayerInventory(player) + : clearMatrixIntoStorage(player); + } + + private boolean clearMatrixIntoStorage(final Player player) { + return getNetwork() + .map(network -> network.getComponent(StorageNetworkComponent.class)) + .map(storage -> getCraftingMatrix().clearIntoStorage(storage, player)) + .orElse(false); + } + + @Override + public void transferRecipe(final Player player, final List> recipe) { + getCraftingMatrix().transferRecipe( + player, + getNetwork().map(network -> network.getComponent(StorageNetworkComponent.class)).orElse(null), + recipe + ); + } + + @Override + public void acceptQuickCraft(final Player player, final ItemStack craftedStack) { + if (player.getInventory().add(craftedStack)) { + return; + } + final long inserted = getNetwork() + .map(network -> network.getComponent(StorageNetworkComponent.class)) + .map(rootStorage -> rootStorage.insert( + ItemResource.ofItemStack(craftedStack), + craftedStack.getCount(), + Action.EXECUTE, + new PlayerActor(player) + )) + .orElse(0L); + if (inserted != craftedStack.getCount()) { + final long remainder = craftedStack.getCount() - inserted; + final ItemStack remainderStack = craftedStack.copyWithCount((int) remainder); + player.drop(remainderStack, false); + } + } + @Override public GridData getMenuData() { return GridData.of(this); @@ -113,43 +170,10 @@ public final NonNullList getDrops() { return drops; } - Optional getNetwork() { + private Optional getNetwork() { if (!mainNetworkNode.isActive()) { return Optional.empty(); } return Optional.ofNullable(mainNetworkNode.getNetwork()); } - - Optional getRootStorage() { - return getNetwork().map(network -> network.getComponent(StorageNetworkComponent.class)); - } - - ItemStack insert(final ItemStack stack, final Player player) { - return getRootStorage().map(rootStorage -> doInsert(stack, player, rootStorage)).orElse(stack); - } - - private ItemStack doInsert(final ItemStack stack, - final Player player, - final RootStorage rootStorage) { - final long inserted = rootStorage.insert( - ItemResource.ofItemStack(stack), - stack.getCount(), - Action.EXECUTE, - new PlayerActor(player) - ); - final long remainder = stack.getCount() - inserted; - if (remainder == 0) { - return ItemStack.EMPTY; - } - return stack.copyWithCount((int) remainder); - } - - long extract(final ItemResource resource, final Player player) { - return getRootStorage().map(rootStorage -> rootStorage.extract( - resource, - 1, - Action.EXECUTE, - new PlayerActor(player) - )).orElse(0L); - } } diff --git a/refinedstorage-common/src/main/java/com/refinedmods/refinedstorage/common/grid/CraftingGridContainerMenu.java b/refinedstorage-common/src/main/java/com/refinedmods/refinedstorage/common/grid/CraftingGridContainerMenu.java index 593b1416e..fac7de69f 100644 --- a/refinedstorage-common/src/main/java/com/refinedmods/refinedstorage/common/grid/CraftingGridContainerMenu.java +++ b/refinedstorage-common/src/main/java/com/refinedmods/refinedstorage/common/grid/CraftingGridContainerMenu.java @@ -1,192 +1,31 @@ package com.refinedmods.refinedstorage.common.grid; -import com.refinedmods.refinedstorage.api.grid.view.GridResource; -import com.refinedmods.refinedstorage.api.grid.view.GridView; -import com.refinedmods.refinedstorage.api.resource.ResourceKey; -import com.refinedmods.refinedstorage.api.resource.list.MutableResourceList; import com.refinedmods.refinedstorage.common.content.Menus; -import com.refinedmods.refinedstorage.common.grid.view.ItemGridResource; import com.refinedmods.refinedstorage.common.support.RedstoneMode; import com.refinedmods.refinedstorage.common.support.containermenu.ClientProperty; import com.refinedmods.refinedstorage.common.support.containermenu.PropertyTypes; import com.refinedmods.refinedstorage.common.support.containermenu.ServerProperty; -import com.refinedmods.refinedstorage.common.support.resource.ItemResource; -import java.util.ArrayList; -import java.util.HashSet; -import java.util.List; -import java.util.Set; -import java.util.function.BiPredicate; -import java.util.function.Consumer; -import javax.annotation.Nullable; - -import net.minecraft.world.Container; import net.minecraft.world.entity.player.Inventory; -import net.minecraft.world.entity.player.Player; -import net.minecraft.world.inventory.Slot; -import net.minecraft.world.item.ItemStack; -import org.apiguardian.api.API; - -public class CraftingGridContainerMenu extends AbstractGridContainerMenu { - private static final int Y_OFFSET_BETWEEN_PLAYER_INVENTORY_AND_FIRST_CRAFTING_MATRIX_SLOT = 69; - - private final Player gridPlayer; - private final CraftingGrid craftingGrid; - private final List craftingMatrixSlots = new ArrayList<>(); - - @Nullable - private Consumer activenessListener; - @Nullable - private BiPredicate filterBeforeFilteringBasedOnCraftingMatrixItems; +public class CraftingGridContainerMenu extends AbstractCraftingGridContainerMenu { public CraftingGridContainerMenu(final int syncId, final Inventory playerInventory, final GridData gridData) { super(Menus.INSTANCE.getCraftingGrid(), syncId, playerInventory, gridData); - this.craftingGrid = new ClientCraftingGrid(); - this.gridPlayer = playerInventory.player; - resized(0, 0, 0); registerProperty(new ClientProperty<>(PropertyTypes.REDSTONE_MODE, RedstoneMode.IGNORE)); + resized(0, 0, 0); } CraftingGridContainerMenu(final int syncId, final Inventory playerInventory, - final CraftingGridBlockEntity grid) { - super(Menus.INSTANCE.getCraftingGrid(), syncId, playerInventory, grid); - this.craftingGrid = new CraftingGridImpl(grid); - this.gridPlayer = playerInventory.player; - resized(0, 0, 0); + final CraftingGridBlockEntity craftingGrid) { + super(Menus.INSTANCE.getCraftingGrid(), syncId, playerInventory, craftingGrid); registerProperty(new ServerProperty<>( PropertyTypes.REDSTONE_MODE, - grid::getRedstoneMode, - grid::setRedstoneMode - )); - } - - public void setActivenessListener(@Nullable final Consumer activenessListener) { - this.activenessListener = activenessListener; - } - - @Override - public void onActiveChanged(final boolean newActive) { - super.onActiveChanged(newActive); - if (activenessListener != null) { - activenessListener.accept(newActive); - } - } - - @Override - public boolean canTakeItemForPickAll(final ItemStack stack, final Slot slot) { - if (slot instanceof CraftingGridResultSlot) { - return false; - } - return super.canTakeItemForPickAll(stack, slot); - } - - @Override - @SuppressWarnings("resource") - public ItemStack quickMoveStack(final Player actor, final int slotIndex) { - final Slot slot = getSlot(slotIndex); - if (!actor.level().isClientSide() - && slot instanceof CraftingGridResultSlot resultSlot - && resultSlot.hasItem()) { - final ItemStack craftedStack = resultSlot.onQuickCraft(actor); - craftingGrid.acceptQuickCraft(actor, craftedStack); - return ItemStack.EMPTY; - } - return super.quickMoveStack(actor, slotIndex); - } - - @Override - public void resized(final int playerInventoryY, final int topYStart, final int topYEnd) { - super.resized(playerInventoryY, topYStart, topYEnd); - craftingMatrixSlots.clear(); - for (int y = 0; y < 3; ++y) { - for (int x = 0; x < 3; ++x) { - final int slotX = 26 + ((x % 3) * 18); - final int slotY = playerInventoryY - - Y_OFFSET_BETWEEN_PLAYER_INVENTORY_AND_FIRST_CRAFTING_MATRIX_SLOT - + ((y % 3) * 18); - craftingMatrixSlots.add(addSlot(new Slot(craftingGrid.getCraftingMatrix(), x + y * 3, slotX, slotY))); - } - } - addSlot(new CraftingGridResultSlot( - gridPlayer, - craftingGrid, - 130 + 4, - playerInventoryY - Y_OFFSET_BETWEEN_PLAYER_INVENTORY_AND_FIRST_CRAFTING_MATRIX_SLOT + 18 + craftingGrid::getRedstoneMode, + craftingGrid::setRedstoneMode )); - } - - public List getCraftingMatrixSlots() { - return craftingMatrixSlots; - } - - public void clear(final boolean toPlayerInventory) { - craftingGrid.clearMatrix(gridPlayer, toPlayerInventory); - } - - @API(status = API.Status.INTERNAL) - public MutableResourceList getAvailableListForRecipeTransfer() { - final MutableResourceList available = getView().copyBackingList(); - addContainerToList(craftingGrid.getCraftingMatrix(), available); - addContainerToList(gridPlayer.getInventory(), available); - return available; - } - - private void addContainerToList(final Container container, final MutableResourceList available) { - for (int i = 0; i < container.getContainerSize(); ++i) { - final ItemStack stack = container.getItem(i); - if (stack.isEmpty()) { - continue; - } - available.add(ItemResource.ofItemStack(stack), stack.getCount()); - } - } - - public void transferRecipe(final List> recipe) { - craftingGrid.transferRecipe(gridPlayer, recipe); - } - - public void filterBasedOnCraftingMatrixItems() { - final Set craftingMatrixItems = getCraftingMatrixItems(); - filterBeforeFilteringBasedOnCraftingMatrixItems = getView().setFilterAndSort( - (view, resource) -> resource instanceof ItemGridResource itemResource - && craftingMatrixItems.contains(itemResource.getItemResource()) - ); - } - - private Set getCraftingMatrixItems() { - final Set craftingMatrixItems = new HashSet<>(); - for (int i = 0; i < craftingGrid.getCraftingMatrix().getContainerSize(); ++i) { - final ItemStack craftingMatrixStack = craftingGrid.getCraftingMatrix().getItem(i); - if (craftingMatrixStack.isEmpty()) { - continue; - } - craftingMatrixItems.add(ItemResource.ofItemStack(craftingMatrixStack)); - } - return craftingMatrixItems; - } - - public void stopFilteringBasedOnCraftingMatrixItems() { - if (filterBeforeFilteringBasedOnCraftingMatrixItems == null) { - return; - } - getView().setFilterAndSort(filterBeforeFilteringBasedOnCraftingMatrixItems); - filterBeforeFilteringBasedOnCraftingMatrixItems = null; - } - - @Nullable - @Override - protected ResourceKey getResourceForAutocraftableHint(final Slot slot) { - if (slot.container == craftingGrid.getCraftingMatrix() || slot.container == craftingGrid.getCraftingResult()) { - return ItemResource.ofItemStack(slot.getItem()); - } - return super.getResourceForAutocraftableHint(slot); - } - - @Override - public boolean isLargeSlot(final Slot slot) { - return slot.container == craftingGrid.getCraftingResult() || super.isLargeSlot(slot); + resized(0, 0, 0); } } diff --git a/refinedstorage-common/src/main/java/com/refinedmods/refinedstorage/common/grid/CraftingGridImpl.java b/refinedstorage-common/src/main/java/com/refinedmods/refinedstorage/common/grid/CraftingGridImpl.java deleted file mode 100644 index ac00a7d2a..000000000 --- a/refinedstorage-common/src/main/java/com/refinedmods/refinedstorage/common/grid/CraftingGridImpl.java +++ /dev/null @@ -1,129 +0,0 @@ -package com.refinedmods.refinedstorage.common.grid; - -import com.refinedmods.refinedstorage.api.resource.ResourceKey; -import com.refinedmods.refinedstorage.common.support.RecipeMatrixContainer; -import com.refinedmods.refinedstorage.common.support.network.ResourceSorters; -import com.refinedmods.refinedstorage.common.support.resource.ItemResource; - -import java.util.Comparator; -import java.util.List; - -import net.minecraft.core.NonNullList; -import net.minecraft.world.entity.player.Player; -import net.minecraft.world.inventory.ResultContainer; -import net.minecraft.world.item.ItemStack; -import net.minecraft.world.item.crafting.CraftingInput; - -class CraftingGridImpl implements CraftingGrid { - private final CraftingGridBlockEntity blockEntity; - - CraftingGridImpl(final CraftingGridBlockEntity blockEntity) { - this.blockEntity = blockEntity; - } - - @Override - public RecipeMatrixContainer getCraftingMatrix() { - return blockEntity.getCraftingMatrix(); - } - - @Override - public ResultContainer getCraftingResult() { - return blockEntity.getCraftingResult(); - } - - @Override - public NonNullList getRemainingItems(final Player player, final CraftingInput input) { - return blockEntity.getRemainingItems(player, input); - } - - @Override - public CraftingGridRefillContext openRefillContext() { - return new CraftingGridRefillContextImpl(blockEntity); - } - - @Override - public CraftingGridRefillContext openSnapshotRefillContext(final Player player) { - return new SnapshotCraftingGridRefillContext(player, blockEntity, blockEntity.getCraftingMatrix()); - } - - @Override - public void acceptQuickCraft(final Player player, final ItemStack craftedStack) { - if (player.getInventory().add(craftedStack)) { - return; - } - final ItemStack remainder = blockEntity.insert(craftedStack, player); - if (!remainder.isEmpty()) { - player.drop(remainder, false); - } - } - - @Override - public boolean clearMatrix(final Player player, final boolean toPlayerInventory) { - boolean success = true; - for (int i = 0; i < getCraftingMatrix().getContainerSize(); ++i) { - final ItemStack matrixStack = getCraftingMatrix().getItem(i); - if (matrixStack.isEmpty()) { - continue; - } - if (toPlayerInventory) { - if (player.getInventory().add(matrixStack)) { - getCraftingMatrix().setItem(i, ItemStack.EMPTY); - } else { - success = false; - } - } else { - final ItemStack remainder = blockEntity.insert(matrixStack, player); - if (!remainder.isEmpty()) { - success = false; - } - getCraftingMatrix().setItem(i, remainder); - } - } - return success; - } - - @Override - public void transferRecipe(final Player player, final List> recipe) { - final boolean clearToPlayerInventory = blockEntity.getNetwork().isEmpty(); - if (!clearMatrix(player, clearToPlayerInventory)) { - return; - } - final Comparator sorter = ResourceSorters.create( - blockEntity.getNetwork().orElse(null), - player.getInventory() - ); - for (int i = 0; i < getCraftingMatrix().getContainerSize(); ++i) { - if (i >= recipe.size()) { - break; - } - final List possibilities = recipe.get(i); - possibilities.sort(sorter); - doTransferRecipe(i, possibilities, player); - } - } - - private void doTransferRecipe(final int index, final List sortedPossibilities, final Player player) { - for (final ItemResource possibility : sortedPossibilities) { - boolean extracted = blockEntity.extract(possibility, player) == 1; - if (!extracted) { - extracted = extractFromPlayerInventory(player, possibility); - } - if (extracted) { - getCraftingMatrix().setItem(index, possibility.toItemStack()); - return; - } - } - } - - private boolean extractFromPlayerInventory(final Player player, final ItemResource possibility) { - final ItemStack possibilityStack = possibility.toItemStack(); - for (int i = 0; i < player.getInventory().getContainerSize(); ++i) { - final ItemStack playerStack = player.getInventory().getItem(i); - if (ItemStack.isSameItemSameComponents(playerStack, possibilityStack)) { - player.getInventory().removeItem(i, 1); - return true; - } - } - return false; - } -} diff --git a/refinedstorage-common/src/main/java/com/refinedmods/refinedstorage/common/grid/CraftingGridRefillContext.java b/refinedstorage-common/src/main/java/com/refinedmods/refinedstorage/common/grid/CraftingGridRefillContext.java deleted file mode 100644 index 8ba2f0738..000000000 --- a/refinedstorage-common/src/main/java/com/refinedmods/refinedstorage/common/grid/CraftingGridRefillContext.java +++ /dev/null @@ -1,12 +0,0 @@ -package com.refinedmods.refinedstorage.common.grid; - -import com.refinedmods.refinedstorage.common.support.resource.ItemResource; - -import net.minecraft.world.entity.player.Player; - -interface CraftingGridRefillContext extends AutoCloseable { - boolean extract(ItemResource resource, Player player); - - @Override - void close(); -} diff --git a/refinedstorage-common/src/main/java/com/refinedmods/refinedstorage/common/grid/CraftingGridRefillContextImpl.java b/refinedstorage-common/src/main/java/com/refinedmods/refinedstorage/common/grid/CraftingGridRefillContextImpl.java deleted file mode 100644 index 62ef95490..000000000 --- a/refinedstorage-common/src/main/java/com/refinedmods/refinedstorage/common/grid/CraftingGridRefillContextImpl.java +++ /dev/null @@ -1,23 +0,0 @@ -package com.refinedmods.refinedstorage.common.grid; - -import com.refinedmods.refinedstorage.common.support.resource.ItemResource; - -import net.minecraft.world.entity.player.Player; - -class CraftingGridRefillContextImpl implements CraftingGridRefillContext { - private final CraftingGridBlockEntity blockEntity; - - CraftingGridRefillContextImpl(final CraftingGridBlockEntity blockEntity) { - this.blockEntity = blockEntity; - } - - @Override - public boolean extract(final ItemResource resource, final Player player) { - return blockEntity.extract(resource, player) == 1; - } - - @Override - public void close() { - // no op - } -} diff --git a/refinedstorage-common/src/main/java/com/refinedmods/refinedstorage/common/grid/CraftingGridResultSlot.java b/refinedstorage-common/src/main/java/com/refinedmods/refinedstorage/common/grid/CraftingGridResultSlot.java index 64dba45d1..9650928b8 100644 --- a/refinedstorage-common/src/main/java/com/refinedmods/refinedstorage/common/grid/CraftingGridResultSlot.java +++ b/refinedstorage-common/src/main/java/com/refinedmods/refinedstorage/common/grid/CraftingGridResultSlot.java @@ -28,9 +28,9 @@ public ItemStack onQuickCraft(final Player player) { final ItemStack singleResultStack = getItem().copy(); final int maxCrafted = singleResultStack.getMaxStackSize(); int crafted = 0; - try (CraftingGridRefillContext refillContext = craftingGrid.openSnapshotRefillContext(player)) { + try (ExtractTransaction transaction = craftingGrid.startExtractTransaction(player, false)) { while (ItemStack.isSameItemSameComponents(singleResultStack, getItem()) && crafted < maxCrafted) { - doTake(player, refillContext); + doTake(player, transaction); crafted += singleResultStack.getCount(); } } @@ -43,12 +43,12 @@ public void onTake(final Player player, final ItemStack stack) { if (player.level().isClientSide()) { return; } - try (CraftingGridRefillContext refillContext = craftingGrid.openRefillContext()) { - doTake(player, refillContext); + try (ExtractTransaction transaction = craftingGrid.startExtractTransaction(player, true)) { + doTake(player, transaction); } } - private void doTake(final Player player, final CraftingGridRefillContext refillContext) { + private void doTake(final Player player, final ExtractTransaction transaction) { fireCraftingEvents(player, getItem().copy()); final CraftingInput.Positioned positioned = craftingGrid.getCraftingMatrix().asPositionedCraftInput(); final CraftingInput input = positioned.input(); @@ -64,7 +64,7 @@ private void doTake(final Player player, final CraftingGridRefillContext refillC if (!remainingItem.isEmpty()) { useIngredientWithRemainingItem(player, index, remainingItem); } else if (!matrixStack.isEmpty()) { - useIngredient(player, refillContext, index, matrixStack); + useIngredient(player, transaction, index, matrixStack); } } } @@ -86,10 +86,10 @@ private void useIngredientWithRemainingItem(final Player player, } private void useIngredient(final Player player, - final CraftingGridRefillContext refillContext, + final ExtractTransaction transaction, final int index, final ItemStack matrixStack) { - if (matrixStack.getCount() > 1 || !refillContext.extract(ItemResource.ofItemStack(matrixStack), player)) { + if (matrixStack.getCount() > 1 || !transaction.extract(ItemResource.ofItemStack(matrixStack), player)) { decrementMatrixSlot(index); } } diff --git a/refinedstorage-common/src/main/java/com/refinedmods/refinedstorage/common/grid/DirectCommitExtractTransaction.java b/refinedstorage-common/src/main/java/com/refinedmods/refinedstorage/common/grid/DirectCommitExtractTransaction.java new file mode 100644 index 000000000..8c5d12d35 --- /dev/null +++ b/refinedstorage-common/src/main/java/com/refinedmods/refinedstorage/common/grid/DirectCommitExtractTransaction.java @@ -0,0 +1,26 @@ +package com.refinedmods.refinedstorage.common.grid; + +import com.refinedmods.refinedstorage.api.core.Action; +import com.refinedmods.refinedstorage.api.storage.root.RootStorage; +import com.refinedmods.refinedstorage.common.api.storage.PlayerActor; +import com.refinedmods.refinedstorage.common.support.resource.ItemResource; + +import net.minecraft.world.entity.player.Player; + +public class DirectCommitExtractTransaction implements ExtractTransaction { + private final RootStorage rootStorage; + + public DirectCommitExtractTransaction(final RootStorage rootStorage) { + this.rootStorage = rootStorage; + } + + @Override + public boolean extract(final ItemResource resource, final Player player) { + return rootStorage.extract(resource, 1, Action.EXECUTE, new PlayerActor(player)) == 1; + } + + @Override + public void close() { + // no op + } +} diff --git a/refinedstorage-common/src/main/java/com/refinedmods/refinedstorage/common/grid/ExtractTransaction.java b/refinedstorage-common/src/main/java/com/refinedmods/refinedstorage/common/grid/ExtractTransaction.java new file mode 100644 index 000000000..785d89a3a --- /dev/null +++ b/refinedstorage-common/src/main/java/com/refinedmods/refinedstorage/common/grid/ExtractTransaction.java @@ -0,0 +1,24 @@ +package com.refinedmods.refinedstorage.common.grid; + +import com.refinedmods.refinedstorage.common.support.resource.ItemResource; + +import net.minecraft.world.entity.player.Player; + +public interface ExtractTransaction extends AutoCloseable { + ExtractTransaction NOOP = new ExtractTransaction() { + @Override + public boolean extract(final ItemResource resource, final Player player) { + return false; + } + + @Override + public void close() { + // no op + } + }; + + boolean extract(ItemResource resource, Player player); + + @Override + void close(); +} diff --git a/refinedstorage-common/src/main/java/com/refinedmods/refinedstorage/common/grid/SnapshotCraftingGridRefillContext.java b/refinedstorage-common/src/main/java/com/refinedmods/refinedstorage/common/grid/SnapshotExtractTransaction.java similarity index 61% rename from refinedstorage-common/src/main/java/com/refinedmods/refinedstorage/common/grid/SnapshotCraftingGridRefillContext.java rename to refinedstorage-common/src/main/java/com/refinedmods/refinedstorage/common/grid/SnapshotExtractTransaction.java index 2a3f18d61..6316369ba 100644 --- a/refinedstorage-common/src/main/java/com/refinedmods/refinedstorage/common/grid/SnapshotCraftingGridRefillContext.java +++ b/refinedstorage-common/src/main/java/com/refinedmods/refinedstorage/common/grid/SnapshotExtractTransaction.java @@ -12,42 +12,35 @@ import net.minecraft.world.entity.player.Player; import net.minecraft.world.item.ItemStack; -class SnapshotCraftingGridRefillContext implements CraftingGridRefillContext { +public class SnapshotExtractTransaction implements ExtractTransaction { private final PlayerActor playerActor; - private final CraftingGridBlockEntity blockEntity; + private final RootStorage rootStorage; private final MutableResourceList available = MutableResourceListImpl.create(); private final MutableResourceList used = MutableResourceListImpl.create(); - SnapshotCraftingGridRefillContext( - final Player player, - final CraftingGridBlockEntity blockEntity, - final RecipeMatrixContainer craftingMatrix - ) { + public SnapshotExtractTransaction(final Player player, + final RootStorage rootStorage, + final RecipeMatrixContainer craftingMatrix) { this.playerActor = new PlayerActor(player); - this.blockEntity = blockEntity; + this.rootStorage = rootStorage; addAvailableItems(craftingMatrix); } private void addAvailableItems(final RecipeMatrixContainer craftingMatrix) { - blockEntity.getRootStorage().ifPresent(rootStorage -> { - for (int i = 0; i < craftingMatrix.getContainerSize(); ++i) { - addAvailableItem(craftingMatrix, rootStorage, i); - } - }); + for (int i = 0; i < craftingMatrix.getContainerSize(); ++i) { + addAvailableItem(craftingMatrix, i); + } } - private void addAvailableItem(final RecipeMatrixContainer craftingMatrix, - final RootStorage rootStorage, - final int craftingMatrixSlotIndex) { + private void addAvailableItem(final RecipeMatrixContainer craftingMatrix, final int craftingMatrixSlotIndex) { final ItemStack craftingMatrixStack = craftingMatrix.getItem(craftingMatrixSlotIndex); if (craftingMatrixStack.isEmpty()) { return; } - addAvailableItem(rootStorage, craftingMatrixStack); + addAvailableItem(craftingMatrixStack); } - private void addAvailableItem(final RootStorage rootStorage, - final ItemStack craftingMatrixStack) { + private void addAvailableItem(final ItemStack craftingMatrixStack) { final ItemResource craftingMatrixResource = ItemResource.ofItemStack(craftingMatrixStack); // a single resource can occur multiple times in a recipe, only add it once if (!available.contains(craftingMatrixResource)) { @@ -60,22 +53,16 @@ private void addAvailableItem(final RootStorage rootStorage, @Override public boolean extract(final ItemResource resource, final Player player) { - return blockEntity.getNetwork().map(network -> { - final boolean isAvailable = available.contains(resource); - if (isAvailable) { - available.remove(resource, 1); - used.add(resource, 1); - } - return isAvailable; - }).orElse(false); + final boolean isAvailable = available.contains(resource); + if (isAvailable) { + available.remove(resource, 1); + used.add(resource, 1); + } + return isAvailable; } @Override public void close() { - blockEntity.getRootStorage().ifPresent(this::extractUsedItems); - } - - private void extractUsedItems(final RootStorage rootStorage) { for (final ResourceKey usedResource : used.getAll()) { final long amountUsed = used.get(usedResource); rootStorage.extract(usedResource, amountUsed, Action.EXECUTE, playerActor); 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 e824e73f2..d48a18765 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 @@ -26,6 +26,7 @@ import java.util.List; import java.util.Optional; import java.util.Set; +import java.util.stream.Collectors; import net.minecraft.server.level.ServerPlayer; @@ -45,6 +46,10 @@ private Optional getSecurity() { return context.resolveNetwork().map(network -> network.getComponent(PlatformSecurityNetworkComponent.class)); } + private Optional getAutocrafting() { + return context.resolveNetwork().map(network -> network.getComponent(AutocraftingNetworkComponent.class)); + } + @Override public void addWatcher(final GridWatcher watcher, final Class actorType) { context.drainEnergy(Platform.INSTANCE.getConfig().getWirelessGrid().getOpenEnergyUsage()); @@ -70,9 +75,9 @@ public Storage getItemStorage() { @Override public boolean isGridActive() { - final boolean networkActive = context.resolveNetwork().map( - network -> network.getComponent(EnergyNetworkComponent.class).getStored() > 0 - ).orElse(false); + final boolean networkActive = context.resolveNetwork() + .map(network -> network.getComponent(EnergyNetworkComponent.class).getStored() > 0) + .orElse(false); return networkActive && context.isActive(); } @@ -83,15 +88,21 @@ public List getResources(final Class act @Override public Set getAutocraftableResources() { - return Collections.emptySet(); + return getAutocrafting() + .map(AutocraftingNetworkComponent::getOutputs) + .map(outputs -> outputs.stream() + .filter(PlatformResourceKey.class::isInstance) + .map(PlatformResourceKey.class::cast) + .collect(Collectors.toSet())) + .orElse(Collections.emptySet()); } @Override public GridOperations createOperations(final ResourceType resourceType, final ServerPlayer player) { return getStorage() - .flatMap(rootStorage -> - getSecurity().map(security -> createGridOperations(resourceType, player, rootStorage, security))) - .map(gridOperations -> (GridOperations) new WirelessGridOperations(gridOperations, context, watchers)) + .flatMap(rootStorage -> getSecurity() + .map(security -> createGridOperations(resourceType, player, rootStorage, security))) + .map(operations -> (GridOperations) new WirelessGridOperations(operations, context, watchers)) .orElseGet(NoopGridOperations::new); } @@ -106,15 +117,11 @@ private GridOperations createGridOperations(final ResourceType resourceType, @Override public Optional getPreview(final ResourceKey resource, final long amount) { - return context.resolveNetwork() - .map(network -> network.getComponent(AutocraftingNetworkComponent.class)) - .flatMap(component -> component.getPreview(resource, amount)); + return getAutocrafting().flatMap(component -> component.getPreview(resource, amount)); } @Override public boolean startTask(final ResourceKey resource, final long amount) { - return context.resolveNetwork() - .map(network -> network.getComponent(AutocraftingNetworkComponent.class).startTask(resource, amount)) - .orElse(false); + return getAutocrafting().map(autocrafting -> autocrafting.startTask(resource, amount)).orElse(false); } } diff --git a/refinedstorage-common/src/main/java/com/refinedmods/refinedstorage/common/grid/screen/CraftingGridScreen.java b/refinedstorage-common/src/main/java/com/refinedmods/refinedstorage/common/grid/screen/CraftingGridScreen.java index d2b6a8dc6..162b8fa72 100644 --- a/refinedstorage-common/src/main/java/com/refinedmods/refinedstorage/common/grid/screen/CraftingGridScreen.java +++ b/refinedstorage-common/src/main/java/com/refinedmods/refinedstorage/common/grid/screen/CraftingGridScreen.java @@ -2,7 +2,7 @@ import com.refinedmods.refinedstorage.common.Platform; import com.refinedmods.refinedstorage.common.content.KeyMappings; -import com.refinedmods.refinedstorage.common.grid.CraftingGridContainerMenu; +import com.refinedmods.refinedstorage.common.grid.AbstractCraftingGridContainerMenu; import com.refinedmods.refinedstorage.common.grid.CraftingGridMatrixCloseBehavior; import com.refinedmods.refinedstorage.common.support.tooltip.HelpClientTooltipComponent; import com.refinedmods.refinedstorage.common.support.widget.HoveredImageButton; @@ -27,7 +27,7 @@ import static com.refinedmods.refinedstorage.common.util.IdentifierUtil.createIdentifier; import static com.refinedmods.refinedstorage.common.util.IdentifierUtil.createTranslation; -public class CraftingGridScreen extends AbstractGridScreen { +public class CraftingGridScreen extends AbstractGridScreen { private static final ResourceLocation TEXTURE = createIdentifier("textures/gui/crafting_grid.png"); private static final WidgetSprites CLEAR_BUTTON_TO_PLAYER_INVENTORY_SPRITES = new WidgetSprites( @@ -51,7 +51,9 @@ public class CraftingGridScreen extends AbstractGridScreen> recipe) { + final boolean cleared = rootStorage == null + ? clearToPlayerInventory(player) + : clearIntoStorage(rootStorage, player); + if (!cleared) { + return; + } + final Comparator sorter = ResourceSorters.create(rootStorage, player.getInventory()); + for (int i = 0; i < getContainerSize(); ++i) { + if (i >= recipe.size()) { + break; + } + final List possibilities = recipe.get(i); + possibilities.sort(sorter); + doTransferRecipe(i, possibilities, player, rootStorage); + } + } + + private void doTransferRecipe(final int index, + final List sortedPossibilities, + final Player player, + @Nullable final RootStorage rootStorage) { + for (final ItemResource possibility : sortedPossibilities) { + boolean extracted = rootStorage != null + && rootStorage.extract(possibility, 1, Action.EXECUTE, new PlayerActor(player)) == 1; + if (!extracted) { + extracted = extractSingleItemFromPlayerInventory(player, possibility); + } + if (extracted) { + setItem(index, possibility.toItemStack()); + return; + } + } + } + + private boolean extractSingleItemFromPlayerInventory(final Player player, final ItemResource possibility) { + final ItemStack possibilityStack = possibility.toItemStack(); + for (int i = 0; i < player.getInventory().getContainerSize(); ++i) { + final ItemStack playerStack = player.getInventory().getItem(i); + if (ItemStack.isSameItemSameComponents(playerStack, possibilityStack)) { + player.getInventory().removeItem(i, 1); + return true; + } + } + return false; + } } diff --git a/refinedstorage-common/src/main/java/com/refinedmods/refinedstorage/common/support/network/ResourceSorters.java b/refinedstorage-common/src/main/java/com/refinedmods/refinedstorage/common/support/network/ResourceSorters.java index bf5ff6516..f37df6d3c 100644 --- a/refinedstorage-common/src/main/java/com/refinedmods/refinedstorage/common/support/network/ResourceSorters.java +++ b/refinedstorage-common/src/main/java/com/refinedmods/refinedstorage/common/support/network/ResourceSorters.java @@ -1,11 +1,10 @@ package com.refinedmods.refinedstorage.common.support.network; -import com.refinedmods.refinedstorage.api.network.Network; -import com.refinedmods.refinedstorage.api.network.storage.StorageNetworkComponent; import com.refinedmods.refinedstorage.api.resource.ResourceKey; import com.refinedmods.refinedstorage.api.resource.list.MutableResourceList; import com.refinedmods.refinedstorage.api.resource.list.MutableResourceListImpl; import com.refinedmods.refinedstorage.api.resource.list.ResourceList; +import com.refinedmods.refinedstorage.api.storage.root.RootStorage; import com.refinedmods.refinedstorage.common.support.resource.ItemResource; import java.util.Comparator; @@ -19,22 +18,24 @@ public final class ResourceSorters { private ResourceSorters() { } - public static Comparator create(@Nullable final Network network, final Inventory playerInventory) { - return create(network, playerInventory, Function.identity()); + public static Comparator create(@Nullable final RootStorage rootStorage, + final Inventory playerInventory) { + return create(rootStorage, playerInventory, Function.identity()); } - public static Comparator create(@Nullable final Network network, + public static Comparator create(@Nullable final RootStorage rootStorage, final Inventory playerInventory, final Function resourceExtractor) { final MutableResourceList available = MutableResourceListImpl.create(); - addNetworkItemsIntoList(network, available); + addRootStorageItemsIntoList(rootStorage, available); addPlayerInventoryItemsIntoList(playerInventory, available); return sortByHighestAvailableFirst(available, resourceExtractor); } - private static void addNetworkItemsIntoList(@Nullable final Network network, final MutableResourceList list) { - if (network != null) { - network.getComponent(StorageNetworkComponent.class).getAll().forEach(list::add); + private static void addRootStorageItemsIntoList(@Nullable final RootStorage rootStorage, + final MutableResourceList list) { + if (rootStorage != null) { + rootStorage.getAll().forEach(list::add); } } diff --git a/refinedstorage-common/src/main/java/com/refinedmods/refinedstorage/common/support/packet/c2s/CraftingGridClearPacket.java b/refinedstorage-common/src/main/java/com/refinedmods/refinedstorage/common/support/packet/c2s/CraftingGridClearPacket.java index 2397c8a0f..6bb17100b 100644 --- a/refinedstorage-common/src/main/java/com/refinedmods/refinedstorage/common/support/packet/c2s/CraftingGridClearPacket.java +++ b/refinedstorage-common/src/main/java/com/refinedmods/refinedstorage/common/support/packet/c2s/CraftingGridClearPacket.java @@ -1,6 +1,6 @@ package com.refinedmods.refinedstorage.common.support.packet.c2s; -import com.refinedmods.refinedstorage.common.grid.CraftingGridContainerMenu; +import com.refinedmods.refinedstorage.common.grid.AbstractCraftingGridContainerMenu; import com.refinedmods.refinedstorage.common.support.packet.PacketContext; import net.minecraft.network.RegistryFriendlyByteBuf; @@ -19,7 +19,7 @@ public record CraftingGridClearPacket(boolean toPlayerInventory) implements Cust ); public static void handle(final CraftingGridClearPacket packet, final PacketContext ctx) { - if (ctx.getPlayer().containerMenu instanceof CraftingGridContainerMenu craftingGridContainerMenu) { + if (ctx.getPlayer().containerMenu instanceof AbstractCraftingGridContainerMenu craftingGridContainerMenu) { craftingGridContainerMenu.clear(packet.toPlayerInventory()); } } diff --git a/refinedstorage-common/src/main/java/com/refinedmods/refinedstorage/common/support/packet/c2s/CraftingGridRecipeTransferPacket.java b/refinedstorage-common/src/main/java/com/refinedmods/refinedstorage/common/support/packet/c2s/CraftingGridRecipeTransferPacket.java index be2d9e879..71665c8e2 100644 --- a/refinedstorage-common/src/main/java/com/refinedmods/refinedstorage/common/support/packet/c2s/CraftingGridRecipeTransferPacket.java +++ b/refinedstorage-common/src/main/java/com/refinedmods/refinedstorage/common/support/packet/c2s/CraftingGridRecipeTransferPacket.java @@ -1,6 +1,6 @@ package com.refinedmods.refinedstorage.common.support.packet.c2s; -import com.refinedmods.refinedstorage.common.grid.CraftingGridContainerMenu; +import com.refinedmods.refinedstorage.common.grid.AbstractCraftingGridContainerMenu; import com.refinedmods.refinedstorage.common.support.packet.PacketContext; import com.refinedmods.refinedstorage.common.support.resource.ItemResource; import com.refinedmods.refinedstorage.common.support.resource.ResourceCodecs; @@ -28,7 +28,7 @@ public record CraftingGridRecipeTransferPacket(List> recipe) ); public static void handle(final CraftingGridRecipeTransferPacket packet, final PacketContext ctx) { - if (ctx.getPlayer().containerMenu instanceof CraftingGridContainerMenu craftingGridContainerMenu) { + if (ctx.getPlayer().containerMenu instanceof AbstractCraftingGridContainerMenu craftingGridContainerMenu) { craftingGridContainerMenu.transferRecipe(packet.recipe()); } } 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 db3ddc104..17796cf06 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 @@ -103,7 +103,7 @@ public > void MenuScreens.register(type, factory::create); } }); - registerKeyBindings(); + registerKeyMappings(); registerModelPredicates(); registerResourceRendering(); registerAlternativeGridHints(); @@ -496,7 +496,7 @@ private void registerCustomTooltips() { }); } - private void registerKeyBindings() { + private void registerKeyMappings() { KeyMappings.INSTANCE.setFocusSearchBar(KeyBindingHelper.registerKeyBinding(new KeyMapping( ContentNames.FOCUS_SEARCH_BAR_TRANSLATION_KEY, InputConstants.Type.KEYSYM,