diff --git a/src/main/java/com/pikachu/uhcessentials/gui/base/Window.java b/src/main/java/com/pikachu/uhcessentials/gui/base/Window.java index b5f9539..a47397b 100644 --- a/src/main/java/com/pikachu/uhcessentials/gui/base/Window.java +++ b/src/main/java/com/pikachu/uhcessentials/gui/base/Window.java @@ -45,7 +45,6 @@ public Window(String name, int defaultX, int defaultY) { private boolean clicked; protected FontRenderer fontRenderer = Minecraft.getMinecraft().fontRendererObj; - public int getX() { return x; } @@ -75,6 +74,18 @@ public void reset() { setY(defaultY); } + public void save(boolean alsoSaveConfig) { + Main.getConfig().get(getName(), "x", getDefaultX(), + "Controls the x coordinate of the " + getName() + " window").set(getX()); + Main.getConfig().get(getName(), "y", getDefaultY(), + "Controls the y coordinate of the " + getName() + " window").set(getY()); + Main.getConfig().get(getName(), "enabled", true, + "Controls whether or not the " + getName() + " window is enabled").set(isEnabled()); + if (alsoSaveConfig) { + Main.getConfig().save(); + } + } + public int getDefaultY() { return defaultY; } diff --git a/src/main/java/com/pikachu/uhcessentials/gui/screens/CoordinatesScreen.java b/src/main/java/com/pikachu/uhcessentials/gui/screens/CoordinatesScreen.java index 4c6ed00..6dfdf19 100644 --- a/src/main/java/com/pikachu/uhcessentials/gui/screens/CoordinatesScreen.java +++ b/src/main/java/com/pikachu/uhcessentials/gui/screens/CoordinatesScreen.java @@ -3,9 +3,12 @@ import com.pikachu.uhcessentials.hotkeys.Hotkey; import com.pikachu.uhcessentials.hotkeys.HotkeyStore; import com.pikachu.uhcessentials.utils.Callback; +import com.pikachu.uhcessentials.utils.Util; import net.minecraft.client.Minecraft; +import net.minecraft.client.gui.GuiButton; import net.minecraft.client.gui.GuiScreen; import net.minecraft.client.gui.GuiTextField; +import net.minecraft.client.resources.I18n; import net.minecraft.client.settings.KeyBinding; import net.minecraft.util.BlockPos; import org.lwjgl.input.Keyboard; @@ -18,10 +21,15 @@ public class CoordinatesScreen extends GuiScreen { private GuiTextField zInput; private Callback callback; private CoordinatesScreen instance = this; + private String title = I18n.format("uhcessentials.coordinatesmenutitle"); + private String error = I18n.format("uhcessentials.invalidcoordinates"); + private String okButtonLabel = I18n.format("uhcessentials.savecoordinateslabel"); + private double xValue; + private double zValue; + private GuiButton okButton; public CoordinatesScreen(Callback callback) { this.callback = callback; - this.instance = this; HotkeyStore.add(new Hotkey(this) { @Override public void onPress() { @@ -35,30 +43,103 @@ public KeyBinding getKeyBinding() { }); } + // to be called only after checking coordinatesAreValid + public BlockPos getNewTarget() { + return new BlockPos(xValue, 0, zValue); + } + + public boolean coordinatesAreValid() { + String x = xInput.getText(); + String z = zInput.getText(); + if (Util.endsWithAny(x, "D", "d", "F", "f") || Util.endsWithAny(z, "D", "d", "F", "f")) { + return false; + } + try { + xValue = Double.parseDouble(x); + zValue = Double.parseDouble(z); + return true; + } catch (NumberFormatException e) { + return false; + } + } + + @Override + public void actionPerformed(GuiButton button) throws IOException { + if (button.id == 0 && coordinatesAreValid()) { + close(getNewTarget()); + } + } + + @Override public void initGui() { super.initGui(); - xInput = new GuiTextField(0, fontRendererObj, mc.displayHeight / 2, mc.displayWidth / 4, 100, 20); + okButton = new GuiButton(0, (width / 2) - 75, (height / 4) * 3, 150, 20, okButtonLabel); + buttonList.add(okButton); + okButton.visible = true; + okButton.enabled = false; + xInput = new GuiTextField(0, fontRendererObj, (width / 2) - 50, (height / 2) - (height / 8), 100, 20); xInput.setFocused(true); - xInput.setMaxStringLength(15); - zInput = new GuiTextField(0, fontRendererObj, mc.displayHeight / 2, (mc.displayWidth / 4) * 3, 100, 20); - zInput.setMaxStringLength(15); + xInput.setMaxStringLength(20); + zInput = new GuiTextField(0, fontRendererObj, (width / 2) - 50, (height / 2), 100, 20); + zInput.setMaxStringLength(20); } @Override public void drawScreen(int mouseX, int mouseY, float partialTicks) { - super.drawBackground(0); + super.drawDefaultBackground(); + this.drawCenteredString(fontRendererObj, title, width / 2, 15, Util.WHITE); + okButton.drawButton(mc, mouseX, mouseY); + okButton.enabled = coordinatesAreValid(); + if (!coordinatesAreValid() && !(xInput.getText().isEmpty() || zInput.getText().isEmpty())) { + drawCenteredString(fontRendererObj, error, width / 2, (height / 2) + (height / 8), Util.RED); + } xInput.drawTextBox(); + xInput.setEnabled(true); zInput.drawTextBox(); + zInput.setEnabled(true); + } + + @Override + public void mouseClicked(int mouseX, int mouseY, int mouseButton) throws IOException { + super.mouseClicked(mouseX, mouseY, mouseButton); + xInput.mouseClicked(mouseX, mouseY, mouseButton); + zInput.mouseClicked(mouseX, mouseY, mouseButton); + } + + @Override + public void updateScreen() { + xInput.updateCursorCounter(); + zInput.updateCursorCounter(); + } + + + public void close() { + close(null); + } + + public void close(BlockPos blockPos) { + mc.displayGuiScreen(null); + if (mc.currentScreen == null) { + mc.setIngameFocus(); + } + if (blockPos != null) { + callback.run(blockPos); + } } @Override public void keyTyped(char typedChar, int keyCode) throws IOException { if (keyCode == HotkeyStore.get(this).binding.getKeyCode()) { - mc.displayGuiScreen(null); - if (mc.currentScreen == null) { - mc.setIngameFocus(); - } + close(); + } else if (keyCode == Keyboard.KEY_TAB) { + xInput.setFocused(!xInput.isFocused()); + zInput.setFocused(!zInput.isFocused()); + } else if (keyCode == Keyboard.KEY_RETURN && coordinatesAreValid()) { + close(getNewTarget()); + } else { + xInput.textboxKeyTyped(typedChar, keyCode); + zInput.textboxKeyTyped(typedChar, keyCode); } super.keyTyped(typedChar, keyCode); } diff --git a/src/main/java/com/pikachu/uhcessentials/gui/screens/OptionScreen.java b/src/main/java/com/pikachu/uhcessentials/gui/screens/OptionScreen.java index 29dc511..c189156 100644 --- a/src/main/java/com/pikachu/uhcessentials/gui/screens/OptionScreen.java +++ b/src/main/java/com/pikachu/uhcessentials/gui/screens/OptionScreen.java @@ -48,12 +48,7 @@ public void mouseReleased(int mouseX, int mouseY, int state) { if (hovering(window, mouseX, mouseY) && lastButton == 1) { window.toggle(); } - Main.getConfig().get(window.getName(), "x", window.getDefaultX(), - "Controls the x coordinate of the " + window.getName() + " window").set(window.getX()); - Main.getConfig().get(window.getName(), "y", window.getDefaultY(), - "Controls the y coordinate of the " + window.getName() + " window").set(window.getY()); - Main.getConfig().get(window.getName(), "enabled", true, - "Controls whether or not the " + window.getName() + " window is enabled").set(window.isEnabled()); + window.save(false); window.setClicked(false); } if (Main.getConfig().hasChanged()) { diff --git a/src/main/java/com/pikachu/uhcessentials/gui/windows/ArrowCounter.java b/src/main/java/com/pikachu/uhcessentials/gui/windows/ArrowCounter.java index b9bd354..9a3e6b5 100644 --- a/src/main/java/com/pikachu/uhcessentials/gui/windows/ArrowCounter.java +++ b/src/main/java/com/pikachu/uhcessentials/gui/windows/ArrowCounter.java @@ -6,12 +6,10 @@ import net.minecraft.item.ItemStack; import net.minecraftforge.client.event.RenderGameOverlayEvent; -import java.awt.Color; - public class ArrowCounter extends MovableWindow { private ItemStack arrow = new ItemStack(Items.arrow, 1); - private final int LOW_INDICATOR = new Color(255, 85, 85).getRGB(); + private final int LOW_INDICATOR = Util.RED; private String amount; public ArrowCounter() { diff --git a/src/main/java/com/pikachu/uhcessentials/gui/windows/Coordinates.java b/src/main/java/com/pikachu/uhcessentials/gui/windows/Coordinates.java index 4dd23ec..b75708f 100644 --- a/src/main/java/com/pikachu/uhcessentials/gui/windows/Coordinates.java +++ b/src/main/java/com/pikachu/uhcessentials/gui/windows/Coordinates.java @@ -33,12 +33,13 @@ public void render(RenderGameOverlayEvent.Text event) { String y = String.valueOf(Util.trimmed(Math.floor(player.posY))); String z = String.valueOf(Util.trimmed(Math.floor(player.posZ))); longestString = Util.findLongestString(x, y, z); - double rotation = MathHelper.wrapAngleTo180_float(this.mc.thePlayer.rotationYaw); - int var25 = MathHelper.floor_double((this.mc.thePlayer.rotationYaw * 4.0F / 360.0F) + 0.5D) & 3; + double rotation = MathHelper.wrapAngleTo180_float(player.rotationYaw); String direction = ""; String headingZ = ""; String headingX = ""; + + // rotation calculations credit Sintinium (https://github.com/Sintinium/) if (rotation > -22.5 && rotation <= 22.5) { direction = "S"; headingZ = "+"; @@ -68,6 +69,7 @@ public void render(RenderGameOverlayEvent.Text event) { headingZ = "+"; headingX = "+"; } + // end rotation calculations Gui.drawRect(getX() - LEFT_PADDING, getY(), getX() + getWidth() + RIGHT_PADDING, getY() + getHeight(), getColor()); diff --git a/src/main/java/com/pikachu/uhcessentials/utils/Util.java b/src/main/java/com/pikachu/uhcessentials/utils/Util.java index 80a768a..cf00d76 100644 --- a/src/main/java/com/pikachu/uhcessentials/utils/Util.java +++ b/src/main/java/com/pikachu/uhcessentials/utils/Util.java @@ -22,6 +22,7 @@ private Util() { } public static final int WHITE = new Color(255, 255, 255).getRGB(); + public static final int RED = new Color(255, 85, 85).getRGB(); private static String workingDir = new File("").getAbsolutePath(); private static final DecimalFormat TRIM_FORMAT = new DecimalFormat("0"); @@ -93,4 +94,13 @@ public static String getRenderedEntityCount() { return mc.renderGlobal.getDebugInfoEntities().split("/")[0].split(" ")[1]; } + public static boolean endsWithAny(String check, String... potentialEnds) { + for (String potentialEnd : potentialEnds) { + if (check.endsWith(potentialEnd)) { + return true; + } + } + return false; + } + } diff --git a/src/main/resources/assets/uhcessentials/lang/en_US.lang b/src/main/resources/assets/uhcessentials/lang/en_US.lang index 7d76aed..e66ccaf 100644 --- a/src/main/resources/assets/uhcessentials/lang/en_US.lang +++ b/src/main/resources/assets/uhcessentials/lang/en_US.lang @@ -3,4 +3,6 @@ key.uhcessentials.fullbright=Toggle fullbright key.uhcessentials.optionsmenu=Open options sceen key.uhcessentials.coordinatesmenu=Open coordinates menu -uhcessentials.invalidcoordinates=Invalid coordinates! \ No newline at end of file +uhcessentials.invalidcoordinates=Invalid coordinates! +uhcessentials.coordinatesmenutitle=Compass Coordinates +uhcessentials.savecoordinateslabel=Save Coordinates \ No newline at end of file