mirror of
https://github.com/GTNewHorizons/NotEnoughEnergistics.git
synced 2026-11-18 11:19:11 +00:00
fix bug : crafting helper doesn't check player's inventory
This commit is contained in:
1 parent
34d9d796aa
commit
1ef2b0f60e
7 files changed
+141
-94
No files matched your search
@@ -20,6 +20,7 @@ public class NEEConfig {
|
||||
public static String[] transformPriorityModList = new String[0];
|
||||
|
||||
public static boolean noShift = true;
|
||||
public static boolean matchOtherItems = false;
|
||||
|
||||
public static void loadConfig(File configFile) {
|
||||
config = new Configuration(configFile);
|
||||
@@ -35,6 +36,12 @@ public class NEEConfig {
|
||||
transformPriorityModList = config.get("client", "transformPriorityModList", new String[0],
|
||||
"if oredict has this mod's item, use it first").getStringList();
|
||||
|
||||
noShift = config.get("client", "noShift", true,
|
||||
"if true, you don't need to press shift to use NEI's transfer system in CratingTerminal and PatternTerminal").getBoolean();
|
||||
|
||||
matchOtherItems = config.get("client", "matchOnCraftableItems", false,
|
||||
"If false, Crafting Helper will not match other items").getBoolean();
|
||||
|
||||
if (config.hasChanged()) config.save();
|
||||
}
|
||||
|
||||
@@ -49,6 +56,9 @@ public class NEEConfig {
|
||||
|
||||
transformPriorityModList = config.get("client", "transformPriorityModList", new String[0],
|
||||
"if oredict has this mod's item, use it first").getStringList();
|
||||
|
||||
matchOtherItems = config.get("client", "matchOnCraftableItems", false,
|
||||
"If false, Crafting Helper will not match other items").getBoolean();
|
||||
ItemUtils.reloadConfig();
|
||||
}
|
||||
|
||||
|
||||
@@ -37,7 +37,6 @@ import java.io.ByteArrayOutputStream;
|
||||
import java.io.DataOutputStream;
|
||||
import java.io.IOException;
|
||||
import java.lang.reflect.InvocationTargetException;
|
||||
import java.util.ArrayList;
|
||||
import java.util.LinkedList;
|
||||
import java.util.List;
|
||||
|
||||
@@ -51,80 +50,45 @@ public class NEECraftingHelper implements IOverlayHandler {
|
||||
public void overlayRecipe(GuiContainer firstGui, IRecipeHandler recipe, int recipeIndex, boolean shift) {
|
||||
Container container = Minecraft.getMinecraft().thePlayer.openContainer;
|
||||
if (firstGui instanceof GuiCraftingTerm && NEECraftingHandler.isCraftingTableRecipe(recipe) && container instanceof ContainerCraftingTerm) {
|
||||
tracker = createTracker((GuiCraftingTerm) firstGui, (ContainerCraftingTerm) container, recipe, recipeIndex);
|
||||
if (Keyboard.isKeyDown(NEIClientConfig.getKeyBinding("nee.preview"))) {
|
||||
if (!tracker.getRequireToCraftStacks().isEmpty()) {
|
||||
NEENetworkHandler.getInstance().sendToServer(new PacketCraftingHelper(tracker.getRequireToCraftStacks().get(0), false));
|
||||
noPreview = false;
|
||||
stackIndex = 1;
|
||||
}
|
||||
} else if (Keyboard.isKeyDown(NEIClientConfig.getKeyBinding("nee.nopreview"))) {
|
||||
//no preview
|
||||
if (!tracker.getRequireToCraftStacks().isEmpty()) {
|
||||
NEENetworkHandler.getInstance().sendToServer(new PacketCraftingHelper(tracker.getRequireToCraftStacks().get(0), true));
|
||||
noPreview = true;
|
||||
stackIndex = 1;
|
||||
}
|
||||
tracker = createTracker(firstGui, recipe, recipeIndex);
|
||||
boolean doCraftingHelp = Keyboard.isKeyDown(NEIClientConfig.getKeyBinding("nee.preview")) || Keyboard.isKeyDown(NEIClientConfig.getKeyBinding("nee.nopreview"));
|
||||
noPreview = Keyboard.isKeyDown(NEIClientConfig.getKeyBinding("nee.nopreview"));
|
||||
if (doCraftingHelp && !tracker.getRequireToCraftStacks().isEmpty()) {
|
||||
NEENetworkHandler.getInstance().sendToServer(new PacketCraftingHelper(tracker.getRequireToCraftStacks().get(0), noPreview));
|
||||
stackIndex = 1;
|
||||
} else {
|
||||
/*
|
||||
Copied from GTNewHorizons/Applied-Energistics-2-Unofficial
|
||||
*/
|
||||
try {
|
||||
final List<PositionedStack> ingredients = recipe.getIngredientStacks(recipeIndex);
|
||||
PacketNEIRecipe packet = new PacketNEIRecipe(packIngredients(firstGui, ingredients, false));
|
||||
if (packet.size() >= 32 * 1024) {
|
||||
AELog.warn("Recipe for " + recipe.getRecipeName() + " has too many variants, reduced version will be used");
|
||||
packet = new PacketNEIRecipe(packIngredients(firstGui, ingredients, true));
|
||||
}
|
||||
NetworkHandler.instance.sendToServer(packet);
|
||||
} catch (final Exception | Error ignored) {
|
||||
}
|
||||
moveItem(firstGui, recipe, recipeIndex);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private IngredientTracker createTracker(GuiCraftingTerm firstGui, ContainerCraftingTerm container, IRecipeHandler recipe, int recipeIndex) {
|
||||
IngredientTracker tracker = new IngredientTracker(firstGui);
|
||||
List<PositionedStack> requiredIngredient = new ArrayList<>();
|
||||
for (PositionedStack positionedStack : recipe.getIngredientStacks(recipeIndex)) {
|
||||
ItemStack firstStack = positionedStack.items[0];
|
||||
boolean find = false;
|
||||
for (PositionedStack currentIngredient : requiredIngredient) {
|
||||
ItemStack currentStack = currentIngredient.items[0];
|
||||
if (currentStack.isItemEqual(firstStack) && ItemStack.areItemStackTagsEqual(currentStack, firstStack)) {
|
||||
find = true;
|
||||
currentStack.stackSize = currentStack.stackSize + firstStack.stackSize;
|
||||
}
|
||||
}
|
||||
if (!find) {
|
||||
requiredIngredient.add(positionedStack);
|
||||
}
|
||||
}
|
||||
private IngredientTracker createTracker(GuiContainer firstGui, IRecipeHandler recipe, int recipeIndex) {
|
||||
IngredientTracker tracker = new IngredientTracker(firstGui, recipe, recipeIndex);
|
||||
|
||||
for (PositionedStack currentIngredient : requiredIngredient) {
|
||||
for (ItemStack currentStack : currentIngredient.items) {
|
||||
if (tracker.hasCraftableStack(currentStack)) {
|
||||
ItemStack requiredStack = currentStack.copy();
|
||||
for (ItemStack stack : GuiUtils.getStacksFromCraftingTerminal(container)) {
|
||||
if (stack.isItemEqual(requiredStack) && ItemStack.areItemStackTagsEqual(stack, requiredStack)) {
|
||||
currentIngredient.items[0].stackSize -= stack.stackSize;
|
||||
}
|
||||
}
|
||||
requiredStack.stackSize = currentIngredient.items[0].stackSize;
|
||||
if (requiredStack.stackSize > 0) {
|
||||
tracker.addRequireToCraftStack(requiredStack);
|
||||
}
|
||||
break;
|
||||
}
|
||||
//check stacks in player's inventory and crafting grid
|
||||
for (Slot slot : (List<Slot>) firstGui.inventorySlots.inventorySlots) {
|
||||
boolean canGetStack = slot != null && slot.getHasStack() && slot.getStack().stackSize > 0 && slot.isItemValid(slot.getStack()) && slot.canTakeStack(Minecraft.getMinecraft().thePlayer);
|
||||
if (canGetStack) {
|
||||
tracker.addIngredientStack(slot.getStack());
|
||||
}
|
||||
}
|
||||
|
||||
return tracker;
|
||||
}
|
||||
|
||||
/**
|
||||
* Copied from GTNewHorizons/Applied-Energistics-2-Unofficial
|
||||
*/
|
||||
private void moveItem(GuiContainer firstGui, IRecipeHandler recipe, int recipeIndex) {
|
||||
try {
|
||||
final List<PositionedStack> ingredients = recipe.getIngredientStacks(recipeIndex);
|
||||
PacketNEIRecipe packet = new PacketNEIRecipe(packIngredients(firstGui, ingredients, false));
|
||||
if (packet.size() >= 32 * 1024) {
|
||||
AELog.warn("Recipe for " + recipe.getRecipeName() + " has too many variants, reduced version will be used");
|
||||
packet = new PacketNEIRecipe(packIngredients(firstGui, ingredients, true));
|
||||
}
|
||||
NetworkHandler.instance.sendToServer(packet);
|
||||
} catch (final Exception | Error ignored) {
|
||||
}
|
||||
}
|
||||
|
||||
private boolean testSize(final NBTTagCompound recipe) throws IOException {
|
||||
final ByteArrayOutputStream bytes = new ByteArrayOutputStream();
|
||||
final DataOutputStream outputStream = new DataOutputStream(bytes);
|
||||
@@ -132,9 +96,6 @@ public class NEECraftingHelper implements IOverlayHandler {
|
||||
return bytes.size() > 3 * 1024;
|
||||
}
|
||||
|
||||
/**
|
||||
* Copied from GTNewHorizons/Applied-Energistics-2-Unofficial
|
||||
*/
|
||||
private NBTTagCompound packIngredients(GuiContainer gui, List<PositionedStack> ingredients, boolean limited) throws IOException {
|
||||
final NBTTagCompound recipe = new NBTTagCompound();
|
||||
for (final PositionedStack positionedStack : ingredients) {
|
||||
|
||||
@@ -0,0 +1,36 @@
|
||||
package com.github.vfyjxf.nee.utils;
|
||||
|
||||
import codechicken.nei.PositionedStack;
|
||||
import net.minecraft.item.ItemStack;
|
||||
|
||||
public class Ingredient {
|
||||
|
||||
private ItemStack craftableIngredient;
|
||||
private final int requireCount;
|
||||
private int currentCount = 0;
|
||||
|
||||
public Ingredient(PositionedStack ingredients) {
|
||||
this.requireCount = ingredients.items[0].stackSize;
|
||||
}
|
||||
|
||||
public void setCraftableIngredient(ItemStack craftableIngredient) {
|
||||
this.craftableIngredient = craftableIngredient;
|
||||
}
|
||||
|
||||
public ItemStack getCraftableIngredient() {
|
||||
return craftableIngredient;
|
||||
}
|
||||
|
||||
public int getMissingCount(){
|
||||
return requireCount - currentCount;
|
||||
}
|
||||
|
||||
public boolean requiresToCraft(){
|
||||
return this.getMissingCount() > 0;
|
||||
}
|
||||
|
||||
public void addCurrentCount(int count){
|
||||
currentCount += count;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,10 +1,15 @@
|
||||
package com.github.vfyjxf.nee.utils;
|
||||
|
||||
import appeng.api.AEApi;
|
||||
import appeng.api.storage.data.IAEItemStack;
|
||||
import appeng.client.gui.implementations.GuiCraftingTerm;
|
||||
import appeng.api.storage.data.IItemList;
|
||||
import appeng.client.gui.implementations.GuiMEMonitorable;
|
||||
import appeng.client.me.ItemRepo;
|
||||
import codechicken.nei.PositionedStack;
|
||||
import codechicken.nei.recipe.IRecipeHandler;
|
||||
import com.github.vfyjxf.nee.config.NEEConfig;
|
||||
import cpw.mods.fml.relauncher.ReflectionHelper;
|
||||
import net.minecraft.client.gui.inventory.GuiContainer;
|
||||
import net.minecraft.item.ItemStack;
|
||||
|
||||
import java.util.ArrayList;
|
||||
@@ -13,24 +18,51 @@ import java.util.List;
|
||||
public class IngredientTracker {
|
||||
|
||||
private List<IAEItemStack> craftbleAEItemStacks;
|
||||
private List<ItemStack> requireToCraftStacks = new ArrayList<>();
|
||||
private List<Ingredient> ingredients = new ArrayList<>();
|
||||
|
||||
public IngredientTracker(GuiCraftingTerm gui) {
|
||||
public IngredientTracker(GuiContainer gui, IRecipeHandler recipe, int recipeIndex) {
|
||||
this.craftbleAEItemStacks = getCraftableStacks(gui);
|
||||
List<PositionedStack> requiredIngredients = new ArrayList<>();
|
||||
for (PositionedStack positionedStack : recipe.getIngredientStacks(recipeIndex)) {
|
||||
boolean find = false;
|
||||
for (PositionedStack currentIngredient : requiredIngredients) {
|
||||
boolean areItemStackEquals = currentIngredient.items[0].isItemEqual(positionedStack.items[0]) && ItemStack.areItemStackTagsEqual(currentIngredient.items[0], positionedStack.items[0]);
|
||||
if (areItemStackEquals) {
|
||||
currentIngredient.items[0].stackSize += positionedStack.items[0].stackSize;
|
||||
find = true;
|
||||
}
|
||||
}
|
||||
|
||||
if (!find) {
|
||||
requiredIngredients.add(positionedStack);
|
||||
}
|
||||
|
||||
}
|
||||
for (PositionedStack requiredIngredient : requiredIngredients) {
|
||||
Ingredient ingredient = new Ingredient(requiredIngredient);
|
||||
ingredients.add(ingredient);
|
||||
|
||||
for (IAEItemStack craftableStack : NEEConfig.matchOtherItems ? this.getAEStacks(gui) : craftbleAEItemStacks) {
|
||||
if (requiredIngredient.contains(craftableStack.getItemStack())) {
|
||||
ingredient.setCraftableIngredient(craftableStack.getItemStack());
|
||||
ingredient.addCurrentCount(craftableStack.getItemStack().stackSize);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
private List<IAEItemStack> getCraftableStacks(GuiCraftingTerm gui) {
|
||||
private List<IAEItemStack> getCraftableStacks(GuiContainer gui) {
|
||||
List<IAEItemStack> craftableAEItemSafcks = new ArrayList<>();
|
||||
ArrayList<IAEItemStack> view = null;
|
||||
IItemList<IAEItemStack> list = null;
|
||||
try {
|
||||
ItemRepo repo = (ItemRepo) ReflectionHelper.findField(GuiMEMonitorable.class, "repo").get(gui);
|
||||
view = (ArrayList<IAEItemStack>) ReflectionHelper.findField(ItemRepo.class, "view").get(repo);
|
||||
list = (IItemList<IAEItemStack>) ReflectionHelper.findField(ItemRepo.class, "list").get(repo);
|
||||
} catch (IllegalAccessException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
if (view != null) {
|
||||
for (IAEItemStack stack : view) {
|
||||
if (list != null) {
|
||||
for (IAEItemStack stack : list) {
|
||||
if (stack.isCraftable()) {
|
||||
craftableAEItemSafcks.add(stack);
|
||||
}
|
||||
@@ -39,31 +71,37 @@ public class IngredientTracker {
|
||||
return craftableAEItemSafcks;
|
||||
}
|
||||
|
||||
public boolean hasCraftableStack(ItemStack stack) {
|
||||
for (IAEItemStack aeItemStack : this.craftbleAEItemStacks) {
|
||||
if (aeItemStack.isSameType(stack)) {
|
||||
return true;
|
||||
}
|
||||
private IItemList<IAEItemStack> getAEStacks(GuiContainer gui) {
|
||||
IItemList<IAEItemStack> list = AEApi.instance().storage().createItemList();
|
||||
try {
|
||||
ItemRepo repo = (ItemRepo) ReflectionHelper.findField(GuiMEMonitorable.class, "repo").get(gui);
|
||||
list = (IItemList<IAEItemStack>) ReflectionHelper.findField(ItemRepo.class, "list").get(repo);
|
||||
} catch (IllegalAccessException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
public List<IAEItemStack> getCraftbleAEItemStacks() {
|
||||
return this.craftbleAEItemStacks;
|
||||
return list;
|
||||
}
|
||||
|
||||
public List<ItemStack> getRequireToCraftStacks() {
|
||||
return this.requireToCraftStacks;
|
||||
List<ItemStack> requireToCraftStacks = new ArrayList<>();
|
||||
for (Ingredient ingredient : this.ingredients) {
|
||||
ItemStack craftableStack = ingredient.getCraftableIngredient();
|
||||
if (craftableStack != null && ingredient.requiresToCraft()) {
|
||||
ItemStack requireStack = craftableStack.copy();
|
||||
requireStack.stackSize = ingredient.getMissingCount();
|
||||
requireToCraftStacks.add(requireStack);
|
||||
}
|
||||
}
|
||||
return requireToCraftStacks;
|
||||
}
|
||||
|
||||
|
||||
public void addRequireToCraftStack(ItemStack requiredStack) {
|
||||
for (IAEItemStack craftableStack : this.getCraftbleAEItemStacks()) {
|
||||
if (craftableStack.isSameType(requiredStack)) {
|
||||
int requiredCount = (int) (requiredStack.stackSize - craftableStack.getStackSize());
|
||||
if (requiredCount > 0) {
|
||||
requiredStack.stackSize = requiredCount;
|
||||
this.requireToCraftStacks.add(requiredStack);
|
||||
public void addIngredientStack(ItemStack stack) {
|
||||
for (Ingredient ingredient : this.ingredients) {
|
||||
if (ingredient.requiresToCraft()) {
|
||||
ItemStack craftableStack = ingredient.getCraftableIngredient();
|
||||
if (craftableStack != null && craftableStack.isItemEqual(stack) && ItemStack.areItemStackTagsEqual(craftableStack, stack)) {
|
||||
ingredient.addCurrentCount(stack.stackSize);
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user