Skip to content

Commit

Permalink
feat: add buttons
Browse files Browse the repository at this point in the history
  • Loading branch information
rootEnginear committed Nov 9, 2023
1 parent df11eb0 commit c40bb1e
Show file tree
Hide file tree
Showing 10 changed files with 155 additions and 71 deletions.
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -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
12 changes: 6 additions & 6 deletions src/main/java/rootenginear/sortchest/SortChest.java
Original file line number Diff line number Diff line change
Expand Up @@ -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.");
}
}
52 changes: 52 additions & 0 deletions src/main/java/rootenginear/sortchest/gui/GuiSortChestButton.java
Original file line number Diff line number Diff line change
@@ -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;
}
}
23 changes: 0 additions & 23 deletions src/main/java/rootenginear/sortchest/mixin/GuiChestMixin.java

This file was deleted.

72 changes: 36 additions & 36 deletions src/main/java/rootenginear/sortchest/mixin/GuiContainerMixin.java
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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;
Expand All @@ -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;
Expand All @@ -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);
Expand Down Expand Up @@ -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);
}
}
}
37 changes: 37 additions & 0 deletions src/main/java/rootenginear/sortchest/mixin/GuiScreenMixin.java
Original file line number Diff line number Diff line change
@@ -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();
}
}
}
Original file line number Diff line number Diff line change
@@ -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();
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,11 @@

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;

@Mixin(value = {GuiScreen.class}, remap = false)
public interface GuiScreenAccessor {
@Accessor
Minecraft getMc();

@Accessor
FontRenderer getFontRenderer();
}
9 changes: 9 additions & 0 deletions src/main/java/rootenginear/sortchest/utils/Utils.java
Original file line number Diff line number Diff line change
@@ -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);
}
}
3 changes: 2 additions & 1 deletion src/main/resources/sortchest.mixins.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,10 @@
"package": "rootenginear.sortchest.mixin",
"compatibilityLevel": "JAVA_8",
"client": [
"GuiChestMixin",
"GuiContainerMixin",
"GuiScreenMixin",
"accessor.GuiChestAccessor",
"accessor.GuiContainerAccessor",
"accessor.GuiScreenAccessor"
],
"injectors": {
Expand Down

0 comments on commit c40bb1e

Please sign in to comment.