mirror of
https://github.com/LuckPerms/LuckPerms.git
synced 2024-11-24 11:38:40 +01:00
maybe fix some bukkit migration issues when plugins use strange identifier formats, cleanup legacy message conversion on Bungee
This commit is contained in:
parent
9a420403de
commit
df37491199
@ -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<CommandSender> {
|
||||
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<CommandSender> {
|
||||
|
||||
@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<CommandSender> {
|
||||
|
||||
@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<CommandSender> {
|
||||
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;
|
||||
|
@ -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;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
@ -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;
|
||||
}
|
||||
|
||||
}
|
@ -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<Object> {
|
||||
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;
|
||||
}
|
||||
|
||||
|
@ -185,11 +185,8 @@ public class MigrationGroupManager extends SubCommand<Object> {
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
|
@ -124,22 +124,8 @@ public class MigrationPermissionsEx extends SubCommand<Object> {
|
||||
maxWeight += 5;
|
||||
|
||||
for (PermissionUser user : manager.getUsers()) {
|
||||
UUID u;
|
||||
try {
|
||||
u = UUID.fromString(user.getIdentifier());
|
||||
} catch (IllegalArgumentException e) {
|
||||
u = ni.nameToUUID(user.getIdentifier());
|
||||
UUID u = BukkitMigrationUtils.lookupUuid(log, user.getIdentifier());
|
||||
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;
|
||||
}
|
||||
|
||||
|
@ -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<Object> {
|
||||
// store user data for later
|
||||
Set<Membership> members = entity.getMemberships();
|
||||
for (Membership membership : members) {
|
||||
UUID uuid = Util.parseUuid(membership.getMember());
|
||||
UUID uuid = BukkitMigrationUtils.lookupUuid(log, membership.getMember());
|
||||
if (uuid == null) {
|
||||
continue;
|
||||
}
|
||||
|
@ -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");
|
||||
|
@ -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<CommandSender> {
|
||||
|
||||
@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));
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
|
@ -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<CommandSource> {
|
||||
|
||||
@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));
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
|
Loading…
Reference in New Issue
Block a user