Skip to content

Commit

Permalink
Packet Manager & Handshake Packet
Browse files Browse the repository at this point in the history
  • Loading branch information
topi-banana committed Dec 2, 2024
1 parent 91089b4 commit cd51fba
Show file tree
Hide file tree
Showing 7 changed files with 195 additions and 2 deletions.
2 changes: 1 addition & 1 deletion src/main/java/com/vulpeus/kyoyu/KyoyuMod.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package com.vulpeus.kyoyu;

import com.vulpeus.kyoyu.net.packets.KyoyuPacketPayload;
import com.vulpeus.kyoyu.net.KyoyuPacketPayload;

//? if FORGE
/* import net.minecraftforge.fml.common.Mod; */
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package com.vulpeus.kyoyu.client.mixins;

//? if client {
import com.vulpeus.kyoyu.Kyoyu;
import com.vulpeus.kyoyu.net.KyoyuPacketManager;
import com.vulpeus.kyoyu.net.packets.HandshakePacket;
import net.minecraft.client.multiplayer.ClientPacketListener;
import net.minecraft.network.protocol.game.ClientboundLoginPacket;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.injection.At;
import org.spongepowered.asm.mixin.injection.Inject;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfo;

@Mixin(ClientPacketListener.class)
public class ClientPacketListenerMixin {
@Inject(method = "handleLogin", at = @At("RETURN"))
private void onGameJoin(ClientboundLoginPacket clientboundLoginPacket, CallbackInfo ci) {
Kyoyu.LOGGER.info("Login to Server");
KyoyuPacketManager.sendC2S(new HandshakePacket(Kyoyu.MOD_VERSION));
}
}
//?}
15 changes: 15 additions & 0 deletions src/main/java/com/vulpeus/kyoyu/net/IKyoyuPacket.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package com.vulpeus.kyoyu.net;

import net.minecraft.world.entity.player.Player;

public abstract class IKyoyuPacket {

public IKyoyuPacket(byte[] bytes) {}

public abstract byte[] encode();

public abstract void onServer(Player player);

public abstract void onClient();

}
83 changes: 83 additions & 0 deletions src/main/java/com/vulpeus/kyoyu/net/KyoyuPacketManager.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
package com.vulpeus.kyoyu.net;

import com.vulpeus.kyoyu.Kyoyu;
import com.vulpeus.kyoyu.net.packets.HandshakePacket;
import net.minecraft.server.level.ServerPlayer;

import java.io.*;
import java.util.HashMap;
import java.util.Map;

public class KyoyuPacketManager {
private static final Map<String, Class<? extends IKyoyuPacket>> packetRegistry = new HashMap<>();

static {
packetRegistry.put("handshake", HandshakePacket.class);
}

private static IKyoyuPacket decode(byte[] raw) {
try (ByteArrayInputStream bais = new ByteArrayInputStream(raw); DataInputStream dis = new DataInputStream(bais)) {
String key = dis.readUTF();
int len = dis.readInt();
byte[] data = new byte[len];
dis.readFully(data);
Kyoyu.LOGGER.info("KyoyuPacketManager decode {}", key);
Class<? extends IKyoyuPacket> packetClass = packetRegistry.get(key);
if (packetClass != null) {
try {
IKyoyuPacket packet = packetClass.getDeclaredConstructor(byte[].class).newInstance((Object) data);
} catch (Exception e) {
e.printStackTrace();
}
} else {
Kyoyu.LOGGER.info("Unknow Packet {}", data);
}
} catch (IOException e) {
throw new RuntimeException(e);
}

return null;
}

private static byte[] encode(IKyoyuPacket packet) {
byte[] res = null;
try (ByteArrayOutputStream baos = new ByteArrayOutputStream(); DataOutputStream dos = new DataOutputStream(baos)) {
String key = null;
for (Map.Entry<String, Class<? extends IKyoyuPacket>> entry: packetRegistry.entrySet()) {
if (entry.getValue() == packet.getClass()) {
key = entry.getKey();
break;
}
}
if (key == null) return null;
byte[] data = packet.encode();
dos.writeUTF(key);
dos.writeInt(data.length);
dos.write(data);
res = baos.toByteArray();
} catch (IOException e) {
throw new RuntimeException(e);
}
return res;
}

public static void handleC2S(byte[] data, ServerPlayer player) {
IKyoyuPacket packet = decode(data);
if (packet==null) return;
packet.onServer(player);
}
public static void handleS2C(byte[] data) {
IKyoyuPacket packet = decode(data);
if (packet==null) return;
packet.onClient();
}

public static void sendC2S(IKyoyuPacket packet) {
KyoyuPacketPayload packetPayload = new KyoyuPacketPayload(encode(packet));
packetPayload.sendC2S();
}
public static void sendS2C(IKyoyuPacket packet, ServerPlayer player) {
KyoyuPacketPayload packetPayload = new KyoyuPacketPayload(encode(packet));
packetPayload.sendS2C(player);
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.vulpeus.kyoyu.net.packets;
package com.vulpeus.kyoyu.net;

import com.vulpeus.kyoyu.CompatibleUtils;
import com.vulpeus.kyoyu.Kyoyu;
Expand Down Expand Up @@ -42,6 +42,7 @@
import net.neoforged.neoforge.network.registration.PayloadRegistrar;
*/
//?}
import net.minecraft.server.level.ServerPlayer;
import org.jetbrains.annotations.NotNull;

public class KyoyuPacketPayload
Expand Down Expand Up @@ -137,6 +138,21 @@ public void sendC2S() {
/* PacketDistributor.sendToServer(this); */
//?}
}
public void sendS2C(ServerPlayer player) {
//? FABRIC {
//? >=1.20.6 {
ServerPlayNetworking.send(player, this);
//?} else {
/*
FriendlyByteBuf packetbb = new FriendlyByteBuf(Unpooled.buffer());
packetbb.writeByteArray(this.content);
ServerPlayNetworking.send(player, identifier, packetbb);
*/
//?}
//?} elif NEOFORGE {
/* PacketDistributor.sendToServer(this); */
//?}
}

public void onPacketServer(
//? FABRIC {
Expand All @@ -156,6 +172,17 @@ public void onPacketServer(
//?} else {
Kyoyu.LOGGER.info("onPacketServer {}", context);
//?}

//? if FABRIC {
KyoyuPacketManager.handleC2S(
this.content,
//? if FABRIC && <=1.20.4 {
/* player */
//?} else {
context.player()
//?}
);
//?}
}

public void onPacketClient(
Expand All @@ -176,5 +203,7 @@ public void onPacketClient(
//?} else {
Kyoyu.LOGGER.info("onPacketClient {}", context);
//?}

KyoyuPacketManager.handleS2C(this.content);
}
}
43 changes: 43 additions & 0 deletions src/main/java/com/vulpeus/kyoyu/net/packets/HandshakePacket.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package com.vulpeus.kyoyu.net.packets;

import com.vulpeus.kyoyu.Kyoyu;
import com.vulpeus.kyoyu.net.IKyoyuPacket;
import net.minecraft.world.entity.player.Player;

import java.nio.charset.StandardCharsets;

public class HandshakePacket extends IKyoyuPacket {

private final String version;

public HandshakePacket(String version) {
super(version.getBytes(StandardCharsets.UTF_8));
this.version = version;
}

public HandshakePacket(byte[] bytes) {
super(bytes);
this.version = new String(bytes, StandardCharsets.UTF_8);
}

@Override
public byte[] encode() {
return version.getBytes(StandardCharsets.UTF_8);
}

@Override
public void onServer(Player player) {
Kyoyu.LOGGER.info("Login `{}` with compatible client version `{}`", version, player.getName().getString());

// TODO
// packet = new HandshakePacket(Kyoyu.MOD_VERSION);
// packet.send(player);
}

@Override
public void onClient() {
Kyoyu.LOGGER.info("Login to compatible server version `{}`", version);
// TODO
// initClient(version);
}
}
1 change: 1 addition & 0 deletions src/main/resources/kyoyu.client.mixins.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
"package": "com.vulpeus.kyoyu.client.mixins",
"compatibilityLevel": "${java_compatibility_level}",
"client": [
"ClientPacketListenerMixin",
"litematica.GuiMainMenuMixin",
"litematica.SchematicPlacementMixin",
"litematica.WidgetSchematicPlacementMixin"
Expand Down

0 comments on commit cd51fba

Please sign in to comment.