Minor misc changes

This commit is contained in:
Luck 2018-04-08 22:58:15 +01:00
parent f5c6b9e3d4
commit cb185e4827
No known key found for this signature in database
GPG Key ID: EFA9B3EC5FD90F8B
16 changed files with 88 additions and 76 deletions

View File

@ -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<Player> getPlayer(UUID uuid) {
return Optional.ofNullable(getServer().getPlayer(uuid));
}
@Override

View File

@ -311,11 +311,7 @@ public class LPBukkitPlugin extends AbstractLuckPermsPlugin {
@Override
public Optional<Contexts> 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

View File

@ -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<CommandSender> {
@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

View File

@ -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<ProxiedPlayer> getPlayer(UUID uuid) {
return Optional.ofNullable(getProxy().getPlayer(uuid));
}
@Override

View File

@ -179,11 +179,7 @@ public class LPBungeePlugin extends AbstractLuckPermsPlugin {
@Override
public Optional<Contexts> 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

View File

@ -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<Group> {
return CommandResult.SUCCESS;
}
private static <T> void sendResult(Sender sender, List<HeldPermission<T>> results, Function<T, String> lookupFunction, Message headerMessage, HolderType holderType, String label, int page) {
private static <T extends Comparable<T>> void sendResult(Sender sender, List<HeldPermission<T>> results, Function<T, String> 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<List<HeldPermission<T>>> pages = Iterators.divideIterable(results, 15);

View File

@ -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 <T> void sendResult(Sender sender, List<HeldPermission<T>> results, Function<T, String> lookupFunction, Message headerMessage, HolderType holderType, String label, int page) {
private static <T extends Comparable<T>> void sendResult(Sender sender, List<HeldPermission<T>> results, Function<T, String> 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<List<HeldPermission<T>>> pages = Iterators.divideIterable(results, 15);

View File

@ -0,0 +1,51 @@
/*
* 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.common.node;
import me.lucko.luckperms.api.HeldPermission;
import java.util.Comparator;
public class HeldPermissionComparator<T extends Comparable<T>> implements Comparator<HeldPermission<T>> {
public static <T extends Comparable<T>> Comparator<? super HeldPermission<T>> normal() {
return new HeldPermissionComparator<>();
}
public static <T extends Comparable<T>> Comparator<? super HeldPermission<T>> reverse() {
return HeldPermissionComparator.<T>normal().reversed();
}
@Override
public int compare(HeldPermission<T> o1, HeldPermission<T> o2) {
int i = o1.getHolder().compareTo(o2.getHolder());
if (i != 0) {
return i;
}
return NodeWithContextComparator.normal().compare(o1.asNode(), o2.asNode());
}
}

View File

@ -32,14 +32,14 @@ import java.util.Comparator;
public class NodeComparator implements Comparator<Node> {
private static final Comparator<Node> INSTANCE = new NodeComparator();
private static final Comparator<Node> REVERSE = INSTANCE.reversed();
private static final Comparator<? super Node> INSTANCE = new NodeComparator();
private static final Comparator<? super Node> REVERSE = INSTANCE.reversed();
public static Comparator<Node> normal() {
public static Comparator<? super Node> normal() {
return INSTANCE;
}
public static Comparator<Node> reverse() {
public static Comparator<? super Node> reverse() {
return REVERSE;
}
@ -67,5 +67,4 @@ public class NodeComparator implements Comparator<Node> {
return CollationKeyCache.compareStrings(o1.getPermission(), o2.getPermission()) == 1 ? -1 : 1;
}
}

View File

@ -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<T> implements HeldPermission<T> {
public static <T> NodeHeldPermission<T> of(T holder, NodeModel nodeModel) {
public final class NodeHeldPermission<T extends Comparable<T>> implements HeldPermission<T> {
public static <T extends Comparable<T>> NodeHeldPermission<T> of(T holder, NodeModel nodeModel) {
return of(holder, nodeModel.toNode());
}
public static <T> NodeHeldPermission<T> of(T holder, Node node) {
public static <T extends Comparable<T>> NodeHeldPermission<T> of(T holder, Node node) {
return new NodeHeldPermission<>(holder, node);
}

View File

@ -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<LocalizedNode> {
public class NodeWithContextComparator implements Comparator<Node> {
private static final Comparator<LocalizedNode> INSTANCE = new NodeWithContextComparator();
private static final Comparator<LocalizedNode> REVERSE = INSTANCE.reversed();
private static final Comparator<? super Node> INSTANCE = new NodeWithContextComparator();
private static final Comparator<? super Node> REVERSE = INSTANCE.reversed();
public static Comparator<LocalizedNode> normal() {
public static Comparator<? super Node> normal() {
return INSTANCE;
}
public static Comparator<LocalizedNode> reverse() {
public static Comparator<? super Node> 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;
}

View File

@ -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.

View File

@ -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<Player> getPlayer(UUID uuid) {
return Optional.ofNullable(getServer().getOnlinePlayers().get(uuid));
}
@Override

View File

@ -280,11 +280,7 @@ public class LPNukkitPlugin extends AbstractLuckPermsPlugin {
@Override
public Optional<Contexts> 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

View File

@ -262,14 +262,13 @@ public class LPSpongeBootstrap implements LuckPermsBootstrap {
return getClass().getClassLoader().getResourceAsStream(path);
}
@Nullable
@Override
public Player getPlayer(UUID uuid) {
public Optional<Player> getPlayer(UUID uuid) {
if (!getGame().isServerAvailable()) {
return null;
return Optional.empty();
}
return getGame().getServer().getPlayer(uuid).orElse(null);
return getGame().getServer().getPlayer(uuid);
}
@Override

View File

@ -229,11 +229,7 @@ public class LPSpongePlugin extends AbstractLuckPermsPlugin {
@Override
public Optional<Contexts> 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