From b7541c43c8a77b8501f67a1c5b360aac80a7cdef Mon Sep 17 00:00:00 2001 From: Luck Date: Thu, 2 Nov 2017 18:51:59 +0000 Subject: [PATCH] Add Sender#getNameWithLocation --- .../luckperms/bukkit/BukkitSenderFactory.java | 2 +- .../luckperms/bungee/BungeeSenderFactory.java | 2 +- .../common/actionlog/ExtendedLogEntry.java | 2 +- .../luckperms/common/backup/Exporter.java | 2 +- .../common/backup/ImporterSender.java | 59 ++++++----------- .../impl/generic/other/HolderEditor.java | 2 +- .../commands/sender/AbstractSender.java | 61 ++++++++---------- .../common/commands/sender/Sender.java | 64 +++++++++++++++++-- .../common/commands/sender/SenderFactory.java | 17 +---- .../luckperms/common/constants/Constants.java | 5 +- .../common/verbose/VerboseListener.java | 2 +- .../luckperms/sponge/LPSpongePlugin.java | 19 +----- .../luckperms/sponge/SpongeSenderFactory.java | 2 +- 13 files changed, 113 insertions(+), 126 deletions(-) diff --git a/bukkit/src/main/java/me/lucko/luckperms/bukkit/BukkitSenderFactory.java b/bukkit/src/main/java/me/lucko/luckperms/bukkit/BukkitSenderFactory.java index 067c4f2d..ef328633 100644 --- a/bukkit/src/main/java/me/lucko/luckperms/bukkit/BukkitSenderFactory.java +++ b/bukkit/src/main/java/me/lucko/luckperms/bukkit/BukkitSenderFactory.java @@ -54,7 +54,7 @@ public class BukkitSenderFactory extends SenderFactory { if (sender instanceof Player) { return sender.getName(); } - return getConsoleName(); + return Constants.CONSOLE_NAME; } @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 2b044660..1ef0fc31 100644 --- a/bungee/src/main/java/me/lucko/luckperms/bungee/BungeeSenderFactory.java +++ b/bungee/src/main/java/me/lucko/luckperms/bungee/BungeeSenderFactory.java @@ -50,7 +50,7 @@ public class BungeeSenderFactory extends SenderFactory { if (sender instanceof ProxiedPlayer) { return sender.getName(); } - return getConsoleName(); + return Constants.CONSOLE_NAME; } @Override diff --git a/common/src/main/java/me/lucko/luckperms/common/actionlog/ExtendedLogEntry.java b/common/src/main/java/me/lucko/luckperms/common/actionlog/ExtendedLogEntry.java index 80a158a3..00ef340f 100644 --- a/common/src/main/java/me/lucko/luckperms/common/actionlog/ExtendedLogEntry.java +++ b/common/src/main/java/me/lucko/luckperms/common/actionlog/ExtendedLogEntry.java @@ -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; } diff --git a/common/src/main/java/me/lucko/luckperms/common/backup/Exporter.java b/common/src/main/java/me/lucko/luckperms/common/backup/Exporter.java index 8524e76b..e3fe2454 100644 --- a/common/src/main/java/me/lucko/luckperms/common/backup/Exporter.java +++ b/common/src/main/java/me/lucko/luckperms/common/backup/Exporter.java @@ -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 diff --git a/common/src/main/java/me/lucko/luckperms/common/backup/ImporterSender.java b/common/src/main/java/me/lucko/luckperms/common/backup/ImporterSender.java index 098b1085..e877c21d 100644 --- a/common/src/main/java/me/lucko/luckperms/common/backup/ImporterSender.java +++ b/common/src/main/java/me/lucko/luckperms/common/backup/ImporterSender.java @@ -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; - } } diff --git a/common/src/main/java/me/lucko/luckperms/common/commands/impl/generic/other/HolderEditor.java b/common/src/main/java/me/lucko/luckperms/common/commands/impl/generic/other/HolderEditor.java index 787489e0..2d6c3f51 100644 --- a/common/src/main/java/me/lucko/luckperms/common/commands/impl/generic/other/HolderEditor.java +++ b/common/src/main/java/me/lucko/luckperms/common/commands/impl/generic/other/HolderEditor.java @@ -88,7 +88,7 @@ public class HolderEditor extends SubCommand { Set 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)); diff --git a/common/src/main/java/me/lucko/luckperms/common/commands/sender/AbstractSender.java b/common/src/main/java/me/lucko/luckperms/common/commands/sender/AbstractSender.java index f0eb5160..514fc654 100644 --- a/common/src/main/java/me/lucko/luckperms/common/commands/sender/AbstractSender.java +++ b/common/src/main/java/me/lucko/luckperms/common/commands/sender/AbstractSender.java @@ -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 implements Sender { private static final Splitter NEW_LINE_SPLITTER = Splitter.on("\n"); private final LuckPermsPlugin platform; + + @Getter(AccessLevel.NONE) private final SenderFactory factory; - private final WeakReference ref; - private final String name; + + @Getter(AccessLevel.NONE) + private final WeakReference reference; + private final UUID uuid; + private final String name; AbstractSender(LuckPermsPlugin platform, SenderFactory 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 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 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 getHandle() { + return Optional.ofNullable(reference.get()); + } } diff --git a/common/src/main/java/me/lucko/luckperms/common/commands/sender/Sender.java b/common/src/main/java/me/lucko/luckperms/common/commands/sender/Sender.java index 63b4ee1a..14dd5504 100644 --- a/common/src/main/java/me/lucko/luckperms/common/commands/sender/Sender.java +++ b/common/src/main/java/me/lucko/luckperms/common/commands/sender/Sender.java @@ -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 getHandle() { + return Optional.empty(); + } } diff --git a/common/src/main/java/me/lucko/luckperms/common/commands/sender/SenderFactory.java b/common/src/main/java/me/lucko/luckperms/common/commands/sender/SenderFactory.java index 1e05b2ff..5b8a95f9 100644 --- a/common/src/main/java/me/lucko/luckperms/common/commands/sender/SenderFactory.java +++ b/common/src/main/java/me/lucko/luckperms/common/commands/sender/SenderFactory.java @@ -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 { @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 { 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); } diff --git a/common/src/main/java/me/lucko/luckperms/common/constants/Constants.java b/common/src/main/java/me/lucko/luckperms/common/constants/Constants.java index 3c879ac0..767db34d 100644 --- a/common/src/main/java/me/lucko/luckperms/common/constants/Constants.java +++ b/common/src/main/java/me/lucko/luckperms/common/constants/Constants.java @@ -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 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 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 = '&'; diff --git a/common/src/main/java/me/lucko/luckperms/common/verbose/VerboseListener.java b/common/src/main/java/me/lucko/luckperms/common/verbose/VerboseListener.java index 006594ef..9431e898 100644 --- a/common/src/main/java/me/lucko/luckperms/common/verbose/VerboseListener.java +++ b/common/src/main/java/me/lucko/luckperms/common/verbose/VerboseListener.java @@ -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(""); 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 1e470690..a573d73a 100644 --- a/sponge/src/main/java/me/lucko/luckperms/sponge/LPSpongePlugin.java +++ b/sponge/src/main/java/me/lucko/luckperms/sponge/LPSpongePlugin.java @@ -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); diff --git a/sponge/src/main/java/me/lucko/luckperms/sponge/SpongeSenderFactory.java b/sponge/src/main/java/me/lucko/luckperms/sponge/SpongeSenderFactory.java index e2728600..3dc875cf 100644 --- a/sponge/src/main/java/me/lucko/luckperms/sponge/SpongeSenderFactory.java +++ b/sponge/src/main/java/me/lucko/luckperms/sponge/SpongeSenderFactory.java @@ -51,7 +51,7 @@ public class SpongeSenderFactory extends SenderFactory { if (source instanceof Player) { return source.getName(); } - return getConsoleName(); + return Constants.CONSOLE_NAME; } @Override