From 02d6703da779a5876ceaf5d2fd2dd9833a7a6621 Mon Sep 17 00:00:00 2001 From: Wesley Wolfe Date: Fri, 3 Aug 2012 06:24:55 -0500 Subject: Implement new AsyncPlayerChatEvent. Addresses BUKKIT-2064 Added two utility collections for use with PlayerChatEvents allowing lazier initialization of events and less need to synchronize against the player list. Provided a hidden queue system for similar logic to pre-1.3 chat. When a plugin is listening for the deprecated PlayerChatEvent, all chat will be delayed to be mirror executed from the main thread. All developers are encouraged to immediately update to the developmental Bukkit chat API as a minimum transition for server stability. Additionally, changes were required to bring thread-safety to the flow logic. CopyOnWriteArrayList is the only viable means to produce thread safety with minimal diff; using a sane pre-implemented collection would require reworking of sections of NMS logic. As a minor change, implemented expected functionality for PlayerCommandPreProcessEvent. Setting the player should now change the player executing the command. --- .../org/bukkit/craftbukkit/entity/CraftPlayer.java | 2 +- .../org/bukkit/craftbukkit/util/LazyHashSet.java | 98 ++++++++++++++++++++++ .../org/bukkit/craftbukkit/util/LazyPlayerSet.java | 25 ++++++ 3 files changed, 124 insertions(+), 1 deletion(-) create mode 100644 src/main/java/org/bukkit/craftbukkit/util/LazyHashSet.java create mode 100644 src/main/java/org/bukkit/craftbukkit/util/LazyPlayerSet.java (limited to 'src/main/java/org/bukkit') diff --git a/src/main/java/org/bukkit/craftbukkit/entity/CraftPlayer.java b/src/main/java/org/bukkit/craftbukkit/entity/CraftPlayer.java index dd82cb32..b1906046 100644 --- a/src/main/java/org/bukkit/craftbukkit/entity/CraftPlayer.java +++ b/src/main/java/org/bukkit/craftbukkit/entity/CraftPlayer.java @@ -229,7 +229,7 @@ public class CraftPlayer extends CraftHumanEntity implements Player { public void chat(String msg) { if (getHandle().netServerHandler == null) return; - getHandle().netServerHandler.chat(msg); + getHandle().netServerHandler.chat(msg, false); } public boolean performCommand(String command) { diff --git a/src/main/java/org/bukkit/craftbukkit/util/LazyHashSet.java b/src/main/java/org/bukkit/craftbukkit/util/LazyHashSet.java new file mode 100644 index 00000000..ad83fd87 --- /dev/null +++ b/src/main/java/org/bukkit/craftbukkit/util/LazyHashSet.java @@ -0,0 +1,98 @@ +package org.bukkit.craftbukkit.util; + +import java.util.Collection; +import java.util.Iterator; +import java.util.Set; + + +public abstract class LazyHashSet implements Set { + Set reference = null; + + public int size() { + return getReference().size(); + } + + public boolean isEmpty() { + return getReference().isEmpty(); + } + + public boolean contains(Object o) { + return getReference().contains(o); + } + + public Iterator iterator() { + return getReference().iterator(); + } + + public Object[] toArray() { + return getReference().toArray(); + } + + public T[] toArray(T[] a) { + return getReference().toArray(a); + } + + public boolean add(E o) { + return getReference().add(o); + } + + public boolean remove(Object o) { + return getReference().remove(o); + } + + public boolean containsAll(Collection c) { + return getReference().containsAll(c); + } + + public boolean addAll(Collection c) { + return getReference().addAll(c); + } + + public boolean retainAll(Collection c) { + return getReference().retainAll(c); + } + + public boolean removeAll(Collection c) { + return getReference().removeAll(c); + } + + public void clear() { + getReference().clear(); + } + + public Set getReference() { + Set reference = this.reference ; + if (reference != null) { + return reference; + } + return this.reference = makeReference(); + } + + abstract Set makeReference(); + + public boolean isLazy() { + return reference == null; + } + + @Override + public int hashCode() { + return 157 * getReference().hashCode(); + } + + @Override + public boolean equals(Object obj) { + if (obj == this) { + return true; + } + if (obj == null || this.getClass() != obj.getClass()) { + return false; + } + LazyHashSet that = (LazyHashSet) obj; + return (this.isLazy() && that.isLazy()) || this.getReference().equals(that.getReference()); + } + + @Override + public String toString() { + return getReference().toString(); + } +} diff --git a/src/main/java/org/bukkit/craftbukkit/util/LazyPlayerSet.java b/src/main/java/org/bukkit/craftbukkit/util/LazyPlayerSet.java new file mode 100644 index 00000000..df090190 --- /dev/null +++ b/src/main/java/org/bukkit/craftbukkit/util/LazyPlayerSet.java @@ -0,0 +1,25 @@ +package org.bukkit.craftbukkit.util; + +import java.util.HashSet; +import java.util.List; +import net.minecraft.server.EntityPlayer; +import net.minecraft.server.MinecraftServer; + +import org.bukkit.entity.Player; + +public class LazyPlayerSet extends LazyHashSet { + + @Override + HashSet makeReference() { + if (reference != null) { + throw new IllegalStateException("Reference already created!"); + } + List players = MinecraftServer.getServer().getServerConfigurationManager().players; + HashSet reference = new HashSet(players.size()); + for (EntityPlayer player : players) { + reference.add(player.getBukkitEntity()); + } + return reference; + } + +} -- cgit v1.2.3