Skip to content

Commit

Permalink
refactor(spigot): cache listening whitelist on protocollib listener
Browse files Browse the repository at this point in the history
  • Loading branch information
diogotcorreia committed Sep 11, 2024
1 parent d9a6b71 commit b474694
Showing 1 changed file with 38 additions and 34 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
import com.rexcantor64.triton.spigot.wrappers.WrappedClientConfiguration;
import com.rexcantor64.triton.utils.ComponentUtils;
import com.rexcantor64.triton.wrappers.WrappedPlayerChatMessage;
import lombok.Getter;
import lombok.val;
import net.kyori.adventure.text.Component;
import net.kyori.adventure.text.serializer.legacy.LegacyComponentSerializer;
Expand Down Expand Up @@ -85,6 +86,11 @@ public class ProtocolLibListener implements PacketListener {
private final Map<PacketType, HandlerFunction> packetHandlers = new HashMap<>();
private final AtomicBoolean firstRun = new AtomicBoolean(true);

@Getter
private ListeningWhitelist sendingWhitelist;
@Getter
private ListeningWhitelist receivingWhitelist;

public ProtocolLibListener(SpigotTriton main, HandlerFunction.HandlerType... allowedTypes) {
this.main = main;
this.allowedTypes = Arrays.asList(allowedTypes);
Expand Down Expand Up @@ -185,6 +191,38 @@ private void setupPacketHandlers() {
bossBarPacketHandler.registerPacketTypes(packetHandlers);
entitiesPacketHandler.registerPacketTypes(packetHandlers);
signPacketHandler.registerPacketTypes(packetHandlers);

setupListenerWhitelists();
}

private void setupListenerWhitelists() {
val sendingTypes = packetHandlers.entrySet().stream()
.filter(entry -> this.allowedTypes.contains(entry.getValue().getHandlerType()))
.map(Map.Entry::getKey)
.collect(Collectors.toList());

sendingWhitelist = ListeningWhitelist.newBuilder()
.gamePhase(GamePhase.PLAYING)
.types(sendingTypes)
.mergeOptions(ListenerOptions.ASYNC)
.highest()
.build();

val receivingTypes = new ArrayList<PacketType>();
if (this.allowedTypes.contains(HandlerFunction.HandlerType.SYNC)) {
// only listen for these packets in the sync handler
receivingTypes.add(PacketType.Play.Client.SETTINGS);
if (MinecraftVersion.CONFIG_PHASE_PROTOCOL_UPDATE.atOrAbove()) { // MC 1.20.2
receivingTypes.add(PacketType.Configuration.Client.CLIENT_INFORMATION);
}
}

receivingWhitelist = ListeningWhitelist.newBuilder()
.gamePhase(GamePhase.PLAYING)
.types(receivingTypes)
.mergeOptions(ListenerOptions.ASYNC)
.highest()
.build();
}

/* PACKET HANDLERS */
Expand Down Expand Up @@ -821,40 +859,6 @@ public void onPacketReceiving(PacketEvent packet) {
}
}

@Override
public ListeningWhitelist getSendingWhitelist() {
val types = packetHandlers.entrySet().stream()
.filter(entry -> this.allowedTypes.contains(entry.getValue().getHandlerType()))
.map(Map.Entry::getKey)
.collect(Collectors.toList());

return ListeningWhitelist.newBuilder()
.gamePhase(GamePhase.PLAYING)
.types(types)
.mergeOptions(ListenerOptions.ASYNC)
.highest()
.build();
}

@Override
public ListeningWhitelist getReceivingWhitelist() {
val types = new ArrayList<PacketType>();
if (this.allowedTypes.contains(HandlerFunction.HandlerType.SYNC)) {
// only listen for these packets in the sync handler
types.add(PacketType.Play.Client.SETTINGS);
if (MinecraftVersion.CONFIG_PHASE_PROTOCOL_UPDATE.atOrAbove()) { // MC 1.20.2
types.add(PacketType.Configuration.Client.CLIENT_INFORMATION);
}
}

return ListeningWhitelist.newBuilder()
.gamePhase(GamePhase.PLAYING)
.types(types)
.mergeOptions(ListenerOptions.ASYNC)
.highest()
.build();
}

/* REFRESH */

public void refreshSigns(SpigotLanguagePlayer player) {
Expand Down

0 comments on commit b474694

Please sign in to comment.