Skip to content

Commit

Permalink
Merge pull request #1 from wendavid552/fabric_1.18.x
Browse files Browse the repository at this point in the history
  • Loading branch information
wendavid552 authored Feb 23, 2023
2 parents c40a3c6 + 8f2fa69 commit 5ddf34c
Show file tree
Hide file tree
Showing 10 changed files with 137 additions and 52 deletions.
3 changes: 1 addition & 2 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -42,5 +42,4 @@ jobs:

- uses: actions/[email protected]
with:
name: build-artifacts
path: build/libs/
path: build/libs/*
1 change: 1 addition & 0 deletions .github/workflows/gradle.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ on:
- "versions/**"
- ".github/**"
pull_request:
workflow_dispatch:

jobs:
build:
Expand Down
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,4 @@ eclipse/
.project
.idea
build.number
run/
5 changes: 4 additions & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
# Thu Jan 14 08:33:00 EET 2016
org.gradle.configureondemand=true
org.gradle.jvmargs = -Xmx4G
1
org.gradle.daemon=true
org.gradle.parallel=true

# mod information
group = fi.dy.masa
mod_id = itemscroller
Expand Down
9 changes: 5 additions & 4 deletions src/main/java/fi/dy/masa/itemscroller/config/Configs.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@
import net.minecraft.client.gui.screen.ingame.CraftingScreen;
import net.minecraft.client.gui.screen.ingame.GrindstoneScreen;
import net.minecraft.client.gui.screen.ingame.InventoryScreen;
import net.minecraft.screen.ForgingScreenHandler;
import net.minecraft.screen.GrindstoneScreenHandler;
import net.minecraft.screen.slot.CraftingResultSlot;
import fi.dy.masa.itemscroller.Reference;
import fi.dy.masa.itemscroller.recipes.CraftingHandler;
Expand All @@ -22,7 +24,6 @@
import fi.dy.masa.malilib.config.options.ConfigInteger;
import fi.dy.masa.malilib.util.FileUtils;
import fi.dy.masa.malilib.util.JsonUtils;
import net.minecraft.screen.slot.Slot;

public class Configs implements IConfigHandler
{
Expand Down Expand Up @@ -125,9 +126,9 @@ public static void loadFromFile()
//"net.minecraft.client.gui.inventory.PlayerInventoryScreen,net.minecraft.inventory.SlotCrafting,0,1-4", // vanilla player inventory crafting grid
CraftingHandler.addCraftingGridDefinition(InventoryScreen.class.getName(), CraftingResultSlot.class.getName(), 0, new SlotRange(1, 4));
// vanilla anvil
CraftingHandler.addCraftingGridDefinition(AnvilScreen.class.getName(), Slot.class.getName(), 2, new SlotRange(0, 2));
CraftingHandler.addCraftingGridDefinition(AnvilScreen.class.getName(), ForgingScreenHandler.class.getName()+"$2", 2, new SlotRange(0, 2));
// vanill grindstone
CraftingHandler.addCraftingGridDefinition(GrindstoneScreen.class.getName(), Slot.class.getName(), 2, new SlotRange(0, 2));
CraftingHandler.addCraftingGridDefinition(GrindstoneScreen.class.getName(), GrindstoneScreenHandler.class.getName()+"$4", 2, new SlotRange(0, 2));
}

public static void saveToFile()
Expand Down Expand Up @@ -179,7 +180,7 @@ private static void getStrings(JsonObject obj, Set<String> outputSet, String arr

private static void writeStrings(JsonObject obj, Set<String> inputSet, String arrayName)
{
if (inputSet.isEmpty() == false)
if (!inputSet.isEmpty())
{
JsonArray arr = new JsonArray();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -189,15 +189,19 @@ public void onClientTick(MinecraftClient mc)
CraftingRecipe bookRecipe = InventoryUtils.getBookRecipeFromPattern(recipe);
if (bookRecipe != null && !bookRecipe.isIgnoredInRecipeBook()) { // Use recipe book if possible
// System.out.println("recipe");
mc.interactionManager.clickRecipe(gui.getScreenHandler().syncId, bookRecipe, true);
int option = InventoryUtils.checkRecipeEnough(recipe, gui);
if(option > 0) {
mc.interactionManager.clickRecipe(gui.getScreenHandler().syncId, bookRecipe, option > 1);
}
} else {
// System.out.println("move");
InventoryUtils.tryMoveItemsToFirstCraftingGrid(recipe, gui, true);
}

for (int i = 0; i < recipe.getMaxCraftAmount(); i++) {
InventoryUtils.dropStack(gui, outputSlot.id);
}
// for (int i = 0; i < recipe.getMaxCraftAmount(); i++) {
// InventoryUtils.dropStack(gui, outputSlot.id);
// }
InventoryUtils.dropItem(gui, outputSlot.id);

InventoryUtils.tryClearCursor(gui);
InventoryUtils.throwAllCraftingResultsToGround(recipe, gui);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -232,7 +232,7 @@ private void renderRecipeItems(RecipePattern recipe, int recipeCountPerPage, Han

for (int i = 0, row = 0; row < recipeDimensions; row++)
{
for (int col = 0; col < recipeDimensions; col++, i++)
for (int col = 0; col < recipeDimensions && i < recipe.getRecipeLength(); col++, i++)
{
int xOff = col * 17;
int yOff = row * 17;
Expand All @@ -255,7 +255,7 @@ private ItemStack getHoveredRecipeIngredient(int mouseX, int mouseY, RecipePatte
{
for (int i = 0, row = 0; row < recipeDimensions; row++)
{
for (int col = 0; col < recipeDimensions; col++, i++)
for (int col = 0; col < recipeDimensions && i < recipe.getRecipeLength(); col++, i++)
{
int xOff = col * scaledGridEntry;
int yOff = row * scaledGridEntry;
Expand Down
18 changes: 18 additions & 0 deletions src/main/java/fi/dy/masa/itemscroller/mixin/IMixinAnvilScreen.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package fi.dy.masa.itemscroller.mixin;

import net.minecraft.client.gui.screen.ingame.AnvilScreen;
import net.minecraft.client.gui.widget.TextFieldWidget;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.gen.Accessor;
import org.spongepowered.asm.mixin.gen.Invoker;

import java.awt.*;

@Mixin(AnvilScreen.class)
public interface IMixinAnvilScreen {
@Invoker("onRenamed")
void itemscroller_setItemName(String newName);

@Accessor("nameField")
TextFieldWidget itemscroller_getNameField();
}
97 changes: 77 additions & 20 deletions src/main/java/fi/dy/masa/itemscroller/util/InventoryUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
import fi.dy.masa.itemscroller.ItemScroller;
import fi.dy.masa.itemscroller.config.Configs;
import fi.dy.masa.itemscroller.config.Hotkeys;
import fi.dy.masa.itemscroller.mixin.IMixinAnvilScreen;
import fi.dy.masa.itemscroller.recipes.CraftingHandler;
import fi.dy.masa.itemscroller.recipes.CraftingHandler.SlotRange;
import fi.dy.masa.itemscroller.recipes.RecipePattern;
Expand All @@ -22,20 +23,19 @@
import fi.dy.masa.malilib.util.GuiUtils;
import net.minecraft.client.MinecraftClient;
import net.minecraft.client.gui.screen.Screen;
import net.minecraft.client.gui.screen.ingame.CreativeInventoryScreen;
import net.minecraft.client.gui.screen.ingame.HandledScreen;
import net.minecraft.client.gui.screen.ingame.InventoryScreen;
import net.minecraft.client.gui.screen.ingame.MerchantScreen;
import net.minecraft.client.gui.screen.ingame.*;
import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.entity.player.PlayerInventory;
import net.minecraft.inventory.CraftingInventory;
import net.minecraft.inventory.CraftingResultInventory;
import net.minecraft.inventory.Inventory;
import net.minecraft.item.Item;
import net.minecraft.item.ItemGroup;
import net.minecraft.item.ItemStack;
import net.minecraft.recipe.CraftingRecipe;
import net.minecraft.recipe.RecipeManager;
import net.minecraft.recipe.RecipeType;
import net.minecraft.screen.AnvilScreenHandler;
import net.minecraft.screen.Generic3x3ContainerScreenHandler;
import net.minecraft.screen.MerchantScreenHandler;
import net.minecraft.screen.ScreenHandler;
Expand All @@ -52,6 +52,7 @@
import fi.dy.masa.itemscroller.mixin.IMixinCraftingResultSlot;
import it.unimi.dsi.fastutil.ints.IntArrayList;
import it.unimi.dsi.fastutil.ints.IntComparator;
import org.apache.commons.lang3.StringUtils;

public class InventoryUtils {
private static final Set<Integer> DRAGGED_SLOTS = new HashSet<>();
Expand Down Expand Up @@ -1195,7 +1196,7 @@ private static boolean tryMoveItemsToCraftingGridSlots(RecipePattern recipe,

// This slot is used to check that we get items from a DIFFERENT inventory than
// where this slot is in
Slot slotGridFirst = container.getSlot(range.getFirst());
Slot[] slotGrids = {container.getSlot(range.getFirst()), slot};
Map<ItemType, IntArrayList> ingredientSlots = ItemType.getSlotsPerItem(recipe.getRecipeItems());

for (Map.Entry<ItemType, IntArrayList> entry : ingredientSlots.entrySet()) {
Expand All @@ -1210,9 +1211,9 @@ private static boolean tryMoveItemsToCraftingGridSlots(RecipePattern recipe,
}

if (fillStacks) {
fillCraftingGrid(gui, slotGridFirst, ingredientReference, targetSlots);
fillCraftingGrid(gui, slotGrids, ingredientReference, targetSlots);
} else {
moveOneRecipeItemIntoCraftingGrid(gui, slotGridFirst, ingredientReference, targetSlots);
moveOneRecipeItemIntoCraftingGrid(gui, slotGrids, ingredientReference, targetSlots);
}
}
}
Expand All @@ -1221,7 +1222,7 @@ private static boolean tryMoveItemsToCraftingGridSlots(RecipePattern recipe,
}

private static void fillCraftingGrid(HandledScreen<? extends ScreenHandler> gui,
Slot slotGridFirst,
Slot[] slotGrids,
ItemStack ingredientReference,
IntArrayList targetSlots) {
ScreenHandler container = gui.getScreenHandler();
Expand All @@ -1234,7 +1235,7 @@ private static void fillCraftingGrid(HandledScreen<? extends ScreenHandler> gui,
}

while (true) {
slotNum = getSlotNumberOfLargestMatchingStackFromDifferentInventory(container, slotGridFirst,
slotNum = getSlotNumberOfLargestMatchingStackFromDifferentInventory(container, slotGrids,
ingredientReference);

// Didn't find ingredient items
Expand Down Expand Up @@ -1350,20 +1351,26 @@ public static void craftEverythingPossibleWithCurrentRecipe(RecipePattern recipe
SlotRange range = CraftingHandler.getCraftingGridSlots(gui, slot);

if (range != null) {

String cacheName = null;
for (int i = 0; i < 36; i++) {
CraftingRecipe bookRecipe = getBookRecipeFromPattern(recipe);
if (bookRecipe != null && !bookRecipe.isIgnoredInRecipeBook()) { // Use recipe book if possible
MinecraftClient mc = MinecraftClient.getInstance();
mc.interactionManager.clickRecipe(gui.getScreenHandler().syncId, bookRecipe, true);
} else {
if(cacheName == null && gui instanceof AnvilScreen) {
cacheName = ((IMixinAnvilScreen)gui).itemscroller_getNameField().getText();
}
// Clear all items from the grid first, to avoid unbalanced stacks
if (clearCraftingGridOfItems(recipe, gui, range, false) == false) {
continue;
}

tryMoveItemsToCraftingGridSlots(recipe, slot, gui, true);
}
if(!StringUtils.isBlank(cacheName) && gui instanceof AnvilScreen) {
((IMixinAnvilScreen)gui).itemscroller_setItemName(cacheName);
}
shiftClickSlot(gui, slot.id);
}
// craftAsManyItemsAsPossible(recipe, slot, gui);
Expand Down Expand Up @@ -1488,6 +1495,46 @@ public static void setCraftingGridContentsUsingSwaps(HandledScreen<? extends Scr
}
}

/**
* Check whether the player has enough items in their inventory
* to craft the given recipe even after an extra crafting step.
* For example, if the recipe requires 2 stone items, thus we cannot craft it with exactly 2 stone items left,
* as after the single crafting we could not pick up another stone item if the inventory is full.
* @param recipe The recipe to be crafted
*/
public static int checkRecipeEnough(RecipePattern recipe, HandledScreen<? extends ScreenHandler> gui) {
Slot craftingOutputSlot = CraftingHandler.getFirstCraftingOutputSlotForGui(gui);
ScreenHandler container = gui.getScreenHandler();
int numSlots = container.slots.size();
SlotRange range = CraftingHandler.getCraftingGridSlots(gui, craftingOutputSlot);

// Check that the slot range is valid and that the recipe can fit into this type
// of crafting grid
if (range != null && range.getLast() < numSlots && recipe.getRecipeLength() <= range.getSlotCount()) {
// This slot is used to check that we get items from a DIFFERENT inventory than
// where this slot is in
Map<ItemType, IntArrayList> ingredientSlots = ItemType.getSlotsPerItem(recipe.getRecipeItems());
Slot[] slotReference = {container.getSlot(range.getFirst()), craftingOutputSlot};
for(Map.Entry<ItemType, IntArrayList> entry : ingredientSlots.entrySet()) {
int countItems = 0;
final int numSlotsWithItem = entry.getValue().size();
final ItemStack stackReference = entry.getKey().getStack();
final int itemsCraftOnce = numSlotsWithItem * stackReference.getMaxCount();
for(Slot slot : container.slots) {
if(!areSlotsInSameInventory(slot, slotReference) && slot.hasStack()
&& areStacksEqual(stackReference, slot.getStack())){
countItems += slot.getStack().getCount();
}
}
if(countItems <= itemsCraftOnce && countItems % numSlotsWithItem == 0) {
return countItems == numSlotsWithItem ? 0 : 1;
}
}
return 2;
}
return 0;
}

private static int putSingleItemIntoSlots(HandledScreen<? extends ScreenHandler> gui,
IntArrayList targetSlots,
int startIndex) {
Expand Down Expand Up @@ -1534,7 +1581,7 @@ public static void moveOneSetOfItemsFromSlotToPlayerInventory(HandledScreen<? ex
}

private static void moveOneRecipeItemIntoCraftingGrid(HandledScreen<? extends ScreenHandler> gui,
Slot slotGridFirst,
Slot[] slotGrids,
ItemStack ingredientReference,
IntArrayList targetSlots) {
ScreenHandler container = gui.getScreenHandler();
Expand All @@ -1543,7 +1590,7 @@ private static void moveOneRecipeItemIntoCraftingGrid(HandledScreen<? extends Sc
int slotCount = targetSlots.size();

while (index < slotCount) {
slotNum = getSlotNumberOfSmallestStackFromDifferentInventory(container, slotGridFirst, ingredientReference,
slotNum = getSlotNumberOfSmallestStackFromDifferentInventory(container, slotGrids, ingredientReference,
slotCount);

// Didn't find ingredient items
Expand Down Expand Up @@ -1615,7 +1662,7 @@ private static void moveItemsFromInventory(HandledScreen<? extends ScreenHandler
}

private static int getSlotNumberOfLargestMatchingStackFromDifferentInventory(ScreenHandler container,
Slot slotReference,
Slot[] slotReference,
ItemStack stackReference) {
int slotNum = -1;
int largest = 0;
Expand All @@ -1624,8 +1671,8 @@ private static int getSlotNumberOfLargestMatchingStackFromDifferentInventory(Scr
if (areSlotsInSameInventory(slot, slotReference) == false && slot.hasStack()
&& areStacksEqual(stackReference, slot.getStack())) {
int stackSize = getStackSize(slot.getStack());

if (stackSize > largest) {
// 这个stack应该是legal的,也就是我们强行忽略掉overstacked items
if (stackSize > largest && stackSize <= slot.getStack().getMaxCount()) {
slotNum = slot.id;
largest = stackSize;
}
Expand All @@ -1642,7 +1689,7 @@ && areStacksEqual(stackReference, slot.getStack())) {
* then the largest one is selected.
*/
private static int getSlotNumberOfSmallestStackFromDifferentInventory(ScreenHandler container,
Slot slotReference,
Slot[] slotReference,
ItemStack stackReference,
int idealSize) {
int slotNumSmallest = -1;
Expand Down Expand Up @@ -1797,6 +1844,14 @@ public static boolean areStacksEqual(ItemStack stack1, ItemStack stack2) {
return stack1.isEmpty() == false && stack1.isItemEqual(stack2) && ItemStack.areNbtEqual(stack1, stack2);
}

private static boolean areSlotsInSameInventory(Slot slot1, Slot[] slots) {
for (Slot slot: slots) {
if(areSlotsInSameInventory(slot, slot1)) {
return true;
}
}
return false;
}
private static boolean areSlotsInSameInventory(Slot slot1, Slot slot2) {
return areSlotsInSameInventory(slot1, slot2, false);
}
Expand Down Expand Up @@ -2356,17 +2411,19 @@ private static void dragSplitItemsIntoSlots(HandledScreen<? extends ScreenHandle
return;
}

int numSlots = gui.getScreenHandler().slots.size();

// Start the drag
clickSlot(gui, -999, 0, SlotActionType.QUICK_CRAFT);

ScreenHandler container = gui.getScreenHandler();
int numSlots = container.slots.size();
for (int slotNum : targetSlots) {
if (slotNum >= numSlots) {
break;
}

clickSlot(gui, slotNum, 1, SlotActionType.QUICK_CRAFT);
ItemStack item = container.slots.get(slotNum).getStack();
if(item.getCount() < item.getMaxCount()) {
clickSlot(gui, slotNum, 1, SlotActionType.QUICK_CRAFT);
}
}

// End the drag
Expand Down
Loading

0 comments on commit 5ddf34c

Please sign in to comment.