diff --git a/README.md b/README.md index f52e8d7..4c90a9a 100644 --- a/README.md +++ b/README.md @@ -17,7 +17,7 @@ the [release page](https://minecraft.curseforge.com/projects/just-enough-calcula ## Known Issues -- [ ] 无法覆盖NEI默认点击行为 +- [x] 无法覆盖NEI默认点击行为 - [ ] OreDict 渲染存在问题 - [ ] capability - [x] 带 nbt 的物品存储到本地存在问题 diff --git a/src/main/java/me/towdium/jecalculation/event/Handlers.java b/src/main/java/me/towdium/jecalculation/event/Handlers.java index aa8e0c5..62643df 100644 --- a/src/main/java/me/towdium/jecalculation/event/Handlers.java +++ b/src/main/java/me/towdium/jecalculation/event/Handlers.java @@ -1,8 +1,10 @@ package me.towdium.jecalculation.event; +import codechicken.nei.guihook.GuiContainerManager; import cpw.mods.fml.common.FMLCommonHandler; -import me.towdium.jecalculation.event.handlers.EBEventHandler; +import me.towdium.jecalculation.event.handlers.EBusEventHandler; import me.towdium.jecalculation.event.handlers.FMLBusEventHandler; +import me.towdium.jecalculation.event.handlers.NEIEventHandler; import net.minecraftforge.common.MinecraftForge; import java.util.ArrayList; @@ -16,11 +18,12 @@ public class Handlers { fmlHandlers.add(new FMLBusEventHandler()); ebHandlers = new ArrayList<>(); - ebHandlers.add(new EBEventHandler()); + ebHandlers.add(new EBusEventHandler()); } public static void register() { Handlers.fmlHandlers.forEach(FMLCommonHandler.instance().bus()::register); Handlers.ebHandlers.forEach(MinecraftForge.EVENT_BUS::register); + GuiContainerManager.addInputHandler(new NEIEventHandler()); } } diff --git a/src/main/java/me/towdium/jecalculation/event/handlers/EBEventHandler.java b/src/main/java/me/towdium/jecalculation/event/handlers/EBEventHandler.java deleted file mode 100644 index 62d7c34..0000000 --- a/src/main/java/me/towdium/jecalculation/event/handlers/EBEventHandler.java +++ /dev/null @@ -1,26 +0,0 @@ -package me.towdium.jecalculation.event.handlers; - -import cpw.mods.fml.common.eventhandler.EventPriority; -import cpw.mods.fml.common.eventhandler.SubscribeEvent; -import cpw.mods.fml.relauncher.Side; -import cpw.mods.fml.relauncher.SideOnly; -import me.towdium.jecalculation.JustEnoughCalculation; -import me.towdium.jecalculation.gui.JecaGui; -import net.minecraftforge.client.event.GuiScreenEvent; - -/** - * register to MinecraftForge.EVENT_BUS - */ -public class EBEventHandler { - @SideOnly(Side.CLIENT) - @SubscribeEvent(priority = EventPriority.HIGHEST) - public void onMouseClick(GuiScreenEvent.ActionPerformedEvent.Pre event) { - JustEnoughCalculation.logger.info("on gui screen event action performed event pre"); - if (event.gui instanceof JecaGui) { - JustEnoughCalculation.logger.info("on mouse click in gui screen event"); - JecaGui gui = (JecaGui) event.gui; - event.setCanceled(gui.handleMouseEvent()); - } - } - -} diff --git a/src/main/java/me/towdium/jecalculation/event/handlers/EBusEventHandler.java b/src/main/java/me/towdium/jecalculation/event/handlers/EBusEventHandler.java new file mode 100644 index 0000000..72fa0fa --- /dev/null +++ b/src/main/java/me/towdium/jecalculation/event/handlers/EBusEventHandler.java @@ -0,0 +1,9 @@ +package me.towdium.jecalculation.event.handlers; + +/** + * register to MinecraftForge.EVENT_BUS + */ +public class EBusEventHandler { + + +} diff --git a/src/main/java/me/towdium/jecalculation/event/handlers/FMLBusEventHandler.java b/src/main/java/me/towdium/jecalculation/event/handlers/FMLBusEventHandler.java index a527ca3..0cc9acc 100644 --- a/src/main/java/me/towdium/jecalculation/event/handlers/FMLBusEventHandler.java +++ b/src/main/java/me/towdium/jecalculation/event/handlers/FMLBusEventHandler.java @@ -10,8 +10,6 @@ import me.towdium.jecalculation.JustEnoughCalculation; import me.towdium.jecalculation.data.Controller; import me.towdium.jecalculation.data.label.labels.LPlaceholder; import me.towdium.jecalculation.gui.JecaGui; -import me.towdium.jecalculation.gui.guis.GuiCraft; -import me.towdium.jecalculation.network.ClientHandler; /** * Author: towdium @@ -22,10 +20,15 @@ public class FMLBusEventHandler { @SideOnly(Side.CLIENT) @SubscribeEvent(receiveCanceled = true) public void onKey(InputEvent.KeyInputEvent event) { - if (ClientHandler.keyOpenGuiCraft.isPressed()) Controller.openGuiCraft(); - if (ClientHandler.keyOpenGuiMath.isPressed()) Controller.openGuiMath(); + JecaGui.onKey(); } + /** + * No effect at client only + * + * @param event event + */ + @Deprecated @SubscribeEvent public void onLogout(PlayerEvent.PlayerLoggedOutEvent event) { JustEnoughCalculation.logger.info("on log out event"); diff --git a/src/main/java/me/towdium/jecalculation/event/handlers/NEIEventHandler.java b/src/main/java/me/towdium/jecalculation/event/handlers/NEIEventHandler.java new file mode 100644 index 0000000..9b349a4 --- /dev/null +++ b/src/main/java/me/towdium/jecalculation/event/handlers/NEIEventHandler.java @@ -0,0 +1,52 @@ +package me.towdium.jecalculation.event.handlers; + +import codechicken.nei.guihook.IContainerInputHandler; +import me.towdium.jecalculation.gui.JecaGui; +import net.minecraft.client.gui.inventory.GuiContainer; + +public class NEIEventHandler implements IContainerInputHandler { + @Override + public boolean keyTyped(GuiContainer guiContainer, char c, int i) { + return false; + } + + @Override + public void onKeyTyped(GuiContainer guiContainer, char c, int i) { + + } + + @Override + public boolean lastKeyTyped(GuiContainer guiContainer, char c, int i) { + return false; + } + + @Override + public boolean mouseClicked(GuiContainer guiContainer, int mouseX, int mouseY, int button) { + return JecaGui.onMouse(); + } + + @Override + public void onMouseClicked(GuiContainer guiContainer, int i, int i1, int i2) { + + } + + @Override + public void onMouseUp(GuiContainer guiContainer, int i, int i1, int i2) { + + } + + @Override + public boolean mouseScrolled(GuiContainer guiContainer, int i, int i1, int i2) { + return false; + } + + @Override + public void onMouseScrolled(GuiContainer guiContainer, int i, int i1, int i2) { + + } + + @Override + public void onMouseDragged(GuiContainer guiContainer, int i, int i1, int i2, long l) { + + } +} diff --git a/src/main/java/me/towdium/jecalculation/gui/JecaGui.java b/src/main/java/me/towdium/jecalculation/gui/JecaGui.java index db5dfa6..5769c0f 100644 --- a/src/main/java/me/towdium/jecalculation/gui/JecaGui.java +++ b/src/main/java/me/towdium/jecalculation/gui/JecaGui.java @@ -1,14 +1,16 @@ package me.towdium.jecalculation.gui; -import codechicken.nei.guihook.GuiContainerManager; import cpw.mods.fml.client.config.GuiUtils; import cpw.mods.fml.relauncher.Side; import cpw.mods.fml.relauncher.SideOnly; -import me.towdium.jecalculation.JustEnoughCalculation; +import me.towdium.jecalculation.data.Controller; import me.towdium.jecalculation.data.label.ILabel; +import me.towdium.jecalculation.data.label.labels.LItemStack; import me.towdium.jecalculation.gui.guis.IGui; import me.towdium.jecalculation.nei.NEIPlugin; +import me.towdium.jecalculation.network.ClientHandler; import me.towdium.jecalculation.polyfill.mc.client.renderer.GlStateManager; +import me.towdium.jecalculation.utils.ItemStackHelper; import me.towdium.jecalculation.utils.Utilities; import net.minecraft.client.Minecraft; import net.minecraft.client.gui.FontRenderer; @@ -19,6 +21,9 @@ import net.minecraft.client.renderer.Tessellator; import net.minecraft.client.renderer.texture.TextureMap; import net.minecraft.entity.player.EntityPlayer; import net.minecraft.inventory.Container; +import net.minecraft.inventory.IInventory; +import net.minecraft.inventory.InventoryBasic; +import net.minecraft.inventory.Slot; import net.minecraft.item.ItemStack; import net.minecraft.util.IIcon; import net.minecraftforge.fluids.Fluid; @@ -28,9 +33,9 @@ import org.lwjgl.input.Mouse; import javax.annotation.Nullable; import javax.annotation.ParametersAreNonnullByDefault; import java.util.ArrayList; -import java.util.Collections; import java.util.List; import java.util.Objects; +import java.util.Optional; import static codechicken.lib.gui.GuiDraw.renderEngine; @@ -76,6 +81,48 @@ public class JecaGui extends GuiContainer { } + /** + * @return true to terminate further processing of this event. + */ + public static boolean onMouse() { + Optional optionalJecaGui = currentJecaGui(); + if(!optionalJecaGui.isPresent()) return false; + JecaGui gui = optionalJecaGui.get(); + int mouseX = getMouseX(); + int mouseY = getMouseY(); + int button = Mouse.getEventButton(); + if (button == -1) { + int diff = Mouse.getEventDWheel() / 120; + if (diff != 0) + gui.root.onScroll(gui, mouseX, mouseY, diff); + } else if (Mouse.getEventButtonState()) { + if (gui.root.onClicked(gui, mouseX, mouseY, button)) + return true; + else if (gui.hand != ILabel.EMPTY) { + gui.hand = ILabel.EMPTY; + return true; + } else { + ILabel e = NEIPlugin.getLabelUnderMouse(); + if (e != ILabel.EMPTY) { + gui.hand = e; + return true; + } + } + } + return false; + } + + + @Nullable + public Slot getSlotUnderMouse() { + IInventory i = new InventoryBasic("", false, 1); + Slot s = new Slot(i, 0, 0, 0); + ILabel l = getLabelUnderMouse(); + if (l instanceof LItemStack) + s.putStack(((LItemStack) l).getRep()); + return s; + } + @Override public void initGui() { this.guiLeft = (this.width - this.xSize) / 2; @@ -107,6 +154,15 @@ public class JecaGui extends GuiContainer { r.run(); } + public static Optional currentJecaGui() { + GuiScreen gui = Minecraft.getMinecraft().currentScreen; + if (gui instanceof JecaGui) { + return Optional.of((JecaGui) gui); + } else { + return Optional.empty(); + } + } + /** * @return The currently displayed {@link JecaGui} * Make sure the method is called when a {@link JecaGui} is displayed! @@ -119,7 +175,6 @@ public class JecaGui extends GuiContainer { return ret; } - private static void displayGuiUnsafe(boolean updateParent, boolean acceptsTransfer, IGui root) { Minecraft mc = Minecraft.getMinecraft(); JecaGui parent; @@ -147,6 +202,23 @@ public class JecaGui extends GuiContainer { } } + @Nullable + public ILabel getLabelUnderMouse() { + int xMouse = Mouse.getEventX() * width / mc.displayWidth - guiLeft; + int yMouse = height - Mouse.getEventY() * height / mc.displayHeight - 1 - guiTop; + return root.getLabelUnderMouse(xMouse, yMouse); + } + + /** + * called by {@link me.towdium.jecalculation.event.handlers.FMLBusEventHandler} + */ + public static void onKey() { + if (ClientHandler.keyOpenGuiCraft.isPressed()) + Controller.openGuiCraft(); + if (ClientHandler.keyOpenGuiMath.isPressed()) + Controller.openGuiMath(); + } + public static void onGameTick() { if (scheduled != null) { if (timeout <= 0) { @@ -238,57 +310,12 @@ public class JecaGui extends GuiContainer { } } - /** - * This function handles events within the interface. - * Specifically, handles mouse wheel within interface. - * Different from {@link #handleMouseEvent()}, which is - * used to handle mouse event outside. - */ - @Override - public void handleMouseInput() { - super.handleMouseInput(); - int diff = Mouse.getEventDWheel() / 120; - if (diff != 0) - root.onScroll(this, Mouse.getEventX() * width / mc.displayWidth - guiLeft, - height - Mouse.getEventY() * height / mc.displayHeight - 1 - guiTop, diff); - } - - /** - * @return if the event is canceled - * This function handles click outside the rNormal region, - * especially the overlap with JEI overlay. It handles - * mouse event before JEI. - */ - public boolean handleMouseEvent() { - JustEnoughCalculation.logger.info("handle mouse clicked"); - int xMouse = Mouse.getEventX() * this.width / this.mc.displayWidth; - int yMouse = this.height - Mouse.getEventY() * this.height / this.mc.displayHeight - 1; - int button = Mouse.getEventButton(); - if (Mouse.getEventButtonState()) { - if (hand != ILabel.EMPTY) { - hand = ILabel.EMPTY; - return true; - } else { - ILabel e = NEIPlugin.getLabelUnderMouse(); - if (e != ILabel.EMPTY) { - hand = e; - return true; - } - } - } - return false; - } - public void drawResource(Resource r, int xPos, int yPos) { drawResource(r, xPos, yPos, 0xFFFFFF); } public void drawResource(Resource r, int xPos, int yPos, int color) { - float red = (color >> 16 & 0xFF) / 255.0F; - float green = (color >> 8 & 0xFF) / 255.0F; - float blue = (color & 0xFF) / 255.0F; - float alpha = (~(color >> 24) & 0xFF) / 255.0F; - GlStateManager.color(red, green, blue, alpha); + setColor(color); mc.getTextureManager().bindTexture(r.getResourceLocation()); drawTexturedModalRect(xPos, yPos, r.getXPos(), r.getYPos(), r.getXSize(), r.getYSize()); } @@ -348,16 +375,6 @@ public class JecaGui extends GuiContainer { GlStateManager.enableTexture2D(); } - public void drawOverRectangle(int xPos, int yPos, int xSize, int ySize, int color) { - GlStateManager.disableLighting(); - GlStateManager.disableDepth(); - GlStateManager.colorMask(true, true, true, false); - this.drawGradientRect(xPos, yPos, xPos + xSize, yPos + ySize, color, color); - GlStateManager.colorMask(true, true, true, true); - GlStateManager.enableLighting(); - GlStateManager.enableDepth(); - } - public int getStringWidth(String s) { return fontRendererObj.getStringWidth(s); } @@ -409,7 +426,7 @@ public class JecaGui extends GuiContainer { } public void drawItemStack(int xPos, int yPos, ItemStack is, boolean centred) { - if (is.getItem() == null) { + if (ItemStackHelper.isEmpty(is)) { return; } if (centred) { @@ -426,15 +443,15 @@ public class JecaGui extends GuiContainer { itemRender.zLevel = zLevel - 100F; } - @Override - protected void mouseClicked(int mouseX, int mouseY, int mouseButton) { - JustEnoughCalculation.logger.info("mouse clicked"); - super.mouseClicked(mouseX, mouseY, mouseButton); - boolean inGui = root.onClicked(this, mouseX - guiLeft, mouseY - guiTop, mouseButton); - if (!inGui) { - this.handleMouseEvent(); - } - } + // @Override + // protected void mouseClicked(int mouseX, int mouseY, int mouseButton) { + // JustEnoughCalculation.logger.info("mouse clicked"); + // super.mouseClicked(mouseX, mouseY, mouseButton); + // boolean inGui = root.onClicked(this, mouseX - guiLeft, mouseY - guiTop, mouseButton); + // if (!inGui) { + // this.handleMouseEvent(); + // } + // } @Override protected void keyTyped(char typedChar, int keyCode) { @@ -508,27 +525,8 @@ public class JecaGui extends GuiContainer { public static class ContainerNonTransfer extends JecContainer { } - public int getGuiLeft() { - return guiLeft; - } - - public int getGuiTop() { - return guiTop; - } - - public void drawHoveringText(String text, int x, int y) { - super.drawHoveringText(Collections.singletonList(text), x, y, fontRendererObj); - } - public void drawHoveringText(List textLines, int x, int y) { super.drawHoveringText(textLines, x, y, fontRendererObj); } - public int getXSize() { - return xSize; - } - - public int getYSize() { - return ySize; - } }