diff --git a/gradle.properties b/gradle.properties index fd9f5cb..41b283b 100644 --- a/gradle.properties +++ b/gradle.properties @@ -10,6 +10,6 @@ loader_version=0.14.19-babric.1-bta # halplibe_version=2.3.0 # Mod -mod_version=0.2.0 +mod_version=0.3.0 mod_group=rootenginear mod_name=sortchest diff --git a/src/main/java/rootenginear/sortchest/SortChest.java b/src/main/java/rootenginear/sortchest/SortChest.java index 10c48cd..552e6f3 100644 --- a/src/main/java/rootenginear/sortchest/SortChest.java +++ b/src/main/java/rootenginear/sortchest/SortChest.java @@ -5,11 +5,11 @@ import org.slf4j.LoggerFactory; public class SortChest implements ModInitializer { - public static final String MOD_ID = "sortchest"; - public static final Logger LOGGER = LoggerFactory.getLogger(MOD_ID); + public static final String MOD_ID = "sortchest"; + public static final Logger LOGGER = LoggerFactory.getLogger(MOD_ID); - @Override - public void onInitialize() { - LOGGER.info("Sort Chest initialized."); - } + @Override + public void onInitialize() { + LOGGER.info("Sort Chest initialized."); + } } diff --git a/src/main/java/rootenginear/sortchest/gui/GuiSortChestButton.java b/src/main/java/rootenginear/sortchest/gui/GuiSortChestButton.java new file mode 100644 index 0000000..4686237 --- /dev/null +++ b/src/main/java/rootenginear/sortchest/gui/GuiSortChestButton.java @@ -0,0 +1,52 @@ +package rootenginear.sortchest.gui; + +import net.minecraft.client.Minecraft; +import net.minecraft.client.gui.GuiButton; +import net.minecraft.client.render.FontRenderer; +import org.lwjgl.opengl.GL11; + +public class GuiSortChestButton extends GuiButton { + private final int leftPadding; + private final String tooltipText; + + public GuiSortChestButton(int id, int xPosition, int yPosition, int width, int height, String text, int leftPadding, String tooltipText) { + super(id, xPosition, yPosition, width, height, text); + this.leftPadding = leftPadding; + this.tooltipText = tooltipText; + } + + @Override + public void drawButton(Minecraft minecraft, int i, int j) { + if (!this.visible) { + return; + } + FontRenderer fontrenderer = minecraft.fontRenderer; + GL11.glBindTexture(3553, minecraft.renderEngine.getTexture("/gui/gui.png")); + GL11.glColor4f(1.0f, 1.0f, 1.0f, 1.0f); + boolean flag = i >= this.xPosition && j >= this.yPosition && i < this.xPosition + this.width && j < this.yPosition + this.height; + int k = this.getButtonState(flag); + + // Edited — Draw from 4 corners + this.drawTexturedModalRect(this.xPosition, this.yPosition, 0, 46 + k * 20, this.width / 2, this.height / 2); + this.drawTexturedModalRect(this.xPosition + this.width / 2, this.yPosition, 200 - this.width / 2, 46 + k * 20, this.width / 2, this.height / 2); + this.drawTexturedModalRect(this.xPosition, this.yPosition + this.height / 2, 0, (46 + (k + 1) * 20) - this.height / 2, this.width / 2, this.height / 2); + this.drawTexturedModalRect(this.xPosition + this.width / 2, this.yPosition + this.height / 2, 200 - this.width / 2, (46 + (k + 1) * 20) - this.height / 2, this.width / 2, this.height / 2); + // End + + this.mouseDragged(minecraft, i, j); + + // Edited — Using `leftPadding` on x position + if (!this.enabled) { + this.drawStringCentered(fontrenderer, this.displayString, this.xPosition + this.leftPadding, this.yPosition + (this.height - 8) / 2, -6250336); + } else if (flag) { + this.drawStringCentered(fontrenderer, this.displayString, this.xPosition + this.leftPadding, this.yPosition + (this.height - 8) / 2, 0xFFFFA0); + } else { + this.drawStringCentered(fontrenderer, this.displayString, this.xPosition + this.leftPadding, this.yPosition + (this.height - 8) / 2, 0xE0E0E0); + } + // End + } + + public String getTooltipText() { + return this.tooltipText; + } +} diff --git a/src/main/java/rootenginear/sortchest/mixin/GuiChestMixin.java b/src/main/java/rootenginear/sortchest/mixin/GuiChestMixin.java deleted file mode 100644 index 864b878..0000000 --- a/src/main/java/rootenginear/sortchest/mixin/GuiChestMixin.java +++ /dev/null @@ -1,23 +0,0 @@ -package rootenginear.sortchest.mixin; - -import net.minecraft.client.gui.GuiChest; -import net.minecraft.client.gui.GuiContainer; -import org.spongepowered.asm.mixin.Mixin; -import org.spongepowered.asm.mixin.injection.At; -import org.spongepowered.asm.mixin.injection.Inject; -import org.spongepowered.asm.mixin.injection.callback.CallbackInfo; -import rootenginear.sortchest.mixin.accessor.GuiScreenAccessor; - -@Mixin(value = {GuiChest.class}, remap = false) -public class GuiChestMixin { - @Inject(method = "drawGuiContainerForegroundLayer", at = @At("TAIL")) - private void writeInstruction(CallbackInfo ci) { - GuiScreenAccessor screenThis = (GuiScreenAccessor) this; - GuiContainer containerThis = (GuiContainer) (Object) this; - screenThis.getFontRenderer().drawString("S: Sort ⇵, F: Fill ⊼", containerThis.xSize - 8 - 88 + 2, 6, 0x404040); - screenThis.getFontRenderer().drawString("D: Dump ⊻", containerThis.xSize - 8 - 48 + 2, containerThis.ySize - 96 + 2, 0x404040); -// screenThis.getFontRenderer().drawStringWithShadow("S: Sort ⇵", containerThis.xSize + 4, 6, 0xffffff); -// screenThis.getFontRenderer().drawStringWithShadow("F: Fill ⊼", containerThis.xSize + 4, 6 + 12, 0xffffff); -// screenThis.getFontRenderer().drawStringWithShadow("D: Dump ⊻", containerThis.xSize + 4, containerThis.ySize - 96 + 2, 0xffffff); - } -} diff --git a/src/main/java/rootenginear/sortchest/mixin/GuiContainerMixin.java b/src/main/java/rootenginear/sortchest/mixin/GuiContainerMixin.java index df2f683..f0e1afa 100644 --- a/src/main/java/rootenginear/sortchest/mixin/GuiContainerMixin.java +++ b/src/main/java/rootenginear/sortchest/mixin/GuiContainerMixin.java @@ -1,8 +1,8 @@ package rootenginear.sortchest.mixin; import net.minecraft.client.Minecraft; -import net.minecraft.client.gui.GuiChest; import net.minecraft.client.gui.GuiContainer; +import net.minecraft.client.gui.GuiScreen; import net.minecraft.client.player.controller.PlayerController; import net.minecraft.core.InventoryAction; import net.minecraft.core.entity.player.EntityPlayer; @@ -14,8 +14,11 @@ import org.spongepowered.asm.mixin.injection.At; import org.spongepowered.asm.mixin.injection.Inject; import org.spongepowered.asm.mixin.injection.callback.CallbackInfo; +import rootenginear.sortchest.gui.GuiSortChestButton; import rootenginear.sortchest.mixin.accessor.GuiChestAccessor; +import rootenginear.sortchest.mixin.accessor.GuiContainerAccessor; import rootenginear.sortchest.mixin.accessor.GuiScreenAccessor; +import rootenginear.sortchest.utils.Utils; import java.util.ArrayList; import java.util.Comparator; @@ -26,14 +29,15 @@ public class GuiContainerMixin { @Inject(method = "keyTyped", at = @At("HEAD"), cancellable = true) private void doSort(char c, int _i, int mouseX, int mouseY, CallbackInfo ci) { - GuiContainer containerThis = (GuiContainer) (Object) this; - if (!(containerThis instanceof GuiChest)) return; + if (Utils.isNotChest(this)) return; char key = Character.toLowerCase(c); if (key != 's' && key != 'd' && key != 'f') return; - GuiScreenAccessor screenThis = (GuiScreenAccessor) containerThis; - Minecraft mc = screenThis.getMc(); + GuiContainer containerThis = (GuiContainer) (Object) this; + GuiScreenAccessor screenAccessorThis = (GuiScreenAccessor) this; + + Minecraft mc = screenAccessorThis.getMc(); PlayerController playerController = mc.playerController; EntityPlayer entityPlayer = mc.thePlayer; Container inventorySlots = containerThis.inventorySlots; @@ -57,25 +61,6 @@ private void doSort(char c, int _i, int mouseX, int mouseY, CallbackInfo ci) { return; } - // "*S" - Shuffle -// boolean shiftOrCtrlPressed = Keyboard.isKeyDown(42) || Keyboard.isKeyDown(54) || Keyboard.isKeyDown(29) || Keyboard.isKeyDown(157); -// if (shiftOrCtrlPressed) { -// Random rand = new Random(); -// -// playerController.doInventoryAction(windowId, InventoryAction.CLICK_LEFT, new int[]{0, 0}, entityPlayer); -// for (int i = 0; i < 100; i++) { -// if (rand.nextInt(2) == 0) { -// playerController.doInventoryAction(windowId, InventoryAction.CLICK_LEFT, new int[]{rand.nextInt(countInvSlots - 1) + 1, 0}, entityPlayer); -// } else { -// playerController.doInventoryAction(windowId, InventoryAction.CLICK_RIGHT, new int[]{rand.nextInt(countInvSlots - 1) + 1, 0}, entityPlayer); -// } -// } -// playerController.doInventoryAction(windowId, InventoryAction.CLICK_LEFT, new int[]{0, 0}, entityPlayer); -// -// ci.cancel(); -// return; -// } - // "S" - Sort mergeItemsInChest(playerController, entityPlayer, windowId, countInvSlots, inventorySlots); sortItemsInChest(playerController, entityPlayer, windowId, countInvSlots, inventorySlots); @@ -177,16 +162,31 @@ private void dumpItemToChest(PlayerController playerController, EntityPlayer ent playerController.doInventoryAction(windowId, InventoryAction.MOVE_ALL, new int[]{countInvSlots + (9 * 3), 0}, entityPlayer); } -// @Inject(method = "initGui", at = @At("TAIL")) -// private void addChestButtons(CallbackInfo ci) { -// GuiContainer containerThis = (GuiContainer) (Object) this; -// if (!(containerThis instanceof GuiChest)) return; -// -// GuiScreen screenThis = (GuiScreen) (Object) this; -// screenThis.controlList.clear(); -// int centerX = (screenThis.width - containerThis.xSize) / 2; -// int centerY = (screenThis.height - containerThis.ySize) / 2; -// screenThis.controlList.add(new GuiButton(0, centerX + containerThis.xSize - 8 - 12 - 12 - 4, centerY + 4, 12, 12, "⇵")); -// screenThis.controlList.add(new GuiButton(1, centerX + containerThis.xSize - 8 - 12, centerY + 4, 12, 12, "⊼")); -// } + @Inject(method = "initGui", at = @At("TAIL")) + private void addChestButtons(CallbackInfo ci) { + if (Utils.isNotChest(this)) return; + + GuiContainer containerThis = (GuiContainer) (Object) this; + GuiScreen screenThis = (GuiScreen) (Object) this; + + screenThis.controlList.clear(); + int centerX = (screenThis.width - containerThis.xSize) / 2; + int centerY = (screenThis.height - containerThis.ySize) / 2; + screenThis.controlList.add(new GuiSortChestButton(0, centerX + containerThis.xSize - 8 - 12 - 12 - 2, centerY + 4, 12, 12, "⇵", 3, "Sort [S]")); + screenThis.controlList.add(new GuiSortChestButton(1, centerX + containerThis.xSize - 8 - 12, centerY + 4, 12, 12, "⊼", 3, "Fill [F]")); + screenThis.controlList.add(new GuiSortChestButton(2, centerX + containerThis.xSize - 8 - 12, centerY + containerThis.ySize - 96 - 1, 12, 12, "⊻", 3, "Dump [D]")); + } + + @Inject(method = "drawScreen", at = @At(value = "INVOKE", target = "Lorg/lwjgl/opengl/GL11;glEnable(I)V")) + private void renderChestButtonTooltip(int x, int y, float renderPartialTicks, CallbackInfo ci) { + if (Utils.isNotChest(this)) return; + + GuiScreen screenThis = (GuiScreen) (Object) this; + for (int i = 0; i < screenThis.controlList.size(); ++i) { + GuiSortChestButton button = (GuiSortChestButton) screenThis.controlList.get(i); + if (!button.isHovered(x, y)) continue; + GuiContainerAccessor containerAccessorThis = (GuiContainerAccessor) this; + containerAccessorThis.getGuiTooltip().render(button.getTooltipText(), x, y, 8, -8); + } + } } diff --git a/src/main/java/rootenginear/sortchest/mixin/GuiScreenMixin.java b/src/main/java/rootenginear/sortchest/mixin/GuiScreenMixin.java new file mode 100644 index 0000000..52a27d2 --- /dev/null +++ b/src/main/java/rootenginear/sortchest/mixin/GuiScreenMixin.java @@ -0,0 +1,37 @@ +package rootenginear.sortchest.mixin; + +import net.minecraft.client.gui.GuiButton; +import net.minecraft.client.gui.GuiScreen; +import org.spongepowered.asm.mixin.Mixin; +import org.spongepowered.asm.mixin.injection.At; +import org.spongepowered.asm.mixin.injection.Inject; +import org.spongepowered.asm.mixin.injection.callback.CallbackInfo; +import rootenginear.sortchest.utils.Utils; + +@Mixin(value = {GuiScreen.class}, remap = false) +public class GuiScreenMixin { + @Inject(method = "buttonPressed", at = @At(value = "HEAD"), cancellable = true) + private void chestButtonsAction(GuiButton guibutton, CallbackInfo ci) { + if (Utils.isNotChest(this)) return; + + if (!guibutton.enabled) { + ci.cancel(); + return; + } + + GuiScreen screenThis = (GuiScreen) (Object) this; + switch (guibutton.id) { + case 0: + screenThis.keyTyped('s', 0, 0, 0); + ci.cancel(); + return; + case 1: + screenThis.keyTyped('f', 0, 0, 0); + ci.cancel(); + return; + case 2: + screenThis.keyTyped('d', 0, 0, 0); + ci.cancel(); + } + } +} diff --git a/src/main/java/rootenginear/sortchest/mixin/accessor/GuiContainerAccessor.java b/src/main/java/rootenginear/sortchest/mixin/accessor/GuiContainerAccessor.java new file mode 100644 index 0000000..a714163 --- /dev/null +++ b/src/main/java/rootenginear/sortchest/mixin/accessor/GuiContainerAccessor.java @@ -0,0 +1,12 @@ +package rootenginear.sortchest.mixin.accessor; + +import net.minecraft.client.gui.GuiContainer; +import net.minecraft.client.gui.GuiTooltip; +import org.spongepowered.asm.mixin.Mixin; +import org.spongepowered.asm.mixin.gen.Accessor; + +@Mixin(value = {GuiContainer.class}, remap = false) +public interface GuiContainerAccessor { + @Accessor + GuiTooltip getGuiTooltip(); +} diff --git a/src/main/java/rootenginear/sortchest/mixin/accessor/GuiScreenAccessor.java b/src/main/java/rootenginear/sortchest/mixin/accessor/GuiScreenAccessor.java index 2216104..371bbd2 100644 --- a/src/main/java/rootenginear/sortchest/mixin/accessor/GuiScreenAccessor.java +++ b/src/main/java/rootenginear/sortchest/mixin/accessor/GuiScreenAccessor.java @@ -2,7 +2,6 @@ import net.minecraft.client.Minecraft; import net.minecraft.client.gui.GuiScreen; -import net.minecraft.client.render.FontRenderer; import org.spongepowered.asm.mixin.Mixin; import org.spongepowered.asm.mixin.gen.Accessor; @@ -10,7 +9,4 @@ public interface GuiScreenAccessor { @Accessor Minecraft getMc(); - - @Accessor - FontRenderer getFontRenderer(); } diff --git a/src/main/java/rootenginear/sortchest/utils/Utils.java b/src/main/java/rootenginear/sortchest/utils/Utils.java new file mode 100644 index 0000000..89c6354 --- /dev/null +++ b/src/main/java/rootenginear/sortchest/utils/Utils.java @@ -0,0 +1,9 @@ +package rootenginear.sortchest.utils; + +import net.minecraft.client.gui.GuiChest; + +public class Utils { + public static boolean isNotChest(Object object) { + return !(object instanceof GuiChest); + } +} diff --git a/src/main/resources/sortchest.mixins.json b/src/main/resources/sortchest.mixins.json index 9552485..a420af8 100644 --- a/src/main/resources/sortchest.mixins.json +++ b/src/main/resources/sortchest.mixins.json @@ -4,9 +4,10 @@ "package": "rootenginear.sortchest.mixin", "compatibilityLevel": "JAVA_8", "client": [ - "GuiChestMixin", "GuiContainerMixin", + "GuiScreenMixin", "accessor.GuiChestAccessor", + "accessor.GuiContainerAccessor", "accessor.GuiScreenAccessor" ], "injectors": {