Skip to content

Commit

Permalink
[Networking] Finishing documentation 🚂 for packet ids
Browse files Browse the repository at this point in the history
- Fixed clearinputs packet being sent twice
- Renamed SYNCSTATE packet to PLAYBACK_STATE
- Renamed CLEAR_INPUTS to PLAYBACK_CLEAR_INPUTS
  • Loading branch information
ScribbleTAS committed Nov 16, 2023
1 parent 022d31b commit 9c4e1db
Show file tree
Hide file tree
Showing 5 changed files with 126 additions and 29 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,27 @@
import com.minecrafttas.common.server.Client.Side;

public interface PacketID {
/**
* @return The numerical ID of the packet
*/
public int getID();

public CompactPacketHandler getLambda();

/**
* Only used in combination with {@link #getLambda()}
* @return The side of the packet this is registered to
*/
public Side getSide();

/**
* Used for compact small lambda packet handlers
* @return The lamda to run when receiving a packet
*/
public CompactPacketHandler getLambda();
/**
* @return The name of the packet
*/
public String getName();

/**
* @return Whether the packet should be used in trace messages
*/
public boolean shouldTrace();
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ public String getUsage(ICommandSender sender) {
public void execute(MinecraftServer server, ICommandSender sender, String[] args) throws CommandException {
if(sender instanceof EntityPlayer) {
try {
TASmod.server.sendToAll(new TASmodBufferBuilder(TASmodPackets.CLEAR_INNPUTS));
TASmod.server.sendToAll(new TASmodBufferBuilder(TASmodPackets.PLAYBACK_CLEAR_INPUTS));
} catch (Exception e) {
e.printStackTrace();
}
Expand Down
101 changes: 92 additions & 9 deletions src/main/java/com/minecrafttas/tasmod/networking/TASmodPackets.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@
import com.minecrafttas.common.server.Client.Side;
import com.minecrafttas.common.server.interfaces.PacketID;
import com.minecrafttas.tasmod.commands.CommandFolder;
import com.minecrafttas.tasmod.playback.PlaybackControllerClient;
import com.minecrafttas.tasmod.playback.PlaybackControllerClient.TASstate;
import com.minecrafttas.tasmod.playback.PlaybackSerialiser;
import com.minecrafttas.tasmod.savestates.SavestateHandlerServer.PlayerHandler.MotionData;
import com.minecrafttas.tasmod.tickratechanger.TickrateChangerServer.TickratePauseState;

import net.minecraft.nbt.NBTTagCompound;
Expand All @@ -17,37 +21,31 @@ public enum TASmodPackets implements PacketID {
/**
* <p>Ticksync is a system to sync the tick execution between client and server.
* Both can tick independent from each other causing issues with playback.
*
* <p>This is used to notify the other to start ticking and shouldn't be used otherwise.
*
* <p>SIDE: Both<br>
* ARGS: None
*/
TICKSYNC(false),
/**
* <p>Sets the tickrate/gamespeed
*
* <p>SIDE: Both<br>
* ARGS: int tickrate
*/
TICKRATE_CHANGE,
/**
* <p>Sets the tickrate to 0, pausing the game. Also unpauses the game
*
* <p>SIDE: Both<br>
* ARGS: {@link TickratePauseState} state The paused state
*/
TICKRATE_ZERO,
/**
* <p>While in tickrate 0, advances the game by one tick
*
* <p>SIDE: Both<br>
* ARGS: None
*/
TICKRATE_ADVANCE,
/**
* <p>Creates a savestate
*
* <p>SIDE: Both<br>
* ARGS: <br>
* <strong>Client->Server:</strong> int The index of the savestate that should be created. -1 to create the latest savestate, might overwrite existing savestates.<br>
Expand All @@ -56,7 +54,6 @@ public enum TASmodPackets implements PacketID {
SAVESTATE_SAVE,
/**
* <p>Loads a savestate
*
* <p>SIDE: Both<br>
* ARGS: <br>
* <strong>Client->Server</strong> int The index of the savestate that should be loaded<br>
Expand All @@ -75,17 +72,91 @@ public enum TASmodPackets implements PacketID {
* ARGS: {@link NBTTagCompound} compound The playerdata
*/
SAVESTATE_PLAYER,
/**
* <p>Used for storing the client motion data on the server.
* <p>SIDE: BOTH<br>
* ARGS: <br>
* <strong>Server->Client</strong>None<br>
* <strong>Client->Server</strong> {@link MotionData} motionData An Object containing all necessary motion data<br>
*/
SAVESTATE_REQUEST_MOTION,
/**
* <p>Unloads the chunks on the client side
* <p>SIDE: Client<br>
* ARGS: none
*/
SAVESTATE_UNLOAD_CHUNKS,
CLEAR_INNPUTS,
/**
* <p>Notifies the client to clear all inputs from the input buffer in {@link PlaybackControllerClient}
* <p>SIDE: Both<br>
* ARGS: none
*/
PLAYBACK_CLEAR_INPUTS,
/**
* <p>Notifies the client to quit to the main menu and start recording inputs in {@link PlaybackControllerClient}
* <p>SIDE: Both<br>
* ARGS: none
*/
PLAYBACK_FULLRECORD,
/**
* <p>Notifies the client to quit to the main menu and start playing back inputs in {@link PlaybackControllerClient}
* <p>SIDE: Both<br>
* ARGS: none
*/
PLAYBACK_FULLPLAY,
/**
* <p>Notifies the client to quit the game. Upon restarting the game, the specified tasfile will be loaded and played back in {@link PlaybackControllerClient}
* <p>SIDE: Both<br>
* ARGS: <br>
* <strong>Client->Server</strong> None<br>
* <strong>Server->Client</strong> String filename The TASfile name to load on restart
*/
PLAYBACK_RESTARTANDPLAY,
/**
* <p>Notifies the client to store the current inputs to a file in {@link PlaybackControllerClient}. This is done using {@link PlaybackSerialiser}
* <p>SIDE: Both<br>
* ARGS: <br>
* <strong>Client->Server</strong> None<br>
* <strong>Server->Client</strong> String filename The TASfile name to store
*/
PLAYBACK_SAVE,
/**
* <p>Notifies the client to load the inputs from a file in {@link PlaybackControllerClient}. This is done using {@link PlaybackSerialiser}
* <p>SIDE: Both<br>
* ARGS: <br>
* <strong>Client->Server</strong> None<br>
* <strong>Server->Client</strong> String filename The TASfile name to load
*/
PLAYBACK_LOAD,
/**
* <p>Notifies the client activate "playuntil" in {@link PlaybackControllerClient}. The next playback will stop at the specified tick and the client will enter recording mode
* <p>SIDE: Both<br>
* ARGS: <br>
* <strong>Client->Server</strong> None<br>
* <strong>Server->Client</strong> int tick The tick when to stop playing back and start recording
*/
PLAYBACK_PLAYUNTIL,
/**
* <p>A permissionless teleport packet, used for setting the playerdata on the server. Used for teleporting the player back to the start of the TAS when using <code>/play</code>
* <p>SIDE: Server<br>
* ARGS: <br>
* double x The x value<br>
* double y etc...<br>
* double z<br>
* float angleYaw<br>
* float anglePitch<br>
*/
PLAYBACK_TELEPORT,
STATESYNC,
/**
* <p>Notifies the players to change the {@link TASstate}
* <p>SIDE: Both<br>
* ARGS: <br>
* <strong>Client->Server</strong> {@link TASstate} state The new state everyone should adapt
* <strong>Server->Client</strong>
* {@link TASstate} state The new state everyone should adapt<br>
* boolean verbose If a chat message should be printed
*/
PLAYBACK_STATE,
/**
* <p>Opens a TASmod related folder on the file system
* <p>The action describes which folder to open:
Expand All @@ -109,7 +180,19 @@ public enum TASmodPackets implements PacketID {
break;
}
}),
/**
* <p>Sets the KillTheRNG seed
* <p>SIDE: Both<br>
* ARGS: <br>
* long seed The new KillTheRNG seed
*/
KILLTHERNG_SEED,
/**
* <p>Sets the KillTheRNG start seed when starting recording
* <p>SIDE: Both<br>
* ARGS: <br>
* long seed The new KillTheRNG seed
*/
KILLTHERNG_STARTSEED;

private Side side;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
package com.minecrafttas.tasmod.playback;

import static com.minecrafttas.tasmod.TASmod.LOGGER;
import static com.minecrafttas.tasmod.networking.TASmodPackets.CLEAR_INNPUTS;
import static com.minecrafttas.tasmod.networking.TASmodPackets.PLAYBACK_CLEAR_INPUTS;
import static com.minecrafttas.tasmod.networking.TASmodPackets.PLAYBACK_FULLPLAY;
import static com.minecrafttas.tasmod.networking.TASmodPackets.PLAYBACK_FULLRECORD;
import static com.minecrafttas.tasmod.networking.TASmodPackets.PLAYBACK_LOAD;
import static com.minecrafttas.tasmod.networking.TASmodPackets.PLAYBACK_PLAYUNTIL;
import static com.minecrafttas.tasmod.networking.TASmodPackets.PLAYBACK_RESTARTANDPLAY;
import static com.minecrafttas.tasmod.networking.TASmodPackets.PLAYBACK_SAVE;
import static com.minecrafttas.tasmod.networking.TASmodPackets.PLAYBACK_TELEPORT;
import static com.minecrafttas.tasmod.networking.TASmodPackets.STATESYNC;
import static com.minecrafttas.tasmod.networking.TASmodPackets.PLAYBACK_STATE;

import java.io.File;
import java.io.IOException;
Expand Down Expand Up @@ -147,7 +147,7 @@ public class PlaybackControllerClient implements ClientPacketHandler {
*/
public void setTASState(TASstate stateIn) {
try {
TASmodClient.client.send(new TASmodBufferBuilder(STATESYNC).writeTASState(stateIn));
TASmodClient.client.send(new TASmodBufferBuilder(PLAYBACK_STATE).writeTASState(stateIn));
} catch (Exception e) {
e.printStackTrace();
}
Expand All @@ -171,7 +171,7 @@ public String setTASStateClient(TASstate stateIn) {
* @return The message printed in the chat
*/
public String setTASStateClient(TASstate stateIn, boolean verbose) {
ControlByteHandler.reset();
ControlByteHandler.reset(); // FIXME Controlbytes are resetting when loading a world, due to "Paused" state being active during loading... Fix Paused state shenanigans?
if (state == stateIn) {
switch (stateIn) {
case PLAYBACK:
Expand Down Expand Up @@ -832,8 +832,8 @@ public PacketID[] getAcceptedPacketIDs() {
PLAYBACK_RESTARTANDPLAY,
PLAYBACK_PLAYUNTIL,
PLAYBACK_TELEPORT,
CLEAR_INNPUTS,
STATESYNC
PLAYBACK_CLEAR_INPUTS,
PLAYBACK_STATE
};
}

Expand Down Expand Up @@ -926,14 +926,14 @@ public void onClientPacket(PacketID id, ByteBuffer buf, String username) throws
TASmodClient.virtual.getContainer().setPlayUntil(until);
break;

case CLEAR_INNPUTS:
case PLAYBACK_CLEAR_INPUTS:
TASmodClient.virtual.getContainer().clear();
break;

case PLAYBACK_TELEPORT:
throw new WrongSideException(packet, Side.CLIENT);

case STATESYNC:
case PLAYBACK_STATE:
TASstate networkState = TASmodBufferBuilder.readTASState(buf);
boolean verbose = TASmodBufferBuilder.readBoolean(buf);
Task task = ()->{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,9 @@ public class PlaybackControllerServer implements ServerPacketHandler {
public PacketID[] getAcceptedPacketIDs() {
return new TASmodPackets[]
{
STATESYNC,
PLAYBACK_STATE,
PLAYBACK_TELEPORT,
CLEAR_INNPUTS,
PLAYBACK_CLEAR_INPUTS,
PLAYBACK_FULLPLAY,
PLAYBACK_FULLRECORD,
PLAYBACK_RESTARTANDPLAY,
Expand All @@ -51,7 +51,7 @@ public void onServerPacket(PacketID id, ByteBuffer buf, String username) throws

switch (packet) {

case STATESYNC:
case PLAYBACK_STATE:
TASstate networkState = TASmodBufferBuilder.readTASState(buf);
/* TODO Permissions */
setState(networkState);
Expand All @@ -73,9 +73,9 @@ public void onServerPacket(PacketID id, ByteBuffer buf, String username) throws
});
break;

case CLEAR_INNPUTS:
TASmod.server.sendToAll(new TASmodBufferBuilder(CLEAR_INNPUTS));

case PLAYBACK_CLEAR_INPUTS:
TASmod.server.sendToAll(new TASmodBufferBuilder(PLAYBACK_CLEAR_INPUTS));
break;
case PLAYBACK_FULLPLAY:
case PLAYBACK_FULLRECORD:
case PLAYBACK_RESTARTANDPLAY:
Expand All @@ -93,7 +93,7 @@ public void onServerPacket(PacketID id, ByteBuffer buf, String username) throws
public void setState(TASstate stateIn) {
setServerState(stateIn);
try {
TASmod.server.sendToAll(new TASmodBufferBuilder(TASmodPackets.STATESYNC).writeTASState(state).writeBoolean(true));
TASmod.server.sendToAll(new TASmodBufferBuilder(TASmodPackets.PLAYBACK_STATE).writeTASState(state).writeBoolean(true));
} catch (Exception e) {
e.printStackTrace();
}
Expand Down

0 comments on commit 9c4e1db

Please sign in to comment.