cleanup MonitorAdapter (#1831)

This commit is contained in:
Pasqual Koschmieder 2022-08-24 02:53:55 +02:00 committed by GitHub
parent b7c1e096c4
commit 43145bd478
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -17,10 +17,11 @@
package com.comphenix.protocol.events;
import java.util.Collection;
import java.util.logging.Logger;
import com.comphenix.protocol.PacketType;
import com.comphenix.protocol.injector.packet.PacketRegistry;
import org.bukkit.plugin.Plugin;
/**
@ -30,54 +31,47 @@ import org.bukkit.plugin.Plugin;
*/
public abstract class MonitorAdapter implements PacketListener {
private Plugin plugin;
private ListeningWhitelist sending = ListeningWhitelist.EMPTY_WHITELIST;
private ListeningWhitelist receiving = ListeningWhitelist.EMPTY_WHITELIST;
private final Plugin plugin;
private final ListeningWhitelist sending;
private final ListeningWhitelist receiving;
public MonitorAdapter(Plugin plugin, ConnectionSide side) {
initialize(plugin, side, getLogger(plugin));
}
public MonitorAdapter(Plugin plugin, ConnectionSide side, Logger logger) {
initialize(plugin, side, logger);
}
private void initialize(Plugin plugin, ConnectionSide side, Logger logger) {
this.plugin = plugin;
// Recover in case something goes wrong
if (side.isForServer())
this.sending = ListeningWhitelist.newBuilder().monitor().types(PacketRegistry.getServerPacketTypes()).gamePhaseBoth().build();
if (side.isForClient())
this.receiving = ListeningWhitelist.newBuilder().monitor().types(PacketRegistry.getClientPacketTypes()).gamePhaseBoth().build();
// check the connection side and register the packets for the given side
this.sending = side.isForServer() ? buildWhitelist(PacketRegistry.getServerPacketTypes()) : ListeningWhitelist.EMPTY_WHITELIST;
this.receiving = side.isForClient() ? buildWhitelist(PacketRegistry.getClientPacketTypes()) : ListeningWhitelist.EMPTY_WHITELIST;
}
/**
* Retrieve a logger, even if we're running in a CraftBukkit version that doesn't support it.
* @param plugin - the plugin to retrieve.
* @return The logger.
*/
private Logger getLogger(Plugin plugin) {
try {
return plugin.getLogger();
} catch (NoSuchMethodError e) {
return Logger.getLogger("Minecraft");
@Deprecated
public MonitorAdapter(Plugin plugin, ConnectionSide side, Logger logger) {
this(plugin, side);
}
private static ListeningWhitelist buildWhitelist(Collection<PacketType> packetTypes) {
return ListeningWhitelist.newBuilder().monitor().gamePhaseBoth().types(packetTypes).build();
}
@Override
public ListeningWhitelist getSendingWhitelist() {
return sending;
return this.sending;
}
@Override
public ListeningWhitelist getReceivingWhitelist() {
return receiving;
return this.receiving;
}
@Override
public Plugin getPlugin() {
return plugin;
return this.plugin;
}
@Override
public void onPacketSending(PacketEvent event) {
}
@Override
public void onPacketReceiving(PacketEvent event) {
}
}