Add Sender#getNameWithLocation

This commit is contained in:
Luck 2017-11-02 18:51:59 +00:00
parent 0f10bb0bc9
commit b7541c43c8
No known key found for this signature in database
GPG Key ID: EFA9B3EC5FD90F8B
13 changed files with 113 additions and 126 deletions

View File

@ -54,7 +54,7 @@ public class BukkitSenderFactory extends SenderFactory<CommandSender> {
if (sender instanceof Player) {
return sender.getName();
}
return getConsoleName();
return Constants.CONSOLE_NAME;
}
@Override

View File

@ -50,7 +50,7 @@ public class BungeeSenderFactory extends SenderFactory<CommandSender> {
if (sender instanceof ProxiedPlayer) {
return sender.getName();
}
return getConsoleName();
return Constants.CONSOLE_NAME;
}
@Override

View File

@ -108,7 +108,7 @@ public class ExtendedLogEntry extends LogEntry {
}
public ExtendedLogEntryBuilder actor(Sender actor) {
super.actorName(actor.getName());
super.actorName(actor.getNameWithLocation());
super.actor(actor.getUuid());
return this;
}

View File

@ -93,7 +93,7 @@ public class Exporter implements Runnable {
log.log("Starting.");
write(writer, "# LuckPerms Export File");
write(writer, "# Generated by " + executor.getName() + " at " + DATE_FORMAT.format(new Date(System.currentTimeMillis())));
write(writer, "# Generated by " + executor.getNameWithLocation() + " at " + DATE_FORMAT.format(new Date(System.currentTimeMillis())));
write(writer, "");
// Export Groups

View File

@ -25,12 +25,10 @@
package me.lucko.luckperms.common.backup;
import lombok.AllArgsConstructor;
import lombok.Getter;
import me.lucko.luckperms.api.Tristate;
import me.lucko.luckperms.common.commands.sender.Sender;
import me.lucko.luckperms.common.config.ConfigKeys;
import me.lucko.luckperms.common.constants.CommandPermission;
import me.lucko.luckperms.common.constants.Constants;
import me.lucko.luckperms.common.plugin.LuckPermsPlugin;
import me.lucko.luckperms.common.utils.TextUtils;
@ -39,30 +37,28 @@ import net.kyori.text.Component;
import java.util.UUID;
@AllArgsConstructor
@Getter
public abstract class ImporterSender implements Sender {
private final LuckPermsPlugin plugin;
private final LuckPermsPlugin platform;
private final UUID uuid;
private final String name;
public ImporterSender(LuckPermsPlugin plugin, UUID uuid, String name) {
this.platform = plugin;
this.uuid = uuid;
this.name = name;
}
public ImporterSender(LuckPermsPlugin plugin) {
this(plugin, Constants.IMPORT_UUID, Constants.IMPORT_NAME);
}
protected abstract void consumeMessage(String s);
@Override
public LuckPermsPlugin getPlatform() {
return plugin;
}
@Override
public String getName() {
return Constants.IMPORT_NAME.apply(plugin.getConfiguration().get(ConfigKeys.SERVER));
}
@Override
public UUID getUuid() {
return Constants.IMPORT_UUID;
}
@Override
public void sendMessage(String s) {
consumeMessage(s);
public void sendMessage(String message) {
consumeMessage(message);
}
@SuppressWarnings("deprecation")
@ -81,23 +77,4 @@ public abstract class ImporterSender implements Sender {
return true;
}
@Override
public boolean hasPermission(CommandPermission permission) {
return true;
}
@Override
public boolean isConsole() {
return true;
}
@Override
public boolean isImport() {
return true;
}
@Override
public boolean isValid() {
return true;
}
}

View File

@ -88,7 +88,7 @@ public class HolderEditor<T extends PermissionHolder> extends SubCommand<T> {
Set<NodeModel> nodes = holder.getEnduringNodes().values().stream().map(NodeModel::fromNode).collect(Collectors.toCollection(LinkedHashSet::new));
data.addProperty("who", id(holder));
data.addProperty("cmdAlias", label);
data.addProperty("uploadedBy", sender.getName());
data.addProperty("uploadedBy", sender.getNameWithLocation());
data.addProperty("time", System.currentTimeMillis());
data.add("nodes", serializePermissions(nodes));

View File

@ -25,20 +25,20 @@
package me.lucko.luckperms.common.commands.sender;
import lombok.AccessLevel;
import lombok.EqualsAndHashCode;
import lombok.Getter;
import com.google.common.base.Splitter;
import me.lucko.luckperms.api.Tristate;
import me.lucko.luckperms.common.constants.CommandPermission;
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 java.lang.ref.WeakReference;
import java.util.Optional;
import java.util.UUID;
/**
@ -52,31 +52,36 @@ public final class AbstractSender<T> implements Sender {
private static final Splitter NEW_LINE_SPLITTER = Splitter.on("\n");
private final LuckPermsPlugin platform;
@Getter(AccessLevel.NONE)
private final SenderFactory<T> factory;
private final WeakReference<T> ref;
private final String name;
@Getter(AccessLevel.NONE)
private final WeakReference<T> reference;
private final UUID uuid;
private final String name;
AbstractSender(LuckPermsPlugin platform, SenderFactory<T> factory, T t) {
this.platform = platform;
this.factory = factory;
this.ref = new WeakReference<>(t);
this.name = factory.getName(t);
this.reference = new WeakReference<>(t);
this.uuid = factory.getUuid(t);
this.name = factory.getName(t);
}
@Override
public void sendMessage(String s) {
final T t = ref.get();
public void sendMessage(String message) {
final T t = reference.get();
if (t != null) {
if (!isConsole()) {
factory.sendMessage(t, s);
factory.sendMessage(t, message);
return;
}
// if it is console, split up the lines and send individually.
for (String line : NEW_LINE_SPLITTER.split(s)) {
for (String line : NEW_LINE_SPLITTER.split(message)) {
factory.sendMessage(t, line);
}
}
@ -90,7 +95,7 @@ public final class AbstractSender<T> implements Sender {
return;
}
final T t = ref.get();
final T t = reference.get();
if (t != null) {
factory.sendMessage(t, message);
}
@ -98,49 +103,33 @@ public final class AbstractSender<T> implements Sender {
@Override
public Tristate getPermissionValue(String permission) {
if (isConsole()) return Tristate.TRUE;
T t = ref.get();
T t = reference.get();
if (t != null) {
return factory.getPermissionValue(t, permission);
}
return Tristate.UNDEFINED;
return isConsole() ? Tristate.TRUE : Tristate.UNDEFINED;
}
@Override
public boolean hasPermission(String permission) {
if (isConsole()) return true;
T t = ref.get();
T t = reference.get();
if (t != null) {
if (factory.hasPermission(t, permission)) {
return true;
}
}
return false;
}
@Override
public boolean hasPermission(CommandPermission permission) {
return hasPermission(permission.getPermission());
}
@Override
public boolean isConsole() {
return this.uuid.equals(Constants.CONSOLE_UUID) || this.uuid.equals(Constants.IMPORT_UUID);
}
@Override
public boolean isImport() {
// the importer uses it's own instance of Sender
return false;
return isConsole();
}
@Override
public boolean isValid() {
return ref.get() != null;
return reference.get() != null;
}
@Override
public Optional<Object> getHandle() {
return Optional.ofNullable(reference.get());
}
}

View File

@ -26,12 +26,15 @@
package me.lucko.luckperms.common.commands.sender;
import me.lucko.luckperms.api.Tristate;
import me.lucko.luckperms.common.config.ConfigKeys;
import me.lucko.luckperms.common.config.LuckPermsConfiguration;
import me.lucko.luckperms.common.constants.CommandPermission;
import me.lucko.luckperms.common.constants.Constants;
import me.lucko.luckperms.common.plugin.LuckPermsPlugin;
import net.kyori.text.Component;
import java.util.Optional;
import java.util.UUID;
/**
@ -53,6 +56,37 @@ public interface Sender {
*/
String getName();
/**
* Gets a string representing the senders username, and their current location
* within the network.
*
* @return a friendly identifier for the sender
*/
default String getNameWithLocation() {
String name = getName();
LuckPermsConfiguration config = getPlatform().getConfiguration();
if (config == null) {
return name;
}
String location = config.get(ConfigKeys.SERVER);
if (location == null || location.equalsIgnoreCase("global")) {
location = "";
}
if (!location.isEmpty()) {
location = "@" + location;
}
if (isConsole() && !location.isEmpty()) {
name = name.toLowerCase();
}
return name + location;
}
/**
* Gets the sender's unique id. See {@link Constants#CONSOLE_UUID} for the console's UUID representation.
*
@ -63,9 +97,9 @@ public interface Sender {
/**
* Send a message back to the Sender
*
* @param s the message to send. Supports '§' for message formatting.
* @param message the message to send. Supports '§' for message formatting.
*/
void sendMessage(String s);
void sendMessage(String message);
/**
* Send a json message to the Sender.
@ -96,27 +130,45 @@ public interface Sender {
* @param permission the permission to check for
* @return true if the sender has the permission
*/
boolean hasPermission(CommandPermission permission);
default boolean hasPermission(CommandPermission permission) {
return hasPermission(permission.getPermission());
}
/**
* Gets whether this sender is the console
*
* @return if the sender is the console
*/
boolean isConsole();
default boolean isConsole() {
return Constants.CONSOLE_UUID.equals(getUuid()) || Constants.IMPORT_UUID.equals(getUuid());
}
/**
* Gets whether this sender is an import process
*
* @return if the sender is an import process
*/
boolean isImport();
default boolean isImport() {
return Constants.IMPORT_UUID.equals(getUuid());
}
/**
* Gets whether this sender is still valid & receiving messages.
*
* @return if this sender is valid
*/
boolean isValid();
default boolean isValid() {
return true;
}
/**
* Gets the handle object for this sender. (In most cases, the real
* CommandSender/CommandSource object from the platform)
*
* @return the handle
*/
default Optional<Object> getHandle() {
return Optional.empty();
}
}

View File

@ -31,9 +31,6 @@ import lombok.NonNull;
import lombok.RequiredArgsConstructor;
import me.lucko.luckperms.api.Tristate;
import me.lucko.luckperms.common.config.ConfigKeys;
import me.lucko.luckperms.common.config.LuckPermsConfiguration;
import me.lucko.luckperms.common.constants.Constants;
import me.lucko.luckperms.common.plugin.LuckPermsPlugin;
import net.kyori.text.Component;
@ -51,10 +48,10 @@ public abstract class SenderFactory<T> {
@Getter(AccessLevel.PROTECTED)
private final LuckPermsPlugin plugin;
protected abstract String getName(T t);
protected abstract UUID getUuid(T t);
protected abstract String getName(T t);
protected abstract void sendMessage(T t, String s);
protected abstract void sendMessage(T t, Component message);
@ -63,16 +60,6 @@ public abstract class SenderFactory<T> {
protected abstract boolean hasPermission(T t, String node);
protected String getConsoleName() {
LuckPermsConfiguration config = getPlugin().getConfiguration();
return config == null ? Constants.CONSOLE_NAME.apply(null) : Constants.CONSOLE_NAME.apply(config.get(ConfigKeys.SERVER));
}
protected String getImportName() {
LuckPermsConfiguration config = getPlugin().getConfiguration();
return config == null ? Constants.IMPORT_NAME.apply(null) : Constants.IMPORT_NAME.apply(config.get(ConfigKeys.SERVER));
}
public final Sender wrap(@NonNull T sender) {
return new AbstractSender<>(plugin, this, sender);
}

View File

@ -28,7 +28,6 @@ package me.lucko.luckperms.common.constants;
import lombok.experimental.UtilityClass;
import java.util.UUID;
import java.util.function.Function;
/**
* Random constants used throughout the plugin implementation.
@ -37,10 +36,10 @@ import java.util.function.Function;
public class Constants {
public static final UUID CONSOLE_UUID = UUID.fromString("00000000-0000-0000-0000-000000000000");
public static final Function<String, String> CONSOLE_NAME = s -> s == null || s.equalsIgnoreCase("global") ? "Console" : "console@" + s;
public static final String CONSOLE_NAME = "Console";
public static final UUID IMPORT_UUID = UUID.fromString("11111111-1111-1111-1111-111111111111");
public static final Function<String, String> IMPORT_NAME = s -> s == null || s.equalsIgnoreCase("global") ? "Import" : "import@" + s;
public static final String IMPORT_NAME = "Import";
public static final char COLOR_CHAR = '\u00A7';
public static final char FORMAT_CHAR = '&';

View File

@ -194,7 +194,7 @@ public class VerboseListener {
.add("| End Time | " + endDate + " |")
.add("| Duration | " + duration +" |")
.add("| Count | **" + matchedCounter.get() + "** / " + counter.get() + " |")
.add("| User | " + notifiedSender.getName() + " |")
.add("| User | " + notifiedSender.getNameWithLocation() + " |")
.add("| Filter | " + filter + " |")
.add("| Include traces | " + showTraces + " |")
.add("");

View File

@ -456,24 +456,7 @@ public class LPSpongePlugin implements LuckPermsPlugin {
@Override
public Sender getConsoleSender() {
if (!game.isServerAvailable()) {
return new ImporterSender(this) {
@Override
public String getName() {
LuckPermsConfiguration config = getPlatform().getConfiguration();
return config == null ? Constants.CONSOLE_NAME.apply(null) : Constants.CONSOLE_NAME.apply(config.get(ConfigKeys.SERVER));
}
@Override
public UUID getUuid() {
return Constants.CONSOLE_UUID;
}
@Override
public boolean isImport() {
return false;
}
return new ImporterSender(this, Constants.CONSOLE_UUID, Constants.CONSOLE_NAME) {
@Override
protected void consumeMessage(String s) {
logger.info(s);

View File

@ -51,7 +51,7 @@ public class SpongeSenderFactory extends SenderFactory<CommandSource> {
if (source instanceof Player) {
return source.getName();
}
return getConsoleName();
return Constants.CONSOLE_NAME;
}
@Override