Skip to content

Commit

Permalink
Make PacketProcessor interface, make code more generic
Browse files Browse the repository at this point in the history
  • Loading branch information
iiAhmedYT committed Dec 5, 2024
1 parent e59ad50 commit 5f50ec6
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 29 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@
import to.itsme.itsmyconfig.ItsMyConfig;
import to.itsme.itsmyconfig.component.AbstractComponent;
import to.itsme.itsmyconfig.listener.PacketListener;
import to.itsme.itsmyconfig.processor.PacketForm;
import to.itsme.itsmyconfig.processor.UnpackedPacket;
import to.itsme.itsmyconfig.processor.impl.ProtocolLibProcessor;
import to.itsme.itsmyconfig.processor.PacketContent;
import to.itsme.itsmyconfig.util.Strings;
import to.itsme.itsmyconfig.util.Utilities;

Expand All @@ -21,7 +21,7 @@ public final class PacketChatListener extends PacketListener {
private static final String DEBUG_HYPHEN = "###############################################";

/* Here we cache the packet check types for faster handling */
private final Map<PacketType, PacketForm> packetTypeMap = new HashMap<>(4);
private final Map<PacketType, ProtocolLibProcessor> packetTypeMap = new HashMap<>(4);

public PacketChatListener(
final ItsMyConfig plugin
Expand All @@ -39,7 +39,7 @@ public void onPacketSending(final PacketEvent event) {
final PacketContainer container = event.getPacket();
final PacketType type = container.getType();
Utilities.debug(() -> "################# CHAT PACKET #################\nProccessing packet " + type.name());
final UnpackedPacket packet = this.processPacket(container);
final PacketContent<PacketContainer> packet = this.processPacket(container);
if (packet == null || packet.isEmpty()) {
Utilities.debug(() -> "Packet is null or empty\n" + DEBUG_HYPHEN);
return;
Expand All @@ -65,21 +65,21 @@ public void onPacketSending(final PacketEvent event) {
Utilities.debug(() -> DEBUG_HYPHEN);
}

private UnpackedPacket processPacket(final PacketContainer container) {
private PacketContent<PacketContainer> processPacket(final PacketContainer container) {
final PacketType type = container.getType();
final PacketForm foundForm = packetTypeMap.get(type);
if (foundForm != null) {
Utilities.debug(() -> "Using " + foundForm.name() + " to unpack the packet (cached)");
return foundForm.unpack(container);
final ProtocolLibProcessor foundProcessor = packetTypeMap.get(type);
if (foundProcessor != null) {
Utilities.debug(() -> "Using " + foundProcessor.name() + " to unpack the packet (cached)");
return foundProcessor.unpack(container);
}

Utilities.debug(() -> "Figuring " + type.name() + "'s packet form..");
for (final PacketForm form : PacketForm.values()) {
Utilities.debug(() -> "Trying " + form.name() + "..");
final UnpackedPacket unpacked = form.unpack(container);
Utilities.debug(() -> "Figuring " + type.name() + "'s packet processor..");
for (final ProtocolLibProcessor processor : ProtocolLibProcessor.values()) {
Utilities.debug(() -> "Trying " + processor.name() + "..");
final PacketContent<PacketContainer> unpacked = processor.unpack(container);
if (unpacked != null) {
packetTypeMap.put(type, form);
Utilities.debug(() -> "Matched form " + form.name() + " for packet " + type.name());
packetTypeMap.put(type, processor);
Utilities.debug(() -> "Matched processor " + processor.name() + " for packet " + type.name());
return unpacked;
}
Utilities.debug(() -> "Didn't work, trying next (if there is) ..");
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
package to.itsme.itsmyconfig.processor;

import com.comphenix.protocol.events.PacketContainer;
import net.kyori.adventure.text.Component;

public record UnpackedPacket(PacketForm form, String message) {
public record PacketContent<C>(PacketProcessor<C> form, String message) {

public void save(final PacketContainer container, final Component component) {
public void save(final C container, final Component component) {
this.form.edit(container, component);
}

Expand Down
28 changes: 28 additions & 0 deletions src/main/java/to/itsme/itsmyconfig/processor/PacketProcessor.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package to.itsme.itsmyconfig.processor;

import net.kyori.adventure.text.Component;

/**
* Represents a packet that has been unpacked.
*
* @param <C> The packet container type.
*/
public interface PacketProcessor<C> {

/**
* Edits the packet container with the given component.
*
* @param container The packet container.
* @param component The component to edit with.
*/
void edit(final C container, final Component component);

/**
* Unpacks the packet container into a {@link PacketContent}.
*
* @param container The packet container.
* @return The unpacked packet.
*/
PacketContent<C> unpack(final C container);

}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package to.itsme.itsmyconfig.processor;
package to.itsme.itsmyconfig.processor.impl;

import com.comphenix.protocol.events.PacketContainer;
import com.comphenix.protocol.reflect.StructureModifier;
Expand All @@ -7,10 +7,12 @@
import net.md_5.bungee.api.chat.BaseComponent;
import net.md_5.bungee.api.chat.TextComponent;
import to.itsme.itsmyconfig.component.AbstractComponent;
import to.itsme.itsmyconfig.processor.PacketProcessor;
import to.itsme.itsmyconfig.processor.PacketContent;
import to.itsme.itsmyconfig.util.Utilities;
import to.itsme.itsmyconfig.util.Versions;

public enum PacketForm {
public enum ProtocolLibProcessor implements PacketProcessor<PacketContainer> {

SERVER_ADVENTURE {

Expand All @@ -20,7 +22,7 @@ public void edit(PacketContainer container, Component component) {
}

@Override
public UnpackedPacket unpack(PacketContainer container) {
public PacketContent<PacketContainer> unpack(PacketContainer container) {
if (!Versions.IS_PAPER || Versions.MINOR < 16) {
return null;
}
Expand Down Expand Up @@ -49,7 +51,7 @@ public void edit(PacketContainer container, Component component) {
}

@Override
public UnpackedPacket unpack(PacketContainer container) {
public PacketContent<PacketContainer> unpack(PacketContainer container) {
final WrappedChatComponent wrappedComponent = container.getChatComponents().readSafely(0);
if (wrappedComponent == null) {
return null;
Expand Down Expand Up @@ -80,7 +82,7 @@ public void edit(PacketContainer container, Component component) {
}

@Override
public UnpackedPacket unpack(PacketContainer container) {
public PacketContent<PacketContainer> unpack(PacketContainer container) {
final StructureModifier<TextComponent> textComponentModifier = container.getModifier().withType(TextComponent.class);
if (textComponentModifier.size() == 1) {
return this.of(processBaseComponents(textComponentModifier.readSafely(0)));
Expand All @@ -102,7 +104,7 @@ public void edit(PacketContainer container, Component component) {
}

@Override
public UnpackedPacket unpack(final PacketContainer container) {
public PacketContent<PacketContainer> unpack(final PacketContainer container) {
final String rawMessage = container.getStrings().readSafely(0);
if (rawMessage == null) {
return null;
Expand All @@ -112,11 +114,8 @@ public UnpackedPacket unpack(final PacketContainer container) {

};

public abstract void edit(final PacketContainer container, final Component component);
public abstract UnpackedPacket unpack(final PacketContainer container);

UnpackedPacket of(final String message) {
return new UnpackedPacket(this, message);
PacketContent<PacketContainer> of(final String message) {
return new PacketContent<>(this, message);
}

}

0 comments on commit 5f50ec6

Please sign in to comment.