code improvement !1

This commit is contained in:
Chipmunk 2023-04-30 21:33:47 -04:00
parent 09c1357e1c
commit 8beaa3d290
2 changed files with 8 additions and 11 deletions

View file

@ -6,6 +6,8 @@ import com.github.steveice10.packetlib.Session;
import com.github.steveice10.packetlib.tcp.TcpClientSession;
import com.github.steveice10.packetlib.packet.Packet;
import com.github.steveice10.packetlib.event.session.*;
import com.github.steveice10.mc.auth.data.GameProfile;
import com.github.steveice10.mc.protocol.packet.login.clientbound.ClientboundGameProfilePacket;
import java.util.Timer;
import java.util.TimerTask;
import java.util.List;
@ -16,6 +18,7 @@ import lombok.Setter;
public class Client {
@Getter private Session session;
@Getter private final List<SessionListener> listeners = new ArrayList<>();
@Getter private GameProfile profile;
private String host;
private int port;
@ -44,14 +47,15 @@ public class Client {
session.addListener(new SessionAdapter () {
@Override public void packetReceived(Session session, Packet packet) { for (SessionListener listener : listeners) listener.packetReceived(session, packet); }
@Override public void packetSending (PacketSendingEvent event) { for (SessionListener listener : listeners) listener.packetSending(event); }
@Override public void packetSent (Session session, Packet packet) { for (SessionListener listener : listeners) listener.packetSent(session, packet); }
@Override public void packetSent (Session session, Packet packet) {
if (packet instanceof ClientboundGameProfilePacket) profile = ((ClientboundGameProfilePacket) packet).getProfile();
for (SessionListener listener : listeners) listener.packetSent(session, packet);
}
@Override public void packetError (PacketErrorEvent event) {
event.getCause().printStackTrace();
event.setSuppress(true);
for (SessionListener listener : listeners) listener.packetError(event);
}
@Override public void connected (ConnectedEvent event) { for (SessionListener listener : listeners) listener.connected(event); }
@Override public void disconnecting (DisconnectingEvent event) { for (SessionListener listener : listeners) listener.disconnecting(event); }

View file

@ -8,7 +8,6 @@ import land.chipmunk.chipmunkbot.util.UUIDUtilities;
import com.github.steveice10.mc.protocol.packet.ingame.clientbound.ClientboundSystemChatPacket;
import com.github.steveice10.mc.protocol.packet.ingame.clientbound.ClientboundPlayerChatPacket;
import com.github.steveice10.mc.protocol.packet.ingame.clientbound.ClientboundDisguisedChatPacket;
import com.github.steveice10.mc.protocol.packet.login.clientbound.ClientboundGameProfilePacket; // TODO: Move uuid detection elsewhere
import com.github.steveice10.mc.protocol.packet.ingame.serverbound.ServerboundChatPacket;
import com.github.steveice10.mc.protocol.packet.ingame.serverbound.ServerboundChatCommandPacket;
import com.github.steveice10.mc.protocol.packet.ingame.serverbound.inventory.ServerboundSetCreativeModeSlotPacket;
@ -38,7 +37,6 @@ import land.chipmunk.chipmunkbot.systemChat.*;
public class ChatPlugin extends SessionAdapter {
private final ChipmunkBot client;
@Getter private List<Listener> listeners = new ArrayList<>();
private UUID uuid; // TODO: Move uuid detection elsewhere
private List<SystemChatParser> systemChatParsers;
@ -66,7 +64,6 @@ public class ChatPlugin extends SessionAdapter {
if (packet instanceof ClientboundSystemChatPacket) packetReceived(session, (ClientboundSystemChatPacket) packet);
else if (packet instanceof ClientboundPlayerChatPacket) packetReceived(session, (ClientboundPlayerChatPacket) packet);
else if (packet instanceof ClientboundDisguisedChatPacket) packetReceived(session, (ClientboundDisguisedChatPacket) packet);
else if (packet instanceof ClientboundGameProfilePacket) packetReceived(session, (ClientboundGameProfilePacket) packet); // TODO: Move uuid detection elsewhere
}
public void packetReceived (Session session, ClientboundSystemChatPacket packet) {
@ -114,10 +111,6 @@ public class ChatPlugin extends SessionAdapter {
}
}
public void packetReceived (Session session, ClientboundGameProfilePacket packet) {
uuid = packet.getProfile().getId();
}
// ? Should this be here?
// TODO: Break up the method to make it less messy
public void tellraw (Component message, String targets) {
@ -146,7 +139,7 @@ public class ChatPlugin extends SessionAdapter {
final Session session = client.session();
session.send(new ServerboundSetCreativeModeSlotPacket(26, new ItemStack(1 /* stone */, 1, itemTag)));
client.core().run("minecraft:tellraw " + targets + " {\"nbt\":\"Inventory[0].tag.m\",\"entity\":\"" + uuid.toString() + "\",\"interpret\":" + interpret + "}"); // TODO: Use GSON instead of concatenating strings, and hardcode less of this (it shouldn't matter here but yes)
client.core().run("minecraft:tellraw " + targets + " {\"nbt\":\"Inventory[0].tag.m\",\"entity\":\"" + client.profile().getIdAsString() + "\",\"interpret\":" + interpret + "}"); // TODO: Use GSON instead of concatenating strings, and hardcode less of this (it shouldn't matter here but yes)
return;
}