diff --git a/src/main/java/com/minecrafttas/server/Client.java b/src/main/java/com/minecrafttas/server/Client.java index 759e4176..122acf21 100644 --- a/src/main/java/com/minecrafttas/server/Client.java +++ b/src/main/java/com/minecrafttas/server/Client.java @@ -13,6 +13,12 @@ import java.util.concurrent.Future; import java.util.function.Consumer; +import com.minecrafttas.tasmod.TASmod; +import com.minecrafttas.tasmod.TASmodClient; +import com.minecrafttas.tasmod.tickratechanger.TickrateChangerServer.State; +import com.minecrafttas.tasmod.ticksync.TickSyncClient; +import com.minecrafttas.tasmod.ticksync.TickSyncServer; + import lombok.var; public class Client { @@ -127,17 +133,45 @@ public void close() throws IOException { * Register packet handlers for packets received on the client */ private void registerClientsidePacketHandlers() { + // packet 2: tick client + this.handlers.put(2, buf -> TickSyncClient.onPacket()); + // packet 5: change client tickrate + this.handlers.put(5, buf -> TASmodClient.tickratechanger.changeClientTickrate(buf.getFloat())); } /** * Register packet handlers for packets received on the server */ private void registerServersidePacketHandlers() { + // packet 1: authentication packet this.handlers.put(1, buf -> { this.id = new UUID(buf.getLong(), buf.getLong()); LOGGER.info("Client authenticated: " + this.id); }); + + // packet 3: notify server of tick pass + this.handlers.put(3, buf -> TickSyncServer.onPacket(this.id)); + + // packet 4: request tickrate change + this.handlers.put(4, buf -> TASmod.tickratechanger.changeTickrate(buf.getFloat())); + + // packet 6: tickrate zero toggle + this.handlers.put(6, buf -> { + var state = State.fromShort(buf.getShort()); + 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(); + }); + + // packet 7: request tick advance + this.handlers.put(7, buf -> { + if (TASmod.tickratechanger.ticksPerSecond == 0) + TASmod.tickratechanger.advanceTick(); + }); } /** @@ -146,6 +180,8 @@ private void registerServersidePacketHandlers() { * @throws Exception Unable to send packet */ public void authenticate(UUID id) throws Exception { + this.id = id; + ByteBuffer buf = ByteBuffer.allocate(4+8+8); buf.putInt(1); buf.putLong(id.getMostSignificantBits()); diff --git a/src/main/java/com/minecrafttas/server/Server.java b/src/main/java/com/minecrafttas/server/Server.java index 74b12464..32b37c53 100644 --- a/src/main/java/com/minecrafttas/server/Server.java +++ b/src/main/java/com/minecrafttas/server/Server.java @@ -11,11 +11,14 @@ import java.util.ArrayList; import java.util.List; +import lombok.Getter; import lombok.var; public class Server { private AsynchronousServerSocketChannel socket; + + @Getter private List clients; /** diff --git a/src/main/java/com/minecrafttas/tasmod/TASmod.java b/src/main/java/com/minecrafttas/tasmod/TASmod.java index 608bdfe5..ccd4b5da 100644 --- a/src/main/java/com/minecrafttas/tasmod/TASmod.java +++ b/src/main/java/com/minecrafttas/tasmod/TASmod.java @@ -32,9 +32,6 @@ import com.minecrafttas.tasmod.ktrng.KTRNGSeedPacket; import com.minecrafttas.tasmod.ktrng.KTRNGStartSeedPacket; import com.minecrafttas.tasmod.ktrng.KillTheRNGHandler; -import com.minecrafttas.tasmod.networking.IdentificationPacket; -import com.minecrafttas.tasmod.networking.PacketSerializer; -import com.minecrafttas.tasmod.networking.TASmodNetworkServer; import com.minecrafttas.tasmod.playback.PlaybackController; import com.minecrafttas.tasmod.playback.server.InitialSyncStatePacket; import com.minecrafttas.tasmod.playback.server.SyncStatePacket; @@ -78,8 +75,6 @@ public class TASmod implements ModInitializer, EventServerInit, EventServerStop{ public static KillTheRNGHandler ktrngHandler; - public static TASmodNetworkServer packetServer; - public static TickrateChangerServer tickratechanger; public static final TickScheduler tickSchedulerServer = new TickScheduler(); @@ -115,12 +110,6 @@ public void onServerInit(MinecraftServer server) { savestateHandler=new SavestateHandler(server, LOGGER); - try { - packetServer = new TASmodNetworkServer(LOGGER); - } catch (IOException e) { - e.printStackTrace(); - } - if(!server.isDedicatedServer()) { TASmod.tickratechanger.ticksPerSecond=0F; TASmod.tickratechanger.tickrateSaved=20F; @@ -130,7 +119,6 @@ public void onServerInit(MinecraftServer server) { @Override public void onServerStop(MinecraftServer mcserver) { serverInstance=null; - packetServer.close(); try { if (server != null) server.close(); } catch (IOException e) { @@ -155,18 +143,12 @@ public void onInitialize() { tickratechanger = new TickrateChangerServer(LOGGER); EventListener.register(tickratechanger); - - PacketSerializer.registerPacket(IdentificationPacket.class); - // Ticksync - PacketSerializer.registerPacket(TickSyncPacket.class); - //Tickratechanger PacketSerializer.registerPacket(ChangeTickratePacket.class); PacketSerializer.registerPacket(PauseTickratePacket.class); PacketSerializer.registerPacket(AdvanceTickratePacket.class); - // Savestates PacketSerializer.registerPacket(SavestatePacket.class); PacketSerializer.registerPacket(LoadstatePacket.class); diff --git a/src/main/java/com/minecrafttas/tasmod/TASmodClient.java b/src/main/java/com/minecrafttas/tasmod/TASmodClient.java index 606d8660..e73076e4 100644 --- a/src/main/java/com/minecrafttas/tasmod/TASmodClient.java +++ b/src/main/java/com/minecrafttas/tasmod/TASmodClient.java @@ -21,7 +21,6 @@ import com.minecrafttas.tasmod.gui.InfoHud; import com.minecrafttas.tasmod.handlers.InterpolationHandler; import com.minecrafttas.tasmod.handlers.LoadingScreenHandler; -import com.minecrafttas.tasmod.networking.TASmodNetworkClient; import com.minecrafttas.tasmod.playback.PlaybackController.TASstate; import com.minecrafttas.tasmod.playback.PlaybackSerialiser; import com.minecrafttas.tasmod.playback.server.InitialSyncStatePacket; @@ -57,8 +56,6 @@ public class TASmodClient implements ClientModInitializer, EventClientInit, Even public static ShieldDownloader shieldDownloader; - public static TASmodNetworkClient packetClient; - public static TickrateChangerClient tickratechanger = new TickrateChangerClient(); public static TickScheduler gameLoopSchedulerClient = new TickScheduler(); @@ -174,38 +171,13 @@ public void onClientInit(Minecraft mc) { @Override public void onPlayerJoinedClientSide(EntityPlayerSP player) { - Minecraft mc = Minecraft.getMinecraft(); - - if(mc.isIntegratedServerRunning()) - TASmodClient.packetClient = new TASmodNetworkClient(TASmod.LOGGER); - else { - String full = mc.getCurrentServerData().serverIP; - String[] fullsplit = full.split(":"); - if(fullsplit.length == 1) { - TASmodClient.packetClient = new TASmodNetworkClient(TASmod.LOGGER, full, 3111); - } else if(fullsplit.length == 2){ - String ip = fullsplit[0]; - TASmodClient.packetClient = new TASmodNetworkClient(TASmod.LOGGER, ip, 3111); - } else { - TASmod.LOGGER.error("Something went wrong while connecting. The ip seems to be wrong"); - } - } - - TASmodClient.packetClient.sendToServer(new InitialSyncStatePacket(TASmodClient.virtual.getContainer().getState())); - - + // FIXME: ask how this works + // TASmodClient.packetClient.sendToServer(new InitialSyncStatePacket(TASmodClient.virtual.getContainer().getState())); } @Override public void onPlayerLeaveClientSide(EntityPlayerSP player) { - try { - if(TASmodClient.packetClient!=null) { - TASmodClient.packetClient.killClient(); - TASmodClient.packetClient=null; - } - } catch (IOException e) { - e.printStackTrace(); - } + } } diff --git a/src/main/java/com/minecrafttas/tasmod/commands/clearinputs/ClearInputsPacket.java b/src/main/java/com/minecrafttas/tasmod/commands/clearinputs/ClearInputsPacket.java index f22b5fb6..31979c42 100644 --- a/src/main/java/com/minecrafttas/tasmod/commands/clearinputs/ClearInputsPacket.java +++ b/src/main/java/com/minecrafttas/tasmod/commands/clearinputs/ClearInputsPacket.java @@ -2,9 +2,6 @@ import com.minecrafttas.tasmod.TASmod; import com.minecrafttas.tasmod.TASmodClient; -import com.minecrafttas.tasmod.networking.Packet; -import com.minecrafttas.tasmod.networking.PacketSide; - import net.minecraft.client.Minecraft; import net.minecraft.entity.player.EntityPlayer; import net.minecraft.entity.player.EntityPlayerMP; diff --git a/src/main/java/com/minecrafttas/tasmod/commands/folder/FolderPacket.java b/src/main/java/com/minecrafttas/tasmod/commands/folder/FolderPacket.java index cb805329..3c9906d9 100644 --- a/src/main/java/com/minecrafttas/tasmod/commands/folder/FolderPacket.java +++ b/src/main/java/com/minecrafttas/tasmod/commands/folder/FolderPacket.java @@ -1,8 +1,5 @@ package com.minecrafttas.tasmod.commands.folder; -import com.minecrafttas.tasmod.networking.Packet; -import com.minecrafttas.tasmod.networking.PacketSide; - import net.minecraft.entity.player.EntityPlayer; import net.minecraft.network.PacketBuffer; diff --git a/src/main/java/com/minecrafttas/tasmod/commands/fullplay/FullPlayPacket.java b/src/main/java/com/minecrafttas/tasmod/commands/fullplay/FullPlayPacket.java index 86a7ae0e..6800706b 100644 --- a/src/main/java/com/minecrafttas/tasmod/commands/fullplay/FullPlayPacket.java +++ b/src/main/java/com/minecrafttas/tasmod/commands/fullplay/FullPlayPacket.java @@ -2,8 +2,6 @@ import com.minecrafttas.tasmod.TASmodClient; import com.minecrafttas.tasmod.events.OpenGuiEvents; -import com.minecrafttas.tasmod.networking.Packet; -import com.minecrafttas.tasmod.networking.PacketSide; import com.minecrafttas.tasmod.playback.PlaybackController.TASstate; import net.minecraft.client.Minecraft; diff --git a/src/main/java/com/minecrafttas/tasmod/commands/fullrecord/FullRecordPacket.java b/src/main/java/com/minecrafttas/tasmod/commands/fullrecord/FullRecordPacket.java index 55c4242e..3d556d65 100644 --- a/src/main/java/com/minecrafttas/tasmod/commands/fullrecord/FullRecordPacket.java +++ b/src/main/java/com/minecrafttas/tasmod/commands/fullrecord/FullRecordPacket.java @@ -2,8 +2,6 @@ import com.minecrafttas.tasmod.TASmodClient; import com.minecrafttas.tasmod.events.OpenGuiEvents; -import com.minecrafttas.tasmod.networking.Packet; -import com.minecrafttas.tasmod.networking.PacketSide; import com.minecrafttas.tasmod.playback.PlaybackController.TASstate; import net.minecraft.client.Minecraft; diff --git a/src/main/java/com/minecrafttas/tasmod/commands/loadtas/LoadTASPacket.java b/src/main/java/com/minecrafttas/tasmod/commands/loadtas/LoadTASPacket.java index 09c5059c..4af9941e 100644 --- a/src/main/java/com/minecrafttas/tasmod/commands/loadtas/LoadTASPacket.java +++ b/src/main/java/com/minecrafttas/tasmod/commands/loadtas/LoadTASPacket.java @@ -6,9 +6,6 @@ import com.minecrafttas.tasmod.TASmod; import com.minecrafttas.tasmod.TASmodClient; -import com.minecrafttas.tasmod.networking.Packet; -import com.minecrafttas.tasmod.networking.PacketSide; - import net.minecraft.client.Minecraft; import net.minecraft.entity.player.EntityPlayer; import net.minecraft.entity.player.EntityPlayerMP; diff --git a/src/main/java/com/minecrafttas/tasmod/commands/playuntil/PlayUntilPacket.java b/src/main/java/com/minecrafttas/tasmod/commands/playuntil/PlayUntilPacket.java index b4bdbe58..48a0408a 100644 --- a/src/main/java/com/minecrafttas/tasmod/commands/playuntil/PlayUntilPacket.java +++ b/src/main/java/com/minecrafttas/tasmod/commands/playuntil/PlayUntilPacket.java @@ -1,9 +1,6 @@ package com.minecrafttas.tasmod.commands.playuntil; import com.minecrafttas.tasmod.TASmodClient; -import com.minecrafttas.tasmod.networking.Packet; -import com.minecrafttas.tasmod.networking.PacketSide; - import net.minecraft.entity.player.EntityPlayer; import net.minecraft.network.PacketBuffer; diff --git a/src/main/java/com/minecrafttas/tasmod/commands/restartandplay/RestartAndPlayPacket.java b/src/main/java/com/minecrafttas/tasmod/commands/restartandplay/RestartAndPlayPacket.java index 044db2cf..7f3811aa 100644 --- a/src/main/java/com/minecrafttas/tasmod/commands/restartandplay/RestartAndPlayPacket.java +++ b/src/main/java/com/minecrafttas/tasmod/commands/restartandplay/RestartAndPlayPacket.java @@ -4,9 +4,6 @@ import com.minecrafttas.common.Configuration.ConfigOptions; import com.minecrafttas.tasmod.TASmodClient; -import com.minecrafttas.tasmod.networking.Packet; -import com.minecrafttas.tasmod.networking.PacketSide; - import net.minecraft.client.Minecraft; import net.minecraft.entity.player.EntityPlayer; import net.minecraft.network.PacketBuffer; diff --git a/src/main/java/com/minecrafttas/tasmod/commands/savetas/SaveTASPacket.java b/src/main/java/com/minecrafttas/tasmod/commands/savetas/SaveTASPacket.java index f2f067db..048cbca8 100644 --- a/src/main/java/com/minecrafttas/tasmod/commands/savetas/SaveTASPacket.java +++ b/src/main/java/com/minecrafttas/tasmod/commands/savetas/SaveTASPacket.java @@ -5,9 +5,6 @@ import com.minecrafttas.tasmod.TASmod; import com.minecrafttas.tasmod.TASmodClient; -import com.minecrafttas.tasmod.networking.Packet; -import com.minecrafttas.tasmod.networking.PacketSide; - import net.minecraft.client.Minecraft; import net.minecraft.entity.player.EntityPlayer; import net.minecraft.entity.player.EntityPlayerMP; diff --git a/src/main/java/com/minecrafttas/tasmod/ktrng/KTRNGSeedPacket.java b/src/main/java/com/minecrafttas/tasmod/ktrng/KTRNGSeedPacket.java index c8523c8c..b943d7ed 100644 --- a/src/main/java/com/minecrafttas/tasmod/ktrng/KTRNGSeedPacket.java +++ b/src/main/java/com/minecrafttas/tasmod/ktrng/KTRNGSeedPacket.java @@ -1,9 +1,6 @@ package com.minecrafttas.tasmod.ktrng; import com.minecrafttas.tasmod.TASmod; -import com.minecrafttas.tasmod.networking.Packet; -import com.minecrafttas.tasmod.networking.PacketSide; - import net.minecraft.entity.player.EntityPlayer; import net.minecraft.network.PacketBuffer; diff --git a/src/main/java/com/minecrafttas/tasmod/ktrng/KTRNGStartSeedPacket.java b/src/main/java/com/minecrafttas/tasmod/ktrng/KTRNGStartSeedPacket.java index dce516f1..329486b9 100644 --- a/src/main/java/com/minecrafttas/tasmod/ktrng/KTRNGStartSeedPacket.java +++ b/src/main/java/com/minecrafttas/tasmod/ktrng/KTRNGStartSeedPacket.java @@ -2,9 +2,6 @@ import com.minecrafttas.tasmod.TASmod; import com.minecrafttas.tasmod.TASmodClient; -import com.minecrafttas.tasmod.networking.Packet; -import com.minecrafttas.tasmod.networking.PacketSide; - import net.minecraft.entity.player.EntityPlayer; import net.minecraft.network.PacketBuffer; diff --git a/src/main/java/com/minecrafttas/tasmod/networking/IdentificationPacket.java b/src/main/java/com/minecrafttas/tasmod/networking/IdentificationPacket.java deleted file mode 100644 index 3bcb79e5..00000000 --- a/src/main/java/com/minecrafttas/tasmod/networking/IdentificationPacket.java +++ /dev/null @@ -1,53 +0,0 @@ -package com.minecrafttas.tasmod.networking; - -import java.util.UUID; - -import com.minecrafttas.tasmod.TASmodClient; -import com.minecrafttas.tasmod.ticksync.TickSyncServer; - -import net.minecraft.entity.player.EntityPlayer; -import net.minecraft.network.PacketBuffer; - -/** - * Used to identify the socket. This enables the server to send packets to a specific player - * @author Scribble - */ -public class IdentificationPacket implements Packet { - - private UUID uuid; - - public IdentificationPacket() { - - } - - public IdentificationPacket(UUID uuid) { - this.uuid = uuid; - } - - @Override - public void handle(PacketSide side, EntityPlayer player) { - if (side.isServer()) { - TickSyncServer.onPacket(this.uuid); - } else { - TASmodClient.packetClient.setReady(); - } - } - - @Override - public void serialize(PacketBuffer buf) { - if (uuid != null) { - buf.writeUniqueId(uuid); - } - } - - @Override - public void deserialize(PacketBuffer buf) { - if (buf.capacity() > 0) { - this.uuid = buf.readUniqueId(); - } - } - - public UUID getUuid() { - return uuid; - } -} diff --git a/src/main/java/com/minecrafttas/tasmod/networking/Packet.java b/src/main/java/com/minecrafttas/tasmod/networking/Packet.java deleted file mode 100644 index 776774f3..00000000 --- a/src/main/java/com/minecrafttas/tasmod/networking/Packet.java +++ /dev/null @@ -1,36 +0,0 @@ -package com.minecrafttas.tasmod.networking; - -import net.minecraft.entity.player.EntityPlayer; -import net.minecraft.network.PacketBuffer; - -/** - * This is a Packet transmittable over the TASmod Custom Server. - * - * IMPLEMENTATION NOTICE: - * There are no clientbound or serverbound packets as they were deemed unnecessary. This means both the server and the client can transmit any packet. - * @author Pancake - */ -public interface Packet { - - /** - * Once a packet is being received it is immediately handled in this method. - * - * IMPLEMENTATION NOTICE: - * This process is non-blocking for the game and executed on the tasmod server thread temporarily blocking it. - */ - void handle(PacketSide side, EntityPlayer player); - - /** - * In order to transfer packets over the network connection they need to be serialized into a stream of bytes. - * @param buf Packet buffer to serialize to - * @return A serializable packet buffer - */ - void serialize(PacketBuffer buf); - - /** - * In order to receive packets over the network connection the other end serializes the packet into a stream of bytes. Therefore this end needs to deserialize the packet - * @param buf A deserializable packet buffer - */ - void deserialize(PacketBuffer buf); - -} diff --git a/src/main/java/com/minecrafttas/tasmod/networking/PacketSerializer.java b/src/main/java/com/minecrafttas/tasmod/networking/PacketSerializer.java deleted file mode 100644 index 743912b6..00000000 --- a/src/main/java/com/minecrafttas/tasmod/networking/PacketSerializer.java +++ /dev/null @@ -1,84 +0,0 @@ -package com.minecrafttas.tasmod.networking; - -import java.util.ArrayList; - -import com.minecrafttas.tasmod.TASmod; - -import io.netty.buffer.Unpooled; -import net.minecraft.network.PacketBuffer; - -/** - * This class helps serializing and deserializing packets - * @author Pancake - */ -public class PacketSerializer { - - private static ArrayList> REGISTRY = new ArrayList<>(); - - /** - * Deserialize a TASmod packet from a packet buffer. The packet class is prefixed with an id and read here. - * - * @param buf Serialized byte buffer with id prefix - * @return Deserialized packet - */ - public static Packet deserialize(PacketBuffer buf) { - // Read packet id and deserialize the correct packet - int packetId = buf.readInt(); - - Packet packet=null; - try { - packet = REGISTRY.get(packetId).newInstance(); - } catch (InstantiationException e) { - e.printStackTrace(); - } catch (IllegalAccessException e) { - e.printStackTrace(); - } - - if(packet == null) { - TASmod.LOGGER.warn("Unregistered packet received! Packet Id: " + packetId); - return null; - } - - packet.deserialize(buf); - return packet; - } - - /** - * Serialize a TASmod packet to a packet buffer. The packet class is read and a id prefixed packet buffer is returned - * - * @param packet Non-serialized packet - * @return Serialized packet buffer with id prefix - */ - public static PacketBuffer serialize(Packet packet) { - // Figure out packet class and prefix the correct id - - PacketBuffer buf = new PacketBuffer(Unpooled.buffer()); - int packetID = REGISTRY.indexOf(packet.getClass()); - - if(packetID == -1) { - TASmod.LOGGER.warn("Unregistered packet was trying to be serialized! Packet Class: " + packet.getClass().getSimpleName()); - return null; - } - - buf.writeInt(packetID); - - packet.serialize(buf); - return buf; - } - - public static void registerPacket(Class packet) { - TASmod.LOGGER.trace("Registering packet {}", packet.getClass().getSimpleName()); - if(REGISTRY.contains(packet)) { - TASmod.LOGGER.warn("Trying to register packet which already exists: {}", packet.getClass().getSimpleName()); - } - REGISTRY.add(packet); - } - - public static void unregisterPacket(Class packet) { - if(REGISTRY.contains(packet)) { - TASmod.LOGGER.warn("Trying to unregister packet which doesn't exist in the registry: {}", packet.getClass().getSimpleName()); - } - REGISTRY.remove(packet); - } - -} diff --git a/src/main/java/com/minecrafttas/tasmod/networking/PacketSide.java b/src/main/java/com/minecrafttas/tasmod/networking/PacketSide.java deleted file mode 100644 index 7b9c0e48..00000000 --- a/src/main/java/com/minecrafttas/tasmod/networking/PacketSide.java +++ /dev/null @@ -1,14 +0,0 @@ -package com.minecrafttas.tasmod.networking; - -public enum PacketSide { - CLIENT, - SERVER; - - public boolean isClient() { - return this == CLIENT; - } - - public boolean isServer() { - return this == SERVER; - } -} diff --git a/src/main/java/com/minecrafttas/tasmod/networking/TASmodNetworkClient.java b/src/main/java/com/minecrafttas/tasmod/networking/TASmodNetworkClient.java deleted file mode 100644 index 1fff85d7..00000000 --- a/src/main/java/com/minecrafttas/tasmod/networking/TASmodNetworkClient.java +++ /dev/null @@ -1,145 +0,0 @@ -package com.minecrafttas.tasmod.networking; - -import java.io.BufferedInputStream; -import java.io.BufferedOutputStream; -import java.io.DataInputStream; -import java.io.DataOutputStream; -import java.io.EOFException; -import java.io.IOException; -import java.io.InterruptedIOException; -import java.net.InetSocketAddress; -import java.net.Socket; -import java.net.SocketException; -import java.util.concurrent.BlockingQueue; -import java.util.concurrent.LinkedBlockingQueue; - -import org.apache.logging.log4j.Logger; - -import com.minecrafttas.tasmod.TASmodClient; - -import io.netty.buffer.Unpooled; -import net.minecraft.client.Minecraft; -import net.minecraft.network.PacketBuffer; - -public class TASmodNetworkClient { - - private Logger logger; - - private Thread clientThread; - - private Socket clientSocket; - - private BlockingQueue packetsToSend = new LinkedBlockingQueue<>(); - - private boolean ready = false; - - public TASmodNetworkClient(Logger logger) { - this(logger, "127.0.0.1", 3111); // Set ip for different server - } - - public TASmodNetworkClient(Logger logger, String serverIP, int port) { - this.logger = logger; - this.logger.info("Trying to connect to {}:{}", serverIP, port); - createClient(serverIP, port); - } - - public void sendToServer(Packet packet) { - if(clientThread == null) - return; - if(!clientThread.isAlive()) - return; - packetsToSend.add(packet); - } - - private void createClient(String serverIp, int port) { - - packetsToSend.add(new IdentificationPacket(Minecraft.getMinecraft().player.getUniqueID())); - - clientThread = new Thread(() -> { - try(Socket cSocket = new Socket()){ - cSocket.connect(new InetSocketAddress(serverIp, port)); - this.clientSocket = cSocket; - - clientSocket.setTcpNoDelay(true); - // Prepare the in and out streams. - DataInputStream inputStream = new DataInputStream(new BufferedInputStream(clientSocket.getInputStream())); - - createSendThread(); - - // Use the current thread to indefinitly fetch packets - Packet packet; - while (clientSocket.isConnected()) { - // Handle the next packet. If no packet is avilable, the readInt() call will hang until there is one. - int packetSize = inputStream.readInt(); - byte[] packetData = new byte[packetSize]; - inputStream.read(packetData, 0, packetSize); - PacketBuffer packetBuf = new PacketBuffer(Unpooled.wrappedBuffer(packetData)); - // Deserialize and run the packet - packet = PacketSerializer.deserialize(packetBuf); - packet.handle(PacketSide.CLIENT, Minecraft.getMinecraft().player); - logger.trace("Handled a " + packet.getClass().getSimpleName() + " from the socket."); - } - - } catch (EOFException | SocketException | InterruptedIOException exception) { - // The custom TASmod client was closed and the end of stream was reached. The socket was shut down properly. - logger.info("Custom TASmod client was shutdown"); - } catch (Exception exception) { - logger.error("Custom TASmod client was unexpectedly shutdown {}", exception); - exception.printStackTrace(); - TASmodClient.gameLoopSchedulerClient.add(()->{ - throw new RuntimeException("TASmod networking was shut down. The real errors are above"); - }); - } - }); - clientThread.setName("TASmod Network Client Accept"); - clientThread.setDaemon(true); - clientThread.start(); - } - - private void createSendThread() throws IOException { - DataOutputStream outputStream = new DataOutputStream(new BufferedOutputStream(clientSocket.getOutputStream())); - // Create a new thread that writes packets if available - Thread outputThread = new Thread(() -> { - try { - Packet packet; - while (!clientSocket.isClosed()) { - // Try to poll another packet that wants to be sent - packet = packetsToSend.peek(); - boolean skip = !(packet instanceof IdentificationPacket) && !ready; - if (packet == null || skip) { - Thread.sleep(1); // If nothing has to be done, let the cpu rest by waiting - continue; - } - packetsToSend.poll(); - // A packet was found: Serialize then send it. - byte[] packetData = PacketSerializer.serialize(packet).array(); - outputStream.writeInt(packetData.length); - outputStream.write(packetData); - outputStream.flush(); - logger.trace("Sent a " + packet.getClass().getSimpleName() + " to the socket."); - } - } catch (Exception e) { - e.printStackTrace(); - // This exception is already logged by the thread one layer above - // therefore nothing needs to be done here. - } - }); - outputThread.setDaemon(true); // If daemon is set, the jvm will quit without waiting for this thread to finish - outputThread.setName("TASmod Network Client Send"); - outputThread.start(); - } - - public void killClient() throws IOException { - if(clientThread != null && clientSocket != null) { - clientSocket.close(); - } - } - - public boolean isClosed() { - return clientSocket.isClosed(); - } - - public void setReady() { - ready = true; - } -} diff --git a/src/main/java/com/minecrafttas/tasmod/networking/TASmodNetworkServer.java b/src/main/java/com/minecrafttas/tasmod/networking/TASmodNetworkServer.java deleted file mode 100644 index de3daebd..00000000 --- a/src/main/java/com/minecrafttas/tasmod/networking/TASmodNetworkServer.java +++ /dev/null @@ -1,237 +0,0 @@ -package com.minecrafttas.tasmod.networking; - -import java.io.BufferedInputStream; -import java.io.BufferedOutputStream; -import java.io.DataInputStream; -import java.io.DataOutputStream; -import java.io.EOFException; -import java.io.IOException; -import java.io.InterruptedIOException; -import java.net.ServerSocket; -import java.net.Socket; -import java.net.SocketException; -import java.util.Collections; -import java.util.HashMap; -import java.util.Map; -import java.util.UUID; -import java.util.concurrent.BlockingQueue; -import java.util.concurrent.LinkedBlockingQueue; - -import org.apache.logging.log4j.Logger; - -import com.minecrafttas.tasmod.TASmod; - -import io.netty.buffer.Unpooled; -import net.minecraft.entity.player.EntityPlayerMP; -import net.minecraft.network.PacketBuffer; - -/** - * A custom packet server extending beyond the standard Minecraft network manager - * @author Pancake, Scribble - * - */ -public class TASmodNetworkServer { - - private Logger logger; - - private Thread serverThread; - - private ServerSocket serverSocket; - - private Map connectedPlayers = Collections.synchronizedMap(new HashMap()); - - private Map> queues = Collections.synchronizedMap(new HashMap<>()); - - private int connections = 0; - - public TASmodNetworkServer(Logger logger) throws IOException { - this(logger, 3111); - } - - public TASmodNetworkServer(Logger logger, int port) throws IOException { - this.logger = logger; - createServer(port); - } - - private void createServer(int port) throws IOException { - serverThread = new Thread(() -> { - - try(ServerSocket serverS = new ServerSocket(port)){ - this.serverSocket = serverS; - - while (!this.serverSocket.isClosed()) { - Socket socket = null; - - socket = serverSocket.accept(); - socket.setTcpNoDelay(true); - - final LinkedBlockingQueue queue = new LinkedBlockingQueue<>(); - - connections++; - createSendThread(socket, queue); - createAcceptThread(socket, queue); - } - - } catch (EOFException | SocketException | InterruptedIOException exception) { - logger.debug("Custom TASmod server was shutdown"); - } catch (Exception exception) { - logger.error("Custom TASmod server was unexpectedly shutdown {}", exception); - exception.printStackTrace(); - } - - }); - serverThread.setName("TASmod Network Server Main"); - serverThread.setDaemon(true); - serverThread.start(); - } - - private void createSendThread(Socket socket, LinkedBlockingQueue packetQueue) throws IOException, InterruptedException { - Thread sendThread = new Thread(()->{ - DataOutputStream outputStream; - try { - outputStream = new DataOutputStream(new BufferedOutputStream(socket.getOutputStream())); - } catch (IOException e) { - e.printStackTrace(); - return; - } - Packet packet; - while (!socket.isClosed()) { - try { - // Try to poll another packet that wants to be sent - packet = packetQueue.poll(); - if (packet == null) { - Thread.sleep(1); // If nothing has to be done, let the cpu rest by waiting - continue; - } - // A packet was found: Serialize then send it. - byte[] packetData = PacketSerializer.serialize(packet).array(); - outputStream.writeInt(packetData.length); - outputStream.write(packetData); - outputStream.flush(); - logger.trace("Sent a " + packet.getClass().getSimpleName() + " to the socket."); - } catch(Exception e) { - logger.catching(e); - } - } - }); - sendThread.setDaemon(true); - sendThread.setName("TASmod Network Server Send #"+connections); - sendThread.start(); - } - - private void createAcceptThread(Socket socket, LinkedBlockingQueue packetQueue) throws IOException { - Thread acceptThread = new Thread(()->{ - DataInputStream inputStream; - try { - inputStream = new DataInputStream(new BufferedInputStream(socket.getInputStream())); - } catch (IOException e2) { - e2.printStackTrace(); - return; - } - Packet packet; - while (!socket.isClosed()) { - // Handle the next packet. If no packet is avilable, the readInt() call will - // hang until there is one. - try { - int packetSize = inputStream.readInt(); - byte[] packetData = new byte[packetSize]; - inputStream.read(packetData, 0, packetSize); - - PacketBuffer packetBuf = new PacketBuffer(Unpooled.wrappedBuffer(packetData)); - // Deserialize and run the packet - packet = PacketSerializer.deserialize(packetBuf); - - if(packet instanceof IdentificationPacket) { - handleIdentificationPacket(packet, socket, packetQueue); - } - - if(connectedPlayers.containsKey(socket)) { - UUID id = connectedPlayers.get(socket); - EntityPlayerMP player = TASmod.getServerInstance().getPlayerList().getPlayerByUUID(id); - - packet.handle(PacketSide.SERVER, player); - logger.trace("Handled a " + packet.getClass().getSimpleName() + " from the socket."); - } - }catch (EOFException e){ - logger.info("Client socket was shut down"); - try { - socket.close(); - } catch (IOException e1) { - e1.printStackTrace(); - } - } catch (Exception e) { - logger.error(e.getMessage()); - e.printStackTrace(); - try { - socket.close(); - } catch (IOException e1) { - e1.printStackTrace(); - } - } - } - UUID id = connectedPlayers.get(socket); - connectedPlayers.remove(socket); - queues.remove(id); - connections--; - }); - acceptThread.setDaemon(true); - acceptThread.setName("TASmod Network Server Accept #"+connections); - acceptThread.start(); - } - - private void handleIdentificationPacket(Packet packet, Socket socket, LinkedBlockingQueue packetQueue) { - if(!connectedPlayers.containsKey(socket)) { - IdentificationPacket idPacket = (IdentificationPacket) packet; - logger.info("Identified player with uuid: {}", idPacket.getUuid()); - connectedPlayers.put(socket, idPacket.getUuid()); - queues.put(idPacket.getUuid(), packetQueue); - sendTo(new IdentificationPacket(), idPacket.getUuid()); - } - } - - public void sendToAll(Packet packet) { - if(serverThread.isAlive()) { - queues.forEach((id, queue) -> queue.add(packet)); - } - } - - public void sendTo(Packet packet, EntityPlayerMP... players) { - if(serverThread.isAlive()) { - queues.forEach((id, queue) -> { - for(EntityPlayerMP player : players) { - if(player.getUniqueID().equals(id)) { - queue.add(packet); - } - } - - }); - } - } - - public void sendTo(Packet packet, UUID... uuids) { - if(serverThread.isAlive()) { - queues.forEach((id, queue) -> { - for(UUID idToSend : uuids) { - if(idToSend.equals(id)) { - queue.add(packet); - } - } - }); - } - } - - public int getConnections() { - return connections; - } - - public void close() { - if(serverSocket==null) - return; - try { - serverSocket.close(); - } catch (IOException e) { - e.printStackTrace(); - } - connections = 0; - } -} diff --git a/src/main/java/com/minecrafttas/tasmod/playback/PlaybackController.java b/src/main/java/com/minecrafttas/tasmod/playback/PlaybackController.java index 54eca09b..9e1a52c5 100644 --- a/src/main/java/com/minecrafttas/tasmod/playback/PlaybackController.java +++ b/src/main/java/com/minecrafttas/tasmod/playback/PlaybackController.java @@ -13,8 +13,6 @@ import com.minecrafttas.tasmod.TASmod; import com.minecrafttas.tasmod.TASmodClient; import com.minecrafttas.tasmod.monitoring.DesyncMonitoring; -import com.minecrafttas.tasmod.networking.Packet; -import com.minecrafttas.tasmod.networking.PacketSide; import com.minecrafttas.tasmod.playback.controlbytes.ControlByteHandler; import com.minecrafttas.tasmod.playback.server.TASstateClient; import com.minecrafttas.tasmod.util.LoggerMarkers; diff --git a/src/main/java/com/minecrafttas/tasmod/playback/server/InitialSyncStatePacket.java b/src/main/java/com/minecrafttas/tasmod/playback/server/InitialSyncStatePacket.java index 7eb69aa1..39559d05 100644 --- a/src/main/java/com/minecrafttas/tasmod/playback/server/InitialSyncStatePacket.java +++ b/src/main/java/com/minecrafttas/tasmod/playback/server/InitialSyncStatePacket.java @@ -1,7 +1,6 @@ package com.minecrafttas.tasmod.playback.server; import com.minecrafttas.tasmod.TASmod; -import com.minecrafttas.tasmod.networking.PacketSide; import com.minecrafttas.tasmod.playback.PlaybackController.TASstate; import net.minecraft.entity.player.EntityPlayer; diff --git a/src/main/java/com/minecrafttas/tasmod/playback/server/SyncStatePacket.java b/src/main/java/com/minecrafttas/tasmod/playback/server/SyncStatePacket.java index a6994856..eddf53d3 100644 --- a/src/main/java/com/minecrafttas/tasmod/playback/server/SyncStatePacket.java +++ b/src/main/java/com/minecrafttas/tasmod/playback/server/SyncStatePacket.java @@ -2,8 +2,6 @@ import com.minecrafttas.tasmod.TASmod; import com.minecrafttas.tasmod.TASmodClient; -import com.minecrafttas.tasmod.networking.Packet; -import com.minecrafttas.tasmod.networking.PacketSide; import com.minecrafttas.tasmod.playback.PlaybackController; import com.minecrafttas.tasmod.playback.PlaybackController.TASstate; import com.minecrafttas.tasmod.util.TickScheduler.TickTask; diff --git a/src/main/java/com/minecrafttas/tasmod/savestates/client/InputSavestatesPacket.java b/src/main/java/com/minecrafttas/tasmod/savestates/client/InputSavestatesPacket.java index 17bd1772..a0daf78b 100644 --- a/src/main/java/com/minecrafttas/tasmod/savestates/client/InputSavestatesPacket.java +++ b/src/main/java/com/minecrafttas/tasmod/savestates/client/InputSavestatesPacket.java @@ -4,8 +4,6 @@ import java.nio.charset.Charset; import com.minecrafttas.tasmod.TASmod; -import com.minecrafttas.tasmod.networking.Packet; -import com.minecrafttas.tasmod.networking.PacketSide; import com.minecrafttas.tasmod.savestates.server.exceptions.SavestateException; import net.minecraft.entity.player.EntityPlayer; diff --git a/src/main/java/com/minecrafttas/tasmod/savestates/server/LoadstatePacket.java b/src/main/java/com/minecrafttas/tasmod/savestates/server/LoadstatePacket.java index 188b82e4..2709ed24 100644 --- a/src/main/java/com/minecrafttas/tasmod/savestates/server/LoadstatePacket.java +++ b/src/main/java/com/minecrafttas/tasmod/savestates/server/LoadstatePacket.java @@ -1,8 +1,6 @@ package com.minecrafttas.tasmod.savestates.server; import com.minecrafttas.tasmod.TASmod; -import com.minecrafttas.tasmod.networking.Packet; -import com.minecrafttas.tasmod.networking.PacketSide; import com.minecrafttas.tasmod.savestates.server.SavestateHandler.SavestateState; import com.minecrafttas.tasmod.savestates.server.chunkloading.SavestatesChunkControl; import com.minecrafttas.tasmod.savestates.server.exceptions.LoadstateException; diff --git a/src/main/java/com/minecrafttas/tasmod/savestates/server/SavestatePacket.java b/src/main/java/com/minecrafttas/tasmod/savestates/server/SavestatePacket.java index 8d5cb81f..fb5809be 100644 --- a/src/main/java/com/minecrafttas/tasmod/savestates/server/SavestatePacket.java +++ b/src/main/java/com/minecrafttas/tasmod/savestates/server/SavestatePacket.java @@ -1,8 +1,6 @@ package com.minecrafttas.tasmod.savestates.server; import com.minecrafttas.tasmod.TASmod; -import com.minecrafttas.tasmod.networking.Packet; -import com.minecrafttas.tasmod.networking.PacketSide; import com.minecrafttas.tasmod.savestates.client.gui.GuiSavestateSavingScreen; import com.minecrafttas.tasmod.savestates.server.SavestateHandler.SavestateState; import com.minecrafttas.tasmod.savestates.server.exceptions.SavestateException; 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 index 4968cd2b..67eec718 100644 --- a/src/main/java/com/minecrafttas/tasmod/savestates/server/motion/MotionPacket.java +++ b/src/main/java/com/minecrafttas/tasmod/savestates/server/motion/MotionPacket.java @@ -1,7 +1,5 @@ package com.minecrafttas.tasmod.savestates.server.motion; -import com.minecrafttas.tasmod.networking.Packet; -import com.minecrafttas.tasmod.networking.PacketSide; import com.minecrafttas.tasmod.savestates.server.motion.ClientMotionServer.Saver; import net.minecraft.entity.player.EntityPlayer; 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 index 975db5ae..9b3744aa 100644 --- a/src/main/java/com/minecrafttas/tasmod/savestates/server/motion/RequestMotionPacket.java +++ b/src/main/java/com/minecrafttas/tasmod/savestates/server/motion/RequestMotionPacket.java @@ -1,8 +1,6 @@ package com.minecrafttas.tasmod.savestates.server.motion; import com.minecrafttas.tasmod.TASmodClient; -import com.minecrafttas.tasmod.networking.Packet; -import com.minecrafttas.tasmod.networking.PacketSide; import com.minecrafttas.tasmod.savestates.client.gui.GuiSavestateSavingScreen; import net.minecraft.client.Minecraft; diff --git a/src/main/java/com/minecrafttas/tasmod/savestates/server/playerloading/SavestatePlayerLoadingPacket.java b/src/main/java/com/minecrafttas/tasmod/savestates/server/playerloading/SavestatePlayerLoadingPacket.java index e48e5406..4f7f816f 100644 --- a/src/main/java/com/minecrafttas/tasmod/savestates/server/playerloading/SavestatePlayerLoadingPacket.java +++ b/src/main/java/com/minecrafttas/tasmod/savestates/server/playerloading/SavestatePlayerLoadingPacket.java @@ -1,7 +1,5 @@ package com.minecrafttas.tasmod.savestates.server.playerloading; -import com.minecrafttas.tasmod.networking.Packet; -import com.minecrafttas.tasmod.networking.PacketSide; import com.minecrafttas.tasmod.savestates.server.chunkloading.SavestatesChunkControl; import net.minecraft.client.Minecraft; diff --git a/src/main/java/com/minecrafttas/tasmod/tickratechanger/AdvanceTickratePacket.java b/src/main/java/com/minecrafttas/tasmod/tickratechanger/AdvanceTickratePacket.java index 168725dd..b9479590 100644 --- a/src/main/java/com/minecrafttas/tasmod/tickratechanger/AdvanceTickratePacket.java +++ b/src/main/java/com/minecrafttas/tasmod/tickratechanger/AdvanceTickratePacket.java @@ -2,9 +2,6 @@ import com.minecrafttas.tasmod.TASmod; import com.minecrafttas.tasmod.TASmodClient; -import com.minecrafttas.tasmod.networking.Packet; -import com.minecrafttas.tasmod.networking.PacketSide; - import net.minecraft.entity.player.EntityPlayer; import net.minecraft.network.PacketBuffer; diff --git a/src/main/java/com/minecrafttas/tasmod/tickratechanger/ChangeTickratePacket.java b/src/main/java/com/minecrafttas/tasmod/tickratechanger/ChangeTickratePacket.java index a5987a41..cbe8d703 100644 --- a/src/main/java/com/minecrafttas/tasmod/tickratechanger/ChangeTickratePacket.java +++ b/src/main/java/com/minecrafttas/tasmod/tickratechanger/ChangeTickratePacket.java @@ -2,9 +2,6 @@ import com.minecrafttas.tasmod.TASmod; import com.minecrafttas.tasmod.TASmodClient; -import com.minecrafttas.tasmod.networking.Packet; -import com.minecrafttas.tasmod.networking.PacketSide; - import net.minecraft.entity.player.EntityPlayer; import net.minecraft.network.PacketBuffer; diff --git a/src/main/java/com/minecrafttas/tasmod/tickratechanger/PauseTickratePacket.java b/src/main/java/com/minecrafttas/tasmod/tickratechanger/PauseTickratePacket.java index 041f9437..659c1ace 100644 --- a/src/main/java/com/minecrafttas/tasmod/tickratechanger/PauseTickratePacket.java +++ b/src/main/java/com/minecrafttas/tasmod/tickratechanger/PauseTickratePacket.java @@ -2,9 +2,6 @@ import com.minecrafttas.tasmod.TASmod; import com.minecrafttas.tasmod.TASmodClient; -import com.minecrafttas.tasmod.networking.Packet; -import com.minecrafttas.tasmod.networking.PacketSide; - import net.minecraft.entity.player.EntityPlayer; import net.minecraft.network.PacketBuffer; @@ -45,41 +42,7 @@ public State getState() { * @author Scribble * */ - public enum State { - /** - * Set's the game to tickrate 0 - */ - PAUSE((short) 1), - /** - * Set's the game to "tickrate saved" - */ - UNPAUSE((short) 2), - /** - * Toggles between {@link #PAUSE} and {@link #UNPAUSE} - */ - TOGGLE((short) 0); - - private short id; - State(short i) { - id = i; - } - - public short toShort() { - return id; - } - - public static State fromShort(short i) { - switch (i) { - case 1: - return PAUSE; - case 2: - return UNPAUSE; - default: - return TOGGLE; - } - } - } @Override public void handle(PacketSide side, EntityPlayer player) { diff --git a/src/main/java/com/minecrafttas/tasmod/tickratechanger/TickrateChangerClient.java b/src/main/java/com/minecrafttas/tasmod/tickratechanger/TickrateChangerClient.java index e72feade..d715394a 100644 --- a/src/main/java/com/minecrafttas/tasmod/tickratechanger/TickrateChangerClient.java +++ b/src/main/java/com/minecrafttas/tasmod/tickratechanger/TickrateChangerClient.java @@ -1,9 +1,11 @@ package com.minecrafttas.tasmod.tickratechanger; +import java.nio.ByteBuffer; + import com.minecrafttas.common.events.EventClient.EventClientGameLoop; import com.minecrafttas.tasmod.TASmod; import com.minecrafttas.tasmod.TASmodClient; -import com.minecrafttas.tasmod.ticksync.TickSyncClient; +import com.minecrafttas.tasmod.tickratechanger.TickrateChangerServer.State; import com.minecrafttas.tasmod.util.LoggerMarkers; import net.minecraft.client.Minecraft; @@ -91,7 +93,13 @@ public void changeServerTickrate(float tickrate) { if (tickrate < 0) { return; } - TASmodClient.packetClient.sendToServer(new ChangeTickratePacket(tickrate)); + + try { + // packet 4: request tickrate change + TASmodClient.client.write(ByteBuffer.allocate(8).putInt(4).putFloat(tickrate)); + } catch (Exception e) { + TASmod.LOGGER.error("Unable to send packet to server: {}", e); + } } /** @@ -99,7 +107,12 @@ public void changeServerTickrate(float tickrate) { */ public void togglePause() { if (Minecraft.getMinecraft().world != null) { - TASmodClient.packetClient.sendToServer(new PauseTickratePacket()); + try { + // packet 6: toggle tickrate zero + TASmodClient.client.write(ByteBuffer.allocateDirect(6).putInt(6).putShort(State.TOGGLE.toShort())); + } catch (Exception e) { + TASmod.LOGGER.error("Unable to send packet to server: {}", e); + } } else { togglePauseClient(); } @@ -158,7 +171,12 @@ public void advanceTick() { * Sends a {@link AdvanceTickratePacket} to the server to advance the server */ public void advanceServerTick() { - TASmodClient.packetClient.sendToServer(new AdvanceTickratePacket()); + try { + // packet 7: request tick advance + TASmodClient.client.write(ByteBuffer.allocate(4).putInt(7)); + } catch (Exception e) { + TASmod.LOGGER.error("Unable to send packet to server: {}", e); + } } /** @@ -181,9 +199,7 @@ private static void log(String msg) { @Override public void onRunClientGameLoop(Minecraft mc) { - if (TASmodClient.packetClient != null && TASmodClient.packetClient.isClosed()) { // If the server died, but the client has not left the world - TickSyncClient.shouldTick.set(true); - } + } } diff --git a/src/main/java/com/minecrafttas/tasmod/tickratechanger/TickrateChangerServer.java b/src/main/java/com/minecrafttas/tasmod/tickratechanger/TickrateChangerServer.java index a6d400df..7911a973 100644 --- a/src/main/java/com/minecrafttas/tasmod/tickratechanger/TickrateChangerServer.java +++ b/src/main/java/com/minecrafttas/tasmod/tickratechanger/TickrateChangerServer.java @@ -1,10 +1,14 @@ package com.minecrafttas.tasmod.tickratechanger; +import java.nio.ByteBuffer; + import org.apache.logging.log4j.Logger; import com.minecrafttas.common.events.EventServer.EventPlayerJoinedServerSide; import com.minecrafttas.common.events.EventServer.EventServerStop; 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; @@ -82,7 +86,13 @@ public void changeClientTickrate(float tickrate) { public void changeClientTickrate(float tickrate, boolean log) { if(log) log("Changing the tickrate "+ tickrate + " to all clients"); - TASmod.packetServer.sendToAll(new ChangeTickratePacket(tickrate)); + + try { + // packet 5: send new tickrate to clients + TASmod.server.writeAll(ByteBuffer.allocate(8).putInt(5).putFloat(tickrate)); + } catch (Exception e) { + TASmod.LOGGER.error("Unable to send packet to all clients: {}", e); + } } /** @@ -204,4 +214,41 @@ public void onServerStop(MinecraftServer server) { pauseGame(false); } } + + public static enum State { + /** + * Set's the game to tickrate 0 + */ + PAUSE((short) 1), + /** + * Set's the game to "tickrate saved" + */ + UNPAUSE((short) 2), + /** + * Toggles between {@link #PAUSE} and {@link #UNPAUSE} + */ + TOGGLE((short) 0); + + private short id; + + State(short i) { + id = i; + } + + public short toShort() { + return id; + } + + public static State fromShort(short i) { + switch (i) { + case 1: + return PAUSE; + case 2: + return UNPAUSE; + default: + return TOGGLE; + } + } + } + } diff --git a/src/main/java/com/minecrafttas/tasmod/ticksync/TickSyncClient.java b/src/main/java/com/minecrafttas/tasmod/ticksync/TickSyncClient.java index 184e91a2..5b8a755e 100644 --- a/src/main/java/com/minecrafttas/tasmod/ticksync/TickSyncClient.java +++ b/src/main/java/com/minecrafttas/tasmod/ticksync/TickSyncClient.java @@ -1,7 +1,9 @@ package com.minecrafttas.tasmod.ticksync; +import java.nio.ByteBuffer; import java.util.concurrent.atomic.AtomicBoolean; +import com.minecrafttas.tasmod.TASmod; import com.minecrafttas.tasmod.TASmodClient; import net.minecraft.client.Minecraft; @@ -35,10 +37,16 @@ public static void onPacket() { * @param mc Instance of Minecraft */ public static void clientPostTick(Minecraft mc) { - if (mc.player == null && TASmodClient.packetClient==null) { + if (mc.player == null) { return; } - TASmodClient.packetClient.sendToServer(new TickSyncPacket(mc.player.getGameProfile().getId())); + + try { + // packet 3: notify server of tick pass + TASmodClient.client.write(ByteBuffer.allocate(4).putInt(3)); + } catch (Exception e) { + TASmod.LOGGER.error("Unable to send packet to server: {}", e); + } } } diff --git a/src/main/java/com/minecrafttas/tasmod/ticksync/TickSyncPacket.java b/src/main/java/com/minecrafttas/tasmod/ticksync/TickSyncPacket.java deleted file mode 100644 index 27347e51..00000000 --- a/src/main/java/com/minecrafttas/tasmod/ticksync/TickSyncPacket.java +++ /dev/null @@ -1,48 +0,0 @@ -package com.minecrafttas.tasmod.ticksync; - -import java.util.UUID; - -import com.minecrafttas.tasmod.networking.Packet; -import com.minecrafttas.tasmod.networking.PacketSide; - -import net.minecraft.entity.player.EntityPlayer; -import net.minecraft.network.PacketBuffer; - -/** - * Packet for {@linkplain TickSyncServer} - * @author Scribble - * - */ -public class TickSyncPacket implements Packet { - - protected UUID uuid; - - public TickSyncPacket() { - - } - - public TickSyncPacket(UUID id) { - this.uuid = id; - } - - @Override - public void handle(PacketSide side, EntityPlayer player) { - if(side.isServer()) { - TickSyncServer.onPacket(this.uuid); - }else { - TickSyncClient.onPacket(); - } - } - - @Override - public void serialize(PacketBuffer buf) { - if(uuid!=null) - buf.writeUniqueId(uuid); - } - - @Override - public void deserialize(PacketBuffer buf) { - uuid = buf.readUniqueId(); - } - -} diff --git a/src/main/java/com/minecrafttas/tasmod/ticksync/TickSyncServer.java b/src/main/java/com/minecrafttas/tasmod/ticksync/TickSyncServer.java index fc5337db..b3f81b3f 100644 --- a/src/main/java/com/minecrafttas/tasmod/ticksync/TickSyncServer.java +++ b/src/main/java/com/minecrafttas/tasmod/ticksync/TickSyncServer.java @@ -1,5 +1,6 @@ package com.minecrafttas.tasmod.ticksync; +import java.nio.ByteBuffer; import java.util.ArrayList; import java.util.Collections; import java.util.List; @@ -37,7 +38,7 @@ public static void onPacket(UUID uuid) { public static boolean shouldTick() { synchronized (synchronizedList) { int acknowledged = synchronizedList.size(); - int totalConnections = TASmod.packetServer.getConnections(); + int totalConnections = TASmod.server.getClients().size(); if(acknowledged >= totalConnections) { return true; }else { @@ -51,7 +52,12 @@ public static boolean shouldTick() { * to all clients making them tick */ public static void serverPostTick() { - TASmod.packetServer.sendToAll(new TickSyncPacket()); + try { + // packet 2: tick clients + TASmod.server.writeAll(ByteBuffer.allocate(4).putInt(2)); + } catch (Exception e) { + TASmod.LOGGER.error("Unable to send packet to all clients: {}", e); + } if(synchronizedList.size()>0) synchronizedList.clear(); }