Skip to content

Commit 8001f4b

Browse files
committed
[Savestates] Finish implementing player motion
1 parent 7d6fe51 commit 8001f4b

File tree

8 files changed

+140
-69
lines changed

8 files changed

+140
-69
lines changed

src/main/java/com/minecrafttas/mctcommon/file/AbstractDataFile.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77
import java.lang.reflect.Type;
88
import java.nio.file.Files;
99
import java.nio.file.Path;
10-
import java.nio.file.StandardOpenOption;
1110
import java.util.InvalidPropertiesFormatException;
1211
import java.util.Map.Entry;
1312
import java.util.Properties;
@@ -199,7 +198,7 @@ public void saveToJson(Path file) {
199198
//@formatter:on
200199
try {
201200
String element = json.toJson(properties);
202-
Files.write(file, element.getBytes(), StandardOpenOption.WRITE, StandardOpenOption.CREATE);
201+
Files.write(file, element.getBytes());
203202
} catch (IOException e) {
204203
MCTCommon.LOGGER.catching(e);
205204
}

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,9 @@ public void onInitialize() {
114114
PacketHandlerRegistry.register(startPositionMetadataExtension);
115115
PacketHandlerRegistry.register(tabCompletionUtils);
116116
PacketHandlerRegistry.register(commandFileCommand);
117+
SavestateMotionStorage motionStorage = new SavestateMotionStorage();
118+
PacketHandlerRegistry.register(motionStorage);
119+
EventListenerRegistry.register(motionStorage);
117120
}
118121

119122
@Override
@@ -141,8 +144,6 @@ public void onServerInit(MinecraftServer server) {
141144
PacketHandlerRegistry.register(savestateHandlerServer);
142145
PacketHandlerRegistry.register(savestateHandlerServer.getPlayerHandler());
143146

144-
EventListenerRegistry.register(savestateHandlerServer.getPlayerHandler());
145-
146147
if (!server.isDedicatedServer()) {
147148
TASmod.tickratechanger.ticksPerSecond = 0F;
148149
TASmod.tickratechanger.tickrateSaved = 20F;
@@ -174,7 +175,6 @@ public void onServerStop(MinecraftServer mcserver) {
174175
PacketHandlerRegistry.unregister(savestateHandlerServer); // Unregistering the savestatehandler, as a new instance is registered in onServerStart()
175176
PacketHandlerRegistry.unregister(savestateHandlerServer.getPlayerHandler());
176177

177-
EventListenerRegistry.unregister(savestateHandlerServer.getPlayerHandler());
178178
savestateHandlerServer = null;
179179
}
180180
}

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@
3636
import com.minecrafttas.tasmod.registries.TASmodKeybinds;
3737
import com.minecrafttas.tasmod.registries.TASmodPackets;
3838
import com.minecrafttas.tasmod.savestates.SavestateHandlerClient;
39+
import com.minecrafttas.tasmod.savestates.handlers.SavestatePlayerHandler;
3940
import com.minecrafttas.tasmod.tickratechanger.TickrateChangerClient;
4041
import com.minecrafttas.tasmod.ticksync.TickSyncClient;
4142
import com.minecrafttas.tasmod.util.LoggerMarkers;
@@ -150,6 +151,7 @@ private void registerNetworkPacketHandlers() {
150151
PacketHandlerRegistry.register(ticksyncClient);
151152
PacketHandlerRegistry.register(tickratechanger);
152153
PacketHandlerRegistry.register(savestateHandlerClient);
154+
PacketHandlerRegistry.register(new SavestatePlayerHandler(null));
153155
}
154156

155157
private void registerEventListeners() {

src/main/java/com/minecrafttas/tasmod/savestates/SavestateHandlerClient.java

Lines changed: 36 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -269,32 +269,44 @@ public static void loadPlayer(NBTTagCompound compound) {
269269
// Clear any accidental applied potion particles on the client
270270
((AccessorEntityLivingBase) player).clearPotionEffects();
271271

272+
/*
273+
* TODO
274+
* The following 20 lines are all one
275+
* gross workaround for correctly applying the player motion
276+
* to the client...
277+
*
278+
* The motion is applied
279+
* to the player in a previous step and unfortunately
280+
* player.readFromNBT(compound) overwrites the
281+
* previously applied motion...
282+
*
283+
* So this workaround makes sure that the motion is not overwritten
284+
* Fixing this, requires restructuring the steps for loadstating
285+
* and since I plan to do this anyway at some point, I will
286+
* leave this here and be done for today*/
287+
double x = player.motionX;
288+
double y = player.motionY;
289+
double z = player.motionZ;
290+
291+
float rx = player.moveForward;
292+
float ry = player.moveStrafing;
293+
float rz = player.moveVertical;
294+
295+
boolean sprinting = player.isSprinting();
296+
float jumpVector = player.jumpMovementFactor;
297+
272298
player.readFromNBT(compound);
273-
NBTTagCompound motion = compound.getCompoundTag("clientMotion");
274299

275-
if (motion.hasNoTags()) {
276-
LOGGER.warn(LoggerMarkers.Savestate, "Could not load the motion from the savestate. Savestate seems to be created manually or by a different mod");
277-
} else {
278-
LOGGER.trace(LoggerMarkers.Savestate, "Loading client motion from NBT");
279-
double x = motion.getDouble("x");
280-
double y = motion.getDouble("y");
281-
double z = motion.getDouble("z");
282-
player.motionX = x;
283-
player.motionY = y;
284-
player.motionZ = z;
285-
286-
float rx = motion.getFloat("RelativeX");
287-
float ry = motion.getFloat("RelativeY");
288-
float rz = motion.getFloat("RelativeZ");
289-
player.moveForward = rx;
290-
player.moveVertical = ry;
291-
player.moveStrafing = rz;
292-
293-
boolean sprinting = motion.getBoolean("Sprinting");
294-
float jumpVector = motion.getFloat("JumpFactor");
295-
player.setSprinting(sprinting);
296-
player.jumpMovementFactor = jumpVector;
297-
}
300+
player.motionX = x;
301+
player.motionY = y;
302+
player.motionZ = z;
303+
304+
player.moveForward = rx;
305+
player.moveVertical = ry;
306+
player.moveStrafing = rz;
307+
308+
player.setSprinting(sprinting);
309+
player.jumpMovementFactor = jumpVector;
298310

299311
LOGGER.trace(LoggerMarkers.Savestate, "Setting client gamemode");
300312
// #86

src/main/java/com/minecrafttas/tasmod/savestates/SavestateHandlerServer.java

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -168,9 +168,6 @@ public void saveState(int savestateIndex, boolean tickrate0, boolean changeIndex
168168
// Enable tickrate 0
169169
TASmod.tickratechanger.pauseGame(true);
170170

171-
// Get the motion from the client
172-
playerHandler.requestMotionFromClient();
173-
174171
// Save the world!
175172
server.getPlayerList().saveAllPlayerData();
176173
server.saveAllWorlds(false);
Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
package com.minecrafttas.tasmod.savestates.exceptions;
22

3-
public class LoadstateException extends Exception{
3+
public class LoadstateException extends RuntimeException {
44
/**
55
*
66
*/
@@ -9,4 +9,8 @@ public class LoadstateException extends Exception{
99
public LoadstateException(String s) {
1010
super(s);
1111
}
12+
13+
public LoadstateException(Throwable t, String msg, Object... args) {
14+
super(String.format(msg, args), t);
15+
}
1216
}

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

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -189,7 +189,6 @@ public PacketID[] getAcceptedPacketIDs() {
189189
@Override
190190
public void onServerPacket(PacketID id, ByteBuffer buf, String username) throws PacketNotImplementedException, WrongSideException, Exception {
191191
TASmodPackets packet = (TASmodPackets) id;
192-
EntityPlayerMP player = TASmod.getServerInstance().getPlayerList().getPlayerByUsername(username);
193192

194193
switch (packet) {
195194
case SAVESTATE_PLAYER:

0 commit comments

Comments
 (0)