diff --git a/gradle.properties b/gradle.properties index cc80dd6..10007c5 100644 --- a/gradle.properties +++ b/gradle.properties @@ -6,7 +6,7 @@ org.gradle.daemon=false mc_version=1.7.10 nei_version=2.1.6-GTNH ae2_version=rv3-beta-52-GTNH -mod_version=1.2.22 +mod_version=1.3.0 forge_version=10.13.4.1614 mod_group=com.github.vfyjxf.neenergistics mod_id=neenergistics diff --git a/src/main/java/com/github/vfyjxf/nee/client/GuiHandler.java b/src/main/java/com/github/vfyjxf/nee/client/GuiHandler.java index b065178..3eceb43 100644 --- a/src/main/java/com/github/vfyjxf/nee/client/GuiHandler.java +++ b/src/main/java/com/github/vfyjxf/nee/client/GuiHandler.java @@ -5,20 +5,29 @@ import appeng.client.gui.implementations.GuiCraftingTerm; import appeng.client.gui.implementations.GuiPatternTerm; import appeng.container.slot.SlotFake; import codechicken.nei.NEIClientConfig; +import codechicken.nei.PositionedStack; +import com.github.vfyjxf.nee.config.NEEConfig; +import com.github.vfyjxf.nee.nei.NEECraftingHandler; import com.github.vfyjxf.nee.network.NEENetworkHandler; import com.github.vfyjxf.nee.network.packet.PacketCraftingHelper; +import com.github.vfyjxf.nee.network.packet.PacketRecipeItemChange; import com.github.vfyjxf.nee.network.packet.PacketStackCountChange; import com.github.vfyjxf.nee.utils.GuiUtils; +import com.github.vfyjxf.nee.utils.ItemUtils; import cpw.mods.fml.common.eventhandler.SubscribeEvent; import cpw.mods.fml.common.gameevent.TickEvent; import net.minecraft.client.Minecraft; import net.minecraft.client.gui.GuiScreen; import net.minecraft.client.gui.inventory.GuiContainer; import net.minecraft.inventory.Slot; +import net.minecraft.item.ItemStack; import net.minecraftforge.client.event.GuiOpenEvent; import org.lwjgl.input.Keyboard; import org.lwjgl.input.Mouse; +import java.util.ArrayList; +import java.util.List; + import static com.github.vfyjxf.nee.nei.NEECraftingHelper.*; public class GuiHandler { @@ -36,7 +45,7 @@ public class GuiHandler { if (currentSlot instanceof SlotFake && currentSlot.getHasStack()) { //try to change current itemstack to next ingredient; if (Keyboard.isKeyDown(NEIClientConfig.getKeyBinding("nee.ingredient")) && GuiUtils.isCraftingSlot(currentSlot)) { - GuiUtils.handleRecipeIngredientChange(currentSlot, dWheel); + handleRecipeIngredientChange((GuiContainer) mc.currentScreen, currentSlot, dWheel); } else if (Keyboard.isKeyDown(NEIClientConfig.getKeyBinding("nee.count"))) { int changeCount = dWheel / 120; NEENetworkHandler.getInstance().sendToServer(new PacketStackCountChange(currentSlot.slotNumber, changeCount)); @@ -59,4 +68,61 @@ public class GuiHandler { } } + private void handleRecipeIngredientChange(GuiContainer gui, Slot currentSlot, int dWheel) { + List craftingSlots = new ArrayList<>(); + int currentSlotIndex = currentSlot.getSlotIndex(); + PositionedStack currentIngredients = NEECraftingHandler.ingredients.get("input" + currentSlotIndex); + if (currentIngredients != null && currentIngredients.items.length > 1) { + int currentStackIndex = ItemUtils.getIngredientIndex(currentSlot.getStack(), currentIngredients); + if (currentStackIndex >= 0) { + int nextStackIndex = dWheel / 120; + for (int j = 0; j < Math.abs(nextStackIndex); j++) { + currentStackIndex = nextStackIndex > 0 ? currentStackIndex + 1 : currentStackIndex - 1; + if (currentStackIndex >= currentIngredients.items.length) { + currentStackIndex = 0; + } else if (currentStackIndex < 0) { + currentStackIndex = currentIngredients.items.length - 1; + } + + ItemStack currentStack = currentIngredients.items[currentStackIndex].copy(); + currentStack.stackSize = currentSlot.getStack().stackSize; + + if (NEEConfig.allowSynchronousSwitchIngredient) { + for (Slot slot : getCraftingSlots(gui)) { + + PositionedStack slotIngredients = NEECraftingHandler.ingredients.get("input" + slot.getSlotIndex()); + + boolean areItemStackEqual = currentSlot.getHasStack() && + slot.getHasStack() && + currentSlot.getStack().isItemEqual(slot.getStack()) && + ItemStack.areItemStackTagsEqual(currentSlot.getStack(), slot.getStack()); + + boolean areIngredientEqual = slotIngredients != null && currentIngredients.contains(slotIngredients.items[0]); + + if (areItemStackEqual && areIngredientEqual) { + craftingSlots.add(slot.slotNumber); + } + + } + } else { + craftingSlots.add(currentSlot.slotNumber); + } + NEENetworkHandler.getInstance().sendToServer(new PacketRecipeItemChange(currentStack, craftingSlots)); + } + } + } + + } + + @SuppressWarnings("unchecked") + private List getCraftingSlots(GuiContainer gui) { + List craftingSlots = new ArrayList<>(); + for (Slot slot : (List) gui.inventorySlots.inventorySlots) { + if (GuiUtils.isCraftingSlot(slot)) { + craftingSlots.add(slot); + } + } + return craftingSlots; + } + } diff --git a/src/main/java/com/github/vfyjxf/nee/client/NEEContainerDrawHandler.java b/src/main/java/com/github/vfyjxf/nee/client/NEEContainerDrawHandler.java index 5598766..4c18307 100644 --- a/src/main/java/com/github/vfyjxf/nee/client/NEEContainerDrawHandler.java +++ b/src/main/java/com/github/vfyjxf/nee/client/NEEContainerDrawHandler.java @@ -7,6 +7,7 @@ import codechicken.nei.PositionedStack; import codechicken.nei.guihook.IContainerDrawHandler; import codechicken.nei.recipe.GuiRecipe; import codechicken.nei.recipe.IRecipeHandler; +import com.github.vfyjxf.nee.config.NEEConfig; import com.github.vfyjxf.nee.utils.GuiUtils; import com.github.vfyjxf.nee.utils.Ingredient; import com.github.vfyjxf.nee.utils.IngredientTracker; @@ -73,7 +74,7 @@ public class NEEContainerDrawHandler implements IContainerDrawHandler { @Override public void renderSlotOverlay(GuiContainer gui, Slot slot) { - if (gui instanceof GuiRecipe) { + if (NEEConfig.drawHighlight && gui instanceof GuiRecipe) { Minecraft mc = Minecraft.getMinecraft(); GuiRecipe guiRecipe = (GuiRecipe) gui; GuiContainer firstGui = guiRecipe.firstGui; diff --git a/src/main/java/com/github/vfyjxf/nee/config/NEEConfig.java b/src/main/java/com/github/vfyjxf/nee/config/NEEConfig.java index a05a1b5..15aa91f 100644 --- a/src/main/java/com/github/vfyjxf/nee/config/NEEConfig.java +++ b/src/main/java/com/github/vfyjxf/nee/config/NEEConfig.java @@ -22,6 +22,7 @@ public class NEEConfig { public static boolean noShift = true; public static boolean matchOtherItems = true; public static boolean drawHighlight = true; + public static boolean allowSynchronousSwitchIngredient = true; public static void loadConfig(File configFile) { config = new Configuration(configFile); @@ -46,6 +47,9 @@ public class NEEConfig { drawHighlight = config.get("client", "drawHighlight", true, "if true,it will draw highlight for missing items and item which can autocraft in nei").getBoolean(); + allowSynchronousSwitchIngredient = config.get("client", "allowSynchronousSwitchIngredient", true, + "If true, it will make all similar ingredient switch at the same time").getBoolean(true); + if (config.hasChanged()) config.save(); } @@ -63,6 +67,10 @@ public class NEEConfig { matchOtherItems = config.get("client", "matchOnCraftableItems", false, "If false, Crafting Helper will not match other items").getBoolean(); + + allowSynchronousSwitchIngredient = config.get("client", "allowSynchronousSwitchIngredient", true, + "If true, it will make all similar ingredient switch at the same time").getBoolean(true); + ItemUtils.reloadConfig(); } diff --git a/src/main/java/com/github/vfyjxf/nee/network/packet/PacketRecipeItemChange.java b/src/main/java/com/github/vfyjxf/nee/network/packet/PacketRecipeItemChange.java index f427968..276762b 100644 --- a/src/main/java/com/github/vfyjxf/nee/network/packet/PacketRecipeItemChange.java +++ b/src/main/java/com/github/vfyjxf/nee/network/packet/PacketRecipeItemChange.java @@ -12,51 +12,64 @@ import net.minecraft.inventory.Slot; import net.minecraft.item.ItemStack; import net.minecraft.nbt.NBTTagCompound; +import java.util.ArrayList; +import java.util.List; + /** * @author vfyjxf */ -public class PacketRecipeItemChange implements IMessage, IMessageHandler { +public class PacketRecipeItemChange implements IMessage, IMessageHandler { - private NBTTagCompound stack; - private int slotNumber; + private ItemStack stack; + private List craftingSlots; - public PacketRecipeItemChange(){ + public PacketRecipeItemChange() { } - public PacketRecipeItemChange(NBTTagCompound stack, int slotNumber) { + public PacketRecipeItemChange(ItemStack stack, List craftingSlots) { this.stack = stack; - this.slotNumber = slotNumber; + this.craftingSlots = craftingSlots; } - public NBTTagCompound getStack() { + public ItemStack getStack() { return stack; } - public int getSlotNumber() { - return slotNumber; + public List getCraftingSlots() { + return craftingSlots; } @Override public void fromBytes(ByteBuf buf) { - this.stack = ByteBufUtils.readTag(buf); - this.slotNumber = buf.readInt(); + this.stack = ByteBufUtils.readItemStack(buf); + int craftingSlotsSize = buf.readInt(); + this.craftingSlots = new ArrayList<>(craftingSlotsSize); + for (int i = 0; i < craftingSlotsSize; i++) { + int slotNumber = buf.readInt(); + craftingSlots.add(slotNumber); + } } @Override public void toBytes(ByteBuf buf) { - ByteBufUtils.writeTag(buf,this.stack); - buf.writeInt(this.slotNumber); + ByteBufUtils.writeItemStack(buf, this.stack); + buf.writeInt(this.craftingSlots.size()); + for (Integer craftingSlot : this.craftingSlots) { + buf.writeInt(craftingSlot); + } } @Override public IMessage onMessage(PacketRecipeItemChange message, MessageContext ctx) { Container container = ctx.getServerHandler().playerEntity.openContainer; - if(container instanceof ContainerPatternTerm || GuiUtils.isPatternTermExContainer(container)){ - Slot currentSlot = container.getSlot(message.getSlotNumber()); - ItemStack nextStack = ItemStack.loadItemStackFromNBT(message.getStack()); - if(nextStack != null){ - currentSlot.putStack(nextStack); + if (container instanceof ContainerPatternTerm || GuiUtils.isPatternTermExContainer(container)) { + ItemStack nextStack = message.getStack(); + if (nextStack != null) { + for (Integer craftingSlot : message.getCraftingSlots()) { + Slot currentSlot = container.getSlot(craftingSlot); + currentSlot.putStack(nextStack); + } } } return null; diff --git a/src/main/java/com/github/vfyjxf/nee/utils/GuiUtils.java b/src/main/java/com/github/vfyjxf/nee/utils/GuiUtils.java index 88b290d..bfa77f1 100644 --- a/src/main/java/com/github/vfyjxf/nee/utils/GuiUtils.java +++ b/src/main/java/com/github/vfyjxf/nee/utils/GuiUtils.java @@ -32,11 +32,14 @@ import java.util.List; public class GuiUtils { public static Slot getSlotUnderMouse(GuiContainer guiContainer, int mouseX, int mouseY) { - Method getSlotMethod = ReflectionHelper.findMethod(AEBaseGui.class, (AEBaseGui) guiContainer, new String[]{"getSlot"}, int.class, int.class); - try { - return (Slot) getSlotMethod.invoke(guiContainer, mouseX, mouseY); - } catch (IllegalAccessException | InvocationTargetException e) { - e.printStackTrace(); + + if (guiContainer instanceof AEBaseGui) { + Method getSlotMethod = ReflectionHelper.findMethod(AEBaseGui.class, (AEBaseGui) guiContainer, new String[]{"getSlot"}, int.class, int.class); + try { + return (Slot) getSlotMethod.invoke(guiContainer, mouseX, mouseY); + } catch (IllegalAccessException | InvocationTargetException e) { + e.printStackTrace(); + } } return null; @@ -62,12 +65,17 @@ public class GuiUtils { public static boolean isCraftingSlot(Slot slot) { Container container = Minecraft.getMinecraft().thePlayer.openContainer; - if (container instanceof ContainerPatternTerm || GuiUtils.isPatternTermExContainer(container)) { - IContainerCraftingPacket cct = (IContainerCraftingPacket) container; - IInventory craftMatrix = cct.getInventoryByName("crafting"); - return slot.inventory.equals(craftMatrix); + if (slot != null) { + if (container instanceof ContainerPatternTerm || GuiUtils.isPatternTermExContainer(container)) { + IContainerCraftingPacket cct = (IContainerCraftingPacket) container; + IInventory craftMatrix = cct.getInventoryByName("crafting"); + return craftMatrix.equals(slot.inventory); + } else { + return false; + } + } else { + return false; } - return false; } public static boolean isMouseOverButton(GuiButton button, int mouseX, int mouseY) { @@ -106,27 +114,4 @@ public class GuiUtils { } } - public static void handleRecipeIngredientChange(Slot currentSlot, int dWheel) { - - PositionedStack currentIngredients = NEECraftingHandler.ingredients.get("input" + currentSlot.getSlotIndex()); - if (currentIngredients != null && currentIngredients.items.length > 1) { - int currentStackIndex = ItemUtils.getIngredientIndex(currentSlot.getStack(), currentIngredients); - if (currentStackIndex >= 0) { - int nextStackIndex = dWheel / 120; - for (int i = 0; i < Math.abs(nextStackIndex); i++) { - currentStackIndex = nextStackIndex > 0 ? currentStackIndex + 1 : currentStackIndex - 1; - if (currentStackIndex >= currentIngredients.items.length) { - currentStackIndex = 0; - } else if (currentStackIndex < 0) { - currentStackIndex = currentIngredients.items.length - 1; - } - ItemStack currentStack = currentIngredients.items[currentStackIndex].copy(); - currentStack.stackSize = currentSlot.getStack().stackSize; - NEENetworkHandler.getInstance().sendToServer(new PacketRecipeItemChange(currentStack.writeToNBT(new NBTTagCompound()), currentSlot.slotNumber)); - } - } - } - - } - }