diff --git a/src/main/java/com/minecrafttas/server/Client.java b/src/main/java/com/minecrafttas/server/Client.java index 122acf21..b7bc8480 100644 --- a/src/main/java/com/minecrafttas/server/Client.java +++ b/src/main/java/com/minecrafttas/server/Client.java @@ -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 { @@ -28,6 +35,8 @@ public class Client { private ByteBuffer readBuffer; private Future future; private Map> handlers = new HashMap<>(); + + @Getter private UUID id; /** @@ -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); + } + } + }); } /** @@ -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()))); } /** diff --git a/src/main/java/com/minecrafttas/server/Server.java b/src/main/java/com/minecrafttas/server/Server.java index 32b37c53..58749a81 100644 --- a/src/main/java/com/minecrafttas/server/Server.java +++ b/src/main/java/com/minecrafttas/server/Server.java @@ -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; @@ -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; + } } diff --git a/src/main/java/com/minecrafttas/tasmod/TASmod.java b/src/main/java/com/minecrafttas/tasmod/TASmod.java index bc8ae20a..e9703e7c 100644 --- a/src/main/java/com/minecrafttas/tasmod/TASmod.java +++ b/src/main/java/com/minecrafttas/tasmod/TASmod.java @@ -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; @@ -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); diff --git a/src/main/java/com/minecrafttas/tasmod/ktrng/KillTheRNGHandler.java b/src/main/java/com/minecrafttas/tasmod/ktrng/KillTheRNGHandler.java index 75135a68..3a278d53 100644 --- a/src/main/java/com/minecrafttas/tasmod/ktrng/KillTheRNGHandler.java +++ b/src/main/java/com/minecrafttas/tasmod/ktrng/KillTheRNGHandler.java @@ -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); @@ -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 { diff --git a/src/main/java/com/minecrafttas/tasmod/mixin/MixinMinecraft.java b/src/main/java/com/minecrafttas/tasmod/mixin/MixinMinecraft.java index d51e7a50..566bf0e2 100644 --- a/src/main/java/com/minecrafttas/tasmod/mixin/MixinMinecraft.java +++ b/src/main/java/com/minecrafttas/tasmod/mixin/MixinMinecraft.java @@ -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(); diff --git a/src/main/java/com/minecrafttas/tasmod/savestates/server/SavestateHandler.java b/src/main/java/com/minecrafttas/tasmod/savestates/server/SavestateHandler.java index 2d6d3fe6..39444815 100644 --- a/src/main/java/com/minecrafttas/tasmod/savestates/server/SavestateHandler.java +++ b/src/main/java/com/minecrafttas/tasmod/savestates/server/SavestateHandler.java @@ -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; @@ -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; @@ -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; @@ -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 @@ -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); @@ -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 @@ -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); diff --git a/src/main/java/com/minecrafttas/tasmod/savestates/server/motion/ClientMotionServer.java b/src/main/java/com/minecrafttas/tasmod/savestates/server/motion/ClientMotionServer.java index 49723495..a6bcf3e6 100644 --- a/src/main/java/com/minecrafttas/tasmod/savestates/server/motion/ClientMotionServer.java +++ b/src/main/java/com/minecrafttas/tasmod/savestates/server/motion/ClientMotionServer.java @@ -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; @@ -19,8 +20,14 @@ public static Map 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++; @@ -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!"); diff --git a/src/main/java/com/minecrafttas/tasmod/savestates/server/motion/MotionPacket.java b/src/main/java/com/minecrafttas/tasmod/savestates/server/motion/MotionPacket.java deleted file mode 100644 index 67eec718..00000000 --- a/src/main/java/com/minecrafttas/tasmod/savestates/server/motion/MotionPacket.java +++ /dev/null @@ -1,80 +0,0 @@ -package com.minecrafttas.tasmod.savestates.server.motion; - -import com.minecrafttas.tasmod.savestates.server.motion.ClientMotionServer.Saver; - -import net.minecraft.entity.player.EntityPlayer; -import net.minecraft.entity.player.EntityPlayerMP; -import net.minecraft.network.PacketBuffer; - -/** - * A packet containing the motion data of the player - * @author Scribble - * - */ -public class MotionPacket implements Packet { - private double x = 0; - private double y = 0; - private double z = 0; - private float rx = 0; - private float ry = 0; - private float rz = 0; - private boolean sprinting; - private float jumpMovementVector = 0.2F; - - public MotionPacket() { - } - - /** - * Sends the players motion data to the server - * @param x - * @param y - * @param z - * @param moveForward - * @param moveVertical - * @param moveStrafe - * @param isSprinting - * @param jumpMovementVector - */ - public MotionPacket(double x, double y, double z, float moveForward, float moveVertical, float moveStrafe, boolean isSprinting, float jumpMovementVector) { - this.x = x; - this.y = y; - this.z = z; - this.rx = moveForward; - this.ry = moveVertical; - this.rz = moveStrafe; - sprinting = isSprinting; - this.jumpMovementVector = jumpMovementVector; - } - - @Override - public void handle(PacketSide side, EntityPlayer playercommon) { - if (side.isServer()) { - EntityPlayerMP player = (EntityPlayerMP) playercommon; - ClientMotionServer.getMotion().put(player, new Saver(x, y, z, rx, ry, rz, sprinting, jumpMovementVector)); - } - } - - @Override - public void serialize(PacketBuffer buf) { - buf.writeDouble(x); - buf.writeDouble(y); - buf.writeDouble(z); - buf.writeFloat(rx); - buf.writeFloat(ry); - buf.writeFloat(rz); - buf.writeBoolean(sprinting); - buf.writeFloat(jumpMovementVector); - } - - @Override - public void deserialize(PacketBuffer buf) { - x = buf.readDouble(); - y = buf.readDouble(); - z = buf.readDouble(); - rx = buf.readFloat(); - ry = buf.readFloat(); - rz = buf.readFloat(); - sprinting = buf.readBoolean(); - jumpMovementVector = buf.readFloat(); - } -} diff --git a/src/main/java/com/minecrafttas/tasmod/savestates/server/motion/RequestMotionPacket.java b/src/main/java/com/minecrafttas/tasmod/savestates/server/motion/RequestMotionPacket.java deleted file mode 100644 index 9b3744aa..00000000 --- a/src/main/java/com/minecrafttas/tasmod/savestates/server/motion/RequestMotionPacket.java +++ /dev/null @@ -1,48 +0,0 @@ -package com.minecrafttas.tasmod.savestates.server.motion; - -import com.minecrafttas.tasmod.TASmodClient; -import com.minecrafttas.tasmod.savestates.client.gui.GuiSavestateSavingScreen; - -import net.minecraft.client.Minecraft; -import net.minecraft.client.entity.EntityPlayerSP; -import net.minecraft.entity.player.EntityPlayer; -import net.minecraft.network.PacketBuffer; - -/** - * Requests the motion from the client - * @author Scribble - * - */ -public class RequestMotionPacket implements Packet { - - /** - * Requests the motion from the client - */ - public RequestMotionPacket() { - } - - @Override - public void handle(PacketSide side, EntityPlayer playerz) { - if(side.isClient()) { - EntityPlayerSP player = (EntityPlayerSP) playerz; - if (player != null) { - if (!(Minecraft.getMinecraft().currentScreen instanceof GuiSavestateSavingScreen)) { - Minecraft.getMinecraft().displayGuiScreen(new GuiSavestateSavingScreen()); - } - TASmodClient.packetClient.sendToServer(new MotionPacket(player.motionX, player.motionY, player.motionZ, player.moveForward, player.moveVertical, player.moveStrafing, player.isSprinting(), player.jumpMovementFactor)); - } - } - - } - - @Override - public void serialize(PacketBuffer buf) { - - } - - @Override - public void deserialize(PacketBuffer buf) { - - } - -} diff --git a/src/main/java/com/minecrafttas/tasmod/tickratechanger/AdvanceTickratePacket.java b/src/main/java/com/minecrafttas/tasmod/tickratechanger/AdvanceTickratePacket.java deleted file mode 100644 index b9479590..00000000 --- a/src/main/java/com/minecrafttas/tasmod/tickratechanger/AdvanceTickratePacket.java +++ /dev/null @@ -1,45 +0,0 @@ -package com.minecrafttas.tasmod.tickratechanger; - -import com.minecrafttas.tasmod.TASmod; -import com.minecrafttas.tasmod.TASmodClient; -import net.minecraft.entity.player.EntityPlayer; -import net.minecraft.network.PacketBuffer; - -/** - * Advanced game by 1 tick - * - * @author Scribble - * - */ -public class AdvanceTickratePacket implements Packet { - /** - * Advanced game by 1 tick - */ - public AdvanceTickratePacket() { - } - - @Override - public void handle(PacketSide side, EntityPlayer player) { - if (side.isServer()) { - if (player.canUseCommand(2, "tickrate")) { - if (TASmod.tickratechanger.ticksPerSecond == 0) { - TASmod.tickratechanger.advanceTick(); - } - } - } else { - TASmodClient.tickratechanger.advanceClientTick(); // Using advanceTick() would create an endless loop - } - } - - @Override - public void serialize(PacketBuffer buf) { - - } - - @Override - public void deserialize(PacketBuffer buf) { - - } - - -} diff --git a/src/main/java/com/minecrafttas/tasmod/tickratechanger/ChangeTickratePacket.java b/src/main/java/com/minecrafttas/tasmod/tickratechanger/ChangeTickratePacket.java deleted file mode 100644 index cbe8d703..00000000 --- a/src/main/java/com/minecrafttas/tasmod/tickratechanger/ChangeTickratePacket.java +++ /dev/null @@ -1,51 +0,0 @@ -package com.minecrafttas.tasmod.tickratechanger; - -import com.minecrafttas.tasmod.TASmod; -import com.minecrafttas.tasmod.TASmodClient; -import net.minecraft.entity.player.EntityPlayer; -import net.minecraft.network.PacketBuffer; - -/** - * Changes the tickrate on the other side - * - * @author Scribble - * - */ -public class ChangeTickratePacket implements Packet { - - float tickrate; - - public ChangeTickratePacket() { - } - - /** - * Changes the tickrate on the other side - * - * @param tickrate The new tickrate - */ - public ChangeTickratePacket(float tickrate) { - this.tickrate = tickrate; - } - - @Override - public void handle(PacketSide side, EntityPlayer player) { - if (side.isServer()) { - if (player.canUseCommand(2, "tickrate")) { - TASmod.tickratechanger.changeTickrate(tickrate); - } - } else { - TASmodClient.tickratechanger.changeClientTickrate(tickrate); - } - } - - @Override - public void serialize(PacketBuffer buf) { - buf.writeFloat(tickrate); - } - - @Override - public void deserialize(PacketBuffer buf) { - tickrate = buf.readFloat(); - } - -} diff --git a/src/main/java/com/minecrafttas/tasmod/tickratechanger/PauseTickratePacket.java b/src/main/java/com/minecrafttas/tasmod/tickratechanger/PauseTickratePacket.java deleted file mode 100644 index 659c1ace..00000000 --- a/src/main/java/com/minecrafttas/tasmod/tickratechanger/PauseTickratePacket.java +++ /dev/null @@ -1,79 +0,0 @@ -package com.minecrafttas.tasmod.tickratechanger; - -import com.minecrafttas.tasmod.TASmod; -import com.minecrafttas.tasmod.TASmodClient; -import net.minecraft.entity.player.EntityPlayer; -import net.minecraft.network.PacketBuffer; - -/** - * Sets the game to tickrate 0 and back - * - * @author Scribble - * - */ -public class PauseTickratePacket implements Packet { - - private short status; - - /** - * Toggles the tickrate between 0 and tickrate > 0 - */ - public PauseTickratePacket() { - status = 0; - } - - /** - * Changes the state to either PAUSE UNPAUSE or TOGGLED - * - * @param state The state - */ - public PauseTickratePacket(State state) { - this.status = state.toShort(); - } - - public State getState() { - return State.fromShort(status); - } - - - /** - * Can be {@link State#PAUSE}, {@link State#UNPAUSE} or {@link State#TOGGLE} - * - * @author Scribble - * - */ - - - @Override - public void handle(PacketSide side, EntityPlayer player) { - if (side.isServer()) { - if (player.canUseCommand(2, "tickrate")) { - State state = getState(); - if (state == State.PAUSE) - TASmod.tickratechanger.pauseGame(true); - else if (state == State.UNPAUSE) - TASmod.tickratechanger.pauseGame(false); - else if (state == State.TOGGLE) - TASmod.tickratechanger.togglePause(); - } - } else if (side.isClient()) { - State state = getState(); - if (state == State.PAUSE) - TASmodClient.tickratechanger.pauseClientGame(true); - else if (state == State.UNPAUSE) - TASmodClient.tickratechanger.pauseClientGame(false); - else if (state == State.TOGGLE) - TASmodClient.tickratechanger.togglePauseClient(); - } - } - - @Override - public void serialize(PacketBuffer buf) { - buf.writeShort(status); - } - - @Override - public void deserialize(PacketBuffer buf) { - status = buf.readShort(); - } -} diff --git a/src/main/java/com/minecrafttas/tasmod/tickratechanger/TickrateChangerServer.java b/src/main/java/com/minecrafttas/tasmod/tickratechanger/TickrateChangerServer.java index e0affa27..7992de29 100644 --- a/src/main/java/com/minecrafttas/tasmod/tickratechanger/TickrateChangerServer.java +++ b/src/main/java/com/minecrafttas/tasmod/tickratechanger/TickrateChangerServer.java @@ -7,8 +7,6 @@ import com.minecrafttas.common.events.server.EventServerStop; import com.minecrafttas.common.events.server.player.EventPlayerJoinedServerSide; import com.minecrafttas.tasmod.TASmod; -import com.minecrafttas.tasmod.TASmodClient; -import com.minecrafttas.tasmod.tickratechanger.PauseTickratePacket.State; import com.minecrafttas.tasmod.util.LoggerMarkers; import net.minecraft.entity.player.EntityPlayerMP; @@ -175,8 +173,13 @@ public void advanceTick() { * Sends a {@link AdvanceTickratePacket} to all clients */ private static void advanceClientTick() { - TASmod.packetServer.sendToAll(new AdvanceTickratePacket()); - } + try { + // packet 8: advance tick on clients + TASmod.server.writeAll(ByteBuffer.allocate(4).putInt(8)); + } catch (Exception e) { + TASmod.LOGGER.error("Unable to send packet to all clients: {}", e); + } + } /** * Advances the server by 1 tick @@ -196,7 +199,12 @@ private void advanceServerTick() { public void onPlayerJoinedServerSide(EntityPlayerMP player) { if(TASmod.getServerInstance().isDedicatedServer()) { log("Sending the current tickrate ("+ticksPerSecond+") to " +player.getName()); - TASmod.packetServer.sendTo(new ChangeTickratePacket(ticksPerSecond), player); + try { + // packet 9: change tickrate on client + TASmod.server.getClient(player.getUniqueID()).write(ByteBuffer.allocate(8).putInt(9).putFloat(ticksPerSecond)); + } catch (Exception e) { + TASmod.LOGGER.error("Unable to send packet to {}: {}", player.getUniqueID(), e); + } } }