Skip to content

Commit

Permalink
working like base AE2, transfer to matrix
Browse files Browse the repository at this point in the history
  • Loading branch information
rlnt committed Jun 8, 2024
1 parent 37d7ca7 commit cfd625b
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 73 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
import net.minecraft.client.gui.screens.Screen;
import net.minecraft.client.gui.screens.inventory.AbstractContainerScreen;
import net.minecraft.core.NonNullList;
import net.minecraft.network.chat.Component;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.world.item.ItemStack;
import net.minecraft.world.item.crafting.*;
import org.jetbrains.annotations.Nullable;
Expand Down Expand Up @@ -45,7 +45,7 @@ protected Result transferRecipe(T menu, RecipeHolder<?> holder, EmiRecipe emiRec

boolean craftingRecipe = isCraftingRecipe(recipe, emiRecipe);
if (!craftingRecipe) {
return transferItemsToInventory(menu, recipe);
return transferItemsToInventory(menu, recipeId, recipe, doTransfer);
}

if (!fitsIn3x3Grid(recipe, emiRecipe)) {
Expand Down Expand Up @@ -80,28 +80,27 @@ protected Result transferRecipe(T menu, RecipeHolder<?> holder, EmiRecipe emiRec
return Result.createSuccessful();
}

private Result transferItemsToInventory(T menu, @Nullable Recipe<?> recipe) {
private Result transferItemsToInventory(T menu, ResourceLocation recipeId, @Nullable Recipe<?> recipe, boolean doTransfer) {
if (recipe == null) {
return Result.createNotApplicable();
}

if (!Screen.hasShiftDown()) {
return Result.createFailed(Component.literal("Press Shift to quick transfer items to your inventory."));
}

// get inputs from the recipe
NonNullList<Ingredient> ingredients = recipe.getIngredients();

// find missing and craftable items to highlight them in the recipe screen
CraftingTermMenu.TransferData transferData = menu.findMissingTransferIngredients(ingredients);
// Find missing ingredient
var slotToIngredientMap = getGuiSlotToIngredientMapTransfer(recipe);
var missingSlots = menu.findMissingIngredients(slotToIngredientMap);

var result = Result.createMissingTransferItems(transferData.missing(), transferData.craftable());
if (result.canCraft()) {
// actually transfer
return Result.createSuccessful();
if (doTransfer) {
boolean craftMissing = Screen.hasControlDown();
CraftingHelper.performTransfer(menu, recipeId, recipe, craftMissing);
// craft items and move to player inventory
} else {
if (missingSlots.anyMissingOrCraftable()) {
// Highlight the slots with missing ingredients
return new Result.PartiallyCraftable(missingSlots);
}
}

return result;
return Result.createSuccessful();
}

private Recipe<?> createFakeCraftingRecipe(EmiRecipe display) {
Expand Down Expand Up @@ -142,4 +141,16 @@ public static Map<Integer, Ingredient> getGuiSlotToIngredientMap(Recipe<?> recip
return result;
}

public static Map<Integer, Ingredient> getGuiSlotToIngredientMapTransfer(Recipe<?> recipe) {
var ingredients = recipe.getIngredients();

Map<Integer, Ingredient> result = new HashMap<>();
for (int i = 0; i < ingredients.size(); i++) {
var ingredient = ingredients.get(i);
if (!ingredient.isEmpty()) {
result.put(i, ingredient);
}
}
return result;
}
}
60 changes: 4 additions & 56 deletions src/main/java/appeng/menu/me/items/CraftingTermMenu.java
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,10 @@
import net.minecraft.world.item.crafting.RecipeType;
import net.minecraft.world.level.Level;

import java.util.*;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

/**
* Can only be used with a host that implements {@link ISegmentedInventory} and exposes an inventory named "crafting" to
Expand Down Expand Up @@ -210,61 +213,6 @@ public boolean hasIngredient(Ingredient ingredient, Object2IntOpenHashMap<Object
return super.hasIngredient(ingredient, reservedAmounts);
}

public TransferData findMissingTransferIngredients(List<Ingredient> ingredients) {

List<Ingredient> missing = new ArrayList<>();
List<Ingredient> craftable = new ArrayList<>();

var reservedGridAmounts = new Object2IntOpenHashMap<>();
var playerItems = getPlayerInventory().items;
var reservedPlayerItems = new int[playerItems.size()];

for (var ingredient : ingredients) {

boolean found = false;
// Player inventory is cheaper to check
for (int i = 0; i < playerItems.size(); i++) {
// Do not consider locked slots
if (isPlayerInventorySlotLocked(i)) {
continue;
}

var stack = playerItems.get(i);
if (stack.getCount() - reservedPlayerItems[i] > 0 && ingredient.test(stack)) {
reservedPlayerItems[i]++;
found = true;
break;
}
}

// Then check the terminal screen's repository of network items
if (!found) {
// We use AE stacks to get an easily comparable item type key that ignores stack size
if (hasIngredient(ingredient, reservedGridAmounts)) {
reservedGridAmounts.merge(ingredient, 1, Integer::sum);
found = true;
}
}

// Check the terminal once again, but this time for craftable items
if (!found) {
for (var stack : ingredient.getItems()) {
if (isCraftable(stack)) {
craftable.add(ingredient);
found = true;
break;
}
}
}

if (!found) {
missing.add(ingredient);
}
}

return new TransferData(missing, craftable);
}

/**
* Determines which slots of the given slot-to-item map cannot be filled with items based on the contents of this
* terminal or player inventory.
Expand Down

0 comments on commit cfd625b

Please sign in to comment.