Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

backport: refactor(spigot): cache listening whitelist on protocollib listener #444

Merged
merged 1 commit into from
Sep 11, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
import com.rexcantor64.triton.wrappers.AdventureComponentWrapper;
import com.rexcantor64.triton.wrappers.WrappedClientConfiguration;
import com.rexcantor64.triton.wrappers.WrappedPlayerChatMessage;
import lombok.Getter;
import lombok.val;
import net.md_5.bungee.api.chat.BaseComponent;
import net.md_5.bungee.api.chat.TextComponent;
Expand Down Expand Up @@ -87,6 +88,11 @@ public class ProtocolLibListener implements PacketListener, PacketInterceptor {
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(SpigotMLP main, HandlerFunction.HandlerType... allowedTypes) {
this.main = main;
this.allowedTypes = Arrays.asList(allowedTypes);
Expand Down Expand Up @@ -186,6 +192,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 @@ -802,40 +840,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 */

@Override
Expand Down
Loading