From fe445a7ec9623bef1f2a09c1dbbf0e9f8a8bdf43 Mon Sep 17 00:00:00 2001 From: zml2008 Date: Sat, 4 Aug 2012 20:22:55 -0700 Subject: [PATCH] Remove chat-based WECUI protocol. Everybody running a modern (1.2.5 or newer) WECUI should be fine. This has a bonus of allowing us to stay far far away from that smelly PlayerChatEvent guy who's always late (or early, or both). --- perworldperms.diff | 123 ++++++++++++++++++ .../sk89q/worldedit/LocalConfiguration.java | 5 +- .../java/com/sk89q/worldedit/LocalPlayer.java | 2 + .../com/sk89q/worldedit/LocalSession.java | 18 ++- .../sk89q/worldedit/bukkit/BukkitPlayer.java | 17 +-- .../worldedit/bukkit/CUIChannelListener.java | 14 +- .../worldedit/bukkit/WorldEditListener.java | 39 ------ .../worldedit/bukkit/WorldEditPlugin.java | 17 --- .../spout/WorldEditCUIMessageHandler.java | 10 +- .../worldedit/spout/WorldEditListener.java | 5 - .../util/PropertiesConfiguration.java | 15 +-- .../worldedit/util/YAMLConfiguration.java | 1 - src/main/resources/config.yml | 3 +- 13 files changed, 157 insertions(+), 112 deletions(-) create mode 100644 perworldperms.diff diff --git a/perworldperms.diff b/perworldperms.diff new file mode 100644 index 000000000..bf91a7420 --- /dev/null +++ b/perworldperms.diff @@ -0,0 +1,123 @@ +diff --git a/src/main/java/com/sk89q/wepif/DinnerPermsResolver.java b/src/main/java/com/sk89q/wepif/DinnerPermsResolver.java +index 96256af..ab414ad 100644 +--- a/src/main/java/com/sk89q/wepif/DinnerPermsResolver.java ++++ b/src/main/java/com/sk89q/wepif/DinnerPermsResolver.java +@@ -20,15 +20,22 @@ + package com.sk89q.wepif; + + import com.sk89q.util.yaml.YAMLProcessor; ++import org.bukkit.Bukkit; + import org.bukkit.OfflinePlayer; + import org.bukkit.Server; ++import org.bukkit.World; ++import org.bukkit.permissions.GlobalPermissionsContext; + import org.bukkit.entity.Player; + import org.bukkit.permissions.Permissible; + import org.bukkit.permissions.Permission; + import org.bukkit.permissions.PermissionAttachmentInfo; ++import org.bukkit.permissions.PermissionsContext; ++import org.bukkit.permissions.WorldPermissionsContext; + + import java.util.ArrayList; + import java.util.List; ++import java.util.Map; ++import java.util.WeakHashMap; + + public class DinnerPermsResolver implements PermissionsResolver { + +@@ -87,7 +94,27 @@ public class DinnerPermsResolver implements PermissionsResolver { + } + + public boolean hasPermission(String worldName, OfflinePlayer player, String permission) { +- return hasPermission(player, permission); // no per-world ability to check permissions in dinnerperms ++ Permissible perms = getPermissible(player); ++ if (perms == null) { ++ return false; ++ } ++ switch (internalHasPermission(perms, permission, worldName)) { ++ case -1: ++ return false; ++ case 1: ++ return true; ++ } ++ int dotPos = permission.lastIndexOf("."); ++ while (dotPos > -1) { ++ switch (internalHasPermission(perms, permission.substring(0, dotPos + 1) + "*", worldName)) { ++ case -1: ++ return false; ++ case 1: ++ return true; ++ } ++ dotPos = permission.lastIndexOf(".", dotPos - 1); ++ } ++ return internalHasPermission(perms, "*", worldName) == 1; + } + + public boolean inGroup(OfflinePlayer player, String group) { +@@ -126,18 +153,29 @@ public class DinnerPermsResolver implements PermissionsResolver { + return perm; + } + +- /** +- * Checks the permission from dinnerperms +- * @param perms Permissible to check for +- * @param permission The permission to check +- * @return -1 if the permission is explicitly denied, 1 if the permission is allowed, +- * 0 if the permission is denied by a default. +- */ +- public int internalHasPermission(Permissible perms, String permission) { +- if (perms.isPermissionSet(permission)) { +- return perms.hasPermission(permission) ? 1 : -1; ++ public int internalHasPermission(Permissible perms, String name) { ++ name = name.toLowerCase(); ++ if (perms.isPermissionSet(name)) { ++ return perms.hasPermission(name) ? 1 : 0; ++ } ++ Permission perm = Bukkit.getServer().getPluginManager().getPermission(name); ++ if (perm != null) { ++ return perm.getDefault().getValue(perms.isOp()) ? 1 : 0; + } else { +- Permission perm = server.getPluginManager().getPermission(permission); ++ return 0; ++ } ++ } ++ ++ public int internalHasPermission(Permissible perms, String name, String contextName) { ++ ++ name = name.toLowerCase(); ++ PermissionsContext context = getPermissionsContext(contextName); ++ // More specific overrides less specific ++ if (perms.isPermissionSet(name, context) || perms.isPermissionSet(name, GlobalPermissionsContext.GLOBAL_CONTEXT)) { ++ return perms.hasPermission(name, context) ? 1 : -1; ++ } else { ++ Permission perm = server.getPluginManager().getPermission(name); ++ + if (perm != null) { + return perm.getDefault().getValue(perms.isOp()) ? 1 : 0; + } else { +@@ -146,6 +184,25 @@ public class DinnerPermsResolver implements PermissionsResolver { + } + } + ++ private Map contextLookup = new WeakHashMap(); ++ public PermissionsContext getPermissionsContext(String name) { ++ PermissionsContext context = contextLookup.get(name); ++ if (context == null) { ++ if (name == null || name.equalsIgnoreCase("global")) { ++ context = GlobalPermissionsContext.GLOBAL_CONTEXT; ++ } else { ++ World world = server.getWorld(name); ++ if (world != null) { ++ context = world.getPermissionsContext(); ++ } else { ++ context = new WorldPermissionsContext(name); ++ } ++ } ++ contextLookup.put(name, context); ++ } ++ return context; ++ } ++ + public String getDetectionMessage() { + return "Using the Bukkit Permissions API."; + } diff --git a/src/main/java/com/sk89q/worldedit/LocalConfiguration.java b/src/main/java/com/sk89q/worldedit/LocalConfiguration.java index 570decf20..766949721 100644 --- a/src/main/java/com/sk89q/worldedit/LocalConfiguration.java +++ b/src/main/java/com/sk89q/worldedit/LocalConfiguration.java @@ -28,7 +28,7 @@ import java.util.Set; /** * Represents WorldEdit's configuration. - * + * * @author sk89q */ public abstract class LocalConfiguration { @@ -78,7 +78,6 @@ public abstract class LocalConfiguration { }; public boolean profile = false; - public boolean enableWECUI = true; public Set disallowedBlocks = new HashSet(); public int defaultChangeLimit = -1; public int maxChangeLimit = -1; @@ -113,7 +112,7 @@ public abstract class LocalConfiguration { /** * Get the working directory to work from. - * + * * @return */ public File getWorkingDirectory() { diff --git a/src/main/java/com/sk89q/worldedit/LocalPlayer.java b/src/main/java/com/sk89q/worldedit/LocalPlayer.java index 298aaffe0..6edf837bf 100644 --- a/src/main/java/com/sk89q/worldedit/LocalPlayer.java +++ b/src/main/java/com/sk89q/worldedit/LocalPlayer.java @@ -623,7 +623,9 @@ public abstract class LocalPlayer { /** * Send the CUI handshake. + * @deprecated Not used anymore; The CUI begins the handshake */ + @Deprecated public void dispatchCUIHandshake() { } diff --git a/src/main/java/com/sk89q/worldedit/LocalSession.java b/src/main/java/com/sk89q/worldedit/LocalSession.java index 0553ab927..eace7b886 100644 --- a/src/main/java/com/sk89q/worldedit/LocalSession.java +++ b/src/main/java/com/sk89q/worldedit/LocalSession.java @@ -602,6 +602,22 @@ public class LocalSession { } } + public void handleCUIInitializationMessage(String text) { + if (hasCUISupport()) { + return; + } + + String[] split = text.split("\\|"); + if (split.length > 1 && split[0].equalsIgnoreCase("v")) { // enough fields and right message + setCUISupport(true); + try { + setCUIVersion(Integer.parseInt(split[1])); + } catch (NumberFormatException e) { + WorldEdit.logger.warning("Error while reading CUI init message: " + e.getMessage()); + } + } + } + /** * Gets the status of CUI support. * @@ -617,7 +633,7 @@ public class LocalSession { * @param support */ public void setCUISupport(boolean support) { - hasCUISupport = true; + hasCUISupport = support; } /** diff --git a/src/main/java/com/sk89q/worldedit/bukkit/BukkitPlayer.java b/src/main/java/com/sk89q/worldedit/bukkit/BukkitPlayer.java index 503bf2b45..0e129410f 100644 --- a/src/main/java/com/sk89q/worldedit/bukkit/BukkitPlayer.java +++ b/src/main/java/com/sk89q/worldedit/bukkit/BukkitPlayer.java @@ -133,22 +133,7 @@ public class BukkitPlayer extends LocalPlayer { if (params.length > 0) { send = send + "|" + StringUtil.joinString(params, "|"); } - - if (plugin.hasPluginChannelCUI(getName())) { - player.sendPluginMessage(plugin, WorldEditPlugin.CUI_PLUGIN_CHANNEL, send.getBytes(CUIChannelListener.UTF_8_CHARSET)); - } else { - if (plugin.getLocalConfiguration().enableWECUI) { - player.sendRawMessage("\u00A75\u00A76\u00A74\u00A75" + send); - } - } - } - - @Override - public void dispatchCUIHandshake() { - if (!plugin.hasPluginChannelCUI(getName()) && plugin.getLocalConfiguration().enableWECUI) { - player.sendRawMessage("\u00A75\u00A76\u00A74\u00A75"); - player.sendRawMessage("\u00A74\u00A75\u00A73\u00A74"); - } + player.sendPluginMessage(plugin, WorldEditPlugin.CUI_PLUGIN_CHANNEL, send.getBytes(CUIChannelListener.UTF_8_CHARSET)); } 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 index 95da2a978..e846e6d1c 100644 --- a/src/main/java/com/sk89q/worldedit/bukkit/CUIChannelListener.java +++ b/src/main/java/com/sk89q/worldedit/bukkit/CUIChannelListener.java @@ -39,19 +39,11 @@ public class CUIChannelListener implements PluginMessageListener { @Override public void onPluginMessageReceived(String channel, Player player, byte[] message) { LocalSession session = plugin.getSession(player); - if (session.hasCUISupport() && plugin.hasPluginChannelCUI(player.getName())) { // Already initialized + 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()); - } - } + String text = new String(message, UTF_8_CHARSET); + session.handleCUIInitializationMessage(text); } } diff --git a/src/main/java/com/sk89q/worldedit/bukkit/WorldEditListener.java b/src/main/java/com/sk89q/worldedit/bukkit/WorldEditListener.java index e8f619949..b00c733dc 100644 --- a/src/main/java/com/sk89q/worldedit/bukkit/WorldEditListener.java +++ b/src/main/java/com/sk89q/worldedit/bukkit/WorldEditListener.java @@ -23,23 +23,18 @@ 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; import org.bukkit.event.Listener; import org.bukkit.event.block.Action; -import org.bukkit.event.player.PlayerChatEvent; import org.bukkit.event.player.PlayerCommandPreprocessEvent; import org.bukkit.event.player.PlayerInteractEvent; -import org.bukkit.event.player.PlayerJoinEvent; import org.bukkit.event.player.PlayerQuitEvent; import com.sk89q.worldedit.LocalPlayer; import com.sk89q.worldedit.LocalWorld; import com.sk89q.worldedit.WorldEdit; import com.sk89q.worldedit.WorldVector; -import java.util.regex.Matcher; -import java.util.regex.Pattern; /** * Handles all events thrown in relation to a Player @@ -48,7 +43,6 @@ public class WorldEditListener implements Listener { private WorldEditPlugin plugin; private boolean ignoreLeftClickAir = false; - private final static Pattern cuipattern = Pattern.compile("u00a74u00a75u00a73u00a74([^\\|]*)\\|?(.*)"); /** * Called when a player plays an animation, such as an arm swing @@ -65,20 +59,6 @@ public class WorldEditListener implements Listener { this.plugin = plugin; } - @EventHandler(priority = EventPriority.LOWEST) - public void onPlayerJoin(PlayerJoinEvent event) { - 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); - - } - /** * Called when a player leaves a server * @@ -87,7 +67,6 @@ public class WorldEditListener implements Listener { @EventHandler public void onPlayerQuit(PlayerQuitEvent event) { plugin.getWorldEdit().markExpire(plugin.wrapPlayer(event.getPlayer())); - plugin.setPluginChannelCUI(event.getPlayer().getName(), false); } /** @@ -187,22 +166,4 @@ public class WorldEditListener implements Listener { } } } - - @EventHandler(ignoreCancelled = true) // TODO: Remove this in a bit - public void onPlayerChat(PlayerChatEvent event) { - Matcher matcher = cuipattern.matcher(event.getMessage()); - if (matcher.find()) { - String type = matcher.group(1); - String args = matcher.group(2); - - if( type.equals("v") ) { - try { - plugin.getSession(event.getPlayer()).setCUIVersion(Integer.parseInt(args)); - event.setCancelled(true); - } catch(NumberFormatException ignore) { - } - } - - } - } } diff --git a/src/main/java/com/sk89q/worldedit/bukkit/WorldEditPlugin.java b/src/main/java/com/sk89q/worldedit/bukkit/WorldEditPlugin.java index 4659fc32e..48da1cc60 100644 --- a/src/main/java/com/sk89q/worldedit/bukkit/WorldEditPlugin.java +++ b/src/main/java/com/sk89q/worldedit/bukkit/WorldEditPlugin.java @@ -70,11 +70,6 @@ public class WorldEditPlugin extends JavaPlugin { */ private BukkitConfiguration config; - /** - * Stores players who are using plugin channels for the cui - */ - private final Set pluginChannelCui = new HashSet(); - /** * Called on plugin enable. */ @@ -389,16 +384,4 @@ public class WorldEditPlugin extends JavaPlugin { session.setRegionSelector(BukkitUtil.getLocalWorld(player.getWorld()), sel); session.dispatchCUISelection(wrapPlayer(player)); } - - public void setPluginChannelCUI(String name, boolean value) { - if (value) { - pluginChannelCui.add(name); - } else { - pluginChannelCui.remove(name); - } - } - - public boolean hasPluginChannelCUI(String name) { - return pluginChannelCui.contains(name); - } } diff --git a/src/main/java/com/sk89q/worldedit/spout/WorldEditCUIMessageHandler.java b/src/main/java/com/sk89q/worldedit/spout/WorldEditCUIMessageHandler.java index 901897448..928ecf05c 100644 --- a/src/main/java/com/sk89q/worldedit/spout/WorldEditCUIMessageHandler.java +++ b/src/main/java/com/sk89q/worldedit/spout/WorldEditCUIMessageHandler.java @@ -39,14 +39,6 @@ public class WorldEditCUIMessageHandler extends MessageHandler 1 && text[0].equalsIgnoreCase("v")) { // enough fields and right message - localSession.setCUISupport(true); - try { - localSession.setCUIVersion(Integer.parseInt(text[1])); - } catch (NumberFormatException e) { - plugin.getLogger().warning("Error while reading CUI init message: " + e.getMessage()); - } - } + localSession.handleCUIInitializationMessage(message.getMessage()); } } diff --git a/src/main/java/com/sk89q/worldedit/spout/WorldEditListener.java b/src/main/java/com/sk89q/worldedit/spout/WorldEditListener.java index ba35e5416..c1cc294c9 100644 --- a/src/main/java/com/sk89q/worldedit/spout/WorldEditListener.java +++ b/src/main/java/com/sk89q/worldedit/spout/WorldEditListener.java @@ -64,11 +64,6 @@ public class WorldEditListener implements Listener { this.plugin = plugin; } - @EventHandler(order = Order.EARLIEST) - public void onPlayerJoin(PlayerJoinEvent event) { - plugin.wrapPlayer(event.getPlayer()).dispatchCUIHandshake(); - } - /** * Called when a player leaves a server * diff --git a/src/main/java/com/sk89q/worldedit/util/PropertiesConfiguration.java b/src/main/java/com/sk89q/worldedit/util/PropertiesConfiguration.java index 2a662c7b8..5c7c4d733 100644 --- a/src/main/java/com/sk89q/worldedit/util/PropertiesConfiguration.java +++ b/src/main/java/com/sk89q/worldedit/util/PropertiesConfiguration.java @@ -36,7 +36,7 @@ import com.sk89q.worldedit.snapshots.SnapshotRepository; /** * Simple LocalConfiguration that loads settings using * java.util.Properties. - * + * * @author sk89q */ public class PropertiesConfiguration extends LocalConfiguration { @@ -46,7 +46,7 @@ public class PropertiesConfiguration extends LocalConfiguration { /** * Construct the object. The configuration isn't loaded yet. - * + * * @param path */ public PropertiesConfiguration(File path) { @@ -77,7 +77,6 @@ public class PropertiesConfiguration extends LocalConfiguration { } profile = getBool("profile", profile); - enableWECUI = getBool("enable-wecui-handshake", enableWECUI); disallowedBlocks = getIntSet("disallowed-blocks", defaultDisallowedBlocks); defaultChangeLimit = getInt("default-max-changed-blocks", defaultChangeLimit); maxChangeLimit = getInt("max-changed-blocks", maxChangeLimit); @@ -126,7 +125,7 @@ public class PropertiesConfiguration extends LocalConfiguration { /** * Get a string value. - * + * * @param key * @param def * @return @@ -146,7 +145,7 @@ public class PropertiesConfiguration extends LocalConfiguration { /** * Get a boolean value. - * + * * @param key * @param def * @return @@ -164,7 +163,7 @@ public class PropertiesConfiguration extends LocalConfiguration { /** * Get an integer value. - * + * * @param key * @param def * @return @@ -186,7 +185,7 @@ public class PropertiesConfiguration extends LocalConfiguration { /** * Get a double value. - * + * * @param key * @param def * @return @@ -208,7 +207,7 @@ public class PropertiesConfiguration extends LocalConfiguration { /** * Get a double value. - * + * * @param key * @param def * @return diff --git a/src/main/java/com/sk89q/worldedit/util/YAMLConfiguration.java b/src/main/java/com/sk89q/worldedit/util/YAMLConfiguration.java index d9a40da9b..71ac13307 100644 --- a/src/main/java/com/sk89q/worldedit/util/YAMLConfiguration.java +++ b/src/main/java/com/sk89q/worldedit/util/YAMLConfiguration.java @@ -59,7 +59,6 @@ public class YAMLConfiguration extends LocalConfiguration { showFirstUseVersion = false; profile = config.getBoolean("debug", profile); - enableWECUI = config.getBoolean("enable-wecui-handshake", enableWECUI); wandItem = config.getInt("wand-item", wandItem); defaultChangeLimit = Math.max(-1, config.getInt( "limits.max-blocks-changed.default", defaultChangeLimit)); diff --git a/src/main/resources/config.yml b/src/main/resources/config.yml index d2756114a..a6ba4921c 100644 --- a/src/main/resources/config.yml +++ b/src/main/resources/config.yml @@ -61,8 +61,7 @@ butcher: butcher-default-radius: -1 wand-item: 271 -shell-save-type: +shell-save-type: no-double-slash: false no-op-permissions: false debug: false -enable-wecui-handshake: true \ No newline at end of file