Skip to content

Commit

Permalink
un-listen to useless packets, re-arrange logic for faster experience,…
Browse files Browse the repository at this point in the history
… fix errors
  • Loading branch information
iiAhmedYT committed Jul 17, 2024
1 parent 6fbe25e commit 64ec4d7
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 24 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,12 @@ public final class PacketChatListener extends PacketListener {
public PacketChatListener(
final ItsMyConfig plugin
) {
super(plugin, PacketType.Play.Server.CHAT, PacketType.Play.Server.DISGUISED_CHAT, PacketType.Play.Server.SYSTEM_CHAT);
super(
plugin,
PacketType.Play.Server.SYSTEM_CHAT,
PacketType.Play.Server.KICK_DISCONNECT
);

Method fromComponent;
try {
fromComponent = AdventureComponentConverter.class.getDeclaredMethod(
Expand Down Expand Up @@ -93,48 +98,43 @@ public void onPacketSending(final PacketEvent event) {
}

private PacketResponse processPacket(final PacketContainer container) {
final StructureModifier<TextComponent> textComponentModifier = container.getModifier().withType(TextComponent.class);
if (textComponentModifier.size() == 1) {
Utilities.debug(() -> "Using Bungeecord TextComponent..");
return new PacketResponse(ResponseType.BUNGEE_COMPONENT, processBaseComponents(textComponentModifier.readSafely(0)));
} else {
Utilities.debug(() -> "Failed to use Bungeecord TextComponent, trying ProtocolLib's WrappedChatComponent");
}

final WrappedChatComponent wrappedComponent = container.getChatComponents().readSafely(0);
if (wrappedComponent != null) {
Utilities.debug(() -> "Trying ProtocolLib's ChatComponent..");
final String found = wrappedComponent.getJson();
if (!found.isEmpty()) {
Utilities.debug(() -> "Found String: " + found);
try {
Utilities.debug(() -> "Trying as json");
return new PacketResponse(ResponseType.WRAPPED_COMPONENT, AbstractComponent.parse(found).toMiniMessage());
} catch (final Exception e) {
Utilities.debug(() -> "An error happened while de/serializing " + found + ": ", e);
}
}
}

final String rawMessage = container.getStrings().readSafely(0);
if (rawMessage != null) {
Utilities.debug(() -> "Raw-Parsing message: " + rawMessage);
return new PacketResponse(ResponseType.JSON, AbstractComponent.parse(rawMessage).toMiniMessage());
}

if (internalAdventure) {
try {
final StructureModifier<?> modifier = container.getModifier().withType(AdventureComponentConverter.getComponentClass());
if (modifier.size() == 1) {
Utilities.debug(() -> "Trying SERVER_ADVENTRURE..");
final WrappedChatComponent wrappedAComponent = (WrappedChatComponent) fromComponent.invoke(null, modifier.readSafely(0));
final String json = wrappedAComponent.getJson();
Utilities.debug(() -> "Performing Server-Side Adventure for " + json);
Utilities.debug(() -> "Found JSON: " + json);
return new PacketResponse(ResponseType.SERVER_ADVENTURE, AbstractComponent.parse(json).toMiniMessage());
} else {
Utilities.debug(() -> "Failed to use Server-Side Adventure");
}
} catch (Throwable ignored) {
Utilities.debug(() -> "Failed to use Server-Side Adventure");
}
} catch (Throwable ignored) {}
}

final StructureModifier<TextComponent> textComponentModifier = container.getModifier().withType(TextComponent.class);
if (textComponentModifier.size() == 1) {
Utilities.debug(() -> "Trying Bungeecord TextComponent..");
return new PacketResponse(ResponseType.BUNGEE_COMPONENT, processBaseComponents(textComponentModifier.readSafely(0)));
}

final String rawMessage = container.getStrings().readSafely(0);
if (rawMessage != null) {
Utilities.debug(() -> "Raw-Parsing message: " + rawMessage);
return new PacketResponse(ResponseType.JSON, AbstractComponent.parse(rawMessage).toMiniMessage());
}

return null;
Expand Down
3 changes: 2 additions & 1 deletion src/main/java/to/itsme/itsmyconfig/util/Message.java
Original file line number Diff line number Diff line change
Expand Up @@ -40,12 +40,13 @@ public void send(final BukkitCommandActor actor, final TagResolver... replacers)
}

@Override
@SuppressWarnings("all")
public String toString() {
final Object msg = plugin.getConfig().get("messages." + this.path);

final String result;
if (msg instanceof List<?>) {
result = Strings.toString((List<?>) msg);
result = Strings.toString((List<String>) msg);
} else if (msg instanceof String) {
result = (String) msg;
} else {
Expand Down
5 changes: 4 additions & 1 deletion src/main/java/to/itsme/itsmyconfig/util/Utilities.java
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,10 @@ public final class Utilities {
*/
public static void debug(final Supplier<String> supplier) {
if (plugin.isDebug()) {
plugin.getLogger().info(supplier.get());
final String[] splitText = supplier.get().split("\\n");
for (final String text : splitText) {
plugin.getLogger().info(text);
}
}
}

Expand Down

0 comments on commit 64ec4d7

Please sign in to comment.