Skip to content

Commit

Permalink
Merge pull request #2 from betterclient/1.21
Browse files Browse the repository at this point in the history
Fixes #1
  • Loading branch information
betterclient authored Sep 19, 2024
2 parents ac352c4 + 46f21f0 commit 905276e
Showing 1 changed file with 119 additions and 4 deletions.
123 changes: 119 additions & 4 deletions src/main/java/io/github/betterclient/snaptap/SnapTap.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,22 @@
import net.fabricmc.fabric.api.client.networking.v1.ClientPlayConnectionEvents;
import net.fabricmc.fabric.api.client.networking.v1.ClientPlayNetworking;
import net.fabricmc.fabric.api.networking.v1.PayloadTypeRegistry;
import net.fabricmc.loader.api.FabricLoader;
import net.minecraft.client.MinecraftClient;
import net.minecraft.client.gui.DrawContext;
import net.minecraft.client.option.KeyBinding;
import net.minecraft.client.util.InputUtil;
import net.minecraft.text.Style;
import net.minecraft.text.Text;
import net.minecraft.util.Formatting;
import org.apache.commons.logging.LogFactory;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;

public class SnapTap implements ModInitializer {
public static long LEFT_STRAFE_LAST_PRESS_TIME = 0;
public static long RIGHT_STRAFE_LAST_PRESS_TIME = 0;
Expand All @@ -33,14 +38,30 @@ public class SnapTap implements ModInitializer {

public static Logger LOGGER = LoggerFactory.getLogger("SnapTap");

public File configFile = new File(FabricLoader.getInstance().getConfigDir().toFile(), "snaptap_hud.txt");
public File toggleFile = new File(FabricLoader.getInstance().getConfigDir().toFile(), "snaptap_toggle.txt");
public File toggleKeystrokesFile = new File(FabricLoader.getInstance().getConfigDir().toFile(), "snaptap_toggle_keystrokes.txt");

@Override
public void onInitialize() {
try {
KEYSTROKES_TOGGLED = getOrCreateHud();
} catch (IOException e) {
throw new RuntimeException(e);
}
LEFT_STRAFE_LAST_PRESS_TIME = 0;
RIGHT_STRAFE_LAST_PRESS_TIME = 0;
FORWARD_STRAFE_LAST_PRESS_TIME = 0;
BACKWARD_STRAFE_LAST_PRESS_TIME = 0;

TOGGLE_BIND = new KeyBinding("text.snaptap.toggle", InputUtil.GLFW_KEY_F8, "key.categories.misc") {
int b1;
try {
b1 = getOrCreateToggle();
} catch (IOException e) {
throw new RuntimeException(e);
}

TOGGLE_BIND = new KeyBinding("text.snaptap.toggle", b1, "key.categories.misc") {
@Override
public void setPressed(boolean pressed) {
if (!SERVER_ALLOWS) {
Expand All @@ -60,9 +81,31 @@ public void setPressed(boolean pressed) {

super.setPressed(pressed);
}

@Override
public void setBoundKey(InputUtil.Key boundKey) {
super.setBoundKey(boundKey);

try {
toggleFile.delete();
toggleFile.createNewFile();
FileOutputStream fos = new FileOutputStream(toggleFile);
fos.write(("" + boundKey.getCode()).getBytes());
fos.close();
} catch (Exception e) {
LOGGER.error("Failed to save snap-tap key", e);
}
}
};

KEYSTROKES_TOGGLE_BIND = new KeyBinding("text.snaptap.keystrokestoggle", InputUtil.GLFW_KEY_F7, "key.categories.misc") {
int b2;
try {
b2 = getOrCreateKeystrokesToggle();
} catch (IOException e) {
throw new RuntimeException(e);
}

KEYSTROKES_TOGGLE_BIND = new KeyBinding("text.snaptap.keystrokestoggle", b2, "key.categories.misc") {
@Override
public void setPressed(boolean pressed) {
if (!SERVER_ALLOWS) {
Expand All @@ -76,10 +119,34 @@ public void setPressed(boolean pressed) {
Text.translatable(KEYSTROKES_TOGGLED ? "text.snaptap.enabled" : "options.ao.off")
.fillStyle(Style.EMPTY
.withColor(KEYSTROKES_TOGGLED ? Formatting.GREEN : Formatting.RED))));
try {
configFile.delete();
configFile.createNewFile();
FileOutputStream fos = new FileOutputStream(configFile);
fos.write(("" + KEYSTROKES_TOGGLED).getBytes());
fos.close();
} catch (Exception e) {
LOGGER.error("Failed to save keystrokes settings", e);
}
}

super.setPressed(pressed);
}

@Override
public void setBoundKey(InputUtil.Key boundKey) {
super.setBoundKey(boundKey);

try {
toggleKeystrokesFile.delete();
toggleKeystrokesFile.createNewFile();
FileOutputStream fos = new FileOutputStream(toggleKeystrokesFile);
fos.write(("" + boundKey.getCode()).getBytes());
fos.close();
} catch (Exception e) {
LOGGER.error("Failed to save keystrokes settings", e);
}
}
};

PayloadTypeRegistry.playS2C().register(SnapTapPayload.PAYLOAD_ID, new SnapTapPayload.Codec());
Expand All @@ -98,6 +165,54 @@ public void setPressed(boolean pressed) {
});
}

private int getOrCreateKeystrokesToggle() throws IOException {
if (!toggleKeystrokesFile.exists()) {
toggleKeystrokesFile.createNewFile();
FileOutputStream fos = new FileOutputStream(toggleKeystrokesFile);
fos.write(InputUtil.GLFW_KEY_F7);
fos.close();

return InputUtil.GLFW_KEY_F7;
}
FileInputStream fis = new FileInputStream(toggleKeystrokesFile);
byte[] bites = fis.readAllBytes();
fis.close();

return Integer.parseInt(new String(bites));
}

private int getOrCreateToggle() throws IOException {
if (!toggleFile.exists()) {
toggleFile.createNewFile();
FileOutputStream fos = new FileOutputStream(toggleFile);
fos.write(InputUtil.GLFW_KEY_F8);
fos.close();

return InputUtil.GLFW_KEY_F8;
}
FileInputStream fis = new FileInputStream(toggleFile);
byte[] bites = fis.readAllBytes();
fis.close();

return Integer.parseInt(new String(bites));
}

private boolean getOrCreateHud() throws IOException {
if (!configFile.exists()) {
configFile.createNewFile();
FileOutputStream fos = new FileOutputStream(configFile);
fos.write("true".getBytes());
fos.close();

return true;
}
FileInputStream fis = new FileInputStream(configFile);
byte[] bites = fis.readAllBytes();
fis.close();

return Boolean.parseBoolean(new String(bites));
}

public static void render(DrawContext context) {
MinecraftClient client = MinecraftClient.getInstance();

Expand Down Expand Up @@ -141,4 +256,4 @@ public static void render(DrawContext context) {
context.drawCenteredTextWithShadow(client.textRenderer, backwardKey.getBoundKeyLocalizedText(), 40, 36, -1);
context.drawCenteredTextWithShadow(client.textRenderer, forwardKey.getBoundKeyLocalizedText(), 40, 11, -1);
}
}
}

0 comments on commit 905276e

Please sign in to comment.