ME, FC, WC Terminals (#42)

Co-authored-by: slprime <[email protected]>
This commit is contained in:
slprimeandslprime authored and GitHub committed 2025-06-27 10:43:23 +02:00
1 parent 8603274b1d
commit c9daf24aad
47 files changed
+2484 -2680

No files matched your search

@@ -2,149 +2,166 @@ package com.github.vfyjxf.nee.client;
import static com.github.vfyjxf.nee.config.NEEConfig.draggedStackDefaultSize;
import static com.github.vfyjxf.nee.config.NEEConfig.useStackSizeFromNEI;
import static com.github.vfyjxf.nee.nei.NEECraftingHandler.INPUT_KEY;
import static com.github.vfyjxf.nee.nei.NEECraftingHelper.tracker;
import static com.github.vfyjxf.nee.nei.NEEPatternTerminalHandler.INPUT_KEY;
import static com.github.vfyjxf.nee.utils.GuiUtils.isPatternTerm;
import java.awt.Point;
import java.awt.Rectangle;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import net.minecraft.client.Minecraft;
import net.minecraft.client.gui.GuiButton;
import net.minecraft.client.gui.GuiScreen;
import net.minecraft.client.gui.inventory.GuiContainer;
import net.minecraft.inventory.Container;
import net.minecraft.client.resources.I18n;
import net.minecraft.inventory.Slot;
import net.minecraft.item.ItemStack;
import net.minecraftforge.client.event.GuiOpenEvent;
import net.minecraftforge.client.event.GuiScreenEvent;
import org.lwjgl.input.Keyboard;
import org.lwjgl.input.Mouse;
import com.github.vfyjxf.nee.client.gui.widgets.GuiImgButtonEnableCombination;
import com.github.vfyjxf.nee.config.ItemCombination;
import com.github.vfyjxf.nee.config.NEEConfig;
import com.github.vfyjxf.nee.container.ContainerCraftingConfirm;
import com.github.vfyjxf.nee.nei.NEECraftingHandler;
import com.github.vfyjxf.nee.nei.NEEPatternTerminalHandler;
import com.github.vfyjxf.nee.network.NEENetworkHandler;
import com.github.vfyjxf.nee.network.packet.PacketSlotStackChange;
import com.github.vfyjxf.nee.network.packet.PacketStackCountChange;
import com.github.vfyjxf.nee.network.packet.PacketValueConfigServer;
import com.github.vfyjxf.nee.utils.GuiUtils;
import com.github.vfyjxf.nee.utils.ItemUtils;
import com.github.vfyjxf.nee.utils.ModIDs;
import com.glodblock.github.client.gui.GuiLevelMaintainer;
import com.glodblock.github.client.gui.base.FCGuiEncodeTerminal;
import com.glodblock.github.client.gui.container.base.FCContainerEncodeTerminal;
import appeng.api.events.GuiScrollEvent;
import appeng.client.gui.AEBaseGui;
import appeng.client.gui.implementations.GuiCraftConfirm;
import appeng.client.gui.implementations.GuiInterface;
import appeng.client.gui.implementations.GuiPatternTerm;
import appeng.container.implementations.ContainerCraftConfirm;
import appeng.container.AEBaseContainer;
import appeng.container.implementations.ContainerPatternTerm;
import appeng.container.slot.SlotFake;
import codechicken.lib.gui.GuiDraw;
import codechicken.nei.NEIClientConfig;
import codechicken.nei.NEIServerUtils;
import codechicken.nei.PositionedStack;
import codechicken.nei.VisiblityData;
import codechicken.nei.api.INEIGuiHandler;
import codechicken.nei.api.TaggedInventoryArea;
import codechicken.nei.api.INEIGuiAdapter;
import codechicken.nei.guihook.IContainerTooltipHandler;
import codechicken.nei.recipe.AcceptsFollowingTooltipLineHandler;
import codechicken.nei.recipe.GuiRecipe;
import codechicken.nei.recipe.RecipeInfo;
import codechicken.nei.util.NEIMouseUtils;
import cpw.mods.fml.common.Loader;
import cpw.mods.fml.common.ObfuscationReflectionHelper;
import cpw.mods.fml.common.eventhandler.SubscribeEvent;
public class GuiEventHandler implements INEIGuiHandler {
public class GuiEventHandler extends INEIGuiAdapter implements IContainerTooltipHandler {
public static GuiEventHandler instance = new GuiEventHandler();
public static final GuiEventHandler instance = new GuiEventHandler();
private final GuiImgButtonEnableCombination buttonCombination = new GuiImgButtonEnableCombination(
0,
0,
ItemCombination.ENABLED);
private GuiImgButtonEnableCombination buttonCombination;
private boolean hasDoubleBtn = true;
private boolean hasBeSubstituteBtn = true;
@SubscribeEvent
public void onGuiOpen(GuiOpenEvent event) {
GuiScreen old = Minecraft.getMinecraft().currentScreen;
GuiScreen next = event.gui;
if (old != null) {
if (GuiUtils.isGuiCraftConfirm(old) && isContainerCraftConfirm(((GuiContainer) old).inventorySlots)) {
if (tracker != null) {
if (GuiUtils.isGuiCraftingTerm(next)) {
if (tracker.hasNext()) {
tracker.requestNextIngredient();
} else {
tracker = null;
}
} else {
if (tracker != null) {
tracker = null;
}
}
}
}
}
}
@SubscribeEvent
public void onCraftConfirmActionPerformed(GuiScreenEvent.ActionPerformedEvent.Post event) {
if (tracker != null) {
if (event.gui instanceof GuiCraftConfirm) {
if (getCancelButton((GuiCraftConfirm) event.gui) == event.button) {
tracker = null;
}
}
}
}
private GuiButton getCancelButton(GuiCraftConfirm gui) {
return ObfuscationReflectionHelper.getPrivateValue(GuiCraftConfirm.class, gui, "cancel");
}
private boolean isContainerCraftConfirm(Container container) {
return (container instanceof ContainerCraftConfirm || GuiUtils.isContainerWirelessCraftingConfirm(container))
&& !((container instanceof ContainerCraftingConfirm)
|| (GuiUtils.isWCTContainerCraftingConfirm(container)));
}
private AcceptsFollowingTooltipLineHandler acceptsFollowingTooltipLineHandler;
private List<GuiButton> buttonList;
@SuppressWarnings("unchecked")
@SubscribeEvent
public void onInitGui(GuiScreenEvent.InitGuiEvent.Post event) {
if (event.gui instanceof GuiPatternTerm) {
GuiPatternTerm gui = (GuiPatternTerm) event.gui;
try {
GuiPatternTerm.class.getDeclaredField("doubleBtn");
} catch (NoSuchFieldException e) {
hasDoubleBtn = false;
}
try {
GuiPatternTerm.class.getDeclaredField("beSubstitutionsEnabledBtn");
} catch (NoSuchFieldException e) {
hasBeSubstituteBtn = false;
if (isGuiPatternTerm(event.gui)) {
event.buttonList.add(buttonCombination);
this.buttonList = getButtonList((GuiContainer) event.gui, event.buttonList);
updateCombinationButtonPosition((GuiContainer) event.gui);
} else {
this.buttonList = null;
}
if (event.gui instanceof GuiRecipe<?>) {
NEENetworkHandler.getInstance().sendToServer(new PacketValueConfigServer("PatternInterface.check"));
}
}
@SubscribeEvent
public void onDrawScreen(GuiScreenEvent.DrawScreenEvent.Post event) {
if (this.buttonList != null && isGuiPatternTerm(event.gui)) {
updateCombinationButtonPosition((GuiContainer) event.gui);
}
}
private boolean isGuiPatternTerm(GuiScreen guiScreen) {
if (guiScreen instanceof GuiContainer guiContainer) {
if (Loader.isModLoaded(ModIDs.FC) && guiContainer instanceof FCGuiEncodeTerminal) {
return false;
}
int x, y;
if (hasDoubleBtn && hasBeSubstituteBtn) {
x = gui.guiLeft + 84;
y = gui.guiTop + gui.ySize - 143;
} else if (hasDoubleBtn) {
x = gui.guiLeft + 84;
y = gui.guiTop + gui.ySize - 153;
} else {
x = gui.guiLeft + 74;
y = gui.guiTop + gui.ySize - 153;
}
buttonCombination = new GuiImgButtonEnableCombination(
x,
y,
ItemCombination.valueOf(NEEConfig.itemCombinationMode));
event.buttonList.add(buttonCombination);
return RecipeInfo.getOverlayHandler(guiContainer, "crafting") instanceof NEEPatternTerminalHandler
|| RecipeInfo.getOverlayHandler(guiContainer, "smelting") instanceof NEEPatternTerminalHandler;
}
return false;
}
private List<GuiButton> getButtonList(GuiContainer guiContainer, List<Object> items) {
final Rectangle buttonsArea = new Rectangle(
guiContainer.guiLeft + 70,
guiContainer.guiTop + guiContainer.ySize - 165,
38,
68);
return items.stream().filter(btn -> btn instanceof GuiButton && btn != buttonCombination)
.map(GuiButton.class::cast).filter(btn -> buttonsArea.contains(btn.xPosition, btn.yPosition))
.collect(Collectors.toList());
}
private void updateCombinationButtonPosition(GuiContainer guiContainer) {
final Point leftPoint = new Point(Integer.MAX_VALUE, 0);
final Point rightPoint = new Point(0, 0);
for (GuiButton button : buttonList) {
if (button.visible) {
leftPoint.x = Math.min(leftPoint.x, button.xPosition);
rightPoint.x = Math.max(rightPoint.x, button.xPosition);
}
}
for (GuiButton button : buttonList) {
if (button.visible) {
if (leftPoint.x == button.xPosition) {
leftPoint.y = Math.max(leftPoint.y, button.yPosition + button.height);
}
if (rightPoint.x == button.xPosition) {
rightPoint.y = Math.max(rightPoint.y, button.yPosition + button.height);
}
}
}
if (rightPoint.y < leftPoint.y) {
buttonCombination.xPosition = rightPoint.x;
buttonCombination.yPosition = leftPoint.y - buttonCombination.height;
} else if (rightPoint.y == leftPoint.y) {
buttonCombination.xPosition = leftPoint.x;
buttonCombination.yPosition = leftPoint.y + 2;
} else {
buttonCombination.xPosition = leftPoint.x;
buttonCombination.yPosition = rightPoint.y - buttonCombination.height;
}
buttonCombination.setValue(ItemCombination.valueOf(NEEConfig.itemCombinationMode));
buttonCombination.enabled = buttonCombination.visible = guiContainer.inventorySlots instanceof AEBaseContainer
&& !isCraftingMode((AEBaseContainer) guiContainer.inventorySlots);
}
@SubscribeEvent
public void onActionPerformed(GuiScreenEvent.ActionPerformedEvent.Pre event) {
if (event.button == this.buttonCombination) {
GuiImgButtonEnableCombination button = (GuiImgButtonEnableCombination) event.button;
int ordinal = Mouse.getEventButton() != 2 ? button.getCurrentValue().ordinal() + 1
: button.getCurrentValue().ordinal() - 1;
int ordinal = this.buttonCombination.getCurrentValue().ordinal() + (Mouse.getEventButton() == 0 ? 1 : -1);
if (ordinal >= ItemCombination.values().length) {
ordinal = 0;
@@ -152,38 +169,21 @@ public class GuiEventHandler implements INEIGuiHandler {
if (ordinal < 0) {
ordinal = ItemCombination.values().length - 1;
}
button.setValue(ItemCombination.values()[ordinal]);
this.buttonCombination.setValue(ItemCombination.values()[ordinal]);
NEEConfig.setItemCombinationMode(ItemCombination.values()[ordinal].name());
}
}
@SubscribeEvent
public void onDrawScreen(GuiScreenEvent.DrawScreenEvent.Post event) {
if (event.gui instanceof GuiPatternTerm) {
ContainerPatternTerm container = (ContainerPatternTerm) ((GuiPatternTerm) event.gui).inventorySlots;
if (container.isCraftingMode()) {
buttonCombination.enabled = false;
buttonCombination.visible = false;
} else {
buttonCombination.enabled = true;
buttonCombination.visible = true;
}
private boolean isCraftingMode(AEBaseContainer container) {
if (container instanceof ContainerPatternTerm) {
return ((ContainerPatternTerm) container).isCraftingMode();
} else if (Loader.isModLoaded(ModIDs.FC) && container instanceof FCContainerEncodeTerminal) {
return ((FCContainerEncodeTerminal) container).isCraftingMode();
}
}
@Override
public VisiblityData modifyVisiblity(GuiContainer gui, VisiblityData currentVisibility) {
return null;
}
@Override
public Iterable<Integer> getItemSpawnSlots(GuiContainer gui, ItemStack item) {
return null;
}
@Override
public List<TaggedInventoryArea> getInventoryAreas(GuiContainer gui) {
return null;
return false;
}
@Override
@@ -193,56 +193,47 @@ public class GuiEventHandler implements INEIGuiHandler {
return false;
}
if (NEEConfig.enableNEIDragDrop) {
if (Loader.isModLoaded("ae2fc") && gui instanceof GuiLevelMaintainer) {
return false;
}
if (gui instanceof AEBaseGui) {
if (draggedStack != null) {
Slot currentSlot = gui.getSlotAtPosition(mouseX, mouseY);
if (currentSlot instanceof SlotFake) {
ItemStack slotStack = currentSlot.getStack();
ItemStack copyStack = draggedStack.copy();
boolean sendPacket = false;
int copySize = useStackSizeFromNEI ? copyStack.stackSize : draggedStackDefaultSize;
if (button == 0) {
boolean areStackEqual = slotStack != null && slotStack.isItemEqual(copyStack)
&& ItemStack.areItemStackTagsEqual(slotStack, copyStack);
copyStack.stackSize = areStackEqual ? slotStack.stackSize + copySize : copySize;
sendPacket = true;
} else if (button == 1) {
boolean areStackEqual = slotStack != null && slotStack.isItemEqual(copyStack)
&& ItemStack.areItemStackTagsEqual(slotStack, copyStack);
if (areStackEqual) {
copyStack.stackSize = slotStack.stackSize;
} else {
copyStack.stackSize = slotStack == null ? 1 : copySize;
}
sendPacket = true;
}
if (NEEConfig.enableNEIDragDrop && Loader.isModLoaded("ae2fc") && gui instanceof GuiLevelMaintainer) {
return false;
}
if (sendPacket) {
NEENetworkHandler.getInstance().sendToServer(
new PacketSlotStackChange(
copyStack,
Collections.singletonList(currentSlot.slotNumber)));
if (!NEEConfig.keepGhostitems) {
draggedStack.stackSize = 0;
}
return true;
}
if (NEEConfig.enableNEIDragDrop && draggedStack != null && gui instanceof AEBaseGui) {
Slot currentSlot = gui.getSlotAtPosition(mouseX, mouseY);
if (currentSlot instanceof SlotFake) {
ItemStack slotStack = currentSlot.getStack();
ItemStack copyStack = draggedStack.copy();
boolean sendPacket = false;
int copySize = useStackSizeFromNEI ? copyStack.stackSize : draggedStackDefaultSize;
if (button == 0) {
copyStack.stackSize = NEIServerUtils.areStacksSameTypeCraftingWithNBT(slotStack, copyStack)
? slotStack.stackSize + copySize
: copySize;
sendPacket = true;
} else if (button == 1) {
if (NEIServerUtils.areStacksSameTypeCraftingWithNBT(slotStack, copyStack)) {
copyStack.stackSize = slotStack.stackSize;
} else {
copyStack.stackSize = slotStack == null ? 1 : copySize;
}
if (button == 2) {
sendPacket = true;
}
if (sendPacket) {
NEENetworkHandler.getInstance().sendToServer(
new PacketSlotStackChange(copyStack, Collections.singletonList(currentSlot.slotNumber)));
if (!NEEConfig.keepGhostitems) {
draggedStack.stackSize = 0;
}
return true;
}
}
}
return false;
}
@Override
public boolean hideItemPanelSlot(GuiContainer gui, int x, int y, int w, int h) {
if (button == 2) {
draggedStack.stackSize = 0;
}
}
return false;
}
@@ -253,69 +244,118 @@ public class GuiEventHandler implements INEIGuiHandler {
@SubscribeEvent
public boolean handleMouseWheelInput(GuiScrollEvent event) {
Minecraft mc = Minecraft.getMinecraft();
boolean isPatternTerm = isPatternTerm(mc.currentScreen);
boolean isInterface = event.guiScreen instanceof GuiInterface;
if (isPatternTerm || isInterface) {
Slot currentSlot = (event.guiScreen).getSlotAtPosition(event.mouseX, event.mouseY);
if (currentSlot instanceof SlotFake && currentSlot.getHasStack()) {
// try to change current itemstack to next ingredient;
if (Keyboard.isKeyDown(NEIClientConfig.getKeyBinding("nee.ingredient"))
&& GuiUtils.isCraftingSlot(currentSlot)) {
handleRecipeIngredientChange(event.guiScreen, currentSlot, event.scrollAmount);
return true;
} else if (Keyboard.isKeyDown(NEIClientConfig.getKeyBinding("nee.count"))) {
NEENetworkHandler.getInstance()
.sendToServer(new PacketStackCountChange(currentSlot.slotNumber, event.scrollAmount));
return true;
}
Slot currentSlot = getFakeSlotAtPosition((GuiContainer) mc.currentScreen, event.mouseX, event.mouseY);
if (currentSlot != null) {
// try to change current itemstack to next ingredient;
if (NEIClientConfig.isKeyHashDown("nee.ingredient") && GuiUtils.isCraftingSlot(currentSlot)) {
handleRecipeIngredientChange(event.guiScreen, currentSlot, event.scrollAmount);
return true;
} else if (NEIClientConfig.isKeyHashDown("nee.count")) {
NEENetworkHandler.getInstance()
.sendToServer(new PacketStackCountChange(currentSlot.slotNumber, event.scrollAmount));
return true;
}
}
return false;
}
private static void handleRecipeIngredientChange(GuiContainer gui, Slot currentSlot, int dWheel) {
List<Integer> craftingSlots = new ArrayList<>();
int currentSlotIndex = currentSlot.getSlotIndex();
PositionedStack currentIngredients = NEECraftingHandler.ingredients.get(INPUT_KEY + currentSlotIndex);
if (currentIngredients != null && currentIngredients.items.length > 1) {
int currentStackIndex = ItemUtils.getIngredientIndex(currentSlot.getStack(), currentIngredients);
if (currentStackIndex >= 0) {
for (int j = 0; j < Math.abs(dWheel); j++) {
currentStackIndex += (dWheel < 0) ? -1 : 1;
if (currentStackIndex >= currentIngredients.items.length) {
currentStackIndex = 0;
} else if (currentStackIndex < 0) {
currentStackIndex = currentIngredients.items.length - 1;
}
@Override
public List<String> handleItemTooltip(GuiContainer gui, ItemStack itemstack, int mousex, int mousey,
List<String> currenttip) {
final Slot currentSlot = itemstack != null ? getFakeSlotAtPosition(gui, mousex, mousey) : null;
ItemStack currentStack = currentIngredients.items[currentStackIndex].copy();
currentStack.stackSize = currentSlot.getStack().stackSize;
if (currentSlot != null) {
PositionedStack currentIngredients = NEEPatternTerminalHandler.ingredients
.get(INPUT_KEY + currentSlot.getSlotIndex());
if (currentIngredients != null && currentIngredients.items.length > 1
&& currentIngredients.containsWithNBT(currentSlot.getStack())) {
if (NEEConfig.allowSynchronousSwitchIngredient) {
for (Slot slot : getCraftingSlots(gui)) {
if (this.acceptsFollowingTooltipLineHandler == null
|| this.acceptsFollowingTooltipLineHandler.tooltipGUID != currentIngredients) {
this.acceptsFollowingTooltipLineHandler = AcceptsFollowingTooltipLineHandler.of(
currentIngredients,
currentIngredients.getFilteredPermutations(),
currentIngredients.item);
}
PositionedStack slotIngredients = NEECraftingHandler.ingredients
.get(INPUT_KEY + 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 PacketSlotStackChange(currentStack, craftingSlots));
if (this.acceptsFollowingTooltipLineHandler != null) {
this.acceptsFollowingTooltipLineHandler.setActiveStack(currentIngredients.item);
currenttip.add(
GuiDraw.TOOLTIP_HANDLER + GuiDraw.getTipLineId(this.acceptsFollowingTooltipLineHandler));
}
}
}
return currenttip;
}
@Override
public Map<String, String> handleHotkeys(GuiContainer gui, int mousex, int mousey, Map<String, String> hotkeys) {
final Slot currentSlot = getFakeSlotAtPosition(gui, mousex, mousey);
if (currentSlot != null) {
if (this.acceptsFollowingTooltipLineHandler != null) {
hotkeys.put(
NEIClientConfig.getKeyName(
"nee.ingredient",
0,
NEIMouseUtils.MOUSE_BTN_NONE + NEIMouseUtils.MOUSE_SCROLL),
I18n.format("neenergistics.gui.tooltip.ingredient.permutation"));
}
hotkeys.put(
NEIClientConfig
.getKeyName("nee.count", 0, NEIMouseUtils.MOUSE_BTN_NONE + NEIMouseUtils.MOUSE_SCROLL),
I18n.format("neenergistics.gui.tooltip.ingredient.count"));
}
return hotkeys;
}
private Slot getFakeSlotAtPosition(GuiContainer gui, int mousex, int mousey) {
if (gui instanceof GuiInterface || isPatternTerm(gui)) {
final Slot currentSlot = gui.getSlotAtPosition(mousex, mousey);
if (currentSlot instanceof SlotFake && currentSlot.getHasStack()) {
return currentSlot;
}
}
return null;
}
private void handleRecipeIngredientChange(GuiContainer gui, Slot currentSlot, int dWheel) {
final int currentSlotIndex = currentSlot.getSlotIndex();
final PositionedStack currentIngredients = NEEPatternTerminalHandler.ingredients
.get(INPUT_KEY + currentSlotIndex);
if (currentIngredients != null && currentIngredients.items.length > 1) {
final List<Integer> craftingSlots = new ArrayList<>();
final List<ItemStack> items = currentIngredients.getFilteredPermutations();
final int currentStackIndex = ItemUtils.getPermutationIndex(currentSlot.getStack(), items);
final ItemStack nextStack = items.get((items.size() - dWheel + currentStackIndex) % items.size());
final ItemStack currentStack = currentIngredients.item;
if (NEEConfig.allowSynchronousSwitchIngredient) {
for (Slot slot : getCraftingSlots(gui)) {
final PositionedStack slotIngredients = NEEPatternTerminalHandler.ingredients
.get(INPUT_KEY + slot.getSlotIndex());
if (slotIngredients != null && slotIngredients.containsWithNBT(nextStack)
&& NEIServerUtils.areStacksSameTypeCraftingWithNBT(slotIngredients.item, currentStack)) {
slotIngredients.setPermutationToRender(nextStack);
craftingSlots.add(slot.slotNumber);
}
}
} else {
currentIngredients.setPermutationToRender(nextStack);
craftingSlots.add(currentSlot.slotNumber);
}
NEENetworkHandler.getInstance().sendToServer(new PacketSlotStackChange(nextStack, craftingSlots));
}
}
@SuppressWarnings("unchecked")