From a4895cbd5d1774cfc92509abc87088d6d41751fe Mon Sep 17 00:00:00 2001 From: zml2008 Date: Tue, 27 Mar 2012 22:52:48 -0700 Subject: [PATCH] Added initial support for plugin channels with WECUI --- .../sk89q/worldedit/bukkit/BukkitPlayer.java | 17 ++++-- .../worldedit/bukkit/CUIChannelListener.java | 57 +++++++++++++++++++ .../worldedit/bukkit/WorldEditListener.java | 15 ++++- .../worldedit/bukkit/WorldEditPlugin.java | 28 +++++++++ .../worldedit/cui/SelectionCylinderEvent.java | 4 +- .../cui/SelectionEllipsoidPointEvent.java | 4 +- .../worldedit/cui/SelectionMinMaxEvent.java | 4 +- .../worldedit/cui/SelectionPoint2DEvent.java | 8 +-- .../worldedit/cui/SelectionPointEvent.java | 6 +- .../worldedit/cui/SelectionShapeEvent.java | 2 +- 10 files changed, 123 insertions(+), 22 deletions(-) create mode 100644 src/main/java/com/sk89q/worldedit/bukkit/CUIChannelListener.java diff --git a/src/main/java/com/sk89q/worldedit/bukkit/BukkitPlayer.java b/src/main/java/com/sk89q/worldedit/bukkit/BukkitPlayer.java index dcb8710f6..22307800f 100644 --- a/src/main/java/com/sk89q/worldedit/bukkit/BukkitPlayer.java +++ b/src/main/java/com/sk89q/worldedit/bukkit/BukkitPlayer.java @@ -129,19 +129,24 @@ public class BukkitPlayer extends LocalPlayer { @Override public void dispatchCUIEvent(CUIEvent event) { String[] params = event.getParameters(); - + String send = event.getTypeId(); if (params.length > 0) { - player.sendRawMessage("\u00A75\u00A76\u00A74\u00A75" + event.getTypeId() - + "|" + StringUtil.joinString(params, "|")); + send = send + "|" + StringUtil.joinString(params, "|"); + } + + if (plugin.hasPluginChannelCUI(getName())) { + player.sendPluginMessage(plugin, WorldEditPlugin.CUI_PLUGIN_CHANNEL, send.getBytes(CUIChannelListener.UTF_8_CHARSET)); } else { - player.sendRawMessage("\u00A75\u00A76\u00A74\u00A75" + event.getTypeId()); + player.sendRawMessage("\u00A75\u00A76\u00A74\u00A75" + send); } } @Override public void dispatchCUIHandshake() { - player.sendRawMessage("\u00A75\u00A76\u00A74\u00A75"); - player.sendRawMessage("\u00A74\u00A75\u00A73\u00A74"); + if (!plugin.hasPluginChannelCUI(getName())) { + player.sendRawMessage("\u00A75\u00A76\u00A74\u00A75"); + player.sendRawMessage("\u00A74\u00A75\u00A73\u00A74"); + } } public Player getPlayer() { diff --git a/src/main/java/com/sk89q/worldedit/bukkit/CUIChannelListener.java b/src/main/java/com/sk89q/worldedit/bukkit/CUIChannelListener.java new file mode 100644 index 000000000..c4e729fdf --- /dev/null +++ b/src/main/java/com/sk89q/worldedit/bukkit/CUIChannelListener.java @@ -0,0 +1,57 @@ +/* + * WorldEdit + * Copyright (C) 2012 sk89q and contributors + * + * 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 3 of 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; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. 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. If not, see . + */ + +package com.sk89q.worldedit.bukkit; + +import com.sk89q.worldedit.LocalSession; +import org.bukkit.entity.Player; +import org.bukkit.plugin.messaging.PluginMessageListener; + +import java.nio.charset.Charset; + +/** + * Handles incoming WorldEditCui init message + * @author zml2008 + */ +public class CUIChannelListener implements PluginMessageListener { + public static final Charset UTF_8_CHARSET = Charset.forName("UTF-8"); + private final WorldEditPlugin plugin; + + public CUIChannelListener(WorldEditPlugin plugin) { + this.plugin = plugin; + } + + @Override + public void onPluginMessageReceived(String channel, Player player, byte[] message) { + LocalSession session = plugin.getSession(player); + if (session.hasCUISupport()) { // Already initialized + return; + } + + String[] text = new String(message, UTF_8_CHARSET).split("\\|"); + if (text.length > 1 && text[0].equalsIgnoreCase("v")) { // enough fields and right message + plugin.setPluginChannelCUI(player.getName(), true); + session.setCUISupport(true); + try { + session.setCUIVersion(Integer.parseInt(text[1])); + } catch (NumberFormatException e) { + plugin.getLogger().warning("Error while reading CUI init message: " + e.getMessage()); + } + } + } +} diff --git a/src/main/java/com/sk89q/worldedit/bukkit/WorldEditListener.java b/src/main/java/com/sk89q/worldedit/bukkit/WorldEditListener.java index 5582ce9f0..e8f619949 100644 --- a/src/main/java/com/sk89q/worldedit/bukkit/WorldEditListener.java +++ b/src/main/java/com/sk89q/worldedit/bukkit/WorldEditListener.java @@ -23,6 +23,7 @@ package com.sk89q.worldedit.bukkit; import com.sk89q.util.StringUtil; import org.bukkit.Bukkit; import org.bukkit.block.Block; +import org.bukkit.entity.Player; import org.bukkit.event.Event.Result; import org.bukkit.event.EventHandler; import org.bukkit.event.EventPriority; @@ -66,7 +67,16 @@ public class WorldEditListener implements Listener { @EventHandler(priority = EventPriority.LOWEST) public void onPlayerJoin(PlayerJoinEvent event) { - plugin.wrapPlayer(event.getPlayer()).dispatchCUIHandshake(); + final Player player = event.getPlayer(); + plugin.getServer().getScheduler().scheduleSyncDelayedTask(plugin, new Runnable() { + @Override + public void run() { + if (player.isOnline()) { + plugin.wrapPlayer(player).dispatchCUIHandshake(); + } + } + }, 20 * 2); + } /** @@ -77,6 +87,7 @@ public class WorldEditListener implements Listener { @EventHandler public void onPlayerQuit(PlayerQuitEvent event) { plugin.getWorldEdit().markExpire(plugin.wrapPlayer(event.getPlayer())); + plugin.setPluginChannelCUI(event.getPlayer().getName(), false); } /** @@ -177,7 +188,7 @@ public class WorldEditListener implements Listener { } } - @EventHandler(ignoreCancelled = true) + @EventHandler(ignoreCancelled = true) // TODO: Remove this in a bit public void onPlayerChat(PlayerChatEvent event) { Matcher matcher = cuipattern.matcher(event.getMessage()); if (matcher.find()) { diff --git a/src/main/java/com/sk89q/worldedit/bukkit/WorldEditPlugin.java b/src/main/java/com/sk89q/worldedit/bukkit/WorldEditPlugin.java index 0a07d6c61..f11d8f524 100644 --- a/src/main/java/com/sk89q/worldedit/bukkit/WorldEditPlugin.java +++ b/src/main/java/com/sk89q/worldedit/bukkit/WorldEditPlugin.java @@ -24,6 +24,8 @@ import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; +import java.util.HashMap; +import java.util.Map; import java.util.jar.JarFile; import java.util.logging.Logger; import java.util.zip.ZipEntry; @@ -45,6 +47,12 @@ import com.sk89q.worldedit.regions.*; * @author sk89q */ public class WorldEditPlugin extends JavaPlugin { + + /** + * The name of the CUI's plugin channel registration + */ + public static final String CUI_PLUGIN_CHANNEL = "WECUI"; + /** * WorldEdit messages get sent here. */ @@ -68,6 +76,11 @@ public class WorldEditPlugin extends JavaPlugin { */ private BukkitConfiguration config; + /** + * Stores players who are using plugin channels for the cui + */ + private final Map pluginChannelCui = new HashMap(); + /** * Called on plugin enable. */ @@ -97,6 +110,8 @@ public class WorldEditPlugin extends JavaPlugin { server = new BukkitServerInterface(this, getServer()); controller = new WorldEdit(server, config); api = new WorldEditAPI(this); + getServer().getMessenger().registerIncomingPluginChannel(this, CUI_PLUGIN_CHANNEL, new CUIChannelListener(this)); + getServer().getMessenger().registerOutgoingPluginChannel(this, CUI_PLUGIN_CHANNEL); // Now we can register events! getServer().getPluginManager().registerEvents(new WorldEditListener(this), this); @@ -381,4 +396,17 @@ public class WorldEditPlugin extends JavaPlugin { session.setRegionSelector(BukkitUtil.getLocalWorld(player.getWorld()), sel); session.dispatchCUISelection(wrapPlayer(player)); } + + public void setPluginChannelCUI(String name, boolean value) { + pluginChannelCui.put(name, value); + } + + public boolean hasPluginChannelCUI(String name) { + Boolean val = pluginChannelCui.get(name); + if (val == null) { + return false; + } else { + return val; + } + } } diff --git a/src/main/java/com/sk89q/worldedit/cui/SelectionCylinderEvent.java b/src/main/java/com/sk89q/worldedit/cui/SelectionCylinderEvent.java index 1bde0e426..a3fd03475 100644 --- a/src/main/java/com/sk89q/worldedit/cui/SelectionCylinderEvent.java +++ b/src/main/java/com/sk89q/worldedit/cui/SelectionCylinderEvent.java @@ -23,8 +23,8 @@ import com.sk89q.worldedit.Vector2D; public class SelectionCylinderEvent implements CUIEvent { - protected Vector pos; - protected Vector2D radius; + protected final Vector pos; + protected final Vector2D radius; public SelectionCylinderEvent(Vector pos, Vector2D radius) { this.pos = pos; diff --git a/src/main/java/com/sk89q/worldedit/cui/SelectionEllipsoidPointEvent.java b/src/main/java/com/sk89q/worldedit/cui/SelectionEllipsoidPointEvent.java index eea88af77..0b3ad9b7e 100644 --- a/src/main/java/com/sk89q/worldedit/cui/SelectionEllipsoidPointEvent.java +++ b/src/main/java/com/sk89q/worldedit/cui/SelectionEllipsoidPointEvent.java @@ -22,8 +22,8 @@ package com.sk89q.worldedit.cui; import com.sk89q.worldedit.Vector; public class SelectionEllipsoidPointEvent implements CUIEvent { - protected int id; - protected Vector pos; + protected final int id; + protected final Vector pos; public SelectionEllipsoidPointEvent(int id, Vector pos) { this.id = id; diff --git a/src/main/java/com/sk89q/worldedit/cui/SelectionMinMaxEvent.java b/src/main/java/com/sk89q/worldedit/cui/SelectionMinMaxEvent.java index 2e6af6f05..faccd8bc2 100644 --- a/src/main/java/com/sk89q/worldedit/cui/SelectionMinMaxEvent.java +++ b/src/main/java/com/sk89q/worldedit/cui/SelectionMinMaxEvent.java @@ -21,8 +21,8 @@ package com.sk89q.worldedit.cui; public class SelectionMinMaxEvent implements CUIEvent { - protected int min; - protected int max; + protected final int min; + protected final int max; public SelectionMinMaxEvent(int min, int max) { this.min = min; diff --git a/src/main/java/com/sk89q/worldedit/cui/SelectionPoint2DEvent.java b/src/main/java/com/sk89q/worldedit/cui/SelectionPoint2DEvent.java index 3e72f7e3f..be36dbe61 100644 --- a/src/main/java/com/sk89q/worldedit/cui/SelectionPoint2DEvent.java +++ b/src/main/java/com/sk89q/worldedit/cui/SelectionPoint2DEvent.java @@ -24,10 +24,10 @@ import com.sk89q.worldedit.Vector2D; public class SelectionPoint2DEvent implements CUIEvent { - protected int id; - protected int blockx; - protected int blockz; - protected int area; + protected final int id; + protected final int blockx; + protected final int blockz; + protected final int area; public SelectionPoint2DEvent(int id, Vector2D pos, int area) { this.id = id; diff --git a/src/main/java/com/sk89q/worldedit/cui/SelectionPointEvent.java b/src/main/java/com/sk89q/worldedit/cui/SelectionPointEvent.java index 814526d8c..b19833174 100644 --- a/src/main/java/com/sk89q/worldedit/cui/SelectionPointEvent.java +++ b/src/main/java/com/sk89q/worldedit/cui/SelectionPointEvent.java @@ -23,9 +23,9 @@ import com.sk89q.worldedit.Vector; public class SelectionPointEvent implements CUIEvent { - protected int id; - protected Vector pos; - protected int area; + protected final int id; + protected final Vector pos; + protected final int area; public SelectionPointEvent(int id, Vector pos, int area) { this.id = id; diff --git a/src/main/java/com/sk89q/worldedit/cui/SelectionShapeEvent.java b/src/main/java/com/sk89q/worldedit/cui/SelectionShapeEvent.java index e205d384e..f37175cda 100644 --- a/src/main/java/com/sk89q/worldedit/cui/SelectionShapeEvent.java +++ b/src/main/java/com/sk89q/worldedit/cui/SelectionShapeEvent.java @@ -21,7 +21,7 @@ package com.sk89q.worldedit.cui; public class SelectionShapeEvent implements CUIEvent { - protected String shapeName; + protected final String shapeName; public SelectionShapeEvent(String shapeName) { this.shapeName = shapeName;