diff --git a/src/main/java/com/minecrafttas/tasmod/registries/TASmodKeybinds.java b/src/main/java/com/minecrafttas/tasmod/registries/TASmodKeybinds.java index 58558fe4..f36acdb2 100644 --- a/src/main/java/com/minecrafttas/tasmod/registries/TASmodKeybinds.java +++ b/src/main/java/com/minecrafttas/tasmod/registries/TASmodKeybinds.java @@ -14,16 +14,18 @@ public enum TASmodKeybinds { TICKRATE_0("Tickrate 0 Key", "TASmod", Keyboard.KEY_F8, () -> TASmodClient.tickratechanger.togglePause(), VirtualKeybindings::isKeyDown), - ADVANCE("Advance Tick", "TASmod", Keyboard.KEY_F9, () -> TASmodClient.tickratechanger.advanceTick(), VirtualKeybindings::isKeyDown), - STOP("Recording/Playback Stop", "TASmod", Keyboard.KEY_F10, () -> TASmodClient.controller.setTASState(TASstate.NONE), VirtualKeybindings::isKeyDown), - SAVESTATE("Create Savestate", "TASmod", Keyboard.KEY_J, () -> { + TICKRATE_ADVANCE("Advance Tick", "TASmod", Keyboard.KEY_F9, () -> TASmodClient.tickratechanger.advanceTick(), VirtualKeybindings::isKeyDown), + TICKRATE_INCREASE("Increase Tickrate", "TASmod", Keyboard.KEY_PERIOD, () -> TASmodClient.tickratechanger.increaseTickrate(), VirtualKeybindings::isKeyDown), + TICKRATE_DECREASE("Decrease Tickrate", "TASmod", Keyboard.KEY_COMMA, () -> TASmodClient.tickratechanger.decreaseTickrate(), VirtualKeybindings::isKeyDown), + PLAYBACK_STOP("Recording/Playback Stop", "TASmod", Keyboard.KEY_F10, () -> TASmodClient.controller.setTASState(TASstate.NONE), VirtualKeybindings::isKeyDown), + SAVESTATE_SAVE("Create Savestate", "TASmod", Keyboard.KEY_J, () -> { try { TASmodClient.client.send(new TASmodBufferBuilder(TASmodPackets.SAVESTATE_SAVE).writeInt(-1)); } catch (Exception e) { e.printStackTrace(); } }), - LOADSTATE("Load Latest Savestate", "TASmod", Keyboard.KEY_K, () -> { + SAVESTATE_LOAD("Load Latest Savestate", "TASmod", Keyboard.KEY_K, () -> { try { TASmodClient.client.send(new TASmodBufferBuilder(TASmodPackets.SAVESTATE_LOAD).writeInt(-1)); } catch (Exception e) { @@ -36,6 +38,7 @@ public enum TASmodKeybinds { mc.displayGuiScreen(TASmodClient.hud); } }), + TEST1("Various Testing", "TASmod", Keyboard.KEY_F12, () -> { }, VirtualKeybindings::isKeyDown), TEST2("Various Testing2", "TASmod", Keyboard.KEY_F7, () -> { diff --git a/src/main/java/com/minecrafttas/tasmod/tickratechanger/TickrateChangerClient.java b/src/main/java/com/minecrafttas/tasmod/tickratechanger/TickrateChangerClient.java index 0cd9c6a6..6aef7eef 100644 --- a/src/main/java/com/minecrafttas/tasmod/tickratechanger/TickrateChangerClient.java +++ b/src/main/java/com/minecrafttas/tasmod/tickratechanger/TickrateChangerClient.java @@ -32,8 +32,8 @@ public class TickrateChangerClient implements ClientPacketHandler { public float ticksPerSecond; /** - * The tickrate before {@link #ticksPerSecond} was changed to 0, used to toggle - * pausing + *

The tickrate before {@link #ticksPerSecond} was changed to 0 + *

Used to toggle pausing */ public float tickrateSaved = 20F; @@ -42,12 +42,33 @@ public class TickrateChangerClient implements ClientPacketHandler { */ public boolean advanceTick = false; + /** + * How many milliseconds should pass in a tick. + */ public long millisecondsPerTick = 50L; + /** + * The tickrate steps that can be set via {@link #increaseTickrate()} and {@link #decreaseTickrate()} + */ + private float[] rates = new float[] { .1f, .2f, .5f, 1f, 2f, 5f, 10f, 20f, 40f, 100f }; + /** + * The current index of the {@link #rates} + */ + private short rateIndex = 7; // Defaults to tickrate 20 + + /** + *

Creates a new Tickratechanger that is intended to run solely on the client side + *

The initial tickrate will be set to 20 ticks/s + */ public TickrateChangerClient() { this(20f); } + /** + *

Creates a new Tickratechanger that is intended to run solely on the client side + * + * @param initialTickrate The initial tickrate of the client + */ public TickrateChangerClient(float initialTickrate) { ticksPerSecond = initialTickrate; } @@ -62,16 +83,24 @@ public void changeTickrate(float tickrate) { changeServerTickrate(tickrate); } + /** + *

Changes the tickrate of the client + *

If tickrate is zero, it will pause the game and store the previous tickrate + * in {@link #tickrateSaved} + * + * @param tickrate The new tickrate of the client + */ public void changeClientTickrate(float tickrate) { changeClientTickrate(tickrate, true); } /** - * Changes the tickrate of the client
- * If tickrate is zero, it will pause the game and store the previous tickrate + *

Changes the tickrate of the client + *

If tickrate is zero, it will pause the game and store the previous tickrate * in {@link #tickrateSaved} * * @param tickrate The new tickrate of the client + * @param log Whether this interaction should be logged */ public void changeClientTickrate(float tickrate, boolean log) { if (tickrate < 0) { @@ -95,8 +124,8 @@ public void changeClientTickrate(float tickrate, boolean log) { } /** - * Attempts to change the tickrate on the server. Sends a - * {@link TASmodPackets#TICKRATE_CHANGE} packet to the server + *

Attempts to change the tickrate on the server. + *

Sends a {@link TASmodPackets#TICKRATE_CHANGE} packet to the server * * @param tickrate The new server tickrate */ @@ -114,7 +143,7 @@ public void changeServerTickrate(float tickrate) { } /** - * Toggles between tickrate 0 and tickrate > 0 + *

Toggles between tickrate 0 and tickrate > 0 */ public void togglePause() { try { @@ -126,7 +155,7 @@ public void togglePause() { } /** - * Pauses and unpauses the client, used in main menus + *

Pauses and unpauses the client, used in main menus */ public void togglePauseClient() { if (ticksPerSecond > 0) { @@ -138,7 +167,7 @@ public void togglePauseClient() { } /** - * Enables tickrate 0 + *

Enables tickrate 0 * * @param pause True if the game should be paused, false if unpause */ @@ -152,7 +181,7 @@ public void pauseGame(boolean pause) { } /** - * Pauses the game without sending a command to the server + *

Pauses the game without sending a command to the server * * @param pause The state of the client */ @@ -165,8 +194,9 @@ public void pauseClientGame(boolean pause) { } /** - * Advances the game by 1 tick. Sends a {@link AdvanceTickratePacket} to the - * server or calls {@link #advanceClientTick()} if the world is null + *

Advances the game by 1 tick. + *

Sends a {@link TASmodPackets#TICKRATE_ADVANCE} to the server

+ * or calls {@link #advanceClientTick()} if the world is null. */ public void advanceTick() { if (Minecraft.getMinecraft().world != null) { @@ -177,7 +207,7 @@ public void advanceTick() { } /** - * Sends a {@link AdvanceTickratePacket} to the server to advance the server + *

Sends a {@link TASmodPackets#TICKRATE_ADVANCE} packet to the server */ public void advanceServerTick() { try { @@ -188,7 +218,7 @@ public void advanceServerTick() { } /** - * Advances the game by 1 tick. Doesn't send a packet to the server + *

Advances the game by 1 tick. Doesn't send a packet to the server */ public void advanceClientTick() { if (ticksPerSecond == 0) { @@ -197,6 +227,26 @@ public void advanceClientTick() { } } + /** + *

Increases the tickrate to the next value of {@link #rateIndex} in {@link #rates} + */ + public void increaseTickrate() { + rateIndex = findClosestRateIndex(ticksPerSecond); + rateIndex++; + rateIndex = (short) clamp(rateIndex, 0, rates.length - 1); + changeTickrate(rates[rateIndex]); + } + + /** + *

Decreases the tickrate to the previous value of {@link #rateIndex} in {@link #rates} + */ + public void decreaseTickrate() { + rateIndex = findClosestRateIndex(ticksPerSecond); + rateIndex--; + rateIndex = (short) clamp(rateIndex, 0, rates.length - 1); + changeTickrate(rates[rateIndex]); + } + public void joinServer() { changeServerTickrate(ticksPerSecond); } @@ -244,4 +294,52 @@ public void onClientPacket(PacketID id, ByteBuffer buf, String username) throws } } + /** + *

Finds the nearest rate index from the current tickrate + * @param tickrate The current tickrate to find the rateIndex for + * @return The rateIndex + */ + private short findClosestRateIndex(float tickrate) { + for (int i = 0; i < rates.length; i++) { + int iMinus1 = i - 1; + + float min = 0f; + if (iMinus1 >= 0) { + min = rates[iMinus1]; + } + float max = rates[i]; + + if (tickrate >= min && tickrate < max) { + if (min == 0f) { + return (short) i; + } + + float distanceToMin = tickrate - min; + float distanceToMax = max - tickrate; + + if (distanceToMin < distanceToMax) { + return (short) iMinus1; + } else if (distanceToMax < distanceToMin) { + return (short) i; + } else { + return (short) iMinus1; + } + } + } + return (short) (rates.length - 1); + } + + /** + * Basic clamping method + * @param value The value to clamp + * @param min The minimum value + * @param max The maximum value + * @return The clamped value + */ + private static int clamp(long value, int min, int max) { + if (min > max) { + throw new IllegalArgumentException(min + " > " + max); + } + return (int) Math.min(max, Math.max(value, min)); + } }