Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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, () -> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
* <p>The tickrate before {@link #ticksPerSecond} was changed to 0
* <p>Used to toggle pausing
*/
public float tickrateSaved = 20F;

Expand All @@ -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

/**
* <p>Creates a new Tickratechanger that is intended to run solely on the client side
* <p>The initial tickrate will be set to 20 ticks/s
*/
public TickrateChangerClient() {
this(20f);
}

/**
* <p>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;
}
Expand All @@ -62,16 +83,24 @@ public void changeTickrate(float tickrate) {
changeServerTickrate(tickrate);
}

/**
* <p>Changes the tickrate of the client
* <p>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 <br>
* If tickrate is zero, it will pause the game and store the previous tickrate
* <p>Changes the tickrate of the client
* <p>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) {
Expand All @@ -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
* <p>Attempts to change the tickrate on the server.
* <p>Sends a {@link TASmodPackets#TICKRATE_CHANGE} packet to the server
*
* @param tickrate The new server tickrate
*/
Expand All @@ -114,7 +143,7 @@ public void changeServerTickrate(float tickrate) {
}

/**
* Toggles between tickrate 0 and tickrate > 0
* <p>Toggles between tickrate 0 and tickrate > 0
*/
public void togglePause() {
try {
Expand All @@ -126,7 +155,7 @@ public void togglePause() {
}

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

/**
* Enables tickrate 0
* <p>Enables tickrate 0
*
* @param pause True if the game should be paused, false if unpause
*/
Expand All @@ -152,7 +181,7 @@ public void pauseGame(boolean pause) {
}

/**
* Pauses the game without sending a command to the server
* <p>Pauses the game without sending a command to the server
*
* @param pause The state of the client
*/
Expand All @@ -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
* <p>Advances the game by 1 tick.
* <p>Sends a {@link TASmodPackets#TICKRATE_ADVANCE} to the server<p>
* or calls {@link #advanceClientTick()} if the world is null.
*/
public void advanceTick() {
if (Minecraft.getMinecraft().world != null) {
Expand All @@ -177,7 +207,7 @@ public void advanceTick() {
}

/**
* Sends a {@link AdvanceTickratePacket} to the server to advance the server
* <p>Sends a {@link TASmodPackets#TICKRATE_ADVANCE} packet to the server
*/
public void advanceServerTick() {
try {
Expand All @@ -188,7 +218,7 @@ public void advanceServerTick() {
}

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

/**
* <p>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]);
}

/**
* <p>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);
}
Expand Down Expand Up @@ -244,4 +294,52 @@ public void onClientPacket(PacketID id, ByteBuffer buf, String username) throws
}
}

/**
* <p>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));
}
}