maybe fix some bukkit migration issues when plugins use strange identifier formats, cleanup legacy message conversion on Bungee

This commit is contained in:
Luck 2017-11-12 15:05:06 +00:00
parent 9a420403de
commit df37491199
No known key found for this signature in database
GPG Key ID: EFA9B3EC5FD90F8B
10 changed files with 109 additions and 142 deletions

View File

@ -28,12 +28,16 @@ package me.lucko.luckperms.bukkit;
import lombok.AllArgsConstructor; import lombok.AllArgsConstructor;
import me.lucko.luckperms.api.Tristate; 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.commands.sender.SenderFactory;
import me.lucko.luckperms.common.constants.Constants; import me.lucko.luckperms.common.constants.Constants;
import me.lucko.luckperms.common.plugin.LuckPermsPlugin; import me.lucko.luckperms.common.plugin.LuckPermsPlugin;
import me.lucko.luckperms.common.utils.TextUtils;
import net.kyori.text.Component; import net.kyori.text.Component;
import net.kyori.text.serializer.ComponentSerializers;
import org.bukkit.command.BlockCommandSender; import org.bukkit.command.BlockCommandSender;
import org.bukkit.command.CommandSender; import org.bukkit.command.CommandSender;
@ -42,11 +46,13 @@ import org.bukkit.entity.Player;
import java.util.UUID; import java.util.UUID;
public class BukkitSenderFactory extends SenderFactory<CommandSender> { public class BukkitSenderFactory extends SenderFactory<CommandSender> {
private final MessageHandler messageHandler; private final BukkitJsonMessageHandler bukkitHandler;
private final SpigotJsonMessageHandler spigotHandler;
public BukkitSenderFactory(LuckPermsPlugin plugin) { public BukkitSenderFactory(LuckPermsPlugin plugin) {
super(plugin); super(plugin);
messageHandler = new MessageHandler(); bukkitHandler = new BukkitJsonMessageHandler();
spigotHandler = isSpigot() ? new SpigotJsonMessageHandler() : null;
} }
@Override @Override
@ -67,7 +73,6 @@ public class BukkitSenderFactory extends SenderFactory<CommandSender> {
@Override @Override
protected void sendMessage(CommandSender sender, String s) { protected void sendMessage(CommandSender sender, String s) {
// send sync if command block // send sync if command block
if (sender instanceof BlockCommandSender) { if (sender instanceof BlockCommandSender) {
getPlugin().getScheduler().doSync(new BlockMessageAgent(((BlockCommandSender) sender), s)); getPlugin().getScheduler().doSync(new BlockMessageAgent(((BlockCommandSender) sender), s));
@ -79,7 +84,23 @@ public class BukkitSenderFactory extends SenderFactory<CommandSender> {
@Override @Override
protected void sendMessage(CommandSender sender, Component message) { 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 @Override
@ -95,6 +116,15 @@ public class BukkitSenderFactory extends SenderFactory<CommandSender> {
return sender.hasPermission(node); 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 @AllArgsConstructor
private static final class BlockMessageAgent implements Runnable { private static final class BlockMessageAgent implements Runnable {
private final BlockCommandSender block; private final BlockCommandSender block;

View File

@ -1,77 +0,0 @@
/*
* This file is part of LuckPerms, licensed under the MIT License.
*
* Copyright (c) lucko (Luck) <luck@lucko.me>
* 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;
}
}
}

View File

@ -0,0 +1,56 @@
/*
* This file is part of LuckPerms, licensed under the MIT License.
*
* Copyright (c) lucko (Luck) <luck@lucko.me>
* 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;
}
}

View File

@ -48,7 +48,6 @@ import me.lucko.luckperms.common.node.NodeFactory;
import me.lucko.luckperms.common.plugin.LuckPermsPlugin; import me.lucko.luckperms.common.plugin.LuckPermsPlugin;
import me.lucko.luckperms.common.utils.Predicates; import me.lucko.luckperms.common.utils.Predicates;
import org.bukkit.Bukkit;
import org.bukkit.configuration.ConfigurationSection; import org.bukkit.configuration.ConfigurationSection;
import org.bukkit.configuration.file.YamlConfiguration; import org.bukkit.configuration.file.YamlConfiguration;
@ -156,20 +155,8 @@ public class MigrationBPermissions extends SubCommand<Object> {
AtomicInteger userCount = new AtomicInteger(0); AtomicInteger userCount = new AtomicInteger(0);
for (Calculable user : world.getAll(CalculableType.USER)) { for (Calculable user : world.getAll(CalculableType.USER)) {
// There is no mention of UUIDs in the API. I assume that name = uuid. idk? // There is no mention of UUIDs in the API. I assume that name = uuid. idk?
UUID uuid = null; UUID uuid = BukkitMigrationUtils.lookupUuid(log, user.getName());
try {
uuid = UUID.fromString(user.getName());
} catch (IllegalArgumentException e) {
try {
//noinspection deprecation
uuid = Bukkit.getOfflinePlayer(user.getName()).getUniqueId();
} catch (Exception ex) {
e.printStackTrace();
}
}
if (uuid == null) { if (uuid == null) {
log.logErr("Unable to migrate user " + user.getName() + ". Cannot to get UUID.");
continue; continue;
} }

View File

@ -185,11 +185,8 @@ public class MigrationGroupManager extends SubCommand<Object> {
AtomicInteger userWorldCount = new AtomicInteger(0); AtomicInteger userWorldCount = new AtomicInteger(0);
for (User user : wdh.getUserList()) { for (User user : wdh.getUserList()) {
UUID uuid; UUID uuid = BukkitMigrationUtils.lookupUuid(log, user.getUUID());
try { if (uuid == null) {
uuid = UUID.fromString(user.getUUID());
} catch (IllegalArgumentException e) {
log.logErr("Could not parse UUID for user: " + user.getUUID());
continue; continue;
} }

View File

@ -124,22 +124,8 @@ public class MigrationPermissionsEx extends SubCommand<Object> {
maxWeight += 5; maxWeight += 5;
for (PermissionUser user : manager.getUsers()) { for (PermissionUser user : manager.getUsers()) {
UUID u; UUID u = BukkitMigrationUtils.lookupUuid(log, user.getIdentifier());
try {
u = UUID.fromString(user.getIdentifier());
} catch (IllegalArgumentException e) {
u = ni.nameToUUID(user.getIdentifier());
if (u == null) { if (u == null) {
try {
u = Bukkit.getOfflinePlayer(user.getIdentifier()).getUniqueId();
} catch (Exception ex) {
e.printStackTrace();
}
}
}
if (u == null) {
log.logErr("Unable to get a UUID for user identifier: " + user.getIdentifier());
continue; continue;
} }

View File

@ -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.abstraction.SubCommand;
import me.lucko.luckperms.common.commands.impl.migration.MigrationUtils; import me.lucko.luckperms.common.commands.impl.migration.MigrationUtils;
import me.lucko.luckperms.common.commands.sender.Sender; 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.constants.CommandPermission;
import me.lucko.luckperms.common.locale.CommandSpec; import me.lucko.luckperms.common.locale.CommandSpec;
import me.lucko.luckperms.common.locale.LocaleManager; import me.lucko.luckperms.common.locale.LocaleManager;
@ -118,7 +117,7 @@ public class MigrationZPermissions extends SubCommand<Object> {
// store user data for later // store user data for later
Set<Membership> members = entity.getMemberships(); Set<Membership> members = entity.getMemberships();
for (Membership membership : members) { for (Membership membership : members) {
UUID uuid = Util.parseUuid(membership.getMember()); UUID uuid = BukkitMigrationUtils.lookupUuid(log, membership.getMember());
if (uuid == null) { if (uuid == null) {
continue; continue;
} }

View File

@ -32,7 +32,6 @@ import com.google.common.base.Preconditions;
import me.lucko.luckperms.api.Contexts; import me.lucko.luckperms.api.Contexts;
import me.lucko.luckperms.api.Node; import me.lucko.luckperms.api.Node;
import me.lucko.luckperms.api.context.ImmutableContextSet;
import me.lucko.luckperms.api.context.MutableContextSet; import me.lucko.luckperms.api.context.MutableContextSet;
import me.lucko.luckperms.bukkit.LPBukkitPlugin; import me.lucko.luckperms.bukkit.LPBukkitPlugin;
import me.lucko.luckperms.common.config.ConfigKeys; import me.lucko.luckperms.common.config.ConfigKeys;
@ -462,21 +461,23 @@ public class VaultPermissionHook extends Permission {
} }
public Contexts createContextForWorldLookup(String world) { public Contexts createContextForWorldLookup(String world) {
MutableContextSet context = MutableContextSet.create(); MutableContextSet context = plugin.getContextManager().getStaticContext().mutableCopy();
if (world != null && !world.equals("") && !world.equalsIgnoreCase("global")) {
// 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("world", world.toLowerCase());
} }
context.add("server", getServer()); context.add("server", getServer());
context.addAll(plugin.getConfiguration().getContextsFile().getStaticContexts());
return new Contexts(context, isIncludeGlobal(), true, true, true, true, false); return new Contexts(context, isIncludeGlobal(), true, true, true, true, false);
} }
public Contexts createContextForWorldLookup(@NonNull Player player, String world) { public Contexts createContextForWorldLookup(@NonNull Player player, String world) {
MutableContextSet context = MutableContextSet.create(); MutableContextSet context = plugin.getContextManager().getApplicableContext(player).mutableCopy();
// use player context
ImmutableContextSet applicableContext = plugin.getContextManager().getApplicableContext(player);
context.addAll(applicableContext);
// worlds & servers get set depending on the config setting // worlds & servers get set depending on the config setting
context.removeAll("world"); context.removeAll("world");

View File

@ -35,7 +35,6 @@ import me.lucko.luckperms.common.utils.TextUtils;
import net.kyori.text.Component; import net.kyori.text.Component;
import net.kyori.text.serializer.ComponentSerializers; import net.kyori.text.serializer.ComponentSerializers;
import net.md_5.bungee.api.CommandSender; import net.md_5.bungee.api.CommandSender;
import net.md_5.bungee.api.chat.TextComponent;
import net.md_5.bungee.api.connection.ProxiedPlayer; import net.md_5.bungee.api.connection.ProxiedPlayer;
import java.util.UUID; import java.util.UUID;
@ -63,17 +62,12 @@ public class BungeeSenderFactory extends SenderFactory<CommandSender> {
@Override @Override
protected void sendMessage(CommandSender sender, String s) { protected void sendMessage(CommandSender sender, String s) {
sender.sendMessage(new TextComponent(s)); sendMessage(sender, TextUtils.fromLegacy(s));
} }
@Override @Override
protected void sendMessage(CommandSender sender, Component message) { protected void sendMessage(CommandSender sender, Component message) {
try {
sender.sendMessage(net.md_5.bungee.chat.ComponentSerializer.parse(ComponentSerializers.JSON.serialize(message))); sender.sendMessage(net.md_5.bungee.chat.ComponentSerializer.parse(ComponentSerializers.JSON.serialize(message)));
} catch (Exception e) {
//noinspection deprecation
sendMessage(sender, TextUtils.toLegacy(message));
}
} }
@Override @Override

View File

@ -29,7 +29,6 @@ import me.lucko.luckperms.api.Tristate;
import me.lucko.luckperms.common.commands.sender.SenderFactory; import me.lucko.luckperms.common.commands.sender.SenderFactory;
import me.lucko.luckperms.common.constants.Constants; import me.lucko.luckperms.common.constants.Constants;
import me.lucko.luckperms.common.plugin.LuckPermsPlugin; import me.lucko.luckperms.common.plugin.LuckPermsPlugin;
import me.lucko.luckperms.common.utils.TextUtils;
import me.lucko.luckperms.sponge.service.model.CompatibilityUtil; import me.lucko.luckperms.sponge.service.model.CompatibilityUtil;
import net.kyori.text.Component; import net.kyori.text.Component;
@ -70,12 +69,7 @@ public class SpongeSenderFactory extends SenderFactory<CommandSource> {
@Override @Override
protected void sendMessage(CommandSource source, Component message) { protected void sendMessage(CommandSource source, Component message) {
try {
source.sendMessage(TextSerializers.JSON.deserialize(ComponentSerializers.JSON.serialize(message))); source.sendMessage(TextSerializers.JSON.deserialize(ComponentSerializers.JSON.serialize(message)));
} catch (Exception e) {
//noinspection deprecation
sendMessage(source, TextUtils.toLegacy(message));
}
} }
@Override @Override