Skip to content

Commit

Permalink
fix: Avoid creating packet listener instances twice and instead regis…
Browse files Browse the repository at this point in the history
…ter the current packet listener instance if it is loadable
  • Loading branch information
MemencioPerez committed May 19, 2024
1 parent f2d9f92 commit 23cdda2
Show file tree
Hide file tree
Showing 8 changed files with 25 additions and 42 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
import com.velocitypowered.api.proxy.ProxyServer;
import com.velocitypowered.proxy.VelocityServer;
import io.github._4drian3d.unsignedvelocity.configuration.Configuration;
import io.github._4drian3d.unsignedvelocity.listener.LoadableEventListener;
import io.github._4drian3d.unsignedvelocity.listener.LoadablePacketListener;
import io.github._4drian3d.unsignedvelocity.listener.packet.chat.ChatListener;
import io.github._4drian3d.unsignedvelocity.listener.packet.command.CommandListener;
import io.github._4drian3d.unsignedvelocity.listener.packet.login.LoginListener;
Expand Down Expand Up @@ -86,8 +86,8 @@ public void onProxyInitialize(ProxyInitializeEvent event) {
ServerDataListener.class,
ServerResponseListener.class
).map(injector::getInstance)
.filter(LoadableEventListener::canBeLoaded)
.forEach(listener -> listener.register(this));
.filter(LoadablePacketListener::canBeLoaded)
.forEach(LoadablePacketListener::register);

PacketEvents.getAPI().init();

Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package io.github._4drian3d.unsignedvelocity.listener;

import com.github.retrooper.packetevents.PacketEvents;
import com.github.retrooper.packetevents.event.PacketListenerCommon;

public interface LoadablePacketListener {
default void register() {
PacketEvents.getAPI().getEventManager().registerListener((PacketListenerCommon) this);
}

boolean canBeLoaded();
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package io.github._4drian3d.unsignedvelocity.listener.packet.chat;

import com.github.retrooper.packetevents.PacketEvents;
import com.github.retrooper.packetevents.event.PacketListenerAbstract;
import com.github.retrooper.packetevents.event.PacketListenerPriority;
import com.github.retrooper.packetevents.event.PacketReceiveEvent;
Expand All @@ -11,12 +10,12 @@
import com.google.inject.Inject;
import com.velocitypowered.proxy.connection.client.ConnectedPlayer;
import io.github._4drian3d.unsignedvelocity.UnSignedVelocity;
import io.github._4drian3d.unsignedvelocity.listener.LoadableEventListener;
import io.github._4drian3d.unsignedvelocity.listener.LoadablePacketListener;
import io.github._4drian3d.unsignedvelocity.utils.ConnectionUtil;

import java.time.Instant;

public final class ChatListener extends PacketListenerAbstract implements LoadableEventListener {
public final class ChatListener extends PacketListenerAbstract implements LoadablePacketListener {
private final UnSignedVelocity plugin;

@Inject
Expand All @@ -25,9 +24,6 @@ public ChatListener(UnSignedVelocity plugin) {
this.plugin = plugin;
}

@Override
public void register(UnSignedVelocity plugin) { PacketEvents.getAPI().getEventManager().registerListener(new ChatListener(plugin)); }

@Override
public boolean canBeLoaded() {
return plugin.getConfiguration().applyChatMessages();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package io.github._4drian3d.unsignedvelocity.listener.packet.command;

import com.github.retrooper.packetevents.PacketEvents;
import com.github.retrooper.packetevents.event.PacketListenerAbstract;
import com.github.retrooper.packetevents.event.PacketListenerPriority;
import com.github.retrooper.packetevents.event.PacketReceiveEvent;
Expand All @@ -11,12 +10,12 @@
import com.google.inject.Inject;
import com.velocitypowered.proxy.connection.client.ConnectedPlayer;
import io.github._4drian3d.unsignedvelocity.UnSignedVelocity;
import io.github._4drian3d.unsignedvelocity.listener.LoadableEventListener;
import io.github._4drian3d.unsignedvelocity.listener.LoadablePacketListener;
import io.github._4drian3d.unsignedvelocity.utils.ConnectionUtil;

import java.time.Instant;

public final class CommandListener extends PacketListenerAbstract implements LoadableEventListener {
public final class CommandListener extends PacketListenerAbstract implements LoadablePacketListener {
private final UnSignedVelocity plugin;

@Inject
Expand All @@ -25,9 +24,6 @@ public CommandListener(UnSignedVelocity plugin) {
this.plugin = plugin;
}

@Override
public void register(UnSignedVelocity plugin) { PacketEvents.getAPI().getEventManager().registerListener(new CommandListener(plugin)); }

@Override
public boolean canBeLoaded() {
return plugin.getConfiguration().removeSignedCommandInformation();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package io.github._4drian3d.unsignedvelocity.listener.packet.data;

import com.github.retrooper.packetevents.PacketEvents;
import com.github.retrooper.packetevents.event.PacketListenerAbstract;
import com.github.retrooper.packetevents.event.PacketListenerPriority;
import com.github.retrooper.packetevents.event.PacketSendEvent;
Expand All @@ -11,9 +10,9 @@
import com.github.retrooper.packetevents.wrapper.play.server.WrapperPlayServerServerData;
import com.google.inject.Inject;
import io.github._4drian3d.unsignedvelocity.UnSignedVelocity;
import io.github._4drian3d.unsignedvelocity.listener.LoadableEventListener;
import io.github._4drian3d.unsignedvelocity.listener.LoadablePacketListener;

public final class ServerDataListener extends PacketListenerAbstract implements LoadableEventListener {
public final class ServerDataListener extends PacketListenerAbstract implements LoadablePacketListener {
private final UnSignedVelocity plugin;

@Inject
Expand All @@ -22,9 +21,6 @@ public ServerDataListener(UnSignedVelocity plugin) {
this.plugin = plugin;
}

@Override
public void register(UnSignedVelocity plugin) { PacketEvents.getAPI().getEventManager().registerListener(new ServerDataListener(plugin)); }

@Override
public boolean canBeLoaded() {
return plugin.getConfiguration().sendSecureChatData();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package io.github._4drian3d.unsignedvelocity.listener.packet.login;

import com.github.retrooper.packetevents.PacketEvents;
import com.github.retrooper.packetevents.event.PacketListenerAbstract;
import com.github.retrooper.packetevents.event.PacketListenerPriority;
import com.github.retrooper.packetevents.event.PacketReceiveEvent;
Expand All @@ -13,13 +12,13 @@
import com.github.retrooper.packetevents.wrapper.login.server.WrapperLoginServerEncryptionRequest;
import com.google.inject.Inject;
import io.github._4drian3d.unsignedvelocity.UnSignedVelocity;
import io.github._4drian3d.unsignedvelocity.listener.LoadableEventListener;
import io.github._4drian3d.unsignedvelocity.listener.LoadablePacketListener;
import io.github._4drian3d.unsignedvelocity.utils.ClientVersionUtil;

import java.security.PublicKey;
import java.util.WeakHashMap;

public final class LoginListener extends PacketListenerAbstract implements LoadableEventListener {
public final class LoginListener extends PacketListenerAbstract implements LoadablePacketListener {
private final UnSignedVelocity plugin;
public static final WeakHashMap<User, byte[]> SERVER_ENCRYPTED_VERIFY_TOKENS_CACHE = new WeakHashMap<>();

Expand All @@ -29,9 +28,6 @@ public LoginListener(UnSignedVelocity plugin) {
this.plugin = plugin;
}

@Override
public void register(UnSignedVelocity plugin) { PacketEvents.getAPI().getEventManager().registerListener(new LoginListener(plugin)); }

@Override
public boolean canBeLoaded() {
return plugin.getConfiguration().removeSignedKey();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package io.github._4drian3d.unsignedvelocity.listener.packet.status;

import com.github.retrooper.packetevents.PacketEvents;
import com.github.retrooper.packetevents.event.PacketListenerAbstract;
import com.github.retrooper.packetevents.event.PacketListenerPriority;
import com.github.retrooper.packetevents.event.PacketSendEvent;
Expand All @@ -11,9 +10,9 @@
import com.google.gson.JsonObject;
import com.google.inject.Inject;
import io.github._4drian3d.unsignedvelocity.UnSignedVelocity;
import io.github._4drian3d.unsignedvelocity.listener.LoadableEventListener;
import io.github._4drian3d.unsignedvelocity.listener.LoadablePacketListener;

public final class ServerResponseListener extends PacketListenerAbstract implements LoadableEventListener {
public final class ServerResponseListener extends PacketListenerAbstract implements LoadablePacketListener {
private final UnSignedVelocity plugin;

@Inject
Expand All @@ -22,9 +21,6 @@ public ServerResponseListener(UnSignedVelocity plugin) {
this.plugin = plugin;
}

@Override
public void register(UnSignedVelocity plugin) { PacketEvents.getAPI().getEventManager().registerListener(new ServerResponseListener(plugin)); }

@Override
public boolean canBeLoaded() {
return plugin.getConfiguration().sendSafeServerStatus();
Expand Down

0 comments on commit 23cdda2

Please sign in to comment.