feat: add support for nei keybinding

This commit is contained in:
Discreater committed 2021-05-08 19:17:08 +08:00
1 parent 0a09a18d0a
commit f2aa21f616
7 files changed
+108 -22

No files matched your search

@@ -1,22 +1,47 @@
package me.towdium.jecalculation.event.handlers;
import codechicken.nei.NEIClientConfig;
import codechicken.nei.NEIClientUtils;
import codechicken.nei.guihook.IContainerInputHandler;
import me.towdium.jecalculation.JustEnoughCalculation;
import me.towdium.jecalculation.data.label.ILabel;
import me.towdium.jecalculation.gui.JecaGui;
import me.towdium.jecalculation.nei.NEIPlugin;
import net.minecraft.client.Minecraft;
import net.minecraft.client.gui.inventory.GuiContainer;
public class NEIEventHandler implements IContainerInputHandler {
@Override
public boolean keyTyped(GuiContainer guiContainer, char c, int i) {
JustEnoughCalculation.logger.info("nei key typed");
return false;
}
@Override
public void onKeyTyped(GuiContainer guiContainer, char c, int i) {
JustEnoughCalculation.logger.info("nei on key typed");
}
@Override
public boolean lastKeyTyped(GuiContainer guiContainer, char c, int i) {
public boolean lastKeyTyped(GuiContainer guiContainer, char keyChar, int keyCode) {
if (guiContainer == Minecraft.getMinecraft().currentScreen && guiContainer instanceof JecaGui) {
JecaGui gui = (JecaGui) guiContainer;
ILabel label = gui.getLabelUnderMouse();
if (label == null)
return false;
Object stack = label.getRepresentation();
if (keyCode == NEIClientConfig.getKeyBinding("gui.usage") ||
(keyCode == NEIClientConfig.getKeyBinding("gui.recipe") && NEIClientUtils.shiftKey())) {
return NEIPlugin.openRecipeGui(stack, true);
}
if (keyCode == NEIClientConfig.getKeyBinding("gui.recipe")) {
return NEIPlugin.openRecipeGui(stack, false);
}
return false;
}
return false;
}
@@ -120,7 +120,7 @@ public class JecaGui extends GuiContainer {
}
return false;
}
public static void onMouseReleased() {
Optional<JecaGui> optionalJecaGui = currentJecaGui();
if (!optionalJecaGui.isPresent())
@@ -133,15 +133,14 @@ public class JecaGui extends GuiContainer {
}
@Nullable
public Slot getSlotUnderMouse() {
IInventory i = new InventoryBasic("", false, 1);
Slot s = new Slot(i, 0, 0, 0);
ILabel l = getLabelUnderMouse();
Object rep = l == null ? null : l.getRepresentation();
if (rep instanceof ItemStack) s.putStack((ItemStack) rep);
if (rep instanceof ItemStack)
s.putStack((ItemStack) rep);
return s;
}
@@ -221,7 +220,10 @@ public class JecaGui extends GuiContainer {
}
/**
* called by {@link me.towdium.jecalculation.event.handlers.FMLBusEventHandler}
* Called by {@link me.towdium.jecalculation.event.handlers.FMLBusEventHandler}.
* This event happened only when not in gui.
* For nei keybinding event, see {@link me.towdium.jecalculation.event.handlers.NEIEventHandler#lastKeyTyped(GuiContainer, char, int)}
* I think they won't conflict
*/
public static void onKey() {
if (keyOpenGuiCraft.isPressed())
@@ -300,10 +302,12 @@ public class JecaGui extends GuiContainer {
if (j > i)
i = j;
//noinspection StringEquality
if (s == JecaGui.SEPARATOR) separators++;
if (s == JecaGui.SEPARATOR)
separators++;
}
//noinspection StringEquality
if (textLines.get(textLines.size() - 1) == SEPARATOR) separators--;
if (textLines.get(textLines.size() - 1) == SEPARATOR)
separators--;
int l1 = x + 12;
int i2 = y - 12;
int k = 8 + (textLines.size() - separators - 1) * 10 + 2 * separators;
@@ -324,7 +328,8 @@ public class JecaGui extends GuiContainer {
drawGradientRect(l1 - 3, i2 + k + 2, l1 + i + 3, i2 + k + 3, 1344798847, 1344798847);
for (String s1 : textLines) {
//noinspection StringEquality
if (s1 == SEPARATOR) i2 += 2;
if (s1 == SEPARATOR)
i2 += 2;
else {
font.drawStringWithShadow(s1, l1, i2, -1);
i2 += 10;
@@ -17,7 +17,6 @@ import me.towdium.jecalculation.utils.Utilities;
import me.towdium.jecalculation.utils.wrappers.Pair;
import net.minecraft.entity.player.InventoryPlayer;
import net.minecraft.item.ItemStack;
import org.lwjgl.input.Mouse;
import javax.annotation.ParametersAreNonnullByDefault;
import java.util.*;
@@ -42,7 +41,10 @@ public class GuiCraft extends WContainer implements IGui {
(i, v) -> label.setLabel(i.get(v).getLabel().copy(), true));
WLabelScroll result = new WLabelScroll(7, 87, 8, 4, false).setLsnrLeftClick((i, v) -> {
Object rep = i.get(v).getLabel().getRepresentation();
NEIPlugin.openRecipeGui(rep, Mouse.getEventButton());
NEIPlugin.openRecipeGui(rep, false);
}).setLsnrRightClick((widget, value) -> {
Object rep = widget.get(value).getLabel().getRepresentation();
NEIPlugin.openRecipeGui(rep, true);
}).setFmtAmount(i -> i.getAmountString(true)).setFmtTooltip((i, j) -> i.getToolTip(j, true));
WButton steps = new WButtonIcon(64, 62, 20, 20, Resource.BTN_LIST, "craft.step").setListener(i -> setMode(STEPS));
WButton catalyst = new WButtonIcon(45, 62, 20, 20, Resource.BTN_CAT, "common.catalyst").setListener(
@@ -46,19 +46,17 @@ public class NEIPlugin {
NEIPlugin.currentItemStack = itemStack;
}
public static void openRecipeGui(Object rep, int button) {
public static boolean openRecipeGui(Object rep, boolean usage) {
if ((rep instanceof ItemStack || rep instanceof FluidStack)) {
String id = rep instanceof ItemStack ? "item" : "liquid";
if (button == 0) {
GuiCraftingRecipe.openRecipeGui(id, rep);
} else if (button == 1) {
GuiUsageRecipe.openRecipeGui(id, rep);
if (!usage) {
return GuiCraftingRecipe.openRecipeGui(id, rep);
} else {
// TODO check
JustEnoughCalculation.logger.warn("unknown button " + button);
return GuiUsageRecipe.openRecipeGui(id, rep);
}
} else if (rep != null) {
JustEnoughCalculation.logger.warn("unknown label representation " + rep);
}
return false;
}
}