Skip to content

Commit 7d6fe51

Browse files
committed
[Savestates] Start rewriting player motion
1 parent 460a04d commit 7d6fe51

File tree

5 files changed

+264
-197
lines changed

5 files changed

+264
-197
lines changed

src/main/java/com/minecrafttas/tasmod/TASmod.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
import com.minecrafttas.tasmod.playback.metadata.integrated.StartpositionMetadataExtension;
3030
import com.minecrafttas.tasmod.registries.TASmodPackets;
3131
import com.minecrafttas.tasmod.savestates.SavestateHandlerServer;
32+
import com.minecrafttas.tasmod.savestates.storage.SavestateMotionStorage;
3233
import com.minecrafttas.tasmod.tickratechanger.TickrateChangerServer;
3334
import com.minecrafttas.tasmod.ticksync.TickSyncServer;
3435
import com.minecrafttas.tasmod.util.LoggerMarkers;

src/main/java/com/minecrafttas/tasmod/networking/TASmodBufferBuilder.java

Lines changed: 25 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -10,61 +10,61 @@
1010
import com.minecrafttas.mctcommon.networking.ByteBufferBuilder;
1111
import com.minecrafttas.mctcommon.networking.interfaces.PacketID;
1212
import com.minecrafttas.tasmod.playback.PlaybackControllerClient.TASstate;
13-
import com.minecrafttas.tasmod.savestates.handlers.SavestatePlayerHandler.MotionData;
13+
import com.minecrafttas.tasmod.savestates.storage.SavestateMotionStorage.MotionData;
1414
import com.minecrafttas.tasmod.tickratechanger.TickrateChangerServer.TickratePauseState;
1515

1616
import net.minecraft.nbt.CompressedStreamTools;
1717
import net.minecraft.nbt.NBTSizeTracker;
1818
import net.minecraft.nbt.NBTTagCompound;
1919

20-
public class TASmodBufferBuilder extends ByteBufferBuilder{
20+
public class TASmodBufferBuilder extends ByteBufferBuilder {
2121

2222
public TASmodBufferBuilder(int id) {
2323
super(id);
2424
}
25-
25+
2626
public TASmodBufferBuilder(PacketID packet) {
2727
super(packet);
2828
}
29-
29+
3030
public TASmodBufferBuilder(ByteBuffer buf) {
3131
super(buf);
3232
}
33-
33+
3434
public TASmodBufferBuilder writeTASState(TASstate state) {
35-
this.writeShort((short)state.ordinal());
35+
this.writeShort((short) state.ordinal());
3636
return this;
3737
}
38-
38+
3939
public TASmodBufferBuilder writeNBTTagCompound(NBTTagCompound compound) {
40-
40+
4141
ByteArrayOutputStream out = new ByteArrayOutputStream();
42-
42+
4343
DataOutputStream dataout = new DataOutputStream(out);
44-
44+
4545
try {
4646
CompressedStreamTools.write(compound, dataout);
4747
} catch (IOException e) {
4848
e.printStackTrace();
4949
}
50-
50+
5151
this.writeByteArray(out.toByteArray());
52-
52+
5353
try {
5454
out.close();
5555
dataout.close();
5656
} catch (IOException e) {
5757
e.printStackTrace();
5858
}
59-
59+
6060
return this;
6161
}
62-
62+
6363
public TASmodBufferBuilder writeTickratePauseState(TickratePauseState state) {
6464
writeShort((short) state.ordinal());
6565
return this;
6666
}
67-
67+
6868
public TASmodBufferBuilder writeMotionData(MotionData data) {
6969
writeDouble(data.getClientX());
7070
writeDouble(data.getClientY());
@@ -76,28 +76,28 @@ public TASmodBufferBuilder writeMotionData(MotionData data) {
7676
writeBoolean(data.isSprinting());
7777
return this;
7878
}
79-
79+
8080
public static TASstate readTASState(ByteBuffer buf) {
8181
return TASstate.values()[buf.getShort()];
8282
}
83-
83+
8484
public static NBTTagCompound readNBTTagCompound(ByteBuffer buf) throws IOException {
8585
ByteArrayInputStream input = new ByteArrayInputStream(readByteArray(buf));
86-
86+
8787
DataInputStream datain = new DataInputStream(input);
88-
88+
8989
NBTTagCompound compound = CompressedStreamTools.read(datain, NBTSizeTracker.INFINITE);
90-
90+
9191
input.close();
9292
datain.close();
93-
93+
9494
return compound;
9595
}
96-
96+
9797
public static TickratePauseState readTickratePauseState(ByteBuffer buf) {
9898
return TickratePauseState.values()[buf.getShort()];
9999
}
100-
100+
101101
public static MotionData readMotionData(ByteBuffer buf) {
102102
double x = TASmodBufferBuilder.readDouble(buf);
103103
double y = TASmodBufferBuilder.readDouble(buf);
@@ -107,8 +107,8 @@ public static MotionData readMotionData(ByteBuffer buf) {
107107
float rz = TASmodBufferBuilder.readFloat(buf);
108108
float jumpMovementVector = TASmodBufferBuilder.readFloat(buf);
109109
boolean sprinting = TASmodBufferBuilder.readBoolean(buf);
110-
110+
111111
return new MotionData(x, y, z, rx, ry, rz, sprinting, jumpMovementVector);
112112
}
113-
113+
114114
}

src/main/java/com/minecrafttas/tasmod/registries/TASmodPackets.java

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
import com.minecrafttas.tasmod.playback.PlaybackControllerClient.TASstate;
99
import com.minecrafttas.tasmod.playback.filecommands.PlaybackFileCommand.PlaybackFileCommandExtension;
1010
import com.minecrafttas.tasmod.playback.tasfile.flavor.SerialiserFlavorBase;
11-
import com.minecrafttas.tasmod.savestates.handlers.SavestatePlayerHandler.MotionData;
11+
import com.minecrafttas.tasmod.savestates.storage.SavestateMotionStorage.MotionData;
1212
import com.minecrafttas.tasmod.tickratechanger.TickrateChangerServer.TickratePauseState;
1313
import com.minecrafttas.tasmod.util.Ducks.ScoreboardDuck;
1414

@@ -83,6 +83,13 @@ public enum TASmodPackets implements PacketID {
8383
* <strong>Client->Server</strong> {@link MotionData} motionData An Object containing all necessary motion data<br>
8484
*/
8585
SAVESTATE_REQUEST_MOTION,
86+
/**
87+
* <p>Used for setting the client motion data after it was loaded from a savestate
88+
* <p>Side: Client<br>
89+
* ARGS: <br>
90+
* <strong>Server->Client</strong> {@link MotionData} motionData An Object containing all necessary motion data<br>
91+
*/
92+
SAVESTATE_SET_MOTION,
8693
/**
8794
* <p>Unloads the chunks on the client side
8895
* <p>SIDE: Client<br>

src/main/java/com/minecrafttas/tasmod/savestates/handlers/SavestatePlayerHandler.java

Lines changed: 1 addition & 171 deletions
Original file line numberDiff line numberDiff line change
@@ -2,18 +2,11 @@
22

33
import static com.minecrafttas.tasmod.TASmod.LOGGER;
44
import static com.minecrafttas.tasmod.registries.TASmodPackets.SAVESTATE_PLAYER;
5-
import static com.minecrafttas.tasmod.registries.TASmodPackets.SAVESTATE_REQUEST_MOTION;
65

76
import java.io.IOException;
87
import java.nio.ByteBuffer;
9-
import java.util.HashMap;
108
import java.util.List;
11-
import java.util.Map;
129
import java.util.UUID;
13-
import java.util.concurrent.CompletableFuture;
14-
import java.util.concurrent.ExecutionException;
15-
import java.util.concurrent.TimeUnit;
16-
import java.util.concurrent.TimeoutException;
1710

1811
import com.minecrafttas.mctcommon.networking.Client.Side;
1912
import com.minecrafttas.mctcommon.networking.exception.PacketNotImplementedException;
@@ -22,19 +15,14 @@
2215
import com.minecrafttas.mctcommon.networking.interfaces.PacketID;
2316
import com.minecrafttas.mctcommon.networking.interfaces.ServerPacketHandler;
2417
import com.minecrafttas.tasmod.TASmod;
25-
import com.minecrafttas.tasmod.TASmodClient;
26-
import com.minecrafttas.tasmod.events.EventNBT;
2718
import com.minecrafttas.tasmod.networking.TASmodBufferBuilder;
2819
import com.minecrafttas.tasmod.registries.TASmodPackets;
2920
import com.minecrafttas.tasmod.savestates.SavestateHandlerClient;
30-
import com.minecrafttas.tasmod.savestates.exceptions.SavestateException;
31-
import com.minecrafttas.tasmod.savestates.gui.GuiSavestateSavingScreen;
3221
import com.minecrafttas.tasmod.util.LoggerMarkers;
3322

3423
import net.fabricmc.api.EnvType;
3524
import net.fabricmc.api.Environment;
3625
import net.minecraft.client.Minecraft;
37-
import net.minecraft.client.entity.EntityPlayerSP;
3826
import net.minecraft.entity.Entity;
3927
import net.minecraft.entity.player.EntityPlayerMP;
4028
import net.minecraft.nbt.NBTTagCompound;
@@ -48,18 +36,12 @@
4836
/**
4937
* Handles player related savestating methods
5038
*/
51-
public class SavestatePlayerHandler implements ClientPacketHandler, ServerPacketHandler, EventNBT.EventPlayerRead, EventNBT.EventPlayerWrite {
39+
public class SavestatePlayerHandler implements ClientPacketHandler, ServerPacketHandler {
5240

5341
private final MinecraftServer server;
5442

55-
private final Map<EntityPlayerMP, CompletableFuture<MotionData>> futures;
56-
57-
private final Map<EntityPlayerMP, MotionData> motionData;
58-
5943
public SavestatePlayerHandler(MinecraftServer server) {
6044
this.server = server;
61-
this.futures = new HashMap<>();
62-
this.motionData = new HashMap<>();
6345
}
6446

6547
/**
@@ -195,39 +177,10 @@ public void clearScoreboard() {
195177
}
196178
}
197179

198-
public void requestMotionFromClient() {
199-
LOGGER.trace(LoggerMarkers.Savestate, "Request motion from client");
200-
201-
this.futures.clear();
202-
203-
List<EntityPlayerMP> playerList = server.getPlayerList().getPlayers();
204-
playerList.forEach(player -> {
205-
futures.put(player, new CompletableFuture<>());
206-
});
207-
208-
try {
209-
// request client motion
210-
TASmod.server.sendToAll(new TASmodBufferBuilder(SAVESTATE_REQUEST_MOTION));
211-
} catch (Exception e) {
212-
e.printStackTrace();
213-
}
214-
215-
futures.forEach((player, future) -> {
216-
try {
217-
this.motionData.put(player, future.get(5, TimeUnit.SECONDS));
218-
} catch (TimeoutException e) {
219-
throw new SavestateException(e, "Writing client motion for %s timed out!", player.getName());
220-
} catch (ExecutionException | InterruptedException e) {
221-
throw new SavestateException(e, "Writing client motion for %s", player.getName());
222-
}
223-
});
224-
}
225-
226180
@Override
227181
public PacketID[] getAcceptedPacketIDs() {
228182
return new PacketID[] {
229183
//@formatter:off
230-
SAVESTATE_REQUEST_MOTION,
231184
SAVESTATE_PLAYER
232185
//@formatter:on
233186
};
@@ -239,11 +192,6 @@ public void onServerPacket(PacketID id, ByteBuffer buf, String username) throws
239192
EntityPlayerMP player = TASmod.getServerInstance().getPlayerList().getPlayerByUsername(username);
240193

241194
switch (packet) {
242-
case SAVESTATE_REQUEST_MOTION:
243-
MotionData data = TASmodBufferBuilder.readMotionData(buf);
244-
CompletableFuture<MotionData> future = this.futures.get(player);
245-
future.complete(data);
246-
break;
247195
case SAVESTATE_PLAYER:
248196
throw new WrongSideException(packet, Side.SERVER);
249197
default:
@@ -275,126 +223,8 @@ public void onClientPacket(PacketID id, ByteBuffer buf, String username) throws
275223
});
276224
break;
277225

278-
case SAVESTATE_REQUEST_MOTION:
279-
EntityPlayerSP player = Minecraft.getMinecraft().player;
280-
if (player != null) {
281-
if (!(Minecraft.getMinecraft().currentScreen instanceof GuiSavestateSavingScreen)) {
282-
Minecraft.getMinecraft().displayGuiScreen(new GuiSavestateSavingScreen());
283-
}
284-
//@formatter:off
285-
MotionData motionData = new MotionData(
286-
player.motionX,
287-
player.motionY,
288-
player.motionZ,
289-
player.moveForward,
290-
player.moveVertical,
291-
player.moveStrafing,
292-
player.isSprinting(),
293-
player.jumpMovementFactor
294-
);
295-
//@formatter:on
296-
TASmodClient.client.send(new TASmodBufferBuilder(TASmodPackets.SAVESTATE_REQUEST_MOTION).writeMotionData(motionData));
297-
}
298-
break;
299-
300226
default:
301227
break;
302228
}
303229
}
304-
305-
public static class MotionData {
306-
307-
private double clientX;
308-
private double clientY;
309-
private double clientZ;
310-
private float clientrX;
311-
private float clientrY;
312-
private float clientrZ;
313-
private boolean sprinting;
314-
private float jumpMovementVector;
315-
316-
public MotionData(double x, double y, double z, float rx, float ry, float rz, boolean sprinting, float jumpMovementVector) {
317-
clientX = x;
318-
clientY = y;
319-
clientZ = z;
320-
clientrX = rx;
321-
clientrY = ry;
322-
clientrZ = rz;
323-
this.sprinting = sprinting;
324-
this.jumpMovementVector = jumpMovementVector;
325-
}
326-
327-
public MotionData() {
328-
this(0D, 0D, 0D, 0f, 0f, 0f, false, 0f);
329-
}
330-
331-
public double getClientX() {
332-
return clientX;
333-
}
334-
335-
public double getClientY() {
336-
return clientY;
337-
}
338-
339-
public double getClientZ() {
340-
return clientZ;
341-
}
342-
343-
public float getClientrX() {
344-
return clientrX;
345-
}
346-
347-
public float getClientrY() {
348-
return clientrY;
349-
}
350-
351-
public float getClientrZ() {
352-
return clientrZ;
353-
}
354-
355-
public boolean isSprinting() {
356-
return sprinting;
357-
}
358-
359-
public float getJumpMovementVector() {
360-
return jumpMovementVector;
361-
}
362-
}
363-
364-
@Override
365-
public void onPlayerWriteNBT(NBTTagCompound compound, EntityPlayerMP player) {
366-
NBTTagCompound nbttagcompound = new NBTTagCompound();
367-
368-
MotionData saver = new MotionData();
369-
if (motionData.containsKey(player)) {
370-
saver = motionData.get(player);
371-
}
372-
373-
nbttagcompound.setDouble("x", saver.getClientX());
374-
nbttagcompound.setDouble("y", saver.getClientY());
375-
nbttagcompound.setDouble("z", saver.getClientZ());
376-
nbttagcompound.setFloat("RelativeX", saver.getClientrX());
377-
nbttagcompound.setFloat("RelativeY", saver.getClientrY());
378-
nbttagcompound.setFloat("RelativeZ", saver.getClientrZ());
379-
nbttagcompound.setBoolean("Sprinting", saver.isSprinting());
380-
nbttagcompound.setFloat("JumpFactor", saver.getJumpMovementVector());
381-
compound.setTag("clientMotion", nbttagcompound);
382-
}
383-
384-
@Override
385-
public void onPlayerReadNBT(NBTTagCompound compound, EntityPlayerMP player) {
386-
NBTTagCompound nbttagcompound = compound.getCompoundTag("clientMotion");
387-
388-
double clientmotionX = nbttagcompound.getDouble("x");
389-
double clientmotionY = nbttagcompound.getDouble("y");
390-
double clientmotionZ = nbttagcompound.getDouble("z");
391-
float clientmotionrX = nbttagcompound.getFloat("RelativeX");
392-
float clientmotionrY = nbttagcompound.getFloat("RelativeY");
393-
float clientmotionrZ = nbttagcompound.getFloat("RelativeZ");
394-
boolean sprinting = nbttagcompound.getBoolean("Sprinting");
395-
float jumpVector = nbttagcompound.getFloat("JumpFactor");
396-
397-
MotionData motion = new MotionData(clientmotionX, clientmotionY, clientmotionZ, clientmotionrX, clientmotionrY, clientmotionrZ, sprinting, jumpVector);
398-
motionData.put(player, motion);
399-
}
400230
}

0 commit comments

Comments
 (0)