Change Sender#getNameWithLocation behaviour

This commit is contained in:
Luck 2019-05-14 20:32:35 +01:00
parent 1ebed37297
commit 2e16b15b0e
No known key found for this signature in database
GPG Key ID: EFA9B3EC5FD90F8B
2 changed files with 18 additions and 63 deletions

View File

@ -1,55 +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.common.context;
import me.lucko.luckperms.api.Contexts;
import me.lucko.luckperms.api.context.ContextSet;
import java.util.Map;
import java.util.Optional;
import java.util.Set;
import java.util.stream.Collectors;
public final class ContextSetFormatter {
private ContextSetFormatter() {}
public static Optional<String> toMinimalString(ContextSet contextSet) {
Set<Map.Entry<String, String>> entries = contextSet.toSet();
if (entries.isEmpty()) {
return Optional.empty();
}
// effectively: if entries contains any non-server keys
if (entries.stream().anyMatch(pair -> !pair.getKey().equals(Contexts.SERVER_KEY))) {
// return all entries in 'key=value' form
return Optional.of(entries.stream().map(pair -> pair.getKey() + "=" + pair.getValue()).collect(Collectors.joining(";")));
} else {
// just return the server ids, without the 'server='
return Optional.of(entries.stream().map(Map.Entry::getValue).collect(Collectors.joining(";")));
}
}
}

View File

@ -25,16 +25,19 @@
package me.lucko.luckperms.common.sender;
import me.lucko.luckperms.api.Contexts;
import me.lucko.luckperms.api.Tristate;
import me.lucko.luckperms.api.context.ImmutableContextSet;
import me.lucko.luckperms.common.command.CommandManager;
import me.lucko.luckperms.common.command.access.CommandPermission;
import me.lucko.luckperms.common.context.ContextManager;
import me.lucko.luckperms.common.context.ContextSetFormatter;
import me.lucko.luckperms.common.plugin.LuckPermsPlugin;
import net.kyori.text.Component;
import java.util.Set;
import java.util.UUID;
import java.util.stream.Collectors;
/**
* Wrapper interface to represent a CommandSender/CommandSource within the common command implementations.
@ -78,17 +81,24 @@ public interface Sender {
return name;
}
String location = ContextSetFormatter.toMinimalString(contextManager.getStaticContext()).orElse(null);
if (location == null) {
ImmutableContextSet staticContext = contextManager.getStaticContext();
String location;
if (staticContext.isEmpty()) {
return name;
} else if (staticContext.size() == 1) {
location = staticContext.iterator().next().getValue();
} else {
Set<String> servers = staticContext.getValues(Contexts.SERVER_KEY);
if (servers.size() == 1) {
location = servers.iterator().next();
} else {
location = staticContext.toSet().stream().map(pair -> pair.getKey() + "=" + pair.getValue()).collect(Collectors.joining(";"));
}
}
if (isConsole()) {
return name.toLowerCase() + "@" + location;
} else {
return name + "@" + location;
}
}
/**
* Gets the sender's unique id.