2012-10-13 22:19:01 +02:00
|
|
|
/*
|
|
|
|
* ProtocolLib - Bukkit server library that allows access to the Minecraft protocol.
|
|
|
|
* Copyright (C) 2012 Kristian S. Stangeland
|
|
|
|
*
|
2015-06-17 20:25:39 +02:00
|
|
|
* 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
|
2012-10-13 22:19:01 +02:00
|
|
|
* the License, or (at your option) any later version.
|
|
|
|
*
|
2015-06-17 20:25:39 +02:00
|
|
|
* 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.
|
2012-10-13 22:19:01 +02:00
|
|
|
* See the GNU General Public License for more details.
|
|
|
|
*
|
2015-06-17 20:25:39 +02:00
|
|
|
* 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
|
2012-10-13 22:19:01 +02:00
|
|
|
* 02111-1307 USA
|
|
|
|
*/
|
|
|
|
|
|
|
|
package com.comphenix.protocol.events;
|
|
|
|
|
2013-12-05 06:52:20 +01:00
|
|
|
import com.comphenix.protocol.PacketType;
|
2012-10-16 22:24:30 +02:00
|
|
|
import com.comphenix.protocol.injector.GamePhase;
|
2013-07-19 03:46:51 +02:00
|
|
|
import com.google.common.base.Preconditions;
|
2013-12-07 17:30:59 +01:00
|
|
|
import com.google.common.collect.Iterables;
|
2013-12-07 19:52:02 +01:00
|
|
|
import com.google.common.collect.Sets;
|
2022-03-08 04:09:04 +01:00
|
|
|
import java.util.Set;
|
|
|
|
import javax.annotation.Nonnull;
|
2020-06-06 21:13:29 +02:00
|
|
|
import org.bukkit.plugin.Plugin;
|
|
|
|
|
2012-10-13 22:19:01 +02:00
|
|
|
/**
|
|
|
|
* Represents a packet listener with useful constructors.
|
2012-10-16 22:24:30 +02:00
|
|
|
* <p>
|
|
|
|
* Remember to override onPacketReceiving() and onPacketSending(), depending on the ConnectionSide.
|
2022-03-08 04:09:04 +01:00
|
|
|
*
|
2012-10-13 22:19:01 +02:00
|
|
|
* @author Kristian
|
|
|
|
*/
|
|
|
|
public abstract class PacketAdapter implements PacketListener {
|
2022-03-08 04:09:04 +01:00
|
|
|
|
2012-10-13 22:19:01 +02:00
|
|
|
protected Plugin plugin;
|
|
|
|
protected ConnectionSide connectionSide;
|
|
|
|
protected ListeningWhitelist receivingWhitelist = ListeningWhitelist.EMPTY_WHITELIST;
|
|
|
|
protected ListeningWhitelist sendingWhitelist = ListeningWhitelist.EMPTY_WHITELIST;
|
|
|
|
|
2013-07-19 03:46:51 +02:00
|
|
|
/**
|
2022-03-08 04:09:04 +01:00
|
|
|
* Initialize a packet adapter using a collection of parameters. Use {@link #params()} to get an instance to this
|
|
|
|
* builder.
|
|
|
|
*
|
2013-07-19 03:46:51 +02:00
|
|
|
* @param params - the parameters.
|
|
|
|
*/
|
2016-02-19 18:56:57 +01:00
|
|
|
public PacketAdapter(@Nonnull AdapterParameteters params) {
|
2013-07-19 03:46:51 +02:00
|
|
|
this(
|
2022-03-08 04:09:04 +01:00
|
|
|
checkValidity(params).plugin, params.connectionSide, params.listenerPriority,
|
|
|
|
params.gamePhase, params.options, params.packets
|
2013-07-19 03:46:51 +02:00
|
|
|
);
|
|
|
|
}
|
2022-03-08 04:09:04 +01:00
|
|
|
|
2013-12-05 21:27:22 +01:00
|
|
|
/**
|
|
|
|
* Initialize a packet listener with the given parameters.
|
2022-03-08 04:09:04 +01:00
|
|
|
*
|
2013-12-05 21:27:22 +01:00
|
|
|
* @param plugin - the plugin.
|
2022-03-08 04:09:04 +01:00
|
|
|
* @param types - the packet types.
|
2013-12-05 21:27:22 +01:00
|
|
|
*/
|
|
|
|
public PacketAdapter(Plugin plugin, PacketType... types) {
|
|
|
|
this(plugin, ListenerPriority.NORMAL, types);
|
|
|
|
}
|
2022-03-08 04:09:04 +01:00
|
|
|
|
2013-12-07 17:30:59 +01:00
|
|
|
/**
|
|
|
|
* Initialize a packet listener with the given parameters.
|
2022-03-08 04:09:04 +01:00
|
|
|
*
|
2013-12-07 17:30:59 +01:00
|
|
|
* @param plugin - the plugin.
|
2022-03-08 04:09:04 +01:00
|
|
|
* @param types - the packet types.
|
2013-12-07 17:30:59 +01:00
|
|
|
*/
|
|
|
|
public PacketAdapter(Plugin plugin, Iterable<? extends PacketType> types) {
|
|
|
|
this(params(plugin, Iterables.toArray(types, PacketType.class)));
|
|
|
|
}
|
2022-03-08 04:09:04 +01:00
|
|
|
|
2013-12-07 17:30:59 +01:00
|
|
|
/**
|
|
|
|
* Initialize a packet listener with the given parameters.
|
2022-03-08 04:09:04 +01:00
|
|
|
*
|
|
|
|
* @param plugin - the plugin.
|
2013-12-07 17:30:59 +01:00
|
|
|
* @param listenerPriority - the priority.
|
2022-03-08 04:09:04 +01:00
|
|
|
* @param types - the packet types.
|
2013-12-07 17:30:59 +01:00
|
|
|
*/
|
|
|
|
public PacketAdapter(Plugin plugin, ListenerPriority listenerPriority, Iterable<? extends PacketType> types) {
|
|
|
|
this(params(plugin, Iterables.toArray(types, PacketType.class)).listenerPriority(listenerPriority));
|
|
|
|
}
|
2022-03-08 04:09:04 +01:00
|
|
|
|
2013-12-07 19:14:03 +01:00
|
|
|
/**
|
|
|
|
* Initialize a packet listener with the given parameters.
|
2022-03-08 04:09:04 +01:00
|
|
|
*
|
|
|
|
* @param plugin - the plugin.
|
2013-12-07 19:14:03 +01:00
|
|
|
* @param listenerPriority - the priority.
|
2022-03-08 04:09:04 +01:00
|
|
|
* @param types - the packet types.
|
|
|
|
* @param options - the options.
|
2013-12-07 19:14:03 +01:00
|
|
|
*/
|
2022-03-08 04:09:04 +01:00
|
|
|
public PacketAdapter(Plugin plugin, ListenerPriority listenerPriority, Iterable<? extends PacketType> types,
|
|
|
|
ListenerOptions... options) {
|
|
|
|
this(
|
|
|
|
params(plugin, Iterables.toArray(types, PacketType.class)).listenerPriority(listenerPriority).options(options));
|
2013-12-07 19:14:03 +01:00
|
|
|
}
|
2022-03-08 04:09:04 +01:00
|
|
|
|
2013-12-05 21:27:22 +01:00
|
|
|
/**
|
|
|
|
* Initialize a packet listener with the given parameters.
|
2022-03-08 04:09:04 +01:00
|
|
|
*
|
|
|
|
* @param plugin - the plugin.
|
2013-12-05 21:27:22 +01:00
|
|
|
* @param listenerPriority - the priority.
|
2022-03-08 04:09:04 +01:00
|
|
|
* @param types - the packet types.
|
2013-12-05 21:27:22 +01:00
|
|
|
*/
|
|
|
|
public PacketAdapter(Plugin plugin, ListenerPriority listenerPriority, PacketType... types) {
|
|
|
|
this(params(plugin, types).listenerPriority(listenerPriority));
|
|
|
|
}
|
2020-06-06 21:13:29 +02:00
|
|
|
|
2013-12-05 06:52:20 +01:00
|
|
|
// For internal use only
|
|
|
|
private PacketAdapter(
|
2015-06-17 20:25:39 +02:00
|
|
|
Plugin plugin, ConnectionSide connectionSide, ListenerPriority listenerPriority,
|
2013-12-05 06:52:20 +01:00
|
|
|
GamePhase gamePhase, ListenerOptions[] options, PacketType... packets) {
|
2022-03-08 04:09:04 +01:00
|
|
|
|
|
|
|
if (plugin == null) {
|
2012-10-13 22:19:01 +02:00
|
|
|
throw new IllegalArgumentException("plugin cannot be null");
|
2022-03-08 04:09:04 +01:00
|
|
|
}
|
|
|
|
if (connectionSide == null) {
|
2012-10-13 22:19:01 +02:00
|
|
|
throw new IllegalArgumentException("connectionSide cannot be null");
|
2022-03-08 04:09:04 +01:00
|
|
|
}
|
|
|
|
if (listenerPriority == null) {
|
2012-10-13 22:19:01 +02:00
|
|
|
throw new IllegalArgumentException("listenerPriority cannot be null");
|
2022-03-08 04:09:04 +01:00
|
|
|
}
|
|
|
|
if (gamePhase == null) {
|
2012-10-16 22:24:30 +02:00
|
|
|
throw new IllegalArgumentException("gamePhase cannot be NULL");
|
2022-03-08 04:09:04 +01:00
|
|
|
}
|
|
|
|
if (packets == null) {
|
2012-10-13 22:19:01 +02:00
|
|
|
throw new IllegalArgumentException("packets cannot be null");
|
2022-03-08 04:09:04 +01:00
|
|
|
}
|
|
|
|
if (options == null) {
|
2013-07-17 06:46:34 +02:00
|
|
|
throw new IllegalArgumentException("options cannot be null");
|
2013-07-19 19:47:22 +02:00
|
|
|
}
|
2022-03-08 04:09:04 +01:00
|
|
|
|
2012-10-13 22:19:01 +02:00
|
|
|
// Add whitelists
|
2022-03-08 04:09:04 +01:00
|
|
|
if (connectionSide.isForServer()) {
|
|
|
|
sendingWhitelist = ListeningWhitelist.newBuilder()
|
|
|
|
.priority(listenerPriority)
|
|
|
|
.types(packets)
|
|
|
|
.gamePhase(gamePhase)
|
|
|
|
.options(options)
|
|
|
|
.build();
|
|
|
|
}
|
|
|
|
|
|
|
|
if (connectionSide.isForClient()) {
|
|
|
|
receivingWhitelist = ListeningWhitelist.newBuilder()
|
|
|
|
.priority(listenerPriority)
|
|
|
|
.types(packets)
|
|
|
|
.gamePhase(gamePhase)
|
|
|
|
.options(options)
|
|
|
|
.build();
|
|
|
|
}
|
|
|
|
|
2012-10-13 22:19:01 +02:00
|
|
|
this.plugin = plugin;
|
|
|
|
this.connectionSide = connectionSide;
|
|
|
|
}
|
2022-03-08 04:09:04 +01:00
|
|
|
|
2012-10-13 22:19:01 +02:00
|
|
|
/**
|
|
|
|
* Retrieves the name of the plugin that has been associated with the listener.
|
2022-03-08 04:09:04 +01:00
|
|
|
*
|
2012-10-29 04:17:53 +01:00
|
|
|
* @param listener - the listener.
|
2012-10-13 22:19:01 +02:00
|
|
|
* @return Name of the associated plugin.
|
|
|
|
*/
|
|
|
|
public static String getPluginName(PacketListener listener) {
|
2012-10-29 04:17:53 +01:00
|
|
|
return getPluginName(listener.getPlugin());
|
|
|
|
}
|
2022-03-08 04:09:04 +01:00
|
|
|
|
2012-10-29 04:17:53 +01:00
|
|
|
/**
|
|
|
|
* Retrieves the name of the given plugin.
|
2022-03-08 04:09:04 +01:00
|
|
|
*
|
2012-10-29 04:17:53 +01:00
|
|
|
* @param plugin - the plugin.
|
|
|
|
* @return Name of the given plugin.
|
|
|
|
*/
|
|
|
|
public static String getPluginName(Plugin plugin) {
|
2022-03-08 04:09:04 +01:00
|
|
|
if (plugin == null) {
|
2015-11-24 21:35:23 +01:00
|
|
|
return "UNKNOWN";
|
2022-03-08 04:09:04 +01:00
|
|
|
}
|
2015-11-24 21:35:23 +01:00
|
|
|
|
2012-10-13 22:19:01 +02:00
|
|
|
try {
|
2015-11-24 21:35:23 +01:00
|
|
|
return plugin.getName();
|
2012-10-13 22:19:01 +02:00
|
|
|
} catch (NoSuchMethodError e) {
|
|
|
|
return plugin.toString();
|
|
|
|
}
|
|
|
|
}
|
2022-03-08 04:09:04 +01:00
|
|
|
|
2013-07-19 03:46:51 +02:00
|
|
|
/**
|
|
|
|
* Construct a helper object for passing parameters to the packet adapter.
|
|
|
|
* <p>
|
|
|
|
* This is often simpler and better than passing them directly to each constructor.
|
2022-03-08 04:09:04 +01:00
|
|
|
*
|
2013-07-19 03:46:51 +02:00
|
|
|
* @return Helper object.
|
|
|
|
*/
|
2016-02-19 18:56:57 +01:00
|
|
|
public static AdapterParameteters params() {
|
|
|
|
return new AdapterParameteters();
|
2013-07-19 03:46:51 +02:00
|
|
|
}
|
2013-12-06 01:38:34 +01:00
|
|
|
|
2013-12-05 20:51:28 +01:00
|
|
|
/**
|
|
|
|
* Construct a helper object for passing parameters to the packet adapter.
|
|
|
|
* <p>
|
|
|
|
* This is often simpler and better than passing them directly to each constructor.
|
2022-03-08 04:09:04 +01:00
|
|
|
*
|
|
|
|
* @param plugin - the plugin that spawned this listener.
|
2013-12-05 20:51:28 +01:00
|
|
|
* @param packets - the packet types the listener is looking for.
|
|
|
|
* @return Helper object.
|
|
|
|
*/
|
2016-02-19 18:56:57 +01:00
|
|
|
public static AdapterParameteters params(Plugin plugin, PacketType... packets) {
|
|
|
|
return new AdapterParameteters().plugin(plugin).types(packets);
|
2013-12-05 20:51:28 +01:00
|
|
|
}
|
2022-03-08 04:09:04 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Determine if the required parameters are set.
|
|
|
|
*/
|
|
|
|
private static AdapterParameteters checkValidity(AdapterParameteters params) {
|
|
|
|
if (params == null) {
|
|
|
|
throw new IllegalArgumentException("params cannot be NULL.");
|
|
|
|
}
|
|
|
|
if (params.plugin == null) {
|
|
|
|
throw new IllegalStateException("Plugin was never set in the parameters.");
|
|
|
|
}
|
|
|
|
if (params.connectionSide == null) {
|
|
|
|
throw new IllegalStateException("Connection side was never set in the parameters.");
|
|
|
|
}
|
|
|
|
if (params.packets == null) {
|
|
|
|
throw new IllegalStateException("Packet IDs was never set in the parameters.");
|
|
|
|
}
|
|
|
|
return params;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onPacketReceiving(PacketEvent event) {
|
|
|
|
// Lets prevent some bugs
|
|
|
|
throw new IllegalStateException("Override onPacketReceiving to get notifcations of received packets!");
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onPacketSending(PacketEvent event) {
|
|
|
|
// Lets prevent some bugs
|
|
|
|
throw new IllegalStateException("Override onPacketSending to get notifcations of sent packets!");
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public ListeningWhitelist getReceivingWhitelist() {
|
|
|
|
return receivingWhitelist;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public ListeningWhitelist getSendingWhitelist() {
|
|
|
|
return sendingWhitelist;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public Plugin getPlugin() {
|
|
|
|
return plugin;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public String toString() {
|
|
|
|
// This is used by the error reporter
|
|
|
|
return String.format("PacketAdapter[plugin=%s, sending=%s, receiving=%s]",
|
|
|
|
getPluginName(this),
|
|
|
|
sendingWhitelist,
|
|
|
|
receivingWhitelist);
|
|
|
|
}
|
|
|
|
|
2013-07-19 03:46:51 +02:00
|
|
|
/**
|
|
|
|
* Represents a builder for passing parameters to the packet adapter constructor.
|
2013-12-05 20:51:28 +01:00
|
|
|
* <p>
|
2016-02-19 18:56:57 +01:00
|
|
|
* Note: Never make spelling mistakes in a public API!
|
2022-03-08 04:09:04 +01:00
|
|
|
*
|
2013-07-19 03:46:51 +02:00
|
|
|
* @author Kristian
|
|
|
|
*/
|
2016-02-19 18:56:57 +01:00
|
|
|
public static class AdapterParameteters {
|
2022-03-08 04:09:04 +01:00
|
|
|
|
2013-07-19 03:46:51 +02:00
|
|
|
private Plugin plugin;
|
|
|
|
private ConnectionSide connectionSide;
|
2013-12-05 06:52:20 +01:00
|
|
|
private PacketType[] packets;
|
2022-03-08 04:09:04 +01:00
|
|
|
|
2013-07-19 03:46:51 +02:00
|
|
|
// Parameters with default values
|
|
|
|
private GamePhase gamePhase = GamePhase.PLAYING;
|
|
|
|
private ListenerOptions[] options = new ListenerOptions[0];
|
|
|
|
private ListenerPriority listenerPriority = ListenerPriority.NORMAL;
|
2022-03-08 04:09:04 +01:00
|
|
|
|
2013-07-19 03:46:51 +02:00
|
|
|
/**
|
|
|
|
* Set the plugin that spawned this listener. This parameter is required.
|
2022-03-08 04:09:04 +01:00
|
|
|
*
|
2013-07-19 03:46:51 +02:00
|
|
|
* @param plugin - the plugin.
|
|
|
|
* @return This builder, for chaining.
|
|
|
|
*/
|
2016-02-19 18:56:57 +01:00
|
|
|
public AdapterParameteters plugin(@Nonnull Plugin plugin) {
|
2013-07-19 03:46:51 +02:00
|
|
|
this.plugin = Preconditions.checkNotNull(plugin, "plugin cannot be NULL.");
|
|
|
|
return this;
|
|
|
|
}
|
2022-03-08 04:09:04 +01:00
|
|
|
|
2013-07-19 03:46:51 +02:00
|
|
|
/**
|
|
|
|
* Set the packet types this listener is looking for. This parameter is required.
|
2022-03-08 04:09:04 +01:00
|
|
|
*
|
2013-07-19 03:46:51 +02:00
|
|
|
* @param connectionSide - the new packet type.
|
|
|
|
* @return This builder, for chaining.
|
|
|
|
*/
|
2016-02-19 18:56:57 +01:00
|
|
|
public AdapterParameteters connectionSide(@Nonnull ConnectionSide connectionSide) {
|
2013-07-19 03:46:51 +02:00
|
|
|
this.connectionSide = Preconditions.checkNotNull(connectionSide, "connectionside cannot be NULL.");
|
|
|
|
return this;
|
|
|
|
}
|
2022-03-08 04:09:04 +01:00
|
|
|
|
2013-07-19 03:46:51 +02:00
|
|
|
/**
|
2015-06-17 20:25:39 +02:00
|
|
|
* Set this adapter to also look for client-side packets.
|
2022-03-08 04:09:04 +01:00
|
|
|
*
|
2013-07-19 03:46:51 +02:00
|
|
|
* @return This builder, for chaining.
|
|
|
|
*/
|
2016-02-19 18:56:57 +01:00
|
|
|
public AdapterParameteters clientSide() {
|
2013-07-19 19:31:04 +02:00
|
|
|
return connectionSide(ConnectionSide.add(connectionSide, ConnectionSide.CLIENT_SIDE));
|
2013-07-19 03:46:51 +02:00
|
|
|
}
|
2022-03-08 04:09:04 +01:00
|
|
|
|
2013-07-19 03:46:51 +02:00
|
|
|
/**
|
2015-06-17 20:25:39 +02:00
|
|
|
* Set this adapter to also look for server-side packets.
|
2022-03-08 04:09:04 +01:00
|
|
|
*
|
2013-07-19 03:46:51 +02:00
|
|
|
* @return This builder, for chaining.
|
|
|
|
*/
|
2016-02-19 18:56:57 +01:00
|
|
|
public AdapterParameteters serverSide() {
|
2013-07-19 19:31:04 +02:00
|
|
|
return connectionSide(ConnectionSide.add(connectionSide, ConnectionSide.SERVER_SIDE));
|
2013-07-19 03:46:51 +02:00
|
|
|
}
|
2022-03-08 04:09:04 +01:00
|
|
|
|
2013-07-19 03:46:51 +02:00
|
|
|
/**
|
2015-06-17 20:25:39 +02:00
|
|
|
* Set the the event priority, where the execution is in ascending order from lowest to highest.
|
2013-07-19 03:46:51 +02:00
|
|
|
* <p>
|
|
|
|
* Default is {@link ListenerPriority#NORMAL}.
|
2022-03-08 04:09:04 +01:00
|
|
|
*
|
2013-07-19 03:46:51 +02:00
|
|
|
* @param listenerPriority - the new event priority.
|
|
|
|
* @return This builder, for chaining.
|
|
|
|
*/
|
2016-02-19 18:56:57 +01:00
|
|
|
public AdapterParameteters listenerPriority(@Nonnull ListenerPriority listenerPriority) {
|
2013-07-19 03:46:51 +02:00
|
|
|
this.listenerPriority = Preconditions.checkNotNull(listenerPriority, "listener priority cannot be NULL.");
|
|
|
|
return this;
|
|
|
|
}
|
2022-03-08 04:09:04 +01:00
|
|
|
|
2013-07-19 03:46:51 +02:00
|
|
|
/**
|
2022-03-08 04:09:04 +01:00
|
|
|
* Set which game phase this listener is active under. This is a hint for ProtocolLib to start intercepting login
|
|
|
|
* packets.
|
2013-07-19 03:46:51 +02:00
|
|
|
* <p>
|
|
|
|
* Default is {@link GamePhase#PLAYING}, which will not intercept login packets.
|
2022-03-08 04:09:04 +01:00
|
|
|
*
|
2013-07-19 03:46:51 +02:00
|
|
|
* @param gamePhase - the new game phase.
|
|
|
|
* @return This builder, for chaining.
|
|
|
|
*/
|
2016-02-19 18:56:57 +01:00
|
|
|
public AdapterParameteters gamePhase(@Nonnull GamePhase gamePhase) {
|
2013-07-19 03:46:51 +02:00
|
|
|
this.gamePhase = Preconditions.checkNotNull(gamePhase, "gamePhase cannot be NULL.");
|
|
|
|
return this;
|
|
|
|
}
|
2022-03-08 04:09:04 +01:00
|
|
|
|
2013-07-19 03:46:51 +02:00
|
|
|
/**
|
|
|
|
* Set the game phase to {@link GamePhase#LOGIN}, allowing ProtocolLib to intercept login packets.
|
2022-03-08 04:09:04 +01:00
|
|
|
*
|
2013-07-19 03:46:51 +02:00
|
|
|
* @return This builder, for chaining.
|
|
|
|
*/
|
2016-02-19 18:56:57 +01:00
|
|
|
public AdapterParameteters loginPhase() {
|
2013-07-19 03:46:51 +02:00
|
|
|
return gamePhase(GamePhase.LOGIN);
|
|
|
|
}
|
2022-03-08 04:09:04 +01:00
|
|
|
|
2013-07-19 03:46:51 +02:00
|
|
|
/**
|
2022-03-08 04:09:04 +01:00
|
|
|
* Set listener options that decide whether or not to intercept the raw packet data as read from the network
|
|
|
|
* stream.
|
2013-07-19 03:46:51 +02:00
|
|
|
* <p>
|
|
|
|
* The default is to disable this raw packet interception.
|
2022-03-08 04:09:04 +01:00
|
|
|
*
|
2013-07-19 03:46:51 +02:00
|
|
|
* @param options - every option to use.
|
|
|
|
* @return This builder, for chaining.
|
|
|
|
*/
|
2016-02-19 18:56:57 +01:00
|
|
|
public AdapterParameteters options(@Nonnull ListenerOptions... options) {
|
2013-07-19 03:46:51 +02:00
|
|
|
this.options = Preconditions.checkNotNull(options, "options cannot be NULL.");
|
|
|
|
return this;
|
|
|
|
}
|
2013-12-07 19:14:03 +01:00
|
|
|
|
|
|
|
/**
|
2022-03-08 04:09:04 +01:00
|
|
|
* Set listener options that decide whether or not to intercept the raw packet data as read from the network
|
|
|
|
* stream.
|
2013-12-07 19:14:03 +01:00
|
|
|
* <p>
|
|
|
|
* The default is to disable this raw packet interception.
|
2022-03-08 04:09:04 +01:00
|
|
|
*
|
2013-12-07 19:14:03 +01:00
|
|
|
* @param options - every option to use.
|
|
|
|
* @return This builder, for chaining.
|
|
|
|
*/
|
2016-02-19 18:56:57 +01:00
|
|
|
public AdapterParameteters options(@Nonnull Set<? extends ListenerOptions> options) {
|
2013-12-07 19:14:03 +01:00
|
|
|
Preconditions.checkNotNull(options, "options cannot be NULL.");
|
|
|
|
this.options = options.toArray(new ListenerOptions[0]);
|
|
|
|
return this;
|
|
|
|
}
|
2022-03-08 04:09:04 +01:00
|
|
|
|
2013-12-07 19:14:03 +01:00
|
|
|
/**
|
|
|
|
* Add a given option to the current builder.
|
2022-03-08 04:09:04 +01:00
|
|
|
*
|
2013-12-07 19:14:03 +01:00
|
|
|
* @param option - the option to add.
|
|
|
|
* @return This builder, for chaining.
|
|
|
|
*/
|
2016-02-19 18:56:57 +01:00
|
|
|
private AdapterParameteters addOption(ListenerOptions option) {
|
2013-12-07 19:14:03 +01:00
|
|
|
if (options == null) {
|
|
|
|
return options(option);
|
|
|
|
} else {
|
2022-05-26 05:30:08 +02:00
|
|
|
final Set<ListenerOptions> current = Sets.newHashSet(options);
|
2013-12-07 19:14:03 +01:00
|
|
|
current.add(option);
|
|
|
|
return options(current);
|
|
|
|
}
|
|
|
|
}
|
2020-06-06 21:13:29 +02:00
|
|
|
|
2013-12-07 19:14:03 +01:00
|
|
|
/**
|
2013-12-14 17:49:51 +01:00
|
|
|
* Set the listener option to {@link ListenerOptions#ASYNC}, indicating that our listener is thread safe.
|
|
|
|
* <p>
|
|
|
|
* This allows ProtocolLib to perform certain optimizations.
|
2022-03-08 04:09:04 +01:00
|
|
|
*
|
2013-12-07 19:14:03 +01:00
|
|
|
* @return This builder, for chaining.
|
|
|
|
*/
|
2016-02-19 18:56:57 +01:00
|
|
|
public AdapterParameteters optionAsync() {
|
2013-12-14 17:49:51 +01:00
|
|
|
return addOption(ListenerOptions.ASYNC);
|
2013-07-19 03:46:51 +02:00
|
|
|
}
|
2020-06-06 21:13:29 +02:00
|
|
|
|
2013-12-05 06:52:20 +01:00
|
|
|
/**
|
|
|
|
* Set the packet types the listener is looking for.
|
|
|
|
* <p>
|
|
|
|
* This parameter is required.
|
2022-03-08 04:09:04 +01:00
|
|
|
*
|
2013-12-05 20:55:00 +01:00
|
|
|
* @param packets - the packet types to look for.
|
2013-12-05 06:52:20 +01:00
|
|
|
* @return This builder, for chaining.
|
|
|
|
*/
|
2016-02-19 18:56:57 +01:00
|
|
|
public AdapterParameteters types(@Nonnull PacketType... packets) {
|
2013-12-05 21:27:22 +01:00
|
|
|
// Set the connection side as well
|
|
|
|
if (connectionSide == null) {
|
|
|
|
for (PacketType type : packets) {
|
|
|
|
this.connectionSide = ConnectionSide.add(this.connectionSide, type.getSender().toSide());
|
|
|
|
}
|
|
|
|
}
|
2013-12-05 06:52:20 +01:00
|
|
|
this.packets = Preconditions.checkNotNull(packets, "packets cannot be NULL");
|
2022-03-08 04:09:04 +01:00
|
|
|
|
|
|
|
if (packets.length == 0) {
|
2013-12-07 17:39:06 +01:00
|
|
|
throw new IllegalArgumentException("Passed an empty packet type array.");
|
2022-03-08 04:09:04 +01:00
|
|
|
}
|
2013-12-05 06:52:20 +01:00
|
|
|
return this;
|
|
|
|
}
|
2022-03-08 04:09:04 +01:00
|
|
|
|
2013-12-05 06:52:20 +01:00
|
|
|
/**
|
|
|
|
* Set the packet types the listener is looking for.
|
|
|
|
* <p>
|
|
|
|
* This parameter is required.
|
2022-03-08 04:09:04 +01:00
|
|
|
*
|
2013-12-05 20:55:00 +01:00
|
|
|
* @param packets - a set of packet types to look for.
|
2013-12-05 06:52:20 +01:00
|
|
|
* @return This builder, for chaining.
|
|
|
|
*/
|
2016-02-19 18:56:57 +01:00
|
|
|
public AdapterParameteters types(@Nonnull Set<PacketType> packets) {
|
2013-12-05 06:52:20 +01:00
|
|
|
return types(packets.toArray(new PacketType[0]));
|
|
|
|
}
|
2013-07-19 03:46:51 +02:00
|
|
|
}
|
2012-10-13 22:19:01 +02:00
|
|
|
}
|