Browse Source

Buggy Autocrafting

Unknown 6 năm trước cách đây
mục cha
commit
0ad3f7821f

+ 1 - 1
build.gradle

@@ -6,7 +6,7 @@ sourceCompatibility = 1.8
 targetCompatibility = 1.8
 
 archivesBaseName = "RoughlyEnoughItems"
-version = "1.5-11"
+version = "1.5-12"
 
 minecraft {
 }

+ 19 - 0
src/main/java/me/shedaniel/api/DisplayCategoryCraftable.java

@@ -0,0 +1,19 @@
+package me.shedaniel.api;
+
+import me.shedaniel.api.IDisplayCategory;
+import me.shedaniel.api.IRecipe;
+import me.shedaniel.gui.RecipeGui;
+import me.shedaniel.gui.widget.Control;
+import net.minecraft.client.gui.Gui;
+
+import java.util.List;
+
+public interface DisplayCategoryCraftable<T extends IRecipe> extends IDisplayCategory<T> {
+    
+    public boolean canAutoCraftHere(Class<? extends Gui> guiClass, T recipe);
+    
+    public boolean performAutoCraft(Gui gui, T recipe);
+    
+    public void registerAutoCraftButton(List<Control> control, RecipeGui recipeGui, Gui parentGui, T recipe, int number);
+    
+}

+ 1 - 0
src/main/java/me/shedaniel/api/IRecipe.java

@@ -12,4 +12,5 @@ public interface IRecipe<T> {
     public List<T> getOutput();
 
     public List<List<T>> getInput();
+    
 }

+ 14 - 6
src/main/java/me/shedaniel/gui/RecipeGui.java

@@ -1,6 +1,7 @@
 package me.shedaniel.gui;
 
 import com.mojang.blaze3d.platform.GlStateManager;
+import me.shedaniel.api.DisplayCategoryCraftable;
 import me.shedaniel.api.IDisplayCategory;
 import me.shedaniel.api.IRecipe;
 import me.shedaniel.gui.widget.Button;
@@ -53,6 +54,10 @@ public class RecipeGui extends ContainerGui {
         setupCategories();
     }
     
+    public Gui getPrevScreen() {
+        return prevScreen;
+    }
+    
     private void setupCategories() {
         for(IDisplayCategory adapter : REIRecipeManager.instance().getDisplayAdapters())
             if (recipes.containsKey(adapter))
@@ -101,9 +106,7 @@ public class RecipeGui extends ContainerGui {
     }
     
     private void updateRecipe() {
-        int categoryPointer = categories.indexOf(selectedCategory);
-        
-        IRecipe recipe = recipes.get(categories.get(categoryPointer)).get(recipePointer);
+        IRecipe recipe = recipes.get(selectedCategory).get(recipePointer);
         selectedCategory.resetRecipes();
         selectedCategory.addRecipe(recipe);
         slots = selectedCategory.setupDisplay(0);
@@ -161,9 +164,14 @@ public class RecipeGui extends ContainerGui {
         controls.add(btnRecipeRight);
         
         List<Control> newControls = new LinkedList<>();
-        categories.get(categoryPointer).addWidget(newControls, 0);
-        if (recipes.get(categories.get(categoryPointer)).size() >= recipePointer + 2)
-            categories.get(categoryPointer).addWidget(newControls, 1);
+        selectedCategory.addWidget(newControls, 0);
+        if (selectedCategory instanceof DisplayCategoryCraftable)
+            ((DisplayCategoryCraftable) selectedCategory).registerAutoCraftButton(newControls, this, getPrevScreen(), recipe, 0);
+        if (recipes.get(selectedCategory).size() >= recipePointer + 2) {
+            selectedCategory.addWidget(newControls, 1);
+            if (selectedCategory instanceof DisplayCategoryCraftable)
+                ((DisplayCategoryCraftable) selectedCategory).registerAutoCraftButton(newControls, this, getPrevScreen(), recipes.get(selectedCategory).get(recipePointer + 1), 1);
+        }
         newControls.forEach(f -> f.move(left, top));
         controls.addAll(newControls);
         

+ 1 - 1
src/main/java/me/shedaniel/gui/widget/Button.java

@@ -13,6 +13,7 @@ import java.awt.*;
  * Created by James on 7/29/2018.
  */
 public class Button extends Control {
+    
     private String buttonText;
     protected static final Identifier BUTTON_TEXTURES = new Identifier("textures/gui/widgets.png");
     
@@ -61,5 +62,4 @@ public class Button extends Control {
         GlStateManager.popMatrix();
     }
     
-    
 }

+ 61 - 0
src/main/java/me/shedaniel/gui/widget/SmallButton.java

@@ -0,0 +1,61 @@
+package me.shedaniel.gui.widget;
+
+import com.mojang.blaze3d.platform.GlStateManager;
+import me.shedaniel.gui.REIRenderHelper;
+import net.minecraft.client.MinecraftClient;
+import net.minecraft.client.font.FontRenderer;
+import net.minecraft.client.gui.ContainerGui;
+import net.minecraft.util.Identifier;
+
+import java.awt.*;
+
+public class SmallButton extends Control {
+    
+    private String buttonText;
+    protected static final Identifier BUTTON_TEXTURES = new Identifier("roughlyenoughitems", "textures/gui/recipecontainer.png");
+    
+    
+    public SmallButton(int x, int y, int width, int height, String buttonText) {
+        super(x, y, width, height);
+        this.buttonText = buttonText;
+    }
+    
+    public SmallButton(Rectangle rect, String buttonText) {
+        super(rect);
+        this.buttonText = buttonText;
+    }
+    
+    public void setString(String text) {
+        buttonText = text;
+    }
+    
+    
+    @Override
+    public void draw() {
+        GlStateManager.pushMatrix();
+        GlStateManager.disableLighting();
+        ContainerGui gui = REIRenderHelper.getOverlayedGui();
+        MinecraftClient lvt_4_1_ = MinecraftClient.getInstance();
+        FontRenderer lvt_5_1_ = lvt_4_1_.fontRenderer;
+        lvt_4_1_.getTextureManager().bindTexture(BUTTON_TEXTURES);
+        GlStateManager.color4f(1.0F, 1.0F, 1.0F, 1.0F);
+        int hoverState = (byte) 0;
+        if (this.isEnabled()) {
+            if (!this.isHighlighted())
+                hoverState = (byte) 1;
+            else
+                hoverState = (byte) 2;
+        }
+        
+        GlStateManager.enableBlend();
+        GlStateManager.blendFuncSeparate(GlStateManager.SrcBlendFactor.SRC_ALPHA, GlStateManager.DstBlendFactor.ONE_MINUS_SRC_ALPHA, GlStateManager.SrcBlendFactor.ONE, GlStateManager.DstBlendFactor.ZERO);
+        GlStateManager.blendFunc(GlStateManager.SrcBlendFactor.SRC_ALPHA, GlStateManager.DstBlendFactor.ONE_MINUS_SRC_ALPHA);
+        gui.drawTexturedRect(rect.x, rect.y, 18 + 44, 222 + hoverState * 10, rect.width, rect.height);
+        int lvt_7_1_ = 14737632;
+        
+        gui.drawStringCentered(lvt_5_1_, this.buttonText, rect.x + rect.width / 2, rect.y + (rect.height - 8) / 2, lvt_7_1_);
+        GlStateManager.enableLighting();
+        GlStateManager.popMatrix();
+    }
+    
+}

+ 10 - 4
src/main/java/me/shedaniel/impl/REIRecipeManager.java

@@ -11,12 +11,10 @@ import net.minecraft.client.gui.Gui;
 import net.minecraft.item.Item;
 import net.minecraft.item.ItemStack;
 import net.minecraft.recipe.RecipeManager;
-import net.minecraft.util.BooleanBiFunction;
 
 import java.awt.*;
 import java.util.*;
 import java.util.List;
-import java.util.function.BooleanSupplier;
 import java.util.function.Function;
 
 /**
@@ -165,7 +163,11 @@ public class REIRecipeManager implements IRecipeManager {
         Map<IDisplayCategory, List<IRecipe>> recipes = REIRecipeManager.instance().getRecipesFor(stack);
         if (recipes.isEmpty())
             return;
-        RecipeGui gui = new RecipeGui(null, MinecraftClient.getInstance().currentGui, recipes);
+        RecipeGui gui;
+        if (MinecraftClient.getInstance().currentGui instanceof RecipeGui)
+            gui = new RecipeGui(null, ((RecipeGui) MinecraftClient.getInstance().currentGui).getPrevScreen(), recipes);
+        else
+            gui = new RecipeGui(null, MinecraftClient.getInstance().currentGui, recipes);
         MinecraftClient.getInstance().openGui(gui);
     }
     
@@ -173,7 +175,11 @@ public class REIRecipeManager implements IRecipeManager {
         Map<IDisplayCategory, List<IRecipe>> recipes = REIRecipeManager.instance().getUsesFor(stack);
         if (recipes.isEmpty())
             return;
-        RecipeGui gui = new RecipeGui(null, MinecraftClient.getInstance().currentGui, recipes);
+        RecipeGui gui;
+        if (MinecraftClient.getInstance().currentGui instanceof RecipeGui)
+            gui = new RecipeGui(null, ((RecipeGui) MinecraftClient.getInstance().currentGui).getPrevScreen(), recipes);
+        else
+            gui = new RecipeGui(null, MinecraftClient.getInstance().currentGui, recipes);
         MinecraftClient.getInstance().openGui(gui);
     }
 }

+ 9 - 0
src/main/java/me/shedaniel/listenerdefinitions/IMixinRecipeBookGui.java

@@ -0,0 +1,9 @@
+package me.shedaniel.listenerdefinitions;
+
+import net.minecraft.client.gui.widget.RecipeBookGhostSlots;
+
+public interface IMixinRecipeBookGui {
+    
+    public RecipeBookGhostSlots getGhostSlots();
+    
+}

+ 1 - 1
src/main/java/me/shedaniel/mixins/MixinBrewingRecipeRegistry.java

@@ -13,7 +13,7 @@ import org.spongepowered.asm.mixin.injection.callback.CallbackInfo;
 @Mixin(BrewingRecipeRegistry.class)
 public class MixinBrewingRecipeRegistry {
     
-    @Inject(method = "registerPotionRecipe", at = @At("RETURN"))
+    @Inject(method = "registerPotionRecipe", at = @At("HEAD"))
     private static void registerPotionRecipe(Potion potion_1, Item item_1, Potion potion_2, CallbackInfo info) {
         Core.getListeners(PotionCraftingAdder.class).forEach(potionCraftingAdder -> potionCraftingAdder.addPotionRecipe(potion_1, item_1, potion_2));
     }

+ 20 - 0
src/main/java/me/shedaniel/mixins/MixinRecipeBookGui.java

@@ -0,0 +1,20 @@
+package me.shedaniel.mixins;
+
+import me.shedaniel.listenerdefinitions.IMixinRecipeBookGui;
+import net.minecraft.client.gui.recipebook.RecipeBookGui;
+import net.minecraft.client.gui.widget.RecipeBookGhostSlots;
+import org.spongepowered.asm.mixin.Final;
+import org.spongepowered.asm.mixin.Mixin;
+import org.spongepowered.asm.mixin.Shadow;
+
+@Mixin(RecipeBookGui.class)
+public class MixinRecipeBookGui implements IMixinRecipeBookGui {
+    
+    @Shadow @Final protected RecipeBookGhostSlots ghostSlots;
+    
+    @Override
+    public RecipeBookGhostSlots getGhostSlots() {
+        return ghostSlots;
+    }
+    
+}

+ 19 - 21
src/main/java/me/shedaniel/plugin/VanillaPlugin.java

@@ -30,8 +30,6 @@ import net.minecraft.recipe.smelting.SmeltingRecipe;
 import net.minecraft.recipe.smelting.SmokingRecipe;
 import net.minecraft.util.registry.Registry;
 
-import java.lang.reflect.Array;
-import java.util.Arrays;
 import java.util.LinkedList;
 import java.util.List;
 import java.util.stream.Collectors;
@@ -39,13 +37,13 @@ import java.util.stream.Collectors;
 public class VanillaPlugin implements IREIPlugin, PotionCraftingAdder {
     
     private List<VanillaPotionRecipe> potionRecipes = new LinkedList<>();
+    private List<VanillaCraftingRecipe> recipes = new LinkedList<>();
+    private List<VanillaFurnaceRecipe> furnaceRecipes = new LinkedList<>();
+    private List<VanillaSmokerRecipe> smokerRecipes = new LinkedList<>();
+    private List<VanillaBlastFurnaceRecipe> blastFurnaceRecipes = new LinkedList<>();
     
     @Override
-    public void register() {
-        List<VanillaCraftingRecipe> recipes = new LinkedList<>();
-        List<VanillaFurnaceRecipe> furnaceRecipes = new LinkedList<>();
-        List<VanillaSmokerRecipe> smokerRecipes = new LinkedList<>();
-        List<VanillaBlastFurnaceRecipe> blastFurnaceRecipes = new LinkedList<>();
+    public void registerCategories() {
         REIRecipeManager.instance().addDisplayAdapter(new VanillaCraftingCategory());
         REIRecipeManager.instance().addDisplayAdapter(new VanillaFurnaceCategory());
         REIRecipeManager.instance().addDisplayAdapter(new VanillaSmokerCategory());
@@ -56,25 +54,21 @@ public class VanillaPlugin implements IREIPlugin, PotionCraftingAdder {
 //        REIRecipeManager.instance().addDisplayAdapter(new TestRandomCategory("c", new ItemStack(Items.ITEM_FRAME)));
 //        REIRecipeManager.instance().addDisplayAdapter(new TestRandomCategory("d", new ItemStack(Items.ITEM_FRAME)));
 //        REIRecipeManager.instance().addDisplayAdapter(new TestRandomCategory("e", new ItemStack(Items.ITEM_FRAME)));
-        
-        
-        for(Recipe recipe : REIRecipeManager.instance().recipeManager.values()) {
-            if (recipe instanceof ShapelessRecipe) {
+    }
+    
+    @Override
+    public void registerRecipes() {
+        for(Recipe recipe : REIRecipeManager.instance().recipeManager.values())
+            if (recipe instanceof ShapelessRecipe)
                 recipes.add(new VanillaShapelessCraftingRecipe((ShapelessRecipe) recipe));
-            }
-            if (recipe instanceof ShapedRecipe) {
+            else if (recipe instanceof ShapedRecipe)
                 recipes.add(new VanillaShapedCraftingRecipe((ShapedRecipe) recipe));
-            }
-            if (recipe instanceof SmeltingRecipe) {
+            else if (recipe instanceof SmeltingRecipe)
                 furnaceRecipes.add(new VanillaFurnaceRecipe((SmeltingRecipe) recipe));
-            }
-            if (recipe instanceof SmokingRecipe) {
+            else if (recipe instanceof SmokingRecipe)
                 smokerRecipes.add(new VanillaSmokerRecipe((SmokingRecipe) recipe));
-            }
-            if (recipe instanceof BlastingRecipe) {
+            else if (recipe instanceof BlastingRecipe)
                 blastFurnaceRecipes.add(new VanillaBlastFurnaceRecipe((BlastingRecipe) recipe));
-            }
-        }
         Registry.POTION.stream().filter(potion -> !potion.equals(Potions.EMPTY)).forEach(potion -> {
             ItemStack basePotion = PotionUtil.setPotion(new ItemStack(Items.POTION), potion),
                     splashPotion = PotionUtil.setPotion(new ItemStack(Items.SPLASH_POTION), potion),
@@ -97,6 +91,10 @@ public class VanillaPlugin implements IREIPlugin, PotionCraftingAdder {
 //        REIRecipeManager.instance().addRecipe("e", Arrays.asList(new RandomRecipe("e")));
     }
     
+    @Override
+    public void registerSpecialGuiExclusion() {
+    
+    }
     
     @Override
     public void addPotionRecipe(Potion inputType, Item reagent, Potion outputType) {

+ 37 - 5
src/main/java/me/shedaniel/plugin/crafting/VanillaCraftingCategory.java

@@ -1,11 +1,15 @@
 package me.shedaniel.plugin.crafting;
 
-import me.shedaniel.api.IDisplayCategory;
-import me.shedaniel.gui.widget.Control;
-import me.shedaniel.gui.widget.REISlot;
-import me.shedaniel.gui.widget.WidgetArrow;
+import me.shedaniel.api.DisplayCategoryCraftable;
+import me.shedaniel.gui.RecipeGui;
+import me.shedaniel.gui.widget.*;
+import me.shedaniel.listenerdefinitions.IMixinRecipeBookGui;
+import me.shedaniel.mixins.MixinRecipeBookGui;
 import net.minecraft.block.Blocks;
 import net.minecraft.client.MinecraftClient;
+import net.minecraft.client.gui.Gui;
+import net.minecraft.client.gui.container.CraftingTableGui;
+import net.minecraft.client.gui.ingame.PlayerInventoryGui;
 import net.minecraft.client.resource.language.I18n;
 import net.minecraft.client.util.Window;
 import net.minecraft.item.ItemStack;
@@ -14,7 +18,7 @@ import java.util.ArrayList;
 import java.util.LinkedList;
 import java.util.List;
 
-public class VanillaCraftingCategory implements IDisplayCategory<VanillaCraftingRecipe> {
+public class VanillaCraftingCategory implements DisplayCategoryCraftable<VanillaCraftingRecipe> {
     Window mainWindow = MinecraftClient.getInstance().window;
     private List<VanillaCraftingRecipe> recipes;
     
@@ -117,4 +121,32 @@ public class VanillaCraftingCategory implements IDisplayCategory<VanillaCrafting
         return new ItemStack(Blocks.CRAFTING_TABLE.getItem());
     }
     
+    @Override
+    public boolean canAutoCraftHere(Class<? extends Gui> guiClass, VanillaCraftingRecipe recipe) {
+        return guiClass.isAssignableFrom(CraftingTableGui.class) || (guiClass.isAssignableFrom(PlayerInventoryGui.class) && recipe.getHeight() < 3 && recipe.getWidth() < 3);
+    }
+    
+    @Override
+    public boolean performAutoCraft(Gui gui, VanillaCraftingRecipe recipe) {
+        if (gui.getClass().isAssignableFrom(CraftingTableGui.class))
+            ((IMixinRecipeBookGui) (((CraftingTableGui) gui).getRecipeBookGui())).getGhostSlots().reset();
+        else if (gui.getClass().isAssignableFrom(PlayerInventoryGui.class))
+            ((IMixinRecipeBookGui) (((PlayerInventoryGui) gui).getRecipeBookGui())).getGhostSlots().reset();
+        MinecraftClient.getInstance().interactionManager.clickRecipe(MinecraftClient.getInstance().player.container.syncId, recipe.getRecipe(), Gui.isShiftPressed());
+        return true;
+    }
+    
+    @Override
+    public void registerAutoCraftButton(List<Control> control, RecipeGui recipeGui, Gui parentGui, VanillaCraftingRecipe recipe, int number) {
+        SmallButton button = new SmallButton(78, 75 + 6 + 36 + number * 75, 10, 10, "+");
+        button.setOnClick(mouse -> {
+            System.out.println(parentGui.getClass().getName());
+            recipeGui.close();
+            MinecraftClient.getInstance().openGui(parentGui);
+            return canAutoCraftHere(parentGui.getClass(), recipe) && performAutoCraft(parentGui, recipe);
+        });
+        button.setEnabled(canAutoCraftHere(parentGui.getClass(), recipe));
+        control.add(button);
+    }
+    
 }

+ 3 - 0
src/main/java/me/shedaniel/plugin/crafting/VanillaCraftingRecipe.java

@@ -2,6 +2,7 @@ package me.shedaniel.plugin.crafting;
 
 import me.shedaniel.api.IRecipe;
 import net.minecraft.item.ItemStack;
+import net.minecraft.recipe.Recipe;
 
 public abstract class VanillaCraftingRecipe implements IRecipe<ItemStack> {
     
@@ -13,4 +14,6 @@ public abstract class VanillaCraftingRecipe implements IRecipe<ItemStack> {
         return 2;
     }
     
+    public abstract Recipe getRecipe();
+    
 }

+ 5 - 1
src/main/java/me/shedaniel/plugin/crafting/VanillaShapedCraftingRecipe.java

@@ -12,10 +12,14 @@ public class VanillaShapedCraftingRecipe extends VanillaCraftingRecipe {
     private final ShapedRecipe recipe;
     
     public VanillaShapedCraftingRecipe(ShapedRecipe recipe) {
-        
         this.recipe = recipe;
     }
     
+    @Override
+    public ShapedRecipe getRecipe() {
+        return recipe;
+    }
+    
     @Override
     public int getWidth() {
         return recipe.getWidth();

+ 5 - 1
src/main/java/me/shedaniel/plugin/crafting/VanillaShapelessCraftingRecipe.java

@@ -12,10 +12,14 @@ public class VanillaShapelessCraftingRecipe extends VanillaCraftingRecipe {
     private final ShapelessRecipe recipe;
     
     public VanillaShapelessCraftingRecipe(ShapelessRecipe recipe) {
-        
         this.recipe = recipe;
     }
     
+    @Override
+    public ShapelessRecipe getRecipe() {
+        return recipe;
+    }
+    
     @Override
     public String getId() {
         return "vanilla";

BIN
src/main/resources/assets/roughlyenoughitems/textures/gui/recipecontainer.png


+ 2 - 1
src/main/resources/roughlyenoughitems.client.json

@@ -9,7 +9,8 @@
     "MixinMinecraftResize",
     "MixinKeyboardListener",
     "MixinRecipeManager",
-    "MixinCreativePlayerInventoryGui"
+    "MixinCreativePlayerInventoryGui",
+    "MixinRecipeBookGui"
   ],
   "injectors": {
     "defaultRequire": 1