Skip to content

Commit

Permalink
[Savestates] Finish implementing player motion
Browse files Browse the repository at this point in the history
  • Loading branch information
ScribbleTAS committed Dec 13, 2024
1 parent 7d6fe51 commit 8001f4b
Show file tree
Hide file tree
Showing 8 changed files with 140 additions and 69 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
import java.lang.reflect.Type;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.StandardOpenOption;
import java.util.InvalidPropertiesFormatException;
import java.util.Map.Entry;
import java.util.Properties;
Expand Down Expand Up @@ -199,7 +198,7 @@ public void saveToJson(Path file) {
//@formatter:on
try {
String element = json.toJson(properties);
Files.write(file, element.getBytes(), StandardOpenOption.WRITE, StandardOpenOption.CREATE);
Files.write(file, element.getBytes());
} catch (IOException e) {
MCTCommon.LOGGER.catching(e);
}
Expand Down
6 changes: 3 additions & 3 deletions src/main/java/com/minecrafttas/tasmod/TASmod.java
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,9 @@ public void onInitialize() {
PacketHandlerRegistry.register(startPositionMetadataExtension);
PacketHandlerRegistry.register(tabCompletionUtils);
PacketHandlerRegistry.register(commandFileCommand);
SavestateMotionStorage motionStorage = new SavestateMotionStorage();
PacketHandlerRegistry.register(motionStorage);
EventListenerRegistry.register(motionStorage);
}

@Override
Expand Down Expand Up @@ -141,8 +144,6 @@ public void onServerInit(MinecraftServer server) {
PacketHandlerRegistry.register(savestateHandlerServer);
PacketHandlerRegistry.register(savestateHandlerServer.getPlayerHandler());

EventListenerRegistry.register(savestateHandlerServer.getPlayerHandler());

if (!server.isDedicatedServer()) {
TASmod.tickratechanger.ticksPerSecond = 0F;
TASmod.tickratechanger.tickrateSaved = 20F;
Expand Down Expand Up @@ -174,7 +175,6 @@ public void onServerStop(MinecraftServer mcserver) {
PacketHandlerRegistry.unregister(savestateHandlerServer); // Unregistering the savestatehandler, as a new instance is registered in onServerStart()
PacketHandlerRegistry.unregister(savestateHandlerServer.getPlayerHandler());

EventListenerRegistry.unregister(savestateHandlerServer.getPlayerHandler());
savestateHandlerServer = null;
}
}
Expand Down
2 changes: 2 additions & 0 deletions src/main/java/com/minecrafttas/tasmod/TASmodClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
import com.minecrafttas.tasmod.registries.TASmodKeybinds;
import com.minecrafttas.tasmod.registries.TASmodPackets;
import com.minecrafttas.tasmod.savestates.SavestateHandlerClient;
import com.minecrafttas.tasmod.savestates.handlers.SavestatePlayerHandler;
import com.minecrafttas.tasmod.tickratechanger.TickrateChangerClient;
import com.minecrafttas.tasmod.ticksync.TickSyncClient;
import com.minecrafttas.tasmod.util.LoggerMarkers;
Expand Down Expand Up @@ -150,6 +151,7 @@ private void registerNetworkPacketHandlers() {
PacketHandlerRegistry.register(ticksyncClient);
PacketHandlerRegistry.register(tickratechanger);
PacketHandlerRegistry.register(savestateHandlerClient);
PacketHandlerRegistry.register(new SavestatePlayerHandler(null));
}

private void registerEventListeners() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -269,32 +269,44 @@ public static void loadPlayer(NBTTagCompound compound) {
// Clear any accidental applied potion particles on the client
((AccessorEntityLivingBase) player).clearPotionEffects();

/*
* TODO
* The following 20 lines are all one
* gross workaround for correctly applying the player motion
* to the client...
*
* The motion is applied
* to the player in a previous step and unfortunately
* player.readFromNBT(compound) overwrites the
* previously applied motion...
*
* So this workaround makes sure that the motion is not overwritten
* Fixing this, requires restructuring the steps for loadstating
* and since I plan to do this anyway at some point, I will
* leave this here and be done for today*/
double x = player.motionX;
double y = player.motionY;
double z = player.motionZ;

float rx = player.moveForward;
float ry = player.moveStrafing;
float rz = player.moveVertical;

boolean sprinting = player.isSprinting();
float jumpVector = player.jumpMovementFactor;

player.readFromNBT(compound);
NBTTagCompound motion = compound.getCompoundTag("clientMotion");

if (motion.hasNoTags()) {
LOGGER.warn(LoggerMarkers.Savestate, "Could not load the motion from the savestate. Savestate seems to be created manually or by a different mod");
} else {
LOGGER.trace(LoggerMarkers.Savestate, "Loading client motion from NBT");
double x = motion.getDouble("x");
double y = motion.getDouble("y");
double z = motion.getDouble("z");
player.motionX = x;
player.motionY = y;
player.motionZ = z;

float rx = motion.getFloat("RelativeX");
float ry = motion.getFloat("RelativeY");
float rz = motion.getFloat("RelativeZ");
player.moveForward = rx;
player.moveVertical = ry;
player.moveStrafing = rz;

boolean sprinting = motion.getBoolean("Sprinting");
float jumpVector = motion.getFloat("JumpFactor");
player.setSprinting(sprinting);
player.jumpMovementFactor = jumpVector;
}
player.motionX = x;
player.motionY = y;
player.motionZ = z;

player.moveForward = rx;
player.moveVertical = ry;
player.moveStrafing = rz;

player.setSprinting(sprinting);
player.jumpMovementFactor = jumpVector;

LOGGER.trace(LoggerMarkers.Savestate, "Setting client gamemode");
// #86
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -168,9 +168,6 @@ public void saveState(int savestateIndex, boolean tickrate0, boolean changeIndex
// Enable tickrate 0
TASmod.tickratechanger.pauseGame(true);

// Get the motion from the client
playerHandler.requestMotionFromClient();

// Save the world!
server.getPlayerList().saveAllPlayerData();
server.saveAllWorlds(false);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package com.minecrafttas.tasmod.savestates.exceptions;

public class LoadstateException extends Exception{
public class LoadstateException extends RuntimeException {
/**
*
*/
Expand All @@ -9,4 +9,8 @@ public class LoadstateException extends Exception{
public LoadstateException(String s) {
super(s);
}

public LoadstateException(Throwable t, String msg, Object... args) {
super(String.format(msg, args), t);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -189,7 +189,6 @@ public PacketID[] getAcceptedPacketIDs() {
@Override
public void onServerPacket(PacketID id, ByteBuffer buf, String username) throws PacketNotImplementedException, WrongSideException, Exception {
TASmodPackets packet = (TASmodPackets) id;
EntityPlayerMP player = TASmod.getServerInstance().getPlayerList().getPlayerByUsername(username);

switch (packet) {
case SAVESTATE_PLAYER:
Expand Down
Loading

0 comments on commit 8001f4b

Please sign in to comment.