Skip to content

Commit

Permalink
Merge pull request #8 from wendavid552/fabric_1.18.x_dev
Browse files Browse the repository at this point in the history
使切石机可用于massCraft
  • Loading branch information
wendavid552 authored Feb 23, 2023
2 parents 8f2fa69 + a1b95c8 commit e912543
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 18 deletions.
10 changes: 5 additions & 5 deletions src/main/java/fi/dy/masa/itemscroller/config/Configs.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,10 @@
import com.google.gson.JsonArray;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import net.minecraft.client.gui.screen.ingame.AnvilScreen;
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.client.gui.screen.ingame.*;
import net.minecraft.screen.ForgingScreenHandler;
import net.minecraft.screen.GrindstoneScreenHandler;
import net.minecraft.screen.StonecutterScreenHandler;
import net.minecraft.screen.slot.CraftingResultSlot;
import fi.dy.masa.itemscroller.Reference;
import fi.dy.masa.itemscroller.recipes.CraftingHandler;
Expand Down Expand Up @@ -127,8 +125,10 @@ public static void loadFromFile()
CraftingHandler.addCraftingGridDefinition(InventoryScreen.class.getName(), CraftingResultSlot.class.getName(), 0, new SlotRange(1, 4));
// vanilla anvil
CraftingHandler.addCraftingGridDefinition(AnvilScreen.class.getName(), ForgingScreenHandler.class.getName()+"$2", 2, new SlotRange(0, 2));
// vanill grindstone
// vanilla grindstone
CraftingHandler.addCraftingGridDefinition(GrindstoneScreen.class.getName(), GrindstoneScreenHandler.class.getName()+"$4", 2, new SlotRange(0, 2));
// vanilla stonecutter
CraftingHandler.addCraftingGridDefinition(StonecutterScreen.class.getName(), StonecutterScreenHandler.class.getName()+"$2", 1, new SlotRange(0, 1));
}

public static void saveToFile()
Expand Down
25 changes: 18 additions & 7 deletions src/main/java/fi/dy/masa/itemscroller/event/KeybindCallbacks.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,10 @@
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.StonecutterScreen;
import net.minecraft.recipe.CraftingRecipe;
import net.minecraft.recipe.StonecuttingRecipe;
import net.minecraft.screen.StonecutterScreenHandler;
import net.minecraft.screen.slot.Slot;
import fi.dy.masa.itemscroller.ItemScroller;
import fi.dy.masa.itemscroller.config.Configs;
Expand Down Expand Up @@ -187,21 +190,29 @@ public void onClientTick(MinecraftClient mc)
RecipePattern recipe = RecipeStorage.getInstance().getSelectedRecipe();

CraftingRecipe bookRecipe = InventoryUtils.getBookRecipeFromPattern(recipe);
if (bookRecipe != null && !bookRecipe.isIgnoredInRecipeBook()) { // Use recipe book if possible
int stonecuttingRecipeIndex = InventoryUtils.getStonecuttingRecipeFromPattern(recipe);
if (!(gui instanceof StonecutterScreen) && bookRecipe != null && !bookRecipe.isIgnoredInRecipeBook()) { // Use recipe book if possible
// System.out.println("recipe");
int option = InventoryUtils.checkRecipeEnough(recipe, gui);
if(option > 0) {
mc.interactionManager.clickRecipe(gui.getScreenHandler().syncId, bookRecipe, option > 1);
}
} else {
// System.out.println("move");
}
else {
InventoryUtils.tryMoveItemsToFirstCraftingGrid(recipe, gui, true);
if(stonecuttingRecipeIndex != -1 && gui instanceof StonecutterScreen) {
mc.interactionManager.clickButton((gui.getScreenHandler()).syncId, stonecuttingRecipeIndex);
}
}

// for (int i = 0; i < recipe.getMaxCraftAmount(); i++) {
// InventoryUtils.dropStack(gui, outputSlot.id);
// }
InventoryUtils.dropItem(gui, outputSlot.id);
if(recipe.getResult().isStackable()) {
for (int i = 0; i < recipe.getMaxCraftAmount(); i++) {
InventoryUtils.dropStack(gui, outputSlot.id);
}
}
else {
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 @@ -9,6 +9,7 @@
import net.minecraft.nbt.NbtCompound;
import net.minecraft.nbt.NbtList;
import net.minecraft.recipe.CraftingRecipe;
import net.minecraft.recipe.StonecuttingRecipe;
import net.minecraft.screen.ScreenHandler;
import net.minecraft.screen.slot.Slot;
import fi.dy.masa.itemscroller.recipes.CraftingHandler.SlotRange;
Expand All @@ -20,6 +21,8 @@ public class RecipePattern
private ItemStack result = InventoryUtils.EMPTY_STACK;
private ItemStack[] recipe = new ItemStack[9];
public CraftingRecipe cachedRecipeFromBook = null;

public int cachedRecipeFromStonecutting = -1;
private int maxCraftAmount = 64;
private HashSet<Item> recipeRemainders = new HashSet<Item>();

Expand Down
35 changes: 29 additions & 6 deletions src/main/java/fi/dy/masa/itemscroller/util/InventoryUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,29 +27,26 @@
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.inventory.SimpleInventory;
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.recipe.StonecuttingRecipe;
import net.minecraft.screen.Generic3x3ContainerScreenHandler;
import net.minecraft.screen.MerchantScreenHandler;
import net.minecraft.screen.ScreenHandler;
import net.minecraft.screen.slot.CraftingResultSlot;
import net.minecraft.screen.StonecutterScreenHandler;
import net.minecraft.screen.slot.Slot;
import net.minecraft.screen.slot.SlotActionType;
import net.minecraft.screen.slot.TradeOutputSlot;
import net.minecraft.util.Identifier;
import net.minecraft.util.registry.Registry;
import net.minecraft.village.TradeOffer;
import net.minecraft.village.TradeOfferList;
import net.minecraft.world.GameRules;
import net.minecraft.world.World;
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;
Expand Down Expand Up @@ -1342,6 +1339,32 @@ public static CraftingRecipe getBookRecipeFromPattern(RecipePattern recipe) {
return null;
}

public static int getStonecuttingRecipeFromPattern(RecipePattern recipe) {
if(recipe.cachedRecipeFromStonecutting != -1) {
return recipe.cachedRecipeFromStonecutting;
}
else {
MinecraftClient mc = MinecraftClient.getInstance();
RecipeManager recipeManager = mc.world.getRecipeManager();

ItemStack[] items = recipe.getRecipeItems();
SimpleInventory search = new SimpleInventory(items.length);
// Set dummy slots with recipe pattern
for (int i = 0; i < items.length; i++) {
search.setStack(i, items[i]);
}
List<StonecuttingRecipe> inputRecipes = recipeManager.getAllMatches(RecipeType.STONECUTTING, search, mc.world);
for(int i=0; i<inputRecipes.size(); ++i) {
StonecuttingRecipe inputRecipe = inputRecipes.get(i);
if(inputRecipe.getOutput().getItem() == recipe.getResult().getItem()) {
recipe.cachedRecipeFromStonecutting = i;
return recipe.cachedRecipeFromStonecutting;
}
}
}
return -1;
}

public static void craftEverythingPossibleWithCurrentRecipe(RecipePattern recipe,
HandledScreen<? extends ScreenHandler> gui) {
Slot slot = CraftingHandler.getFirstCraftingOutputSlotForGui(gui);
Expand Down

0 comments on commit e912543

Please sign in to comment.