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
1 changed files with 36 additions and 42 deletions

View File

@ -2,82 +2,76 @@
* ProtocolLib - Bukkit server library that allows access to the Minecraft protocol. * ProtocolLib - Bukkit server library that allows access to the Minecraft protocol.
* Copyright (C) 2012 Kristian S. Stangeland * Copyright (C) 2012 Kristian S. Stangeland
* *
* This program is free software; you can redistribute it and/or modify it under the terms of the * This program is free software; you can redistribute it and/or modify it under the terms of the
* GNU General Public License as published by the Free Software Foundation; either version 2 of * GNU General Public License as published by the Free Software Foundation; either version 2 of
* the License, or (at your option) any later version. * the License, or (at your option) any later version.
* *
* This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; * This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
* without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
* See the GNU General Public License for more details. * See the GNU General Public License for more details.
* *
* You should have received a copy of the GNU General Public License along with this program; * You should have received a copy of the GNU General Public License along with this program;
* if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA * if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
* 02111-1307 USA * 02111-1307 USA
*/ */
package com.comphenix.protocol.events; package com.comphenix.protocol.events;
import java.util.Collection;
import java.util.logging.Logger; import java.util.logging.Logger;
import com.comphenix.protocol.PacketType;
import com.comphenix.protocol.injector.packet.PacketRegistry; import com.comphenix.protocol.injector.packet.PacketRegistry;
import org.bukkit.plugin.Plugin; import org.bukkit.plugin.Plugin;
/** /**
* Represents a listener that is notified of every sent and received packet. * Represents a listener that is notified of every sent and received packet.
* *
* @author Kristian * @author Kristian
*/ */
public abstract class MonitorAdapter implements PacketListener { public abstract class MonitorAdapter implements PacketListener {
private Plugin plugin; private final Plugin plugin;
private ListeningWhitelist sending = ListeningWhitelist.EMPTY_WHITELIST; private final ListeningWhitelist sending;
private ListeningWhitelist receiving = ListeningWhitelist.EMPTY_WHITELIST; private final ListeningWhitelist receiving;
public MonitorAdapter(Plugin plugin, ConnectionSide side) { 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; this.plugin = plugin;
// Recover in case something goes wrong // check the connection side and register the packets for the given side
if (side.isForServer()) this.sending = side.isForServer() ? buildWhitelist(PacketRegistry.getServerPacketTypes()) : ListeningWhitelist.EMPTY_WHITELIST;
this.sending = ListeningWhitelist.newBuilder().monitor().types(PacketRegistry.getServerPacketTypes()).gamePhaseBoth().build(); this.receiving = side.isForClient() ? buildWhitelist(PacketRegistry.getClientPacketTypes()) : ListeningWhitelist.EMPTY_WHITELIST;
if (side.isForClient())
this.receiving = ListeningWhitelist.newBuilder().monitor().types(PacketRegistry.getClientPacketTypes()).gamePhaseBoth().build();
} }
/** @Deprecated
* Retrieve a logger, even if we're running in a CraftBukkit version that doesn't support it. public MonitorAdapter(Plugin plugin, ConnectionSide side, Logger logger) {
* @param plugin - the plugin to retrieve. this(plugin, side);
* @return The logger.
*/
private Logger getLogger(Plugin plugin) {
try {
return plugin.getLogger();
} catch (NoSuchMethodError e) {
return Logger.getLogger("Minecraft");
}
} }
private static ListeningWhitelist buildWhitelist(Collection<PacketType> packetTypes) {
return ListeningWhitelist.newBuilder().monitor().gamePhaseBoth().types(packetTypes).build();
}
@Override @Override
public ListeningWhitelist getSendingWhitelist() { public ListeningWhitelist getSendingWhitelist() {
return sending; return this.sending;
} }
@Override @Override
public ListeningWhitelist getReceivingWhitelist() { public ListeningWhitelist getReceivingWhitelist() {
return receiving; return this.receiving;
} }
@Override @Override
public Plugin getPlugin() { public Plugin getPlugin() {
return plugin; return this.plugin;
}
@Override
public void onPacketSending(PacketEvent event) {
}
@Override
public void onPacketReceiving(PacketEvent event) {
} }
} }