diff options
author | Wesley Wolfe <weswolf@aol.com> | 2012-08-03 06:24:55 -0500 |
---|---|---|
committer | Wesley Wolfe <weswolf@aol.com> | 2012-08-03 23:44:45 -0500 |
commit | 02d6703da779a5876ceaf5d2fd2dd9833a7a6621 (patch) | |
tree | 03ac669aa3c8f9f1d91c98142de9d2eb36644f0c /src/main/java/net/minecraft/server | |
parent | 4942f5184a29e945d7b47aaf8b93f668ea20ce28 (diff) | |
download | craftbukkit-02d6703da779a5876ceaf5d2fd2dd9833a7a6621.tar craftbukkit-02d6703da779a5876ceaf5d2fd2dd9833a7a6621.tar.gz craftbukkit-02d6703da779a5876ceaf5d2fd2dd9833a7a6621.tar.lz craftbukkit-02d6703da779a5876ceaf5d2fd2dd9833a7a6621.tar.xz craftbukkit-02d6703da779a5876ceaf5d2fd2dd9833a7a6621.zip |
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.
Diffstat (limited to 'src/main/java/net/minecraft/server')
3 files changed, 49 insertions, 14 deletions
diff --git a/src/main/java/net/minecraft/server/MinecraftServer.java b/src/main/java/net/minecraft/server/MinecraftServer.java index bba95156..3e779357 100644 --- a/src/main/java/net/minecraft/server/MinecraftServer.java +++ b/src/main/java/net/minecraft/server/MinecraftServer.java @@ -19,6 +19,7 @@ import joptsimple.OptionSet; import org.bukkit.World.Environment; import org.bukkit.event.server.RemoteServerCommandEvent; import org.bukkit.event.world.WorldSaveEvent; +import org.bukkit.event.player.PlayerChatEvent; // CraftBukkit end public abstract class MinecraftServer implements Runnable, IMojangStatistics, ICommandListener { @@ -78,6 +79,7 @@ public abstract class MinecraftServer implements Runnable, IMojangStatistics, IC public ConsoleReader reader; public static int currentTick; public final Thread primaryThread; + public java.util.Queue<PlayerChatEvent> chatQueue = new java.util.concurrent.ConcurrentLinkedQueue<PlayerChatEvent>(); // CraftBukkit end public MinecraftServer(OptionSet options) { // CraftBukkit - signature file -> OptionSet @@ -509,6 +511,28 @@ public abstract class MinecraftServer implements Runnable, IMojangStatistics, IC // CraftBukkit start - only send timeupdates to the people in that world this.server.getScheduler().mainThreadHeartbeat(this.ticks); + // Fix for old plugins still using deprecated event + while (!chatQueue.isEmpty()) { + PlayerChatEvent event = chatQueue.remove(); + org.bukkit.Bukkit.getPluginManager().callEvent(event); + + if (event.isCancelled()) { + continue; + } + + String message = String.format(event.getFormat(), event.getPlayer().getDisplayName(), event.getMessage()); + console.sendMessage(message); + if (((org.bukkit.craftbukkit.util.LazyPlayerSet) event.getRecipients()).isLazy()) { + for (Object player : getServerConfigurationManager().players) { + ((EntityPlayer) player).sendMessage(message); + } + } else { + for (org.bukkit.entity.Player player : event.getRecipients()) { + player.sendMessage(message); + } + } + } + // Send timeupdates to everyone, it will get the right time from the world the player is in. if (this.ticks % 20 == 0) { for (int i = 0; i < this.getServerConfigurationManager().players.size(); ++i) { diff --git a/src/main/java/net/minecraft/server/NetServerHandler.java b/src/main/java/net/minecraft/server/NetServerHandler.java index aa43435e..e512b2b7 100644 --- a/src/main/java/net/minecraft/server/NetServerHandler.java +++ b/src/main/java/net/minecraft/server/NetServerHandler.java @@ -16,12 +16,14 @@ import java.util.HashSet; import org.bukkit.Location; import org.bukkit.craftbukkit.inventory.CraftInventoryView; import org.bukkit.craftbukkit.inventory.CraftItemStack; +import org.bukkit.craftbukkit.util.LazyPlayerSet; import org.bukkit.craftbukkit.entity.CraftPlayer; import org.bukkit.craftbukkit.event.CraftEventFactory; import org.bukkit.entity.Player; import org.bukkit.event.Event; import org.bukkit.event.block.Action; import org.bukkit.event.block.SignChangeEvent; +import org.bukkit.event.player.AsyncPlayerChatEvent; import org.bukkit.event.player.PlayerAnimationEvent; import org.bukkit.event.player.PlayerChatEvent; import org.bukkit.event.player.PlayerCommandPreprocessEvent; @@ -801,12 +803,12 @@ public class NetServerHandler extends NetHandler { return; } - this.chat(s); + this.chat(s, packet3chat.a_()); } } } - public boolean chat(String s) { + public boolean chat(String s, boolean async) { if (!this.player.dead) { if (s.length() == 0) { logger.warning(this.player.name + " tried to send an empty message"); @@ -823,17 +825,26 @@ public class NetServerHandler extends NetHandler { return true; } else { Player player = this.getPlayer(); - PlayerChatEvent event = new PlayerChatEvent(player, s); + AsyncPlayerChatEvent event = new AsyncPlayerChatEvent(async, player, s, new LazyPlayerSet()); this.server.getPluginManager().callEvent(event); - if (event.isCancelled()) { - return true; - } - - s = String.format(event.getFormat(), event.getPlayer().getDisplayName(), event.getMessage()); - minecraftServer.console.sendMessage(s); - for (Player recipient : event.getRecipients()) { - recipient.sendMessage(s); + if (PlayerChatEvent.getHandlerList().getRegisteredListeners().length != 0) { + // Evil plugins still listening to deprecated event + PlayerChatEvent queueEvent = new PlayerChatEvent(player, event.getMessage(), event.getFormat(), event.getRecipients()); + queueEvent.setCancelled(event.isCancelled()); + minecraftServer.chatQueue.add(queueEvent); + } else { + s = String.format(event.getFormat(), event.getPlayer().getDisplayName(), event.getMessage()); + minecraftServer.console.sendMessage(s); + if (((LazyPlayerSet) event.getRecipients()).isLazy()) { + for (Object recipient : minecraftServer.getServerConfigurationManager().players) { + ((EntityPlayer) recipient).sendMessage(s); + } + } else { + for (org.bukkit.entity.Player recipient : event.getRecipients()) { + recipient.sendMessage(s); + } + } } } @@ -851,7 +862,7 @@ public class NetServerHandler extends NetHandler { // CraftBukkit start CraftPlayer player = this.getPlayer(); - PlayerCommandPreprocessEvent event = new PlayerCommandPreprocessEvent(player, s); + PlayerCommandPreprocessEvent event = new PlayerCommandPreprocessEvent(player, s, new LazyPlayerSet()); this.server.getPluginManager().callEvent(event); if (event.isCancelled()) { @@ -859,7 +870,7 @@ public class NetServerHandler extends NetHandler { } try { - if (this.server.dispatchCommand(player, event.getMessage().substring(1))) { + if (this.server.dispatchCommand(event.getPlayer(), event.getMessage().substring(1))) { return; } } catch (org.bukkit.command.CommandException ex) { diff --git a/src/main/java/net/minecraft/server/ServerConfigurationManagerAbstract.java b/src/main/java/net/minecraft/server/ServerConfigurationManagerAbstract.java index ba248008..35fa2cdd 100644 --- a/src/main/java/net/minecraft/server/ServerConfigurationManagerAbstract.java +++ b/src/main/java/net/minecraft/server/ServerConfigurationManagerAbstract.java @@ -30,7 +30,7 @@ public abstract class ServerConfigurationManagerAbstract { private static final SimpleDateFormat e = new SimpleDateFormat("yyyy-MM-dd \'at\' HH:mm:ss z"); public static final Logger a = Logger.getLogger("Minecraft"); private final MinecraftServer server; - public final List players = new ArrayList(); + public final List players = new java.util.concurrent.CopyOnWriteArrayList(); // CraftBukkit - ArrayList -> CopyOnWriteArrayList: Iterator safety private final BanList banByName = new BanList(new File("banned-players.txt")); private final BanList banByIP = new BanList(new File("banned-ips.txt")); private Set operators = new HashSet(); |