From cb185e4827427ed45e6e4d240ed53a2bd0145f77 Mon Sep 17 00:00:00 2001 From: Luck Date: Sun, 8 Apr 2018 22:58:15 +0100 Subject: [PATCH] Minor misc changes --- .../luckperms/bukkit/LPBukkitBootstrap.java | 5 +- .../luckperms/bukkit/LPBukkitPlugin.java | 6 +-- .../luckperms/bungee/BungeeSenderFactory.java | 3 +- .../luckperms/bungee/LPBungeeBootstrap.java | 5 +- .../luckperms/bungee/LPBungeePlugin.java | 6 +-- .../commands/group/GroupListMembers.java | 14 ++--- .../common/commands/misc/SearchCommand.java | 15 ++---- .../common/node/HeldPermissionComparator.java | 51 +++++++++++++++++++ .../luckperms/common/node/NodeComparator.java | 9 ++-- .../common/node/NodeHeldPermission.java | 7 +-- .../node/NodeWithContextComparator.java | 16 +++--- .../plugin/bootstrap/LuckPermsBootstrap.java | 3 +- .../luckperms/nukkit/LPNukkitBootstrap.java | 5 +- .../luckperms/nukkit/LPNukkitPlugin.java | 6 +-- .../luckperms/sponge/LPSpongeBootstrap.java | 7 ++- .../luckperms/sponge/LPSpongePlugin.java | 6 +-- 16 files changed, 88 insertions(+), 76 deletions(-) create mode 100644 common/src/main/java/me/lucko/luckperms/common/node/HeldPermissionComparator.java diff --git a/bukkit/src/main/java/me/lucko/luckperms/bukkit/LPBukkitBootstrap.java b/bukkit/src/main/java/me/lucko/luckperms/bukkit/LPBukkitBootstrap.java index f2161561..0f6b7909 100644 --- a/bukkit/src/main/java/me/lucko/luckperms/bukkit/LPBukkitBootstrap.java +++ b/bukkit/src/main/java/me/lucko/luckperms/bukkit/LPBukkitBootstrap.java @@ -206,10 +206,9 @@ public class LPBukkitBootstrap extends JavaPlugin implements LuckPermsBootstrap return getResource(path); } - @Nullable @Override - public Player getPlayer(UUID uuid) { - return getServer().getPlayer(uuid); + public Optional getPlayer(UUID uuid) { + return Optional.ofNullable(getServer().getPlayer(uuid)); } @Override diff --git a/bukkit/src/main/java/me/lucko/luckperms/bukkit/LPBukkitPlugin.java b/bukkit/src/main/java/me/lucko/luckperms/bukkit/LPBukkitPlugin.java index 49cbf5ad..416cba9f 100644 --- a/bukkit/src/main/java/me/lucko/luckperms/bukkit/LPBukkitPlugin.java +++ b/bukkit/src/main/java/me/lucko/luckperms/bukkit/LPBukkitPlugin.java @@ -311,11 +311,7 @@ public class LPBukkitPlugin extends AbstractLuckPermsPlugin { @Override public Optional getContextForUser(User user) { - Player player = this.bootstrap.getPlayer(user.getUuid()); - if (player == null) { - return Optional.empty(); - } - return Optional.of(this.contextManager.getApplicableContexts(player)); + return this.bootstrap.getPlayer(user.getUuid()).map(player -> this.contextManager.getApplicableContexts(player)); } @Override 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 062acfef..6fcde8c7 100644 --- a/bungee/src/main/java/me/lucko/luckperms/bungee/BungeeSenderFactory.java +++ b/bungee/src/main/java/me/lucko/luckperms/bungee/BungeeSenderFactory.java @@ -36,6 +36,7 @@ import net.kyori.text.Component; import net.kyori.text.serializer.ComponentSerializers; import net.md_5.bungee.api.CommandSender; import net.md_5.bungee.api.connection.ProxiedPlayer; +import net.md_5.bungee.chat.ComponentSerializer; import java.util.UUID; @@ -67,7 +68,7 @@ public class BungeeSenderFactory extends SenderFactory { @Override protected void sendMessage(CommandSender sender, Component message) { - sender.sendMessage(net.md_5.bungee.chat.ComponentSerializer.parse(ComponentSerializers.JSON.serialize(message))); + sender.sendMessage(ComponentSerializer.parse(ComponentSerializers.JSON.serialize(message))); } @Override diff --git a/bungee/src/main/java/me/lucko/luckperms/bungee/LPBungeeBootstrap.java b/bungee/src/main/java/me/lucko/luckperms/bungee/LPBungeeBootstrap.java index 36988003..2fb1840d 100644 --- a/bungee/src/main/java/me/lucko/luckperms/bungee/LPBungeeBootstrap.java +++ b/bungee/src/main/java/me/lucko/luckperms/bungee/LPBungeeBootstrap.java @@ -166,10 +166,9 @@ public class LPBungeeBootstrap extends Plugin implements LuckPermsBootstrap { return getResourceAsStream(path); } - @Nullable @Override - public ProxiedPlayer getPlayer(UUID uuid) { - return getProxy().getPlayer(uuid); + public Optional getPlayer(UUID uuid) { + return Optional.ofNullable(getProxy().getPlayer(uuid)); } @Override diff --git a/bungee/src/main/java/me/lucko/luckperms/bungee/LPBungeePlugin.java b/bungee/src/main/java/me/lucko/luckperms/bungee/LPBungeePlugin.java index 6905b6ae..0061d6b6 100644 --- a/bungee/src/main/java/me/lucko/luckperms/bungee/LPBungeePlugin.java +++ b/bungee/src/main/java/me/lucko/luckperms/bungee/LPBungeePlugin.java @@ -179,11 +179,7 @@ public class LPBungeePlugin extends AbstractLuckPermsPlugin { @Override public Optional getContextForUser(User user) { - ProxiedPlayer player = this.bootstrap.getPlayer(user.getUuid()); - if (player == null) { - return Optional.empty(); - } - return Optional.of(this.contextManager.getApplicableContexts(player)); + return this.bootstrap.getPlayer(user.getUuid()).map(player -> this.contextManager.getApplicableContexts(player)); } @Override diff --git a/common/src/main/java/me/lucko/luckperms/common/commands/group/GroupListMembers.java b/common/src/main/java/me/lucko/luckperms/common/commands/group/GroupListMembers.java index 7ccaab0a..742058e2 100644 --- a/common/src/main/java/me/lucko/luckperms/common/commands/group/GroupListMembers.java +++ b/common/src/main/java/me/lucko/luckperms/common/commands/group/GroupListMembers.java @@ -42,6 +42,7 @@ import me.lucko.luckperms.common.locale.LocaleManager; import me.lucko.luckperms.common.locale.command.CommandSpec; import me.lucko.luckperms.common.locale.message.Message; import me.lucko.luckperms.common.model.Group; +import me.lucko.luckperms.common.node.HeldPermissionComparator; import me.lucko.luckperms.common.node.NodeFactory; import me.lucko.luckperms.common.plugin.LuckPermsPlugin; import me.lucko.luckperms.common.references.HolderType; @@ -108,18 +109,9 @@ public class GroupListMembers extends SubCommand { return CommandResult.SUCCESS; } - private static void sendResult(Sender sender, List> results, Function lookupFunction, Message headerMessage, HolderType holderType, String label, int page) { + private static > void sendResult(Sender sender, List> results, Function lookupFunction, Message headerMessage, HolderType holderType, String label, int page) { results = new ArrayList<>(results); - - // we need a deterministic sort order - // even though we're comparing uuids here in some cases - it doesn't matter - // the import thing is that the order is the same each time the command is executed - results.sort((o1, o2) -> { - Comparable h1 = (Comparable) o1.getHolder(); - Comparable h2 = (Comparable) o2.getHolder(); - //noinspection unchecked - return h1.compareTo(h2); - }); + results.sort(HeldPermissionComparator.normal()); int pageIndex = page - 1; List>> pages = Iterators.divideIterable(results, 15); diff --git a/common/src/main/java/me/lucko/luckperms/common/commands/misc/SearchCommand.java b/common/src/main/java/me/lucko/luckperms/common/commands/misc/SearchCommand.java index d4dd52f6..0869519c 100644 --- a/common/src/main/java/me/lucko/luckperms/common/commands/misc/SearchCommand.java +++ b/common/src/main/java/me/lucko/luckperms/common/commands/misc/SearchCommand.java @@ -41,7 +41,9 @@ import me.lucko.luckperms.common.command.utils.TabCompletions; import me.lucko.luckperms.common.locale.LocaleManager; import me.lucko.luckperms.common.locale.command.CommandSpec; import me.lucko.luckperms.common.locale.message.Message; +import me.lucko.luckperms.common.node.HeldPermissionComparator; import me.lucko.luckperms.common.node.NodeFactory; +import me.lucko.luckperms.common.node.NodeHeldPermission; import me.lucko.luckperms.common.plugin.LuckPermsPlugin; import me.lucko.luckperms.common.references.HolderType; import me.lucko.luckperms.common.sender.Sender; @@ -107,18 +109,9 @@ public class SearchCommand extends SingleCommand { return TabCompletions.getPermissionTabComplete(args, plugin.getPermissionRegistry()); } - private static void sendResult(Sender sender, List> results, Function lookupFunction, Message headerMessage, HolderType holderType, String label, int page) { + private static > void sendResult(Sender sender, List> results, Function lookupFunction, Message headerMessage, HolderType holderType, String label, int page) { results = new ArrayList<>(results); - - // we need a deterministic sort order - // even though we're comparing uuids here in some cases - it doesn't matter - // the import thing is that the order is the same each time the command is executed - results.sort((o1, o2) -> { - Comparable h1 = (Comparable) o1.getHolder(); - Comparable h2 = (Comparable) o2.getHolder(); - //noinspection unchecked - return h1.compareTo(h2); - }); + results.sort(HeldPermissionComparator.normal()); int pageIndex = page - 1; List>> pages = Iterators.divideIterable(results, 15); diff --git a/common/src/main/java/me/lucko/luckperms/common/node/HeldPermissionComparator.java b/common/src/main/java/me/lucko/luckperms/common/node/HeldPermissionComparator.java new file mode 100644 index 00000000..e1496061 --- /dev/null +++ b/common/src/main/java/me/lucko/luckperms/common/node/HeldPermissionComparator.java @@ -0,0 +1,51 @@ +/* + * 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.common.node; + +import me.lucko.luckperms.api.HeldPermission; + +import java.util.Comparator; + +public class HeldPermissionComparator> implements Comparator> { + + public static > Comparator> normal() { + return new HeldPermissionComparator<>(); + } + + public static > Comparator> reverse() { + return HeldPermissionComparator.normal().reversed(); + } + + @Override + public int compare(HeldPermission o1, HeldPermission o2) { + int i = o1.getHolder().compareTo(o2.getHolder()); + if (i != 0) { + return i; + } + + return NodeWithContextComparator.normal().compare(o1.asNode(), o2.asNode()); + } +} diff --git a/common/src/main/java/me/lucko/luckperms/common/node/NodeComparator.java b/common/src/main/java/me/lucko/luckperms/common/node/NodeComparator.java index 9e2fc8a4..6e30faa5 100644 --- a/common/src/main/java/me/lucko/luckperms/common/node/NodeComparator.java +++ b/common/src/main/java/me/lucko/luckperms/common/node/NodeComparator.java @@ -32,14 +32,14 @@ import java.util.Comparator; public class NodeComparator implements Comparator { - private static final Comparator INSTANCE = new NodeComparator(); - private static final Comparator REVERSE = INSTANCE.reversed(); + private static final Comparator INSTANCE = new NodeComparator(); + private static final Comparator REVERSE = INSTANCE.reversed(); - public static Comparator normal() { + public static Comparator normal() { return INSTANCE; } - public static Comparator reverse() { + public static Comparator reverse() { return REVERSE; } @@ -67,5 +67,4 @@ public class NodeComparator implements Comparator { return CollationKeyCache.compareStrings(o1.getPermission(), o2.getPermission()) == 1 ? -1 : 1; } - } diff --git a/common/src/main/java/me/lucko/luckperms/common/node/NodeHeldPermission.java b/common/src/main/java/me/lucko/luckperms/common/node/NodeHeldPermission.java index 712fb281..ff57a888 100644 --- a/common/src/main/java/me/lucko/luckperms/common/node/NodeHeldPermission.java +++ b/common/src/main/java/me/lucko/luckperms/common/node/NodeHeldPermission.java @@ -31,15 +31,16 @@ import me.lucko.luckperms.api.context.ContextSet; import java.util.Optional; import java.util.OptionalLong; +import java.util.UUID; import javax.annotation.Nonnull; -public final class NodeHeldPermission implements HeldPermission { - public static NodeHeldPermission of(T holder, NodeModel nodeModel) { +public final class NodeHeldPermission> implements HeldPermission { + public static > NodeHeldPermission of(T holder, NodeModel nodeModel) { return of(holder, nodeModel.toNode()); } - public static NodeHeldPermission of(T holder, Node node) { + public static > NodeHeldPermission of(T holder, Node node) { return new NodeHeldPermission<>(holder, node); } diff --git a/common/src/main/java/me/lucko/luckperms/common/node/NodeWithContextComparator.java b/common/src/main/java/me/lucko/luckperms/common/node/NodeWithContextComparator.java index 14c7eeb6..c0c45c55 100644 --- a/common/src/main/java/me/lucko/luckperms/common/node/NodeWithContextComparator.java +++ b/common/src/main/java/me/lucko/luckperms/common/node/NodeWithContextComparator.java @@ -25,7 +25,6 @@ package me.lucko.luckperms.common.node; -import me.lucko.luckperms.api.LocalizedNode; import me.lucko.luckperms.api.Node; import me.lucko.luckperms.common.utils.CollationKeyCache; @@ -34,26 +33,23 @@ import java.util.Comparator; /** * Compares permission nodes based upon their supposed "priority". */ -public class NodeWithContextComparator implements Comparator { +public class NodeWithContextComparator implements Comparator { - private static final Comparator INSTANCE = new NodeWithContextComparator(); - private static final Comparator REVERSE = INSTANCE.reversed(); + private static final Comparator INSTANCE = new NodeWithContextComparator(); + private static final Comparator REVERSE = INSTANCE.reversed(); - public static Comparator normal() { + public static Comparator normal() { return INSTANCE; } - public static Comparator reverse() { + public static Comparator reverse() { return REVERSE; } private NodeWithContextComparator() {} @Override - public int compare(LocalizedNode one, LocalizedNode two) { - Node o1 = one.getNode(); - Node o2 = two.getNode(); - + public int compare(Node o1, Node o2) { if (o1.equals(o2)) { return 0; } diff --git a/common/src/main/java/me/lucko/luckperms/common/plugin/bootstrap/LuckPermsBootstrap.java b/common/src/main/java/me/lucko/luckperms/common/plugin/bootstrap/LuckPermsBootstrap.java index bbfe22de..9f07d23f 100644 --- a/common/src/main/java/me/lucko/luckperms/common/plugin/bootstrap/LuckPermsBootstrap.java +++ b/common/src/main/java/me/lucko/luckperms/common/plugin/bootstrap/LuckPermsBootstrap.java @@ -158,8 +158,7 @@ public interface LuckPermsBootstrap { * @param uuid the users unique id * @return a player object, or null, if one couldn't be found. */ - @Nullable - Object getPlayer(UUID uuid); + Optional getPlayer(UUID uuid); /** * Lookup a uuid from a username, using the servers internal uuid cache. diff --git a/nukkit/src/main/java/me/lucko/luckperms/nukkit/LPNukkitBootstrap.java b/nukkit/src/main/java/me/lucko/luckperms/nukkit/LPNukkitBootstrap.java index ece3fb04..2345b4b4 100644 --- a/nukkit/src/main/java/me/lucko/luckperms/nukkit/LPNukkitBootstrap.java +++ b/nukkit/src/main/java/me/lucko/luckperms/nukkit/LPNukkitBootstrap.java @@ -169,10 +169,9 @@ public class LPNukkitBootstrap extends PluginBase implements LuckPermsBootstrap return getResource(path); } - @Nullable @Override - public Player getPlayer(UUID uuid) { - return getServer().getOnlinePlayers().get(uuid); + public Optional getPlayer(UUID uuid) { + return Optional.ofNullable(getServer().getOnlinePlayers().get(uuid)); } @Override diff --git a/nukkit/src/main/java/me/lucko/luckperms/nukkit/LPNukkitPlugin.java b/nukkit/src/main/java/me/lucko/luckperms/nukkit/LPNukkitPlugin.java index 36239a3b..9742deb0 100644 --- a/nukkit/src/main/java/me/lucko/luckperms/nukkit/LPNukkitPlugin.java +++ b/nukkit/src/main/java/me/lucko/luckperms/nukkit/LPNukkitPlugin.java @@ -280,11 +280,7 @@ public class LPNukkitPlugin extends AbstractLuckPermsPlugin { @Override public Optional getContextForUser(User user) { - Player player = this.bootstrap.getPlayer(user.getUuid()); - if (player == null) { - return Optional.empty(); - } - return Optional.of(this.contextManager.getApplicableContexts(player)); + return this.bootstrap.getPlayer(user.getUuid()).map(player -> this.contextManager.getApplicableContexts(player)); } @Override diff --git a/sponge/src/main/java/me/lucko/luckperms/sponge/LPSpongeBootstrap.java b/sponge/src/main/java/me/lucko/luckperms/sponge/LPSpongeBootstrap.java index f5247359..d185e1b0 100644 --- a/sponge/src/main/java/me/lucko/luckperms/sponge/LPSpongeBootstrap.java +++ b/sponge/src/main/java/me/lucko/luckperms/sponge/LPSpongeBootstrap.java @@ -262,14 +262,13 @@ public class LPSpongeBootstrap implements LuckPermsBootstrap { return getClass().getClassLoader().getResourceAsStream(path); } - @Nullable @Override - public Player getPlayer(UUID uuid) { + public Optional getPlayer(UUID uuid) { if (!getGame().isServerAvailable()) { - return null; + return Optional.empty(); } - return getGame().getServer().getPlayer(uuid).orElse(null); + return getGame().getServer().getPlayer(uuid); } @Override diff --git a/sponge/src/main/java/me/lucko/luckperms/sponge/LPSpongePlugin.java b/sponge/src/main/java/me/lucko/luckperms/sponge/LPSpongePlugin.java index d14b913f..25c41599 100644 --- a/sponge/src/main/java/me/lucko/luckperms/sponge/LPSpongePlugin.java +++ b/sponge/src/main/java/me/lucko/luckperms/sponge/LPSpongePlugin.java @@ -229,11 +229,7 @@ public class LPSpongePlugin extends AbstractLuckPermsPlugin { @Override public Optional getContextForUser(User user) { - Player player = this.bootstrap.getPlayer(user.getUuid()); - if (player == null) { - return Optional.empty(); - } - return Optional.of(this.contextManager.getApplicableContexts(player)); + return this.bootstrap.getPlayer(user.getUuid()).map(player -> this.contextManager.getApplicableContexts(player)); } @Override