Skip to content

Commit

Permalink
Start moving packets to new system
Browse files Browse the repository at this point in the history
this is painful
  • Loading branch information
PancakeTAS committed Jun 14, 2023
1 parent faf669a commit e42630e
Show file tree
Hide file tree
Showing 37 changed files with 131 additions and 766 deletions.
36 changes: 36 additions & 0 deletions src/main/java/com/minecrafttas/server/Client.java
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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();
});
}

/**
Expand All @@ -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());
Expand Down
3 changes: 3 additions & 0 deletions src/main/java/com/minecrafttas/server/Server.java
Original file line number Diff line number Diff line change
Expand Up @@ -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<Client> clients;

/**
Expand Down
18 changes: 0 additions & 18 deletions src/main/java/com/minecrafttas/tasmod/TASmod.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -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;
Expand All @@ -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) {
Expand All @@ -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);
Expand Down
34 changes: 3 additions & 31 deletions src/main/java/com/minecrafttas/tasmod/TASmodClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -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();
}

}

}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
@@ -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;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
@@ -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;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
@@ -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;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down

This file was deleted.

36 changes: 0 additions & 36 deletions src/main/java/com/minecrafttas/tasmod/networking/Packet.java

This file was deleted.

Loading

0 comments on commit e42630e

Please sign in to comment.