Archiviert
13
0

Correctly clone packet listeners.

Dieser Commit ist enthalten in:
Kristian S. Stangeland 2013-08-06 20:41:25 +02:00
Ursprung 65f1371cf4
Commit 18f5998f8a
2 geänderte Dateien mit 165 neuen und 42 gelöschten Zeilen

Datei anzeigen

@ -67,7 +67,7 @@ class NullPacketListener implements PacketListener {
private ListeningWhitelist cloneWhitelist(ListenerPriority priority, ListeningWhitelist whitelist) {
if (whitelist != null)
return new ListeningWhitelist(priority, whitelist.getWhitelist(), whitelist.getGamePhase());
return ListeningWhitelist.newBuilder(whitelist).priority(priority).build();
else
return null;
}

Datei anzeigen

@ -18,11 +18,13 @@
package com.comphenix.protocol.events;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.EnumSet;
import java.util.Set;
import com.comphenix.protocol.injector.GamePhase;
import com.google.common.base.Objects;
import com.google.common.collect.Sets;
@ -32,16 +34,22 @@ import com.google.common.collect.Sets;
* @author Kristian
*/
public class ListeningWhitelist {
/**
* A whitelist with no packets - indicates that the listener shouldn't observe any packets.
*/
public static final ListeningWhitelist EMPTY_WHITELIST = new ListeningWhitelist(ListenerPriority.LOW);
private ListenerPriority priority;
private Set<Integer> whitelist;
private GamePhase gamePhase;
private Set<ListenerOptions> options = EnumSet.noneOf(ListenerOptions.class);
private final ListenerPriority priority;
private final Set<Integer> whitelist;
private final GamePhase gamePhase;
private final Set<ListenerOptions> options;
private ListeningWhitelist(Builder builder) {
this.priority = builder.priority;
this.whitelist = builder.whitelist;
this.gamePhase = builder.gamePhase;
this.options = builder.options;
}
/**
* Creates a packet whitelist for a given priority with a set of packet IDs.
@ -60,8 +68,9 @@ public class ListeningWhitelist {
*/
public ListeningWhitelist(ListenerPriority priority, Set<Integer> whitelist, GamePhase gamePhase) {
this.priority = priority;
this.whitelist = whitelist;
this.whitelist = safeSet(whitelist);
this.gamePhase = gamePhase;
this.options = EnumSet.noneOf(ListenerOptions.class);
}
/**
@ -73,6 +82,7 @@ public class ListeningWhitelist {
this.priority = priority;
this.whitelist = Sets.newHashSet(whitelist);
this.gamePhase = GamePhase.PLAYING;
this.options = EnumSet.noneOf(ListenerOptions.class);
}
/**
@ -85,6 +95,7 @@ public class ListeningWhitelist {
this.priority = priority;
this.whitelist = Sets.newHashSet(whitelist);
this.gamePhase = gamePhase;
this.options = EnumSet.noneOf(ListenerOptions.class);
}
/**
@ -92,15 +103,13 @@ public class ListeningWhitelist {
* @param priority - the listener priority.
* @param whitelist - list of packet IDs to observe/enable.
* @param gamePhase - which game phase to receieve notifications on.
* @param options - every special option associated with this whitelist.
*/
public ListeningWhitelist(ListenerPriority priority, Integer[] whitelist, GamePhase gamePhase, ListenerOptions... options) {
this.priority = priority;
this.whitelist = Sets.newHashSet(whitelist);
this.gamePhase = gamePhase;
if (options != null) {
this.options.addAll(Arrays.asList(options));
}
this.options = safeEnumSet(Arrays.asList(options), ListenerOptions.class);
}
/**
@ -144,7 +153,7 @@ public class ListeningWhitelist {
}
@Override
public int hashCode(){
public int hashCode() {
return Objects.hashCode(priority, whitelist, gamePhase, options);
}
@ -180,14 +189,14 @@ public class ListeningWhitelist {
}
@Override
public boolean equals(final Object obj){
if(obj instanceof ListeningWhitelist){
public boolean equals(final Object obj) {
if (obj instanceof ListeningWhitelist) {
final ListeningWhitelist other = (ListeningWhitelist) obj;
return Objects.equal(priority, other.priority)
&& Objects.equal(whitelist, other.whitelist)
&& Objects.equal(gamePhase, other.gamePhase)
&& Objects.equal(options, other.options);
} else{
} else {
return false;
}
}
@ -197,11 +206,125 @@ public class ListeningWhitelist {
if (this == EMPTY_WHITELIST)
return "EMPTY_WHITELIST";
else
return Objects.toStringHelper(this)
.add("priority", priority)
.add("packets", whitelist)
.add("gamephase", gamePhase)
.add("options", options).
return Objects.toStringHelper(this).
add("priority", priority).
add("packets", whitelist).
add("gamephase", gamePhase).
add("options", options).
toString();
}
/**
* Construct a new builder of whitelists.
* @return New whitelist builder.
*/
public static Builder newBuilder() {
return new Builder(null);
}
/**
* Construct a new builder of whitelists initialized to the same values as the template.
* @param template - the template object.
* @return New whitelist builder.
*/
public static Builder newBuilder(ListeningWhitelist template) {
return new Builder(template);
}
/**
* Construct a copy of a given enum.
* @param options - the options to copy, or NULL to indicate the empty set.
* @return A copy of the enum set.
*/
private static <T extends Enum<T>> EnumSet<T> safeEnumSet(Collection<T> options, Class<T> enumClass) {
if (options != null) {
return EnumSet.copyOf(options);
} else {
return EnumSet.noneOf(enumClass);
}
}
/**
* Construct a copy of a given set.
* @param list - the set to copy.
* @return The copied set.
*/
private static <T> Set<T> safeSet(Collection<T> set) {
if (set != null)
return Sets.newHashSet(set);
else
return Collections.emptySet();
}
/**
* Represents a builder of whitelists.
* @author Kristian
*/
public static class Builder {
private ListenerPriority priority;
private Set<Integer> whitelist;
private GamePhase gamePhase;
private Set<ListenerOptions> options;
/**
* Construct a new listening whitelist template.
* @param template - the template.
*/
private Builder(ListeningWhitelist template) {
if (template != null) {
priority(template.getPriority());
gamePhase(template.getGamePhase());
whitelist(template.getWhitelist());
options(template.getOptions());
}
}
/**
* Set the priority to use when constructing new whitelists.
* @param priority - the priority.
* @return This builder, for chaining.
*/
public Builder priority(ListenerPriority priority) {
this.priority = priority;
return this;
}
/**
* Set the whitelist of packet IDs to copy when constructing new whitelists.
* @param whitelist - the whitelist of packets.
* @return This builder, for chaining.
*/
public Builder whitelist(Collection<Integer> whitelist) {
this.whitelist = safeSet(whitelist);
return this;
}
/**
* Set the gamephase to use when constructing new whitelists.
* @param gamePhase - the gamephase.
* @return This builder, for chaining.
*/
public Builder gamePhase(GamePhase gamePhase) {
this.gamePhase = gamePhase;
return this;
}
/**
* Set the options to copy when constructing new whitelists.
* @param options - the options.
* @return This builder, for chaining.
*/
public Builder options(Set<ListenerOptions> options) {
this.options = safeSet(options);
return this;
}
/**
* Construct a new whitelist from the values in this builder.
* @return The new whitelist.
*/
public ListeningWhitelist build() {
return new ListeningWhitelist(this);
}
}
}