Add the function of synchronously modifying ingredients

This commit is contained in:
vfyjxf committed 2021-10-31 14:58:31 +08:00
1 parent f69a663fce
commit 224456afb1
6 files changed
+127 -54

No files matched your search

@@ -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<Integer> 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<Slot> getCraftingSlots(GuiContainer gui) {
List<Slot> craftingSlots = new ArrayList<>();
for (Slot slot : (List<Slot>) gui.inventorySlots.inventorySlots) {
if (GuiUtils.isCraftingSlot(slot)) {
craftingSlots.add(slot);
}
}
return craftingSlots;
}
}
@@ -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;
@@ -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();
}
@@ -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<PacketRecipeItemChange,IMessage> {
public class PacketRecipeItemChange implements IMessage, IMessageHandler<PacketRecipeItemChange, IMessage> {
private NBTTagCompound stack;
private int slotNumber;
private ItemStack stack;
private List<Integer> craftingSlots;
public PacketRecipeItemChange(){
public PacketRecipeItemChange() {
}
public PacketRecipeItemChange(NBTTagCompound stack, int slotNumber) {
public PacketRecipeItemChange(ItemStack stack, List<Integer> 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<Integer> 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;
@@ -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));
}
}
}
}
}