Skip to content

Commit

Permalink
add compass target changer screen
Browse files Browse the repository at this point in the history
  • Loading branch information
tCInUBkXfaMY committed Feb 19, 2018
1 parent 3d455c3 commit 905ce98
Show file tree
Hide file tree
Showing 7 changed files with 122 additions and 23 deletions.
13 changes: 12 additions & 1 deletion src/main/java/com/pikachu/uhcessentials/gui/base/Window.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down Expand Up @@ -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;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -18,10 +21,15 @@ public class CoordinatesScreen extends GuiScreen {
private GuiTextField zInput;
private Callback<BlockPos> 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<BlockPos> callback) {
this.callback = callback;
this.instance = this;
HotkeyStore.add(new Hotkey(this) {
@Override
public void onPress() {
Expand All @@ -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);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 = "+";
Expand Down Expand Up @@ -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());

Expand Down
10 changes: 10 additions & 0 deletions src/main/java/com/pikachu/uhcessentials/utils/Util.java
Original file line number Diff line number Diff line change
Expand Up @@ -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");
Expand Down Expand Up @@ -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;
}

}
4 changes: 3 additions & 1 deletion src/main/resources/assets/uhcessentials/lang/en_US.lang
Original file line number Diff line number Diff line change
Expand Up @@ -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!
uhcessentials.invalidcoordinates=Invalid coordinates!
uhcessentials.coordinatesmenutitle=Compass Coordinates
uhcessentials.savecoordinateslabel=Save Coordinates

0 comments on commit 905ce98

Please sign in to comment.