Clean up Tool Station GUI code (#249)

This commit is contained in:
Nikolay S. authored and GitHub committed 2026-02-03 15:17:41 +01:00
1 parent 79284a6588
commit 03968731d9
7 files changed
+65 -163

No files matched your search

@@ -23,7 +23,6 @@ import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.init.Blocks;
import net.minecraft.init.Items;
import net.minecraft.item.ItemStack;
import net.minecraft.util.StatCollector;
import net.minecraft.world.World;
import net.minecraftforge.client.MinecraftForgeClient;
import net.minecraftforge.client.event.sound.SoundLoadEvent;
@@ -762,7 +761,7 @@ public class ToolProxyClient extends ToolProxyCommon {
0,
new int[] { 0, 1, 2, 13 },
new int[] { 13, 13, 13, 13 },
StatCollector.translateToLocal("gui.toolforge1"),
"gui.toolforge1",
"gui.toolforge2");
// tier 1 tools
@@ -774,7 +773,7 @@ public class ToolProxyClient extends ToolProxyCommon {
itemIconsT1[i][2],
iconCoordsT1[i * 2],
iconCoordsT1[i * 2 + 1],
tier1Tools[i].getLocalizedToolName(),
tier1Tools[i].getUnlocalizedToolName(),
locString);
}
@@ -787,7 +786,7 @@ public class ToolProxyClient extends ToolProxyCommon {
itemIconsT2[i][2],
iconCoordsT2[i * 2],
iconCoordsT2[i * 2 + 1],
tier2Tools[i].getLocalizedToolName(),
tier2Tools[i].getUnlocalizedToolName(),
locString);
}
}
@@ -1,8 +1,6 @@
package tconstruct.tools.gui;
import net.minecraft.client.gui.GuiButton;
import net.minecraft.entity.player.InventoryPlayer;
import net.minecraft.util.StatCollector;
import net.minecraft.world.World;
import cpw.mods.fml.relauncher.Side;
@@ -14,18 +12,13 @@ import tconstruct.tools.logic.ToolForgeLogic;
@SideOnly(Side.CLIENT)
public class ToolForgeGui extends ToolStationGui {
int selectedButton;
public ToolForgeGui(InventoryPlayer inventoryplayer, ToolForgeLogic stationlogic, World world, int x, int y,
int z) {
super(inventoryplayer, stationlogic, world, x, y, z);
}
@Override
public void initGui() {
super.initGui();
this.buttonList.clear();
protected void createToolButtons() {
ToolGuiElement repair = TConstructClientRegistry.toolButtons.get(0);
GuiButtonTool repairButton = new GuiButtonTool(
0,
@@ -70,40 +63,13 @@ public class ToolForgeGui extends ToolStationGui {
}
@Override
protected void actionPerformed(GuiButton button) {
GuiButtonTool b = (GuiButtonTool) button;
((GuiButton) this.buttonList.get(selectedButton)).enabled = true;
selectedButton = button.id;
button.enabled = false;
setSlotType(b.element.slotType);
iconX = b.element.iconsX;
iconY = b.element.iconsY;
title = "\u00A7n" + b.element.title;
body = StatCollector.translateToLocal(b.element.body);
if (body != null) {
int i;
// for some really weird reason replaceAll doesn't find "\\n", but indexOf does. We have to replace
// manually.
while ((i = body.indexOf("\\n")) >= 0) {
body = body.substring(0, i) + '\n' + body.substring(i + 2);
}
}
}
@Override
void resetGui() {
this.text.setText("");
selectedButton = 0;
setSlotType(0);
protected void setIconUVs() {
iconX = new int[] { 0, 1, 2, 13 };
iconY = new int[] { 13, 13, 13, 13 };
title = "\u00A7n" + StatCollector.translateToLocal("gui.toolforge1");
body = StatCollector.translateToLocal("gui.toolforge2");
}
@Override
void setSlotType(int type) {
protected void setSlotType(int type) {
switch (type) {
case 0:
slotX = new int[] { 56, 38, 38, 14 }; // Repair
@@ -8,6 +8,7 @@ import net.minecraft.client.gui.GuiTextField;
import net.minecraft.client.gui.inventory.GuiContainer;
import net.minecraft.entity.player.InventoryPlayer;
import net.minecraft.item.ItemStack;
import net.minecraft.util.EnumChatFormatting;
import net.minecraft.util.ResourceLocation;
import net.minecraft.util.StatCollector;
import net.minecraft.world.World;
@@ -35,10 +36,8 @@ public class ToolStationGui extends GuiContainer implements INEIGuiHandler {
public ToolStationLogic logic;
public ToolStationContainer toolSlots;
public GuiTextField text;
public String toolName;
public int guiType;
public int selectedButton;
public int[] slotX, slotY, iconX, iconY;
public boolean active;
public String title, body = "";
public ToolStationGui(InventoryPlayer inventoryplayer, ToolStationLogic stationlogic, World world, int x, int y,
@@ -46,38 +45,23 @@ public class ToolStationGui extends GuiContainer implements INEIGuiHandler {
super(stationlogic.getGuiContainer(inventoryplayer, world, x, y, z));
this.logic = stationlogic;
toolSlots = (ToolStationContainer) inventorySlots;
text = new GuiTextField(this.fontRendererObj, 83, 8, 30, 12);
this.text.setMaxStringLength(40);
this.text.setEnableBackgroundDrawing(false);
this.text.setVisible(true);
this.text.setCanLoseFocus(false);
this.text.setFocused(true);
this.text.setTextColor(0xffffff);
toolName = "";
resetGui();
selectedButton = 0;
setSlotType(0);
setIconUVs();
title = EnumChatFormatting.UNDERLINE + StatCollector.translateToLocal("gui.toolforge1");
body = StatCollector.translateToLocal("gui.toolforge2");
Keyboard.enableRepeatEvents(true);
}
@Override
protected void mouseClicked(int mouseX, int mouseY, int mouseButton) {
super.mouseClicked(mouseX, mouseY, mouseButton);
if (mouseButton == 0) {
int gLeft = this.guiLeft + 68 + 110;
int gTop = this.guiTop + 6;
int gwidth = 102;
int gheight = 12;
active = mouseX > gLeft && mouseX < gLeft + gwidth && mouseY > gTop && mouseY < gTop + gheight;
}
this.text.mouseClicked(mouseX - this.guiLeft, mouseY - this.guiTop, mouseButton);
}
void resetGui() {
this.text.setText("");
guiType = 0;
setSlotType(0);
protected void setIconUVs() {
iconX = new int[] { 0, 1, 2 };
iconY = new int[] { 13, 13, 13 };
title = "\u00A7n" + StatCollector.translateToLocal("gui.toolforge1");
body = StatCollector.translateToLocal("gui.toolforge2");
}
@Override
@@ -86,21 +70,22 @@ public class ToolStationGui extends GuiContainer implements INEIGuiHandler {
this.xSize = 176 + 110;
this.guiLeft = (this.width - 176) / 2 - 110;
this.buttonList.clear();
ToolGuiElement repair = TConstructClientRegistry.toolButtons.get(0);
GuiButtonTool repairButton = new GuiButtonTool(
0,
this.guiLeft,
this.guiTop,
repair.buttonIconX,
repair.buttonIconY,
repair.domain,
repair.texture,
repair); // Repair
repairButton.enabled = false;
this.buttonList.add(repairButton);
if (this.text == null) {
this.text = new GuiTextField(this.fontRendererObj, 70 + 110, 8, 102, 12);
this.text.setMaxStringLength(40);
this.text.setEnableBackgroundDrawing(false);
this.text.setVisible(true);
this.text.setCanLoseFocus(true);
this.text.setFocused(false);
this.text.setTextColor(0xffffff);
}
for (int iter = 1; iter < TConstructClientRegistry.toolButtons.size(); iter++) {
this.buttonList.clear();
createToolButtons();
}
protected void createToolButtons() {
for (int iter = 0; iter < TConstructClientRegistry.toolButtons.size(); iter++) {
ToolGuiElement element = TConstructClientRegistry.toolButtons.get(iter);
GuiButtonTool button = new GuiButtonTool(
iter,
@@ -113,31 +98,24 @@ public class ToolStationGui extends GuiContainer implements INEIGuiHandler {
element);
this.buttonList.add(button);
}
this.buttonList.get(0).enabled = false;
}
@Override
protected void actionPerformed(GuiButton button) {
((GuiButton) this.buttonList.get(guiType)).enabled = true;
guiType = button.id;
this.buttonList.get(selectedButton).enabled = true;
selectedButton = button.id;
button.enabled = false;
ToolGuiElement element = TConstructClientRegistry.toolButtons.get(guiType);
ToolGuiElement element = ((GuiButtonTool) button).element;
setSlotType(element.slotType);
iconX = element.iconsX;
iconY = element.iconsY;
title = "\u00A7n" + element.title;
body = StatCollector.translateToLocal(element.body);
if (body != null) {
int i;
// for some really weird reason replaceAll doesn't find "\\n", but indexOf does. We have to replace
// manually.
while ((i = body.indexOf("\\n")) >= 0) {
body = body.substring(0, i) + '\n' + body.substring(i + 2);
}
}
title = "§n" + StatCollector.translateToLocal(element.title);
body = StatCollector.translateToLocal(element.body).replaceAll("\\\\n", "\n");
}
void setSlotType(int type) {
protected void setSlotType(int type) {
switch (type) {
case 0:
slotX = new int[] { 56, 38, 38 }; // Repair
@@ -177,10 +155,13 @@ public class ToolStationGui extends GuiContainer implements INEIGuiHandler {
this.fontRendererObj.drawString(StatCollector.translateToLocal(logic.getInvName()), 116, 8, 0x000000);
this.fontRendererObj
.drawString(StatCollector.translateToLocal("container.inventory"), 118, this.ySize - 96 + 2, 0x000000);
this.fontRendererObj.drawString(toolName + "_", 180, 8, 0xffffff);
this.text.drawTextBox();
if (logic.isStackInSlot(0)) ToolStationGuiHelper.drawToolStats(logic.getStackInSlot(0), 294, 0);
else drawToolInformation();
if (logic.isStackInSlot(0)) {
ToolStationGuiHelper.drawToolStats(logic.getStackInSlot(0), 294, 0);
} else {
drawToolInformation();
}
}
protected void drawToolInformation() {
@@ -203,7 +184,7 @@ public class ToolStationGui extends GuiContainer implements INEIGuiHandler {
final int cornerX = this.guiLeft + 110;
this.drawTexturedModalRect(cornerX, this.guiTop, 0, 0, 176, this.ySize);
if (active) {
if (this.text.isFocused()) {
this.drawTexturedModalRect(cornerX + 62, this.guiTop, 0, this.ySize, 112, 22);
}
@@ -231,39 +212,23 @@ public class ToolStationGui extends GuiContainer implements INEIGuiHandler {
}
@Override
protected void keyTyped(char par1, int keyCode) {
if (keyCode == 1 || (!active && keyCode == this.mc.gameSettings.keyBindInventory.getKeyCode())) {
protected void keyTyped(char typedChar, int keyCode) {
if (keyCode == 1 || (!this.text.isFocused() && keyCode == this.mc.gameSettings.keyBindInventory.getKeyCode())) {
logic.setToolname("");
updateServer("");
Keyboard.enableRepeatEvents(false);
this.mc.thePlayer.closeScreen();
} else if (active) {
text.textboxKeyTyped(par1, keyCode);
toolName = text.getText().trim();
} else if (text.textboxKeyTyped(typedChar, keyCode)) {
final String toolName = text.getText().trim();
logic.setToolname(toolName);
updateServer(toolName);
}
}
void updateServer(String name) {
/*
* ByteArrayOutputStream bos = new ByteArrayOutputStream(8); DataOutputStream outputStream = new
* DataOutputStream(bos); try { outputStream.writeByte(1);
* outputStream.writeInt(logic.getWorld().provider.dimensionId); outputStream.writeInt(logic.xCoord);
* outputStream.writeInt(logic.yCoord); outputStream.writeInt(logic.zCoord); outputStream.writeUTF(name); }
* catch (Exception ex) { ex.printStackTrace(); } Packet250CustomPayload packet = new Packet250CustomPayload();
* packet.channel = "TConstruct"; packet.data = bos.toByteArray(); packet.length = bos.size();
* PacketDispatcher.sendPacketToServer(packet);
*/
private void updateServer(String name) {
TConstruct.packetPipeline.sendToServer(new ToolStationPacket(logic.xCoord, logic.yCoord, logic.zCoord, name));
}
/*
* protected void mouseClicked(int par1, int par2, int par3) { super.mouseClicked(par1, par2, par3);
* text.mouseClicked(par1, par2, par3); }
*/
@Override
public VisiblityData modifyVisiblity(GuiContainer gui, VisiblityData currentVisibility) {
currentVisibility.showWidgets = width - xSize >= 107;
@@ -24,41 +24,41 @@ public class CreativeModifier extends Item {
this.setCreativeTab(TConstructRegistry.materialTab);
}
@SideOnly(Side.CLIENT)
/**
* returns a list of items with the same ID, but different meta (eg: dye returns 16 items)
*/
@Override
public void getSubItems(Item par1, CreativeTabs par2CreativeTabs, List<ItemStack> par3List) {
par3List.add(new ItemStack(par1, 1, 0));
@SideOnly(Side.CLIENT)
public void getSubItems(Item item1, CreativeTabs creativeTabs, List<ItemStack> list) {
list.add(new ItemStack(item1, 1, 0));
for (ToolRecipe recipe : ToolBuilder.instance.combos) {
ToolCore tool = recipe.getType();
ItemStack item = new ItemStack(par1, 1, 0);
ItemStack item = new ItemStack(item1, 1, 0);
NBTTagCompound compound = new NBTTagCompound();
compound.setString("TargetLock", tool.getToolName());
item.setTagCompound(compound);
par3List.add(item);
list.add(item);
}
}
@SideOnly(Side.CLIENT)
@Override
@SideOnly(Side.CLIENT)
public void registerIcons(IIconRegister iconRegister) {
this.itemIcon = iconRegister.registerIcon("tinker:skull_char_gold");
}
@Override
@SideOnly(Side.CLIENT)
public void addInformation(ItemStack stack, EntityPlayer player, List list, boolean par4) {
public void addInformation(ItemStack stack, EntityPlayer player, List<String> list, boolean adv) {
list.add(
StatCollector.translateToLocal("modifier.tooltip.Main") + " "
+ StatCollector.translateToLocal("modifier.tooltip.Creative"));
if (stack.hasTagCompound()) {
String targetLock;
targetLock = stack.getTagCompound().getString("TargetLock");
targetLock = StatCollector.translateToLocal("infitool." + targetLock.toLowerCase());
targetLock = StatCollector.translateToLocal("tool." + targetLock.toLowerCase());
list.add(StatCollector.translateToLocal("creativeModLock.tooltip") + targetLock);
}
}