Skip to content

Commit

Permalink
Continue with packets
Browse files Browse the repository at this point in the history
  • Loading branch information
PancakeTAS committed Jun 14, 2023
1 parent e42630e commit f3fe7ff
Show file tree
Hide file tree
Showing 13 changed files with 153 additions and 336 deletions.
79 changes: 79 additions & 0 deletions src/main/java/com/minecrafttas/server/Client.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,18 @@

import com.minecrafttas.tasmod.TASmod;
import com.minecrafttas.tasmod.TASmodClient;
import com.minecrafttas.tasmod.savestates.client.InputSavestatesHandler;
import com.minecrafttas.tasmod.savestates.client.gui.GuiSavestateSavingScreen;
import com.minecrafttas.tasmod.savestates.server.chunkloading.SavestatesChunkControl;
import com.minecrafttas.tasmod.savestates.server.motion.ClientMotionServer;
import com.minecrafttas.tasmod.savestates.server.motion.ClientMotionServer.Saver;
import com.minecrafttas.tasmod.tickratechanger.TickrateChangerServer.State;
import com.minecrafttas.tasmod.ticksync.TickSyncClient;
import com.minecrafttas.tasmod.ticksync.TickSyncServer;

import lombok.Getter;
import lombok.var;
import net.minecraft.client.Minecraft;

public class Client {

Expand All @@ -28,6 +35,8 @@ public class Client {
private ByteBuffer readBuffer;
private Future<Integer> future;
private Map<Integer, Consumer<ByteBuffer>> handlers = new HashMap<>();

@Getter
private UUID id;

/**
Expand Down Expand Up @@ -138,6 +147,73 @@ private void registerClientsidePacketHandlers() {

// packet 5: change client tickrate
this.handlers.put(5, buf -> TASmodClient.tickratechanger.changeClientTickrate(buf.getFloat()));

// packet 8: advance tick on clients
this.handlers.put(8, buf -> TASmodClient.tickratechanger.advanceClientTick());

// packet 9: change tickrate on client
this.handlers.put(9, buf -> TASmodClient.tickratechanger.changeClientTickrate(buf.getFloat()));

// packet 10: savestate inputs client
this.handlers.put(10, buf -> {
try {
var nameBytes = new byte[buf.getInt()];
buf.get(nameBytes);
var name = new String(nameBytes);
InputSavestatesHandler.savestate(name);
} catch (Exception e) {
TASmod.LOGGER.error("Exception occured during input savestate: {}", e);
}
});

// packet 11: close GuiSavestateScreen on client
this.handlers.put(11, buf ->
Minecraft.getMinecraft().addScheduledTask(() -> {
var mc = Minecraft.getMinecraft();
if (!(mc.currentScreen instanceof GuiSavestateSavingScreen))
mc.displayGuiScreen(new GuiSavestateSavingScreen());
else
mc.displayGuiScreen(null);
})
);

// packet 12: loadstate inputs client
this.handlers.put(12, buf -> {
try {
var nameBytes = new byte[buf.getInt()];
buf.get(nameBytes);
var name = new String(nameBytes);
InputSavestatesHandler.loadstate(name);
} catch (Exception e) {
TASmod.LOGGER.error("Exception occured during input loadstate: {}", e);
}
});

// packet 13: unload chunks on client
this.handlers.put(13, buf ->
Minecraft.getMinecraft().addScheduledTask(() ->
SavestatesChunkControl.unloadAllClientChunks()));

// packet 14: request client motion
this.handlers.put(14, buf -> {
var player = Minecraft.getMinecraft().player;
if (player != null) {
if (!(Minecraft.getMinecraft().currentScreen instanceof GuiSavestateSavingScreen))
Minecraft.getMinecraft().displayGuiScreen(new GuiSavestateSavingScreen());

try {
// packet 15: send client motion to server
TASmodClient.client.write(ByteBuffer.allocate(4 + 8+8+8 + 4+4+4 + 1 + 4).putInt(15)
.putDouble(player.motionX).putDouble(player.motionY).putDouble(player.motionZ)
.putFloat(player.moveForward).putFloat(player.moveVertical).putFloat(player.moveStrafing)
.put((byte) (player.isSprinting() ? 1 : 0))
.putFloat(player.jumpMovementFactor)
);
} catch (Exception e) {
TASmod.LOGGER.error("Unable to send packet to server: {}", e);
}
}
});
}

/**
Expand Down Expand Up @@ -172,6 +248,9 @@ else if (state == State.TOGGLE)
if (TASmod.tickratechanger.ticksPerSecond == 0)
TASmod.tickratechanger.advanceTick();
});

// packet 15: send client motion to server
this.handlers.put(15, buf -> ClientMotionServer.getMotion().put(TASmod.getServerInstance().getPlayerList().getPlayerByUUID(this.id), new Saver(buf.getDouble(), buf.getDouble(), buf.getDouble(), buf.getFloat(), buf.getFloat(), buf.getFloat(), buf.get() == 1 ? true : false, buf.getFloat())));
}

/**
Expand Down
13 changes: 13 additions & 0 deletions src/main/java/com/minecrafttas/server/Server.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import java.nio.channels.CompletionHandler;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

import lombok.Getter;
import lombok.var;
Expand Down Expand Up @@ -73,5 +74,17 @@ public void close() throws IOException {

this.socket.close();
}

/**
* Get client from UUID
* @param uniqueID UUID
*/
public Client getClient(UUID uniqueID) {
for (var client : this.clients)
if (client.getId().equals(uniqueID))
return client;

return null;
}

}
15 changes: 1 addition & 14 deletions src/main/java/com/minecrafttas/tasmod/TASmod.java
Original file line number Diff line number Diff line change
Expand Up @@ -45,12 +45,8 @@
import com.minecrafttas.tasmod.savestates.server.motion.MotionPacket;
import com.minecrafttas.tasmod.savestates.server.motion.RequestMotionPacket;
import com.minecrafttas.tasmod.savestates.server.playerloading.SavestatePlayerLoadingPacket;
import com.minecrafttas.tasmod.tickratechanger.AdvanceTickratePacket;
import com.minecrafttas.tasmod.tickratechanger.ChangeTickratePacket;
import com.minecrafttas.tasmod.tickratechanger.CommandTickrate;
import com.minecrafttas.tasmod.tickratechanger.PauseTickratePacket;
import com.minecrafttas.tasmod.tickratechanger.TickrateChangerServer;
import com.minecrafttas.tasmod.ticksync.TickSyncPacket;
import com.minecrafttas.tasmod.util.TickScheduler;

import net.fabricmc.api.ModInitializer;
Expand Down Expand Up @@ -143,22 +139,13 @@ public void onInitialize() {
tickratechanger = new TickrateChangerServer(LOGGER);
EventListener.register(tickratechanger);


//Tickratechanger
PacketSerializer.registerPacket(ChangeTickratePacket.class);
PacketSerializer.registerPacket(PauseTickratePacket.class);
PacketSerializer.registerPacket(AdvanceTickratePacket.class);

// Savestates
PacketSerializer.registerPacket(SavestatePacket.class);
PacketSerializer.registerPacket(LoadstatePacket.class);

PacketSerializer.registerPacket(InputSavestatesPacket.class);
PacketSerializer.registerPacket(SavestatePlayerLoadingPacket.class);

PacketSerializer.registerPacket(RequestMotionPacket.class);
PacketSerializer.registerPacket(MotionPacket.class);


// KillTheRNG
PacketSerializer.registerPacket(KTRNGSeedPacket.class);
PacketSerializer.registerPacket(KTRNGStartSeedPacket.class);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ public void setGlobalSeedServer(long seedIn) {
@Environment(EnvType.CLIENT)
public void sendGlobalSeedToServer(long seedIn) {
if(isLoaded()) {
if(TASmodClient.packetClient!=null)
if(TASmodClient.client != null)
TASmodClient.packetClient.sendToServer(new KTRNGSeedPacket(seedIn));
else
setGlobalSeedClient(seedIn);
Expand Down Expand Up @@ -129,7 +129,7 @@ public void broadcastStartSeed() {

@Environment(EnvType.CLIENT)
public void setInitialSeed(long initialSeed) {
if(TASmodClient.packetClient != null) {
if(TASmodClient.client != null) {
TASmod.LOGGER.info("Sending initial client seed: {}", initialSeed);
TASmodClient.packetClient.sendToServer(new KTRNGStartSeedPacket(initialSeed)); // TODO Every new player in multiplayer will currently send the initial seed, which is BAD
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -85,9 +85,9 @@ public void redirectRunTick(Minecraft mc) {
@Inject(method = "shutdownMinecraftApplet", at = @At("HEAD"))
public void inject_shutdownMinecraftApplet(CallbackInfo ci) {
try {
if (TASmodClient.packetClient != null) {
if (TASmodClient.client != null) {
TASmodClient.tickratechanger.changeTickrate(20);
TASmodClient.packetClient.killClient();
TASmodClient.client.close();
}
} catch (Exception e) {
e.printStackTrace();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import java.io.File;
import java.io.FileFilter;
import java.io.IOException;
import java.nio.ByteBuffer;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.Collections;
Expand All @@ -19,7 +20,6 @@
import com.minecrafttas.tasmod.events.server.EventSavestate;
import com.minecrafttas.tasmod.mixin.savestates.AccessorAnvilChunkLoader;
import com.minecrafttas.tasmod.mixin.savestates.AccessorChunkLoader;
import com.minecrafttas.tasmod.savestates.client.InputSavestatesPacket;
import com.minecrafttas.tasmod.savestates.server.chunkloading.SavestatesChunkControl;
import com.minecrafttas.tasmod.savestates.server.exceptions.LoadstateException;
import com.minecrafttas.tasmod.savestates.server.exceptions.SavestateDeleteException;
Expand All @@ -31,6 +31,7 @@
import com.minecrafttas.tasmod.savestates.server.playerloading.SavestatePlayerLoading;
import com.minecrafttas.tasmod.util.LoggerMarkers;

import lombok.var;
import net.fabricmc.api.EnvType;
import net.fabricmc.api.Environment;
import net.minecraft.client.Minecraft;
Expand Down Expand Up @@ -182,7 +183,13 @@ public void saveState(int savestateIndex, boolean tickrate0, boolean changeIndex
* Send the name of the world to all players. This will make a savestate of the
* recording on the client with that name
*/
TASmod.packetServer.sendToAll(new InputSavestatesPacket(true, getSavestateName(indexToSave)));
try {
// packet 10: savestate inputs client
var name = this.getSavestateName(indexToSave).getBytes();
TASmod.server.writeAll(ByteBuffer.allocate(8 + name.length).putInt(10).putInt(name.length).put(name));
} catch (Exception e) {
TASmod.LOGGER.error("Unable to send packet to all clients: {}", e);
}
}

// Wait for the chunkloader to save the game
Expand All @@ -206,8 +213,12 @@ public void saveState(int savestateIndex, boolean tickrate0, boolean changeIndex
// Send a notification that the savestate has been loaded
server.getPlayerList().sendMessage(new TextComponentString(TextFormatting.GREEN + "Savestate " + indexToSave + " saved"));

// Close the GuiSavestateScreen on the client
TASmod.packetServer.sendToAll(new SavestatePacket());
try {
// packet 11: close GuiSavestateScreen
TASmod.server.writeAll(ByteBuffer.allocate(4).putInt(11));
} catch (Exception e) {
TASmod.LOGGER.error("Unable to send packet to all clients: {}", e);
}

if (!tickrate0) {
TASmod.tickratechanger.pauseGame(false);
Expand Down Expand Up @@ -308,8 +319,13 @@ public void loadState(int savestateIndex, boolean tickrate0, boolean changeIndex
* InputSavestate)
*/
if (savestateIndex != 0) {
// Load savestate on the client
TASmod.packetServer.sendToAll(new InputSavestatesPacket(false, getSavestateName(indexToLoad)));
try {
// packet 12: loadstate inputs client
var name = this.getSavestateName(indexToLoad).getBytes();
TASmod.server.writeAll(ByteBuffer.allocate(8 + name.length).putInt(12).putInt(name.length).put(name));
} catch (Exception e) {
TASmod.LOGGER.error("Unable to send packet to all clients: {}", e);
}
}

// Disabeling level saving for all worlds in case the auto save kicks in during
Expand All @@ -318,8 +334,13 @@ public void loadState(int savestateIndex, boolean tickrate0, boolean changeIndex
world.disableLevelSaving = true;
}

// Unload chunks on the client
TASmod.packetServer.sendToAll(new LoadstatePacket());

try {
// packet 13: unload chunks on client
TASmod.server.writeAll(ByteBuffer.allocate(4).putInt(13));
} catch (Exception e) {
TASmod.LOGGER.error("Unable to send packet to all clients: {}", e);
}

// Unload chunks on the server
SavestatesChunkControl.disconnectPlayersFromChunkMap(server);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.minecrafttas.tasmod.savestates.server.motion;

import java.nio.ByteBuffer;
import java.util.Map;

import com.google.common.collect.Maps;
Expand All @@ -19,8 +20,14 @@ public static Map<EntityPlayerMP, Saver> getMotion() {
public static void requestMotionFromClient() {
TASmod.LOGGER.trace(LoggerMarkers.Savestate, "Request motion from client");
motion.clear();
TASmod.packetServer.sendToAll(new RequestMotionPacket());
try {
// packet 14: request client motion
TASmod.server.writeAll(ByteBuffer.allocate(4).putInt(14));
} catch (Exception e) {
TASmod.LOGGER.error("Unable to send packet to all clients: {}", e);
}

// TODO: is this still necessary?
int i = 1;
while (motion.size() != TASmod.getServerInstance().getPlayerList().getCurrentPlayerCount()) {
i++;
Expand All @@ -31,7 +38,12 @@ public static void requestMotionFromClient() {
}
if(i % 30 == 1) {
TASmod.LOGGER.debug(LoggerMarkers.Savestate, "Resending motion packet");
TASmod.packetServer.sendToAll(new RequestMotionPacket());
try {
// packet 14: request client motion
TASmod.server.writeAll(ByteBuffer.allocate(4).putInt(14));
} catch (Exception e) {
TASmod.LOGGER.error("Unable to send packet to all clients: {}", e);
}
}
if (i == 1000) {
TASmod.LOGGER.warn(LoggerMarkers.Savestate, "Client motion timed out!");
Expand Down
Loading

0 comments on commit f3fe7ff

Please sign in to comment.