ACTUALLY MAKE IT WORK

yeah its finished
this will probably be the last idea i skid (m a b e)
This commit is contained in:
Chayapak 2023-06-15 20:36:56 +07:00
parent eb73b886ca
commit cd5d3d9132
6 changed files with 85 additions and 52 deletions

View file

@ -12,17 +12,12 @@ import land.chipmunk.chayapak.chomens_bot.data.voiceChat.RawUdpPacket;
import land.chipmunk.chayapak.chomens_bot.util.FriendlyByteBuf;
import land.chipmunk.chayapak.chomens_bot.voiceChat.InitializationData;
import land.chipmunk.chayapak.chomens_bot.voiceChat.NetworkMessage;
import land.chipmunk.chayapak.chomens_bot.voiceChat.packets.KeepAlivePacket;
import land.chipmunk.chayapak.chomens_bot.voiceChat.packets.PingPacket;
import land.chipmunk.chayapak.chomens_bot.voiceChat.packets.SecretPacket;
import land.chipmunk.chayapak.chomens_bot.voiceChat.packets.AuthenticatePacket;
import land.chipmunk.chayapak.chomens_bot.voiceChat.packets.*;
import java.net.*;
// exists for some reason, still wip and will be finished in the next 69 years
// and i prob implemented it in a wrong way lol
// at least when you do `/voicechat test (bot username)` it will show `Client not connected`
// instead of `(bot username) does not have Simple Voice Chat installed`
// most of these codes are from the simple voice chat mod itself including the other voicechat classes
// i didn't implement mic yet because my goal is to make `/voicechat test` work with the bot
public class VoiceChatPlugin extends Bot.Listener {
private final Bot bot;
@ -31,6 +26,8 @@ public class VoiceChatPlugin extends Bot.Listener {
private InetAddress address;
private InetSocketAddress socketAddress;
private boolean running = false;
public VoiceChatPlugin(Bot bot) {
this.bot = bot;
@ -59,16 +56,12 @@ public class VoiceChatPlugin extends Bot.Listener {
"voicechat:update_state",
new FriendlyByteBuf(Unpooled.buffer()).writeBoolean(false).array()
));
running = true;
}
public void packetReceived(ClientboundCustomPayloadPacket _packet) {
// sus
/*
System.out.println("\"" + _packet.getChannel() + "\"");
System.out.println(Arrays.toString(_packet.getData()));
System.out.println(new String(_packet.getData()));
*/
if (_packet.getChannel().equals("voicechat:secret")) {
if (_packet.getChannel().equals("voicechat:secret")) { // fard
final byte[] bytes = _packet.getData();
final FriendlyByteBuf buf = new FriendlyByteBuf(Unpooled.wrappedBuffer(bytes));
@ -91,41 +84,38 @@ public class VoiceChatPlugin extends Bot.Listener {
}
bot.executorService().execute(() -> {
while (true) {
try {
if (socket.isClosed()) continue;
sendToServer(new NetworkMessage(new AuthenticatePacket(initializationData.playerUUID(), initializationData.secret())));
while (running) {
try {
final NetworkMessage message = NetworkMessage.readPacket(socket.read(), initializationData);
if (message == null) continue;
if (message.packet() instanceof AuthenticatePacket) {
System.out.println("SERVER AUTHENTICATED FINALLYYYYYYYYYYYYYYYY");
} else if (message.packet() instanceof PingPacket pingPacket) {
System.out.println("got ping packet");
sendToServer(new NetworkMessage(pingPacket));
} else if (message.packet() instanceof KeepAlivePacket) {
System.out.println("got keep alive packet");
sendToServer(new NetworkMessage(new KeepAlivePacket()));
} else {
System.out.println("got " + message.packet().getClass().getName());
}
if (message.packet() instanceof PingPacket pingPacket) sendToServer(new NetworkMessage(pingPacket));
else if (message.packet() instanceof KeepAlivePacket) sendToServer(new NetworkMessage(new KeepAlivePacket()));
else if (message.packet() instanceof AuthenticateAckPacket) sendToServer(new NetworkMessage(new ConnectionCheckPacket()));
} catch (Exception e) {
e.printStackTrace();
if (running) e.printStackTrace();
else break; // is this neccessary?
}
}
});
}
}
public void sendToServer (NetworkMessage message) throws Exception {
socket.send(
message.writeClient(initializationData),
socketAddress
);
public void sendToServer (NetworkMessage message) {
try {
socket.send(
message.writeClient(initializationData),
socketAddress
);
} catch (Exception e) {
e.printStackTrace();
}
}
private static class VoiceChatSocketBase {
private class VoiceChatSocketBase {
private final byte[] BUFFER = new byte[4096];
public RawUdpPacket read (DatagramSocket socket) {
@ -133,13 +123,8 @@ public class VoiceChatPlugin extends Bot.Listener {
try {
DatagramPacket packet = new DatagramPacket(BUFFER, BUFFER.length);
System.out.println("receiving packet");
// the problem is this next line, just this 1 line. it makes the entire thing froze
socket.receive(packet);
System.out.println("FINALLY DONE RECEIVING AAAAHHHHHHHHHHHHHHHHHHH");
// Setting the timestamp after receiving the packet
long timestamp = System.currentTimeMillis();
byte[] data = new byte[packet.getLength()];
@ -157,9 +142,11 @@ public class VoiceChatPlugin extends Bot.Listener {
@Override
public void disconnected(DisconnectedEvent event) {
socket.close();
running = false;
}
private static class ClientVoiceChatSocket extends VoiceChatSocketBase {
private class ClientVoiceChatSocket extends VoiceChatSocketBase {
private DatagramSocket socket;
public void open() throws SocketException {

View file

@ -4,7 +4,7 @@ import io.netty.buffer.Unpooled;
import land.chipmunk.chayapak.chomens_bot.data.voiceChat.RawUdpPacket;
import land.chipmunk.chayapak.chomens_bot.util.AES;
import land.chipmunk.chayapak.chomens_bot.util.FriendlyByteBuf;
import land.chipmunk.chayapak.chomens_bot.voiceChat.packets.AuthenticatePacket;
import land.chipmunk.chayapak.chomens_bot.voiceChat.packets.*;
import lombok.Getter;
import javax.crypto.BadPaddingException;
@ -49,11 +49,11 @@ public class NetworkMessage {
// packetRegistry.put((byte) 0x3, GroupSoundPacket.class);
// packetRegistry.put((byte) 0x4, LocationSoundPacket.class);
packetRegistry.put((byte) 0x5, AuthenticatePacket.class);
// packetRegistry.put((byte) 0x6, AuthenticateAckPacket.class);
// packetRegistry.put((byte) 0x7, PingPacket.class);
// packetRegistry.put((byte) 0x8, KeepAlivePacket.class);
// packetRegistry.put((byte) 0x9, ConnectionCheckPacket.class);
// packetRegistry.put((byte) 0xA, ConnectionCheckAckPacket.class);
packetRegistry.put((byte) 0x6, AuthenticateAckPacket.class);
packetRegistry.put((byte) 0x7, PingPacket.class);
packetRegistry.put((byte) 0x8, KeepAlivePacket.class);
packetRegistry.put((byte) 0x9, ConnectionCheckPacket.class);
packetRegistry.put((byte) 0xA, ConnectionAckPacket.class);
}
public static NetworkMessage readPacket (RawUdpPacket packet, InitializationData initializationData) throws IllegalAccessException, InstantiationException, InvocationTargetException, NoSuchMethodException {
@ -79,10 +79,7 @@ public class NetworkMessage {
FriendlyByteBuf buffer = new FriendlyByteBuf(Unpooled.wrappedBuffer(decrypt));
byte packetType = buffer.readByte();
final Class<? extends Packet<?>> packetClass = packetRegistry.get(packetType);
if (packetClass == null) {
System.out.println("packet type is null " + packetType);
return null;
}
if (packetClass == null) return null;
Packet<?> p = packetClass.getDeclaredConstructor().newInstance();
NetworkMessage message = new NetworkMessage(timestamp);

View file

@ -0,0 +1,15 @@
package land.chipmunk.chayapak.chomens_bot.voiceChat.packets;
import land.chipmunk.chayapak.chomens_bot.util.FriendlyByteBuf;
import land.chipmunk.chayapak.chomens_bot.voiceChat.Packet;
public class AuthenticateAckPacket implements Packet<AuthenticateAckPacket> {
@Override
public AuthenticateAckPacket fromBytes(FriendlyByteBuf buf) {
return new AuthenticateAckPacket();
}
@Override
public void toBytes(FriendlyByteBuf buf) {
}
}

View file

@ -2,14 +2,18 @@ package land.chipmunk.chayapak.chomens_bot.voiceChat.packets;
import land.chipmunk.chayapak.chomens_bot.util.FriendlyByteBuf;
import land.chipmunk.chayapak.chomens_bot.voiceChat.Packet;
import lombok.AllArgsConstructor;
import lombok.Getter;
import java.util.UUID;
@AllArgsConstructor
public class AuthenticatePacket implements Packet<AuthenticatePacket> {
@Getter private UUID playerUUID;
@Getter private UUID secret;
public AuthenticatePacket () {}
@Override
public AuthenticatePacket fromBytes (FriendlyByteBuf buf) {
AuthenticatePacket packet = new AuthenticatePacket();

View file

@ -0,0 +1,15 @@
package land.chipmunk.chayapak.chomens_bot.voiceChat.packets;
import land.chipmunk.chayapak.chomens_bot.util.FriendlyByteBuf;
import land.chipmunk.chayapak.chomens_bot.voiceChat.Packet;
public class ConnectionAckPacket implements Packet<ConnectionAckPacket> {
@Override
public ConnectionAckPacket fromBytes(FriendlyByteBuf buf) {
return new ConnectionAckPacket();
}
@Override
public void toBytes(FriendlyByteBuf buf) {
}
}

View file

@ -0,0 +1,15 @@
package land.chipmunk.chayapak.chomens_bot.voiceChat.packets;
import land.chipmunk.chayapak.chomens_bot.util.FriendlyByteBuf;
import land.chipmunk.chayapak.chomens_bot.voiceChat.Packet;
public class ConnectionCheckPacket implements Packet<ConnectionCheckPacket> {
@Override
public ConnectionCheckPacket fromBytes(FriendlyByteBuf buf) {
return new ConnectionCheckPacket();
}
@Override
public void toBytes(FriendlyByteBuf buf) {
}
}