-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
91089b4
commit cd51fba
Showing
7 changed files
with
195 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
22 changes: 22 additions & 0 deletions
22
src/main/java/com/vulpeus/kyoyu/client/mixins/ClientPacketListenerMixin.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)); | ||
} | ||
} | ||
//?} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
83
src/main/java/com/vulpeus/kyoyu/net/KyoyuPacketManager.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
43 changes: 43 additions & 0 deletions
43
src/main/java/com/vulpeus/kyoyu/net/packets/HandshakePacket.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters