From df374911991bc6e0100187f61c81d2371b9fd817 Mon Sep 17 00:00:00 2001 From: Luck Date: Sun, 12 Nov 2017 15:05:06 +0000 Subject: [PATCH] maybe fix some bukkit migration issues when plugins use strange identifier formats, cleanup legacy message conversion on Bungee --- .../luckperms/bukkit/BukkitSenderFactory.java | 40 ++++++++-- .../bukkit/compat/MessageHandler.java | 77 ------------------- .../migration/BukkitMigrationUtils.java | 56 ++++++++++++++ .../migration/MigrationBPermissions.java | 15 +--- .../migration/MigrationGroupManager.java | 7 +- .../migration/MigrationPermissionsEx.java | 16 +--- .../migration/MigrationZPermissions.java | 3 +- .../bukkit/vault/VaultPermissionHook.java | 19 ++--- .../luckperms/bungee/BungeeSenderFactory.java | 10 +-- .../luckperms/sponge/SpongeSenderFactory.java | 8 +- 10 files changed, 109 insertions(+), 142 deletions(-) delete mode 100644 bukkit/src/main/java/me/lucko/luckperms/bukkit/compat/MessageHandler.java create mode 100644 bukkit/src/main/java/me/lucko/luckperms/bukkit/migration/BukkitMigrationUtils.java diff --git a/bukkit/src/main/java/me/lucko/luckperms/bukkit/BukkitSenderFactory.java b/bukkit/src/main/java/me/lucko/luckperms/bukkit/BukkitSenderFactory.java index ef328633f..0e230b15c 100644 --- a/bukkit/src/main/java/me/lucko/luckperms/bukkit/BukkitSenderFactory.java +++ b/bukkit/src/main/java/me/lucko/luckperms/bukkit/BukkitSenderFactory.java @@ -28,12 +28,16 @@ package me.lucko.luckperms.bukkit; import lombok.AllArgsConstructor; import me.lucko.luckperms.api.Tristate; -import me.lucko.luckperms.bukkit.compat.MessageHandler; +import me.lucko.luckperms.bukkit.compat.BukkitJsonMessageHandler; +import me.lucko.luckperms.bukkit.compat.ReflectionUtil; +import me.lucko.luckperms.bukkit.compat.SpigotJsonMessageHandler; import me.lucko.luckperms.common.commands.sender.SenderFactory; import me.lucko.luckperms.common.constants.Constants; import me.lucko.luckperms.common.plugin.LuckPermsPlugin; +import me.lucko.luckperms.common.utils.TextUtils; import net.kyori.text.Component; +import net.kyori.text.serializer.ComponentSerializers; import org.bukkit.command.BlockCommandSender; import org.bukkit.command.CommandSender; @@ -42,11 +46,13 @@ import org.bukkit.entity.Player; import java.util.UUID; public class BukkitSenderFactory extends SenderFactory { - private final MessageHandler messageHandler; + private final BukkitJsonMessageHandler bukkitHandler; + private final SpigotJsonMessageHandler spigotHandler; public BukkitSenderFactory(LuckPermsPlugin plugin) { super(plugin); - messageHandler = new MessageHandler(); + bukkitHandler = new BukkitJsonMessageHandler(); + spigotHandler = isSpigot() ? new SpigotJsonMessageHandler() : null; } @Override @@ -67,7 +73,6 @@ public class BukkitSenderFactory extends SenderFactory { @Override protected void sendMessage(CommandSender sender, String s) { - // send sync if command block if (sender instanceof BlockCommandSender) { getPlugin().getScheduler().doSync(new BlockMessageAgent(((BlockCommandSender) sender), s)); @@ -79,7 +84,23 @@ public class BukkitSenderFactory extends SenderFactory { @Override protected void sendMessage(CommandSender sender, Component message) { - messageHandler.sendJsonMessage(sender, message); + if (ReflectionUtil.isChatCompatible() && sender instanceof Player) { + Player player = (Player) sender; + String json = ComponentSerializers.JSON.serialize(message); + + // Try Bukkit. + if (bukkitHandler.sendJsonMessage(player, json)) { + return; + } + + // Try Spigot. + if (spigotHandler != null && spigotHandler.sendJsonMessage(player, json)) { + return; + } + } + + // Fallback to legacy format + sender.sendMessage(TextUtils.toLegacy(message)); } @Override @@ -95,6 +116,15 @@ public class BukkitSenderFactory extends SenderFactory { return sender.hasPermission(node); } + private static boolean isSpigot() { + try { + Class.forName("net.md_5.bungee.chat.ComponentSerializer"); + return true; + } catch (ClassNotFoundException e) { + return false; + } + } + @AllArgsConstructor private static final class BlockMessageAgent implements Runnable { private final BlockCommandSender block; diff --git a/bukkit/src/main/java/me/lucko/luckperms/bukkit/compat/MessageHandler.java b/bukkit/src/main/java/me/lucko/luckperms/bukkit/compat/MessageHandler.java deleted file mode 100644 index 34e3b607b..000000000 --- a/bukkit/src/main/java/me/lucko/luckperms/bukkit/compat/MessageHandler.java +++ /dev/null @@ -1,77 +0,0 @@ -/* - * This file is part of LuckPerms, licensed under the MIT License. - * - * Copyright (c) lucko (Luck) - * Copyright (c) contributors - * - * Permission is hereby granted, free of charge, to any person obtaining a copy - * of this software and associated documentation files (the "Software"), to deal - * in the Software without restriction, including without limitation the rights - * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell - * copies of the Software, and to permit persons to whom the Software is - * furnished to do so, subject to the following conditions: - * - * The above copyright notice and this permission notice shall be included in all - * copies or substantial portions of the Software. - * - * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR - * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, - * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE - * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER - * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, - * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE - * SOFTWARE. - */ - -package me.lucko.luckperms.bukkit.compat; - -import me.lucko.luckperms.common.utils.TextUtils; - -import net.kyori.text.Component; -import net.kyori.text.serializer.ComponentSerializers; - -import org.bukkit.command.CommandSender; -import org.bukkit.entity.Player; - -/** - * Sends a json message to a command sender. - */ -public class MessageHandler { - private final BukkitJsonMessageHandler bukkitHandler; - private final SpigotJsonMessageHandler spigotHandler; - - public MessageHandler() { - bukkitHandler = new BukkitJsonMessageHandler(); - spigotHandler = isSpigot() ? new SpigotJsonMessageHandler() : null; - } - - public void sendJsonMessage(CommandSender sender, Component message) { - if (ReflectionUtil.isChatCompatible() && sender instanceof Player) { - Player player = (Player) sender; - String json = ComponentSerializers.JSON.serialize(message); - - // Try Bukkit. - if (bukkitHandler.sendJsonMessage(player, json)) { - return; - } - - // Try Spigot. - if (spigotHandler != null && spigotHandler.sendJsonMessage(player, json)) { - return; - } - } - - // Fallback to Bukkit - sender.sendMessage(TextUtils.toLegacy(message)); - } - - private static boolean isSpigot() { - try { - Class.forName("net.md_5.bungee.chat.ComponentSerializer"); - return true; - } catch (ClassNotFoundException e) { - return false; - } - } - -} diff --git a/bukkit/src/main/java/me/lucko/luckperms/bukkit/migration/BukkitMigrationUtils.java b/bukkit/src/main/java/me/lucko/luckperms/bukkit/migration/BukkitMigrationUtils.java new file mode 100644 index 000000000..6d9781b1f --- /dev/null +++ b/bukkit/src/main/java/me/lucko/luckperms/bukkit/migration/BukkitMigrationUtils.java @@ -0,0 +1,56 @@ +/* + * This file is part of LuckPerms, licensed under the MIT License. + * + * Copyright (c) lucko (Luck) + * Copyright (c) contributors + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ + +package me.lucko.luckperms.bukkit.migration; + +import lombok.experimental.UtilityClass; + +import me.lucko.luckperms.common.commands.utils.Util; +import me.lucko.luckperms.common.logging.ProgressLogger; + +import org.bukkit.Bukkit; + +import java.util.UUID; + +@UtilityClass +public class BukkitMigrationUtils { + + @SuppressWarnings("deprecation") + public static UUID lookupUuid(ProgressLogger log, String s) { + UUID uuid = Util.parseUuid(s); + if (uuid == null) { + try { + uuid = Bukkit.getOfflinePlayer(s).getUniqueId(); + } catch (Exception ex) { + ex.printStackTrace(); + } + } + if (uuid == null) { + log.logErr("Unable to get a UUID for user identifier: " + s); + } + return uuid; + } + +} diff --git a/bukkit/src/main/java/me/lucko/luckperms/bukkit/migration/MigrationBPermissions.java b/bukkit/src/main/java/me/lucko/luckperms/bukkit/migration/MigrationBPermissions.java index 1cc0de46f..278c19f6f 100644 --- a/bukkit/src/main/java/me/lucko/luckperms/bukkit/migration/MigrationBPermissions.java +++ b/bukkit/src/main/java/me/lucko/luckperms/bukkit/migration/MigrationBPermissions.java @@ -48,7 +48,6 @@ import me.lucko.luckperms.common.node.NodeFactory; import me.lucko.luckperms.common.plugin.LuckPermsPlugin; import me.lucko.luckperms.common.utils.Predicates; -import org.bukkit.Bukkit; import org.bukkit.configuration.ConfigurationSection; import org.bukkit.configuration.file.YamlConfiguration; @@ -156,20 +155,8 @@ public class MigrationBPermissions extends SubCommand { AtomicInteger userCount = new AtomicInteger(0); for (Calculable user : world.getAll(CalculableType.USER)) { // There is no mention of UUIDs in the API. I assume that name = uuid. idk? - UUID uuid = null; - try { - uuid = UUID.fromString(user.getName()); - } catch (IllegalArgumentException e) { - try { - //noinspection deprecation - uuid = Bukkit.getOfflinePlayer(user.getName()).getUniqueId(); - } catch (Exception ex) { - e.printStackTrace(); - } - } - + UUID uuid = BukkitMigrationUtils.lookupUuid(log, user.getName()); if (uuid == null) { - log.logErr("Unable to migrate user " + user.getName() + ". Cannot to get UUID."); continue; } diff --git a/bukkit/src/main/java/me/lucko/luckperms/bukkit/migration/MigrationGroupManager.java b/bukkit/src/main/java/me/lucko/luckperms/bukkit/migration/MigrationGroupManager.java index 548beb9e4..78b572f12 100644 --- a/bukkit/src/main/java/me/lucko/luckperms/bukkit/migration/MigrationGroupManager.java +++ b/bukkit/src/main/java/me/lucko/luckperms/bukkit/migration/MigrationGroupManager.java @@ -185,11 +185,8 @@ public class MigrationGroupManager extends SubCommand { AtomicInteger userWorldCount = new AtomicInteger(0); for (User user : wdh.getUserList()) { - UUID uuid; - try { - uuid = UUID.fromString(user.getUUID()); - } catch (IllegalArgumentException e) { - log.logErr("Could not parse UUID for user: " + user.getUUID()); + UUID uuid = BukkitMigrationUtils.lookupUuid(log, user.getUUID()); + if (uuid == null) { continue; } diff --git a/bukkit/src/main/java/me/lucko/luckperms/bukkit/migration/MigrationPermissionsEx.java b/bukkit/src/main/java/me/lucko/luckperms/bukkit/migration/MigrationPermissionsEx.java index 02c4a8bc9..9f02b0038 100644 --- a/bukkit/src/main/java/me/lucko/luckperms/bukkit/migration/MigrationPermissionsEx.java +++ b/bukkit/src/main/java/me/lucko/luckperms/bukkit/migration/MigrationPermissionsEx.java @@ -124,22 +124,8 @@ public class MigrationPermissionsEx extends SubCommand { maxWeight += 5; for (PermissionUser user : manager.getUsers()) { - UUID u; - try { - u = UUID.fromString(user.getIdentifier()); - } catch (IllegalArgumentException e) { - u = ni.nameToUUID(user.getIdentifier()); - if (u == null) { - try { - u = Bukkit.getOfflinePlayer(user.getIdentifier()).getUniqueId(); - } catch (Exception ex) { - e.printStackTrace(); - } - } - } - + UUID u = BukkitMigrationUtils.lookupUuid(log, user.getIdentifier()); if (u == null) { - log.logErr("Unable to get a UUID for user identifier: " + user.getIdentifier()); continue; } diff --git a/bukkit/src/main/java/me/lucko/luckperms/bukkit/migration/MigrationZPermissions.java b/bukkit/src/main/java/me/lucko/luckperms/bukkit/migration/MigrationZPermissions.java index 63cccc796..1b5abd9a7 100644 --- a/bukkit/src/main/java/me/lucko/luckperms/bukkit/migration/MigrationZPermissions.java +++ b/bukkit/src/main/java/me/lucko/luckperms/bukkit/migration/MigrationZPermissions.java @@ -33,7 +33,6 @@ import me.lucko.luckperms.common.commands.CommandResult; import me.lucko.luckperms.common.commands.abstraction.SubCommand; import me.lucko.luckperms.common.commands.impl.migration.MigrationUtils; import me.lucko.luckperms.common.commands.sender.Sender; -import me.lucko.luckperms.common.commands.utils.Util; import me.lucko.luckperms.common.constants.CommandPermission; import me.lucko.luckperms.common.locale.CommandSpec; import me.lucko.luckperms.common.locale.LocaleManager; @@ -118,7 +117,7 @@ public class MigrationZPermissions extends SubCommand { // store user data for later Set members = entity.getMemberships(); for (Membership membership : members) { - UUID uuid = Util.parseUuid(membership.getMember()); + UUID uuid = BukkitMigrationUtils.lookupUuid(log, membership.getMember()); if (uuid == null) { continue; } diff --git a/bukkit/src/main/java/me/lucko/luckperms/bukkit/vault/VaultPermissionHook.java b/bukkit/src/main/java/me/lucko/luckperms/bukkit/vault/VaultPermissionHook.java index 7b5fa0aeb..33c628435 100644 --- a/bukkit/src/main/java/me/lucko/luckperms/bukkit/vault/VaultPermissionHook.java +++ b/bukkit/src/main/java/me/lucko/luckperms/bukkit/vault/VaultPermissionHook.java @@ -32,7 +32,6 @@ import com.google.common.base.Preconditions; import me.lucko.luckperms.api.Contexts; import me.lucko.luckperms.api.Node; -import me.lucko.luckperms.api.context.ImmutableContextSet; import me.lucko.luckperms.api.context.MutableContextSet; import me.lucko.luckperms.bukkit.LPBukkitPlugin; import me.lucko.luckperms.common.config.ConfigKeys; @@ -462,21 +461,23 @@ public class VaultPermissionHook extends Permission { } public Contexts createContextForWorldLookup(String world) { - MutableContextSet context = MutableContextSet.create(); - if (world != null && !world.equals("") && !world.equalsIgnoreCase("global")) { + MutableContextSet context = plugin.getContextManager().getStaticContext().mutableCopy(); + + // worlds & servers get set depending on the config setting + context.removeAll("world"); + context.removeAll("server"); + + // add the vault settings + if (world != null && !world.isEmpty() && !world.equalsIgnoreCase("global")) { context.add("world", world.toLowerCase()); } context.add("server", getServer()); - context.addAll(plugin.getConfiguration().getContextsFile().getStaticContexts()); + return new Contexts(context, isIncludeGlobal(), true, true, true, true, false); } public Contexts createContextForWorldLookup(@NonNull Player player, String world) { - MutableContextSet context = MutableContextSet.create(); - - // use player context - ImmutableContextSet applicableContext = plugin.getContextManager().getApplicableContext(player); - context.addAll(applicableContext); + MutableContextSet context = plugin.getContextManager().getApplicableContext(player).mutableCopy(); // worlds & servers get set depending on the config setting context.removeAll("world"); diff --git a/bungee/src/main/java/me/lucko/luckperms/bungee/BungeeSenderFactory.java b/bungee/src/main/java/me/lucko/luckperms/bungee/BungeeSenderFactory.java index 1ef0fc312..70f8df1bb 100644 --- a/bungee/src/main/java/me/lucko/luckperms/bungee/BungeeSenderFactory.java +++ b/bungee/src/main/java/me/lucko/luckperms/bungee/BungeeSenderFactory.java @@ -35,7 +35,6 @@ import me.lucko.luckperms.common.utils.TextUtils; import net.kyori.text.Component; import net.kyori.text.serializer.ComponentSerializers; import net.md_5.bungee.api.CommandSender; -import net.md_5.bungee.api.chat.TextComponent; import net.md_5.bungee.api.connection.ProxiedPlayer; import java.util.UUID; @@ -63,17 +62,12 @@ public class BungeeSenderFactory extends SenderFactory { @Override protected void sendMessage(CommandSender sender, String s) { - sender.sendMessage(new TextComponent(s)); + sendMessage(sender, TextUtils.fromLegacy(s)); } @Override protected void sendMessage(CommandSender sender, Component message) { - try { - sender.sendMessage(net.md_5.bungee.chat.ComponentSerializer.parse(ComponentSerializers.JSON.serialize(message))); - } catch (Exception e) { - //noinspection deprecation - sendMessage(sender, TextUtils.toLegacy(message)); - } + sender.sendMessage(net.md_5.bungee.chat.ComponentSerializer.parse(ComponentSerializers.JSON.serialize(message))); } @Override diff --git a/sponge/src/main/java/me/lucko/luckperms/sponge/SpongeSenderFactory.java b/sponge/src/main/java/me/lucko/luckperms/sponge/SpongeSenderFactory.java index 3dc875cfe..966d28575 100644 --- a/sponge/src/main/java/me/lucko/luckperms/sponge/SpongeSenderFactory.java +++ b/sponge/src/main/java/me/lucko/luckperms/sponge/SpongeSenderFactory.java @@ -29,7 +29,6 @@ import me.lucko.luckperms.api.Tristate; import me.lucko.luckperms.common.commands.sender.SenderFactory; import me.lucko.luckperms.common.constants.Constants; import me.lucko.luckperms.common.plugin.LuckPermsPlugin; -import me.lucko.luckperms.common.utils.TextUtils; import me.lucko.luckperms.sponge.service.model.CompatibilityUtil; import net.kyori.text.Component; @@ -70,12 +69,7 @@ public class SpongeSenderFactory extends SenderFactory { @Override protected void sendMessage(CommandSource source, Component message) { - try { - source.sendMessage(TextSerializers.JSON.deserialize(ComponentSerializers.JSON.serialize(message))); - } catch (Exception e) { - //noinspection deprecation - sendMessage(source, TextUtils.toLegacy(message)); - } + source.sendMessage(TextSerializers.JSON.deserialize(ComponentSerializers.JSON.serialize(message))); } @Override