mirror of
https://github.com/lucko/LuckPerms.git
synced 2025-09-08 21:30:55 +02:00
Improve efficiency of action log queries (#3917)
This commit is contained in:
@@ -33,8 +33,16 @@ import java.util.concurrent.CompletableFuture;
|
||||
*/
|
||||
public interface CommandExecutor {
|
||||
|
||||
CompletableFuture<Void> execute(String command);
|
||||
CompletableFuture<Void> execute(StandaloneSender player, String command);
|
||||
|
||||
List<String> tabComplete(String command);
|
||||
List<String> tabComplete(StandaloneSender player, String command);
|
||||
|
||||
default CompletableFuture<Void> execute(String command) {
|
||||
return execute(StandaloneUser.INSTANCE, command);
|
||||
}
|
||||
|
||||
default List<String> tabComplete(String command) {
|
||||
return tabComplete(StandaloneUser.INSTANCE, command);
|
||||
}
|
||||
|
||||
}
|
||||
|
@@ -0,0 +1,48 @@
|
||||
/*
|
||||
* 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.standalone.app.integration;
|
||||
|
||||
import net.kyori.adventure.text.Component;
|
||||
import net.luckperms.api.util.Tristate;
|
||||
|
||||
import java.util.Locale;
|
||||
import java.util.UUID;
|
||||
|
||||
public interface StandaloneSender {
|
||||
String getName();
|
||||
|
||||
UUID getUniqueId();
|
||||
|
||||
void sendMessage(Component component);
|
||||
|
||||
Tristate getPermissionValue(String permission);
|
||||
|
||||
boolean hasPermission(String permission);
|
||||
|
||||
boolean isConsole();
|
||||
|
||||
Locale getLocale();
|
||||
}
|
@@ -28,58 +28,57 @@ package me.lucko.luckperms.standalone.app.integration;
|
||||
import me.lucko.luckperms.standalone.app.LuckPermsApplication;
|
||||
import net.kyori.adventure.text.Component;
|
||||
import net.kyori.adventure.text.serializer.ansi.ANSIComponentSerializer;
|
||||
import net.kyori.ansi.ColorLevel;
|
||||
import net.luckperms.api.util.Tristate;
|
||||
|
||||
import java.util.Set;
|
||||
import java.util.Locale;
|
||||
import java.util.UUID;
|
||||
import java.util.concurrent.CopyOnWriteArraySet;
|
||||
import java.util.function.Consumer;
|
||||
|
||||
/**
|
||||
* Dummy/singleton player class used by the standalone plugin.
|
||||
*
|
||||
* <p>In various places (ContextManager, SenderFactory, ..) the platform "player" type is used
|
||||
* as a generic parameter. This class acts as this type for the standalone plugin.</p>
|
||||
* The sender instance used for the console / users executing commands
|
||||
* on a standalone instance of LuckPerms
|
||||
*/
|
||||
public class SingletonPlayer {
|
||||
public class StandaloneUser implements StandaloneSender {
|
||||
|
||||
/** Empty UUID used by the singleton player. */
|
||||
private static final UUID UUID = new UUID(0, 0);
|
||||
|
||||
/** A message sink that prints the component to stdout */
|
||||
private static final Consumer<Component> PRINT_TO_STDOUT = component -> LuckPermsApplication.LOGGER.info(ANSIComponentSerializer.ansi().serialize(component));
|
||||
public static final StandaloneUser INSTANCE = new StandaloneUser();
|
||||
|
||||
/** Singleton instance */
|
||||
public static final SingletonPlayer INSTANCE = new SingletonPlayer();
|
||||
|
||||
/** A set of message sinks that messages are delivered to */
|
||||
private final Set<Consumer<Component>> messageSinks;
|
||||
|
||||
private SingletonPlayer() {
|
||||
this.messageSinks = new CopyOnWriteArraySet<>();
|
||||
this.messageSinks.add(PRINT_TO_STDOUT);
|
||||
private StandaloneUser() {
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getName() {
|
||||
return "StandaloneUser";
|
||||
}
|
||||
|
||||
@Override
|
||||
public UUID getUniqueId() {
|
||||
return UUID;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void sendMessage(Component component) {
|
||||
for (Consumer<Component> sink : this.messageSinks) {
|
||||
sink.accept(component);
|
||||
}
|
||||
LuckPermsApplication.LOGGER.info(ANSIComponentSerializer.ansi().serialize(component));
|
||||
}
|
||||
|
||||
public void addMessageSink(Consumer<Component> sink) {
|
||||
this.messageSinks.add(sink);
|
||||
@Override
|
||||
public Tristate getPermissionValue(String permission) {
|
||||
return Tristate.TRUE;
|
||||
}
|
||||
|
||||
public void removeMessageSink(Consumer<Component> sink) {
|
||||
this.messageSinks.remove(sink);
|
||||
@Override
|
||||
public boolean hasPermission(String permission) {
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isConsole() {
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Locale getLocale() {
|
||||
return Locale.getDefault();
|
||||
}
|
||||
|
||||
}
|
@@ -39,7 +39,7 @@ import me.lucko.luckperms.common.plugin.AbstractLuckPermsPlugin;
|
||||
import me.lucko.luckperms.common.plugin.util.AbstractConnectionListener;
|
||||
import me.lucko.luckperms.common.sender.Sender;
|
||||
import me.lucko.luckperms.standalone.app.LuckPermsApplication;
|
||||
import me.lucko.luckperms.standalone.app.integration.SingletonPlayer;
|
||||
import me.lucko.luckperms.standalone.app.integration.StandaloneUser;
|
||||
import me.lucko.luckperms.standalone.stub.StandaloneContextManager;
|
||||
import me.lucko.luckperms.standalone.stub.StandaloneDummyConnectionListener;
|
||||
import me.lucko.luckperms.standalone.stub.StandaloneEventBus;
|
||||
@@ -162,7 +162,7 @@ public class LPStandalonePlugin extends AbstractLuckPermsPlugin {
|
||||
|
||||
@Override
|
||||
public Sender getConsoleSender() {
|
||||
return getSenderFactory().wrap(SingletonPlayer.INSTANCE);
|
||||
return getSenderFactory().wrap(StandaloneUser.INSTANCE);
|
||||
}
|
||||
|
||||
public StandaloneSenderFactory getSenderFactory() {
|
||||
|
@@ -29,7 +29,7 @@ import me.lucko.luckperms.common.command.CommandManager;
|
||||
import me.lucko.luckperms.common.command.utils.ArgumentTokenizer;
|
||||
import me.lucko.luckperms.common.sender.Sender;
|
||||
import me.lucko.luckperms.standalone.app.integration.CommandExecutor;
|
||||
import me.lucko.luckperms.standalone.app.integration.SingletonPlayer;
|
||||
import me.lucko.luckperms.standalone.app.integration.StandaloneSender;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.concurrent.CompletableFuture;
|
||||
@@ -43,15 +43,15 @@ public class StandaloneCommandManager extends CommandManager implements CommandE
|
||||
}
|
||||
|
||||
@Override
|
||||
public CompletableFuture<Void> execute(String command) {
|
||||
Sender wrapped = this.plugin.getSenderFactory().wrap(SingletonPlayer.INSTANCE);
|
||||
public CompletableFuture<Void> execute(StandaloneSender player, String command) {
|
||||
Sender wrapped = this.plugin.getSenderFactory().wrap(player);
|
||||
List<String> arguments = ArgumentTokenizer.EXECUTE.tokenizeInput(command);
|
||||
return executeCommand(wrapped, "lp", arguments);
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<String> tabComplete(String command) {
|
||||
Sender wrapped = this.plugin.getSenderFactory().wrap(SingletonPlayer.INSTANCE);
|
||||
public List<String> tabComplete(StandaloneSender player, String command) {
|
||||
Sender wrapped = this.plugin.getSenderFactory().wrap(player);
|
||||
List<String> arguments = ArgumentTokenizer.TAB_COMPLETE.tokenizeInput(command);
|
||||
return tabCompleteCommand(wrapped, arguments);
|
||||
}
|
||||
|
@@ -27,53 +27,56 @@ package me.lucko.luckperms.standalone;
|
||||
|
||||
import me.lucko.luckperms.common.locale.TranslationManager;
|
||||
import me.lucko.luckperms.common.sender.SenderFactory;
|
||||
import me.lucko.luckperms.standalone.app.integration.SingletonPlayer;
|
||||
import me.lucko.luckperms.standalone.app.integration.StandaloneSender;
|
||||
import net.kyori.adventure.text.Component;
|
||||
import net.luckperms.api.util.Tristate;
|
||||
|
||||
import java.util.Locale;
|
||||
import java.util.UUID;
|
||||
|
||||
public class StandaloneSenderFactory extends SenderFactory<LPStandalonePlugin, SingletonPlayer> {
|
||||
public class StandaloneSenderFactory extends SenderFactory<LPStandalonePlugin, StandaloneSender> {
|
||||
|
||||
public StandaloneSenderFactory(LPStandalonePlugin plugin) {
|
||||
super(plugin);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected String getName(SingletonPlayer sender) {
|
||||
protected String getName(StandaloneSender sender) {
|
||||
return sender.getName();
|
||||
}
|
||||
|
||||
@Override
|
||||
protected UUID getUniqueId(SingletonPlayer sender) {
|
||||
protected UUID getUniqueId(StandaloneSender sender) {
|
||||
return sender.getUniqueId();
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void sendMessage(SingletonPlayer sender, Component message) {
|
||||
Component rendered = TranslationManager.render(message, Locale.getDefault());
|
||||
protected void sendMessage(StandaloneSender sender, Component message) {
|
||||
Component rendered = TranslationManager.render(message, sender.getLocale());
|
||||
sender.sendMessage(rendered);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected Tristate getPermissionValue(SingletonPlayer sender, String node) {
|
||||
return Tristate.TRUE;
|
||||
protected Tristate getPermissionValue(StandaloneSender sender, String node) {
|
||||
return sender.getPermissionValue(node);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected boolean hasPermission(SingletonPlayer sender, String node) {
|
||||
protected boolean hasPermission(StandaloneSender sender, String node) {
|
||||
return sender.hasPermission(node);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void performCommand(StandaloneSender sender, String command) {
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
protected boolean isConsole(StandaloneSender sender) {
|
||||
return sender.isConsole();
|
||||
}
|
||||
|
||||
@Override
|
||||
protected boolean shouldSplitNewlines(StandaloneSender sender) {
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void performCommand(SingletonPlayer sender, String command) {
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
protected boolean isConsole(SingletonPlayer sender) {
|
||||
return true;
|
||||
}
|
||||
|
||||
}
|
||||
|
@@ -29,39 +29,45 @@ import me.lucko.luckperms.common.config.ConfigKeys;
|
||||
import me.lucko.luckperms.common.context.manager.ContextManager;
|
||||
import me.lucko.luckperms.common.context.manager.QueryOptionsCache;
|
||||
import me.lucko.luckperms.standalone.LPStandalonePlugin;
|
||||
import me.lucko.luckperms.standalone.app.integration.SingletonPlayer;
|
||||
import me.lucko.luckperms.standalone.app.integration.StandaloneSender;
|
||||
import me.lucko.luckperms.standalone.app.integration.StandaloneUser;
|
||||
import net.luckperms.api.context.ImmutableContextSet;
|
||||
import net.luckperms.api.query.QueryOptions;
|
||||
|
||||
import java.util.UUID;
|
||||
|
||||
public class StandaloneContextManager extends ContextManager<SingletonPlayer, SingletonPlayer> {
|
||||
private final QueryOptionsCache<SingletonPlayer> singletonCache = new QueryOptionsCache<>(SingletonPlayer.INSTANCE, this);
|
||||
public class StandaloneContextManager extends ContextManager<StandaloneSender, StandaloneSender> {
|
||||
private final QueryOptionsCache<StandaloneSender> singletonCache = new QueryOptionsCache<>(StandaloneUser.INSTANCE, this);
|
||||
|
||||
public StandaloneContextManager(LPStandalonePlugin plugin) {
|
||||
super(plugin, SingletonPlayer.class, SingletonPlayer.class);
|
||||
super(plugin, StandaloneSender.class, StandaloneSender.class);
|
||||
}
|
||||
|
||||
@Override
|
||||
public UUID getUniqueId(SingletonPlayer player) {
|
||||
public UUID getUniqueId(StandaloneSender player) {
|
||||
return player.getUniqueId();
|
||||
}
|
||||
|
||||
@Override
|
||||
public QueryOptionsCache<SingletonPlayer> getCacheFor(SingletonPlayer subject) {
|
||||
public QueryOptionsCache<StandaloneSender> getCacheFor(StandaloneSender subject) {
|
||||
if (subject == null) {
|
||||
throw new NullPointerException("subject");
|
||||
}
|
||||
return this.singletonCache;
|
||||
if (subject == StandaloneUser.INSTANCE) {
|
||||
return this.singletonCache;
|
||||
}
|
||||
|
||||
// just return a new one every time - not optimal but this case should only be hit using unit tests anyway
|
||||
return new QueryOptionsCache<>(subject, this);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void invalidateCache(SingletonPlayer subject) {
|
||||
protected void invalidateCache(StandaloneSender subject) {
|
||||
this.singletonCache.invalidate();
|
||||
}
|
||||
|
||||
@Override
|
||||
public QueryOptions formQueryOptions(SingletonPlayer subject, ImmutableContextSet contextSet) {
|
||||
public QueryOptions formQueryOptions(StandaloneSender subject, ImmutableContextSet contextSet) {
|
||||
QueryOptions.Builder queryOptions = this.plugin.getConfiguration().get(ConfigKeys.GLOBAL_QUERY_OPTIONS).toBuilder();
|
||||
return queryOptions.context(contextSet).build();
|
||||
}
|
||||
|
@@ -29,9 +29,9 @@ import me.lucko.luckperms.common.model.Group;
|
||||
import me.lucko.luckperms.common.model.User;
|
||||
import me.lucko.luckperms.common.node.types.Inheritance;
|
||||
import me.lucko.luckperms.standalone.app.integration.CommandExecutor;
|
||||
import me.lucko.luckperms.standalone.app.integration.SingletonPlayer;
|
||||
import me.lucko.luckperms.standalone.utils.CommandTester;
|
||||
import me.lucko.luckperms.standalone.utils.TestPluginProvider;
|
||||
import me.lucko.luckperms.standalone.utils.TestSender;
|
||||
import net.kyori.adventure.text.serializer.plain.PlainTextComponentSerializer;
|
||||
import net.luckperms.api.event.log.LogNotifyEvent;
|
||||
import org.junit.jupiter.api.Test;
|
||||
@@ -54,6 +54,7 @@ public class CommandsIntegrationTest {
|
||||
|
||||
private static final Map<String, String> CONFIG = ImmutableMap.<String, String>builder()
|
||||
.put("log-notify", "false")
|
||||
.put("commands-rate-limit", "false")
|
||||
.build();
|
||||
|
||||
@Test
|
||||
@@ -61,7 +62,7 @@ public class CommandsIntegrationTest {
|
||||
TestPluginProvider.use(tempDir, CONFIG, (app, bootstrap, plugin) -> {
|
||||
CommandExecutor executor = app.getCommandExecutor();
|
||||
|
||||
new CommandTester(plugin, executor)
|
||||
new CommandTester(executor)
|
||||
.givenHasPermissions("luckperms.creategroup")
|
||||
.whenRunCommand("creategroup test")
|
||||
.thenExpect("[LP] test was successfully created.")
|
||||
@@ -97,9 +98,9 @@ public class CommandsIntegrationTest {
|
||||
"""
|
||||
)
|
||||
|
||||
.givenHasAllPermissions()
|
||||
.givenHasPermissions("luckperms.group.meta.set")
|
||||
.whenRunCommand("group test meta set hello world")
|
||||
.clearMessageBuffer()
|
||||
.thenExpect("[LP] Set meta key 'hello' to 'world' for test in context global.")
|
||||
|
||||
.givenHasPermissions("luckperms.group.setweight")
|
||||
.whenRunCommand("group test setweight 10")
|
||||
@@ -182,7 +183,7 @@ public class CommandsIntegrationTest {
|
||||
TestPluginProvider.use(tempDir, CONFIG, (app, bootstrap, plugin) -> {
|
||||
CommandExecutor executor = app.getCommandExecutor();
|
||||
|
||||
new CommandTester(plugin, executor)
|
||||
new CommandTester(executor)
|
||||
.whenRunCommand("creategroup test")
|
||||
.clearMessageBuffer()
|
||||
|
||||
@@ -262,7 +263,7 @@ public class CommandsIntegrationTest {
|
||||
TestPluginProvider.use(tempDir, CONFIG, (app, bootstrap, plugin) -> {
|
||||
CommandExecutor executor = app.getCommandExecutor();
|
||||
|
||||
new CommandTester(plugin, executor)
|
||||
new CommandTester(executor)
|
||||
.whenRunCommand("creategroup test")
|
||||
.whenRunCommand("creategroup test2")
|
||||
.whenRunCommand("creategroup test3")
|
||||
@@ -321,7 +322,7 @@ public class CommandsIntegrationTest {
|
||||
TestPluginProvider.use(tempDir, CONFIG, (app, bootstrap, plugin) -> {
|
||||
CommandExecutor executor = app.getCommandExecutor();
|
||||
|
||||
new CommandTester(plugin, executor)
|
||||
new CommandTester(executor)
|
||||
.whenRunCommand("creategroup test")
|
||||
.clearMessageBuffer()
|
||||
|
||||
@@ -445,7 +446,7 @@ public class CommandsIntegrationTest {
|
||||
plugin.getStorage().savePlayerData(UUID.fromString("c1d60c50-70b5-4722-8057-87767557e50d"), "Luck").join();
|
||||
plugin.getStorage().savePlayerData(UUID.fromString("069a79f4-44e9-4726-a5be-fca90e38aaf5"), "Notch").join();
|
||||
|
||||
new CommandTester(plugin, executor)
|
||||
new CommandTester(executor)
|
||||
.givenHasPermissions("luckperms.user.info")
|
||||
.whenRunCommand("user Luck info")
|
||||
.thenExpect("""
|
||||
@@ -508,7 +509,7 @@ public class CommandsIntegrationTest {
|
||||
|
||||
plugin.getStorage().savePlayerData(UUID.fromString("c1d60c50-70b5-4722-8057-87767557e50d"), "Luck").join();
|
||||
|
||||
new CommandTester(plugin, executor)
|
||||
new CommandTester(executor)
|
||||
.givenHasPermissions("luckperms.user.permission.set")
|
||||
.whenRunCommand("user Luck permission set test.node true")
|
||||
.thenExpect("[LP] Set test.node to true for luck in context global.")
|
||||
@@ -587,7 +588,7 @@ public class CommandsIntegrationTest {
|
||||
|
||||
plugin.getStorage().savePlayerData(UUID.fromString("c1d60c50-70b5-4722-8057-87767557e50d"), "Luck").join();
|
||||
|
||||
new CommandTester(plugin, executor)
|
||||
new CommandTester(executor)
|
||||
.whenRunCommand("creategroup test2")
|
||||
.whenRunCommand("creategroup test3")
|
||||
.clearMessageBuffer()
|
||||
@@ -651,7 +652,7 @@ public class CommandsIntegrationTest {
|
||||
|
||||
plugin.getStorage().savePlayerData(UUID.fromString("c1d60c50-70b5-4722-8057-87767557e50d"), "Luck").join();
|
||||
|
||||
new CommandTester(plugin, executor)
|
||||
new CommandTester(executor)
|
||||
.givenHasPermissions("luckperms.user.meta.info")
|
||||
.whenRunCommand("user Luck meta info")
|
||||
.thenExpect("""
|
||||
@@ -773,7 +774,7 @@ public class CommandsIntegrationTest {
|
||||
TestPluginProvider.use(tempDir, CONFIG, (app, bootstrap, plugin) -> {
|
||||
CommandExecutor executor = app.getCommandExecutor();
|
||||
|
||||
new CommandTester(plugin, executor)
|
||||
new CommandTester(executor)
|
||||
.givenHasPermissions("luckperms.createtrack")
|
||||
.whenRunCommand("createtrack test1")
|
||||
.thenExpect("[LP] test1 was successfully created.")
|
||||
@@ -884,7 +885,7 @@ public class CommandsIntegrationTest {
|
||||
|
||||
plugin.getStorage().savePlayerData(UUID.fromString("c1d60c50-70b5-4722-8057-87767557e50d"), "Luck").join();
|
||||
|
||||
new CommandTester(plugin, executor)
|
||||
new CommandTester(executor)
|
||||
.whenRunCommand("createtrack staff")
|
||||
.whenRunCommand("createtrack premium")
|
||||
|
||||
@@ -1023,7 +1024,7 @@ public class CommandsIntegrationTest {
|
||||
|
||||
plugin.getStorage().savePlayerData(UUID.fromString("c1d60c50-70b5-4722-8057-87767557e50d"), "Luck").join();
|
||||
|
||||
new CommandTester(plugin, executor)
|
||||
new CommandTester(executor)
|
||||
.whenRunCommand("creategroup test")
|
||||
.whenRunCommand("user Luck permission set hello.world true server=survival")
|
||||
.whenRunCommand("group test permission set hello.world true world=nether")
|
||||
@@ -1045,12 +1046,12 @@ public class CommandsIntegrationTest {
|
||||
.givenHasPermissions("luckperms.search")
|
||||
.whenRunCommand("search ~~ group.%")
|
||||
.thenExpect("""
|
||||
[LP] Searching for users and groups with permissions ~~ group.%...
|
||||
[LP] Found 2 entries from 2 users and 0 groups.
|
||||
[LP] Showing user entries: (page 1 of 1 - 2 entries)
|
||||
> luck - (group.test) - true
|
||||
> luck - (group.default) - true
|
||||
"""
|
||||
[LP] Searching for users and groups with permissions ~~ group.%...
|
||||
[LP] Found 2 entries from 2 users and 0 groups.
|
||||
[LP] Showing user entries: (page 1 of 1 - 2 entries)
|
||||
> luck - (group.test) - true
|
||||
> luck - (group.default) - true
|
||||
"""
|
||||
);
|
||||
});
|
||||
}
|
||||
@@ -1068,14 +1069,16 @@ public class CommandsIntegrationTest {
|
||||
|
||||
CountDownLatch completed = new CountDownLatch(1);
|
||||
|
||||
SingletonPlayer.INSTANCE.addMessageSink(component -> {
|
||||
TestSender console = new TestSender();
|
||||
console.setConsole(true);
|
||||
console.addMessageSink(component -> {
|
||||
String plain = PlainTextComponentSerializer.plainText().serialize(component);
|
||||
if (plain.contains("Bulk update completed successfully")) {
|
||||
completed.countDown();
|
||||
}
|
||||
});
|
||||
|
||||
new CommandTester(plugin, executor)
|
||||
new CommandTester(executor, console)
|
||||
.whenRunCommand("creategroup moderator")
|
||||
.whenRunCommand("creategroup admin")
|
||||
.whenRunCommand("user Luck parent add moderator server=survival")
|
||||
@@ -1084,7 +1087,6 @@ public class CommandsIntegrationTest {
|
||||
.whenRunCommand("group moderator rename mod")
|
||||
.clearMessageBuffer()
|
||||
|
||||
.givenHasPermissions("luckperms.bulkupdate")
|
||||
.whenRunCommand("bulkupdate all update permission group.mod \"permission == group.moderator\"")
|
||||
.thenExpectStartsWith("[LP] Running bulk update.");
|
||||
|
||||
@@ -1113,6 +1115,151 @@ public class CommandsIntegrationTest {
|
||||
),
|
||||
notchUser.normalData().asSet()
|
||||
);
|
||||
|
||||
// test normal players are unable to use
|
||||
new CommandTester(executor)
|
||||
.givenHasPermissions("luckperms.bulkupdate")
|
||||
.whenRunCommand("bulkupdate all update permission group.mod \"permission == group.moderator\"")
|
||||
.thenExpect("[LP] The bulk update command can only be used from the console.");
|
||||
});
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testLogCommands(@TempDir Path tempDir) {
|
||||
Map<String, String> config = new HashMap<>(CONFIG);
|
||||
config.put("log-notify", "true");
|
||||
config.put("log-synchronously-in-commands", "true");
|
||||
|
||||
TestPluginProvider.use(tempDir, config, (app, bootstrap, plugin) -> {
|
||||
CommandExecutor executor = app.getCommandExecutor();
|
||||
|
||||
UUID luckUniqueId = UUID.fromString("c1d60c50-70b5-4722-8057-87767557e50d");
|
||||
|
||||
plugin.getStorage().savePlayerData(luckUniqueId, "Luck").join();
|
||||
plugin.getStorage().savePlayerData(UUID.fromString("069a79f4-44e9-4726-a5be-fca90e38aaf5"), "Notch").join();
|
||||
|
||||
TestSender testSender = new TestSender();
|
||||
testSender.setUniqueId(luckUniqueId);
|
||||
testSender.setName("Luck");
|
||||
|
||||
new CommandTester(executor, testSender)
|
||||
.whenRunCommand("group default permission set hello");
|
||||
|
||||
new CommandTester(executor)
|
||||
.whenRunCommand("creategroup moderator")
|
||||
.whenRunCommand("creategroup admin")
|
||||
.whenRunCommand("createtrack staff")
|
||||
.whenRunCommand("user Luck parent add moderator server=survival")
|
||||
.whenRunCommand("user Notch parent add moderator")
|
||||
.whenRunCommand("group admin parent add moderator")
|
||||
.whenRunCommand("group moderator rename mod")
|
||||
.whenRunCommand("user Luck permission set test.1")
|
||||
.whenRunCommand("user Luck permission set test.2 false")
|
||||
.whenRunCommand("user Luck permission set test.3 server=survival")
|
||||
.whenRunCommand("user Luck permission settemp test.4 true 1h")
|
||||
.whenRunCommand("user Luck permission settemp test.5 false 2d")
|
||||
.clearMessageBuffer()
|
||||
|
||||
.givenHasPermissions("luckperms.log.userhistory")
|
||||
.whenRunCommand("log userhistory Luck")
|
||||
.thenExpectReplacing("\\ds ago", "1m ago", """
|
||||
[LP] Showing history for user luck (page 1 of 1)
|
||||
[LP] #1 (1m ago) (StandaloneUser) [U] (luck)
|
||||
[LP] > permission settemp test.5 false 2d
|
||||
[LP] #2 (1m ago) (StandaloneUser) [U] (luck)
|
||||
[LP] > permission settemp test.4 true 1h
|
||||
[LP] #3 (1m ago) (StandaloneUser) [U] (luck)
|
||||
[LP] > permission set test.3 true server=survival
|
||||
[LP] #4 (1m ago) (StandaloneUser) [U] (luck)
|
||||
[LP] > permission set test.2 false
|
||||
[LP] #5 (1m ago) (StandaloneUser) [U] (luck)
|
||||
[LP] > permission set test.1 true
|
||||
[LP] #6 (1m ago) (StandaloneUser) [U] (luck)
|
||||
[LP] > parent add moderator server=survival
|
||||
"""
|
||||
)
|
||||
|
||||
.givenHasPermissions("luckperms.log.grouphistory")
|
||||
.whenRunCommand("log grouphistory admin")
|
||||
.thenExpectReplacing("\\ds ago", "1m ago", """
|
||||
[LP] Showing history for group admin (page 1 of 1)
|
||||
[LP] #1 (1m ago) (StandaloneUser) [G] (admin)
|
||||
[LP] > parent add moderator
|
||||
[LP] #2 (1m ago) (StandaloneUser) [G] (admin)
|
||||
[LP] > create
|
||||
"""
|
||||
)
|
||||
|
||||
.givenHasPermissions("luckperms.log.trackhistory")
|
||||
.whenRunCommand("log trackhistory staff")
|
||||
.thenExpectReplacing("\\ds ago", "1m ago", """
|
||||
[LP] Showing history for track staff (page 1 of 1)
|
||||
[LP] #1 (1m ago) (StandaloneUser) [T] (staff)
|
||||
[LP] > create
|
||||
"""
|
||||
)
|
||||
|
||||
.givenHasPermissions("luckperms.log.recent")
|
||||
.whenRunCommand("log recent")
|
||||
.thenExpectReplacing("\\ds ago", "1m ago", """
|
||||
[LP] Showing recent actions (page 1 of 2)
|
||||
[LP] #1 (1m ago) (StandaloneUser) [U] (luck)
|
||||
[LP] > permission settemp test.5 false 2d
|
||||
[LP] #2 (1m ago) (StandaloneUser) [U] (luck)
|
||||
[LP] > permission settemp test.4 true 1h
|
||||
[LP] #3 (1m ago) (StandaloneUser) [U] (luck)
|
||||
[LP] > permission set test.3 true server=survival
|
||||
[LP] #4 (1m ago) (StandaloneUser) [U] (luck)
|
||||
[LP] > permission set test.2 false
|
||||
[LP] #5 (1m ago) (StandaloneUser) [U] (luck)
|
||||
[LP] > permission set test.1 true
|
||||
[LP] #6 (1m ago) (StandaloneUser) [G] (moderator)
|
||||
[LP] > rename mod
|
||||
[LP] #7 (1m ago) (StandaloneUser) [G] (admin)
|
||||
[LP] > parent add moderator
|
||||
[LP] #8 (1m ago) (StandaloneUser) [U] (notch)
|
||||
[LP] > parent add moderator
|
||||
[LP] #9 (1m ago) (StandaloneUser) [U] (luck)
|
||||
[LP] > parent add moderator server=survival
|
||||
[LP] #10 (1m ago) (StandaloneUser) [T] (staff)
|
||||
[LP] > create
|
||||
"""
|
||||
)
|
||||
|
||||
.givenHasPermissions("luckperms.log.recent")
|
||||
.whenRunCommand("log recent 2")
|
||||
.thenExpectReplacing("\\ds ago", "1m ago", """
|
||||
[LP] Showing recent actions (page 2 of 2)
|
||||
[LP] #11 (1m ago) (StandaloneUser) [G] (admin)
|
||||
[LP] > create
|
||||
[LP] #12 (1m ago) (StandaloneUser) [G] (moderator)
|
||||
[LP] > create
|
||||
[LP] #13 (1m ago) (Luck) [G] (default)
|
||||
[LP] > permission set hello true
|
||||
"""
|
||||
)
|
||||
|
||||
.givenHasPermissions("luckperms.log.recent")
|
||||
.whenRunCommand("log recent 3")
|
||||
.thenExpect("[LP] Invalid page number. Please enter a value between 1 and 2.")
|
||||
|
||||
.givenHasPermissions("luckperms.log.search")
|
||||
.whenRunCommand("log search hello")
|
||||
.thenExpectReplacing("\\ds ago", "1m ago", """
|
||||
[LP] Showing recent actions for query hello (page 1 of 1)
|
||||
[LP] #1 (1m ago) (Luck) [G] (default)
|
||||
[LP] > permission set hello true
|
||||
"""
|
||||
)
|
||||
|
||||
.givenHasPermissions("luckperms.log.recent")
|
||||
.whenRunCommand("log recent Luck")
|
||||
.thenExpectReplacing("\\ds ago", "1m ago", """
|
||||
[LP] Showing recent actions by Luck (page 1 of 1)
|
||||
[LP] #1 (1m ago) (Luck) [G] (default)
|
||||
[LP] > permission set hello true
|
||||
"""
|
||||
);
|
||||
});
|
||||
}
|
||||
|
||||
@@ -1121,7 +1268,7 @@ public class CommandsIntegrationTest {
|
||||
TestPluginProvider.use(tempDir, CONFIG, (app, bootstrap, plugin) -> {
|
||||
CommandExecutor executor = app.getCommandExecutor();
|
||||
|
||||
new CommandTester(plugin, executor)
|
||||
new CommandTester(executor)
|
||||
.givenHasPermissions("luckperms.user.info")
|
||||
.whenRunCommand("user unknown info")
|
||||
.thenExpect("[LP] A user for unknown could not be found.")
|
||||
@@ -1154,7 +1301,7 @@ public class CommandsIntegrationTest {
|
||||
CommandExecutor executor = app.getCommandExecutor();
|
||||
String version = "v" + bootstrap.getVersion();
|
||||
|
||||
new CommandTester(plugin, executor)
|
||||
new CommandTester(executor)
|
||||
.givenHasPermissions(/* empty */)
|
||||
|
||||
.whenRunCommand("")
|
||||
@@ -1188,7 +1335,10 @@ public class CommandsIntegrationTest {
|
||||
// by default, notifications are not sent to the user who initiated the event - override that
|
||||
app.getApi().getEventBus().subscribe(LogNotifyEvent.class, e -> e.setCancelled(false));
|
||||
|
||||
new CommandTester(plugin, executor)
|
||||
TestSender sender = new TestSender();
|
||||
plugin.addOnlineSender(sender);
|
||||
|
||||
new CommandTester(executor, sender)
|
||||
.givenHasPermissions("luckperms.group.permission.set", "luckperms.log.notify")
|
||||
.whenRunCommand("group default permission set hello.world true server=test")
|
||||
.thenExpect("""
|
||||
@@ -1208,7 +1358,7 @@ public class CommandsIntegrationTest {
|
||||
TestPluginProvider.use(tempDir, config, (app, bootstrap, plugin) -> {
|
||||
CommandExecutor executor = app.getCommandExecutor();
|
||||
|
||||
new CommandTester(plugin, executor)
|
||||
new CommandTester(executor)
|
||||
.givenHasPermissions("luckperms.group.permission.set")
|
||||
.whenRunCommand("group default permission set hello.world true server=test")
|
||||
.thenExpect("[LP] You do not have permission to use this command!")
|
||||
|
@@ -28,8 +28,10 @@ package me.lucko.luckperms.standalone;
|
||||
import com.google.common.collect.ImmutableList;
|
||||
import com.google.common.collect.ImmutableMap;
|
||||
import com.google.common.collect.ImmutableSet;
|
||||
import me.lucko.luckperms.common.actionlog.Log;
|
||||
import me.lucko.luckperms.common.actionlog.LogPage;
|
||||
import me.lucko.luckperms.common.actionlog.LoggedAction;
|
||||
import me.lucko.luckperms.common.filter.FilterList;
|
||||
import me.lucko.luckperms.common.filter.PageParameters;
|
||||
import me.lucko.luckperms.common.model.Group;
|
||||
import me.lucko.luckperms.common.model.Track;
|
||||
import me.lucko.luckperms.common.model.User;
|
||||
@@ -182,7 +184,7 @@ public class StorageIntegrationTest {
|
||||
assertNotNull(testTrack);
|
||||
assertEquals(ImmutableList.of("default", "test"), track.getGroups());
|
||||
|
||||
Log actionLog = plugin.getStorage().getLog().join();
|
||||
LogPage actionLog = plugin.getStorage().getLogPage(FilterList.empty(), new PageParameters(1000, 1)).join();
|
||||
assertTrue(actionLog.getContent().contains(exampleLogEntry));
|
||||
|
||||
List<NodeEntry<String, Node>> groupSearchResult = plugin.getStorage().searchGroupNodes(StandardNodeMatchers.key(TEST_PERMISSION_1)).join();
|
||||
|
@@ -36,7 +36,7 @@ import me.lucko.luckperms.common.util.Predicates;
|
||||
import me.lucko.luckperms.common.util.gson.GsonProvider;
|
||||
import me.lucko.luckperms.common.webeditor.WebEditorRequest;
|
||||
import me.lucko.luckperms.common.webeditor.WebEditorSession;
|
||||
import me.lucko.luckperms.standalone.app.integration.SingletonPlayer;
|
||||
import me.lucko.luckperms.standalone.app.integration.StandaloneUser;
|
||||
import me.lucko.luckperms.standalone.utils.TestPluginProvider;
|
||||
import net.luckperms.api.model.data.DataType;
|
||||
import okhttp3.OkHttpClient;
|
||||
@@ -115,7 +115,7 @@ public class WebEditorIntegrationTest {
|
||||
assertFalse(holders.isEmpty());
|
||||
|
||||
// create a new editor session
|
||||
Sender sender = plugin.getSenderFactory().wrap(SingletonPlayer.INSTANCE);
|
||||
Sender sender = plugin.getSenderFactory().wrap(StandaloneUser.INSTANCE);
|
||||
WebEditorSession session = WebEditorSession.create(holders, Collections.emptyList(), sender, "lp", plugin);
|
||||
String bytebinKey = session.open();
|
||||
String bytesocksKey = session.getSocket().getSocket().channelId();
|
||||
|
@@ -26,14 +26,12 @@
|
||||
package me.lucko.luckperms.standalone.utils;
|
||||
|
||||
import me.lucko.luckperms.standalone.app.integration.CommandExecutor;
|
||||
import me.lucko.luckperms.standalone.app.integration.SingletonPlayer;
|
||||
import me.lucko.luckperms.standalone.utils.TestPluginBootstrap.TestPlugin;
|
||||
import me.lucko.luckperms.standalone.utils.TestPluginBootstrap.TestSenderFactory;
|
||||
import net.kyori.adventure.text.Component;
|
||||
import net.kyori.adventure.text.serializer.plain.PlainTextComponentSerializer;
|
||||
import net.luckperms.api.util.Tristate;
|
||||
import org.apache.logging.log4j.LogManager;
|
||||
import org.apache.logging.log4j.Logger;
|
||||
import org.intellij.lang.annotations.RegExp;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collections;
|
||||
@@ -57,12 +55,12 @@ public final class CommandTester implements Consumer<Component>, Function<String
|
||||
|
||||
private static final Logger LOGGER = LogManager.getLogger(CommandTester.class);
|
||||
|
||||
/** The test plugin */
|
||||
private final TestPlugin plugin;
|
||||
|
||||
/** The LuckPerms command executor */
|
||||
private final CommandExecutor executor;
|
||||
|
||||
/** The test player */
|
||||
private final TestSender sender;
|
||||
|
||||
/** The current map of permissions held by the fake executor */
|
||||
private Map<String, Tristate> permissions = null;
|
||||
|
||||
@@ -72,9 +70,16 @@ public final class CommandTester implements Consumer<Component>, Function<String
|
||||
/** A buffer of messages received by the test tool */
|
||||
private final List<Component> messageBuffer = Collections.synchronizedList(new ArrayList<>());
|
||||
|
||||
public CommandTester(TestPlugin plugin, CommandExecutor executor) {
|
||||
this.plugin = plugin;
|
||||
public CommandTester(CommandExecutor executor, TestSender sender) {
|
||||
this.executor = executor;
|
||||
this.sender = sender;
|
||||
|
||||
this.sender.setPermissionChecker(this);
|
||||
this.sender.addMessageSink(this);
|
||||
}
|
||||
|
||||
public CommandTester(CommandExecutor executor) {
|
||||
this(executor, new TestSender());
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -138,15 +143,7 @@ public final class CommandTester implements Consumer<Component>, Function<String
|
||||
*/
|
||||
public CommandTester whenRunCommand(String command) {
|
||||
LOGGER.info("Executing test command: " + command);
|
||||
|
||||
TestSenderFactory senderFactory = this.plugin.getSenderFactory();
|
||||
senderFactory.setPermissionChecker(this);
|
||||
SingletonPlayer.INSTANCE.addMessageSink(this);
|
||||
|
||||
this.executor.execute(command).join();
|
||||
|
||||
SingletonPlayer.INSTANCE.removeMessageSink(this);
|
||||
senderFactory.resetPermissionChecker();
|
||||
this.executor.execute(this.sender, command).join();
|
||||
return this;
|
||||
}
|
||||
|
||||
@@ -184,6 +181,23 @@ public final class CommandTester implements Consumer<Component>, Function<String
|
||||
return this.clearMessageBuffer();
|
||||
}
|
||||
|
||||
/**
|
||||
* Asserts that the current contents of the message buffer matches the given input string.
|
||||
*
|
||||
* @param expected the expected contents
|
||||
* @return this
|
||||
*/
|
||||
public CommandTester thenExpectReplacing(@RegExp String regex, String replacement, String expected) {
|
||||
String actual = this.renderBuffer().replaceAll(regex, replacement);
|
||||
assertEquals(expected.trim(), actual.trim());
|
||||
|
||||
if (this.permissions != null) {
|
||||
assertEquals(this.checkedPermissions, this.permissions.keySet());
|
||||
}
|
||||
|
||||
return this.clearMessageBuffer();
|
||||
}
|
||||
|
||||
/**
|
||||
* Clears the message buffer.
|
||||
*
|
||||
|
@@ -27,21 +27,19 @@ package me.lucko.luckperms.standalone.utils;
|
||||
|
||||
import me.lucko.luckperms.common.dependencies.Dependency;
|
||||
import me.lucko.luckperms.common.dependencies.DependencyManager;
|
||||
import me.lucko.luckperms.common.locale.TranslationManager;
|
||||
import me.lucko.luckperms.common.plugin.classpath.ClassPathAppender;
|
||||
import me.lucko.luckperms.common.sender.Sender;
|
||||
import me.lucko.luckperms.common.storage.StorageType;
|
||||
import me.lucko.luckperms.standalone.LPStandaloneBootstrap;
|
||||
import me.lucko.luckperms.standalone.LPStandalonePlugin;
|
||||
import me.lucko.luckperms.standalone.StandaloneSenderFactory;
|
||||
import me.lucko.luckperms.standalone.app.LuckPermsApplication;
|
||||
import me.lucko.luckperms.standalone.app.integration.SingletonPlayer;
|
||||
import net.kyori.adventure.text.Component;
|
||||
import net.luckperms.api.util.Tristate;
|
||||
import me.lucko.luckperms.standalone.app.integration.StandaloneSender;
|
||||
import me.lucko.luckperms.standalone.app.integration.StandaloneUser;
|
||||
|
||||
import java.nio.file.Path;
|
||||
import java.util.Locale;
|
||||
import java.util.Set;
|
||||
import java.util.function.Function;
|
||||
import java.util.concurrent.CopyOnWriteArraySet;
|
||||
import java.util.stream.Stream;
|
||||
|
||||
/**
|
||||
* An extension standalone bootstrap for testing.
|
||||
@@ -50,7 +48,7 @@ import java.util.function.Function;
|
||||
* <p>
|
||||
* <ul>
|
||||
* <li>Dependency loading system is replaced with a no-op stub that delegates to the test classloader</li>
|
||||
* <li>Sender factory is extended and allows for permission checks to be intercepted</li>
|
||||
* <li>Ability to register additional sender instances as being online</li>
|
||||
* </ul>
|
||||
* </p>
|
||||
*/
|
||||
@@ -81,7 +79,7 @@ public final class TestPluginBootstrap extends LPStandaloneBootstrap {
|
||||
}
|
||||
|
||||
public static final class TestPlugin extends LPStandalonePlugin {
|
||||
private TestSenderFactory senderFactory;
|
||||
private final Set<StandaloneSender> onlineSenders = new CopyOnWriteArraySet<>();
|
||||
|
||||
TestPlugin(LPStandaloneBootstrap bootstrap) {
|
||||
super(bootstrap);
|
||||
@@ -93,13 +91,15 @@ public final class TestPluginBootstrap extends LPStandaloneBootstrap {
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void setupSenderFactory() {
|
||||
this.senderFactory = new TestSenderFactory(this);
|
||||
public Stream<Sender> getOnlineSenders() {
|
||||
return Stream.concat(
|
||||
Stream.of(StandaloneUser.INSTANCE),
|
||||
this.onlineSenders.stream()
|
||||
).map(player -> getSenderFactory().wrap(player));
|
||||
}
|
||||
|
||||
@Override
|
||||
public TestSenderFactory getSenderFactory() {
|
||||
return this.senderFactory;
|
||||
public void addOnlineSender(StandaloneSender player) {
|
||||
this.onlineSenders.add(player);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -125,46 +125,4 @@ public final class TestPluginBootstrap extends LPStandaloneBootstrap {
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
public static final class TestSenderFactory extends StandaloneSenderFactory {
|
||||
|
||||
private Function<String, Tristate> permissionChecker;
|
||||
|
||||
public TestSenderFactory(LPStandalonePlugin plugin) {
|
||||
super(plugin);
|
||||
}
|
||||
|
||||
public void setPermissionChecker(Function<String, Tristate> permissionChecker) {
|
||||
this.permissionChecker = permissionChecker;
|
||||
}
|
||||
|
||||
public void resetPermissionChecker() {
|
||||
this.permissionChecker = null;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected boolean consoleHasAllPermissions() {
|
||||
return false;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void sendMessage(SingletonPlayer sender, Component message) {
|
||||
Component rendered = TranslationManager.render(message, Locale.ENGLISH);
|
||||
sender.sendMessage(rendered);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected Tristate getPermissionValue(SingletonPlayer sender, String node) {
|
||||
return this.permissionChecker == null
|
||||
? super.getPermissionValue(sender, node)
|
||||
: this.permissionChecker.apply(node);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected boolean hasPermission(SingletonPlayer sender, String node) {
|
||||
return this.permissionChecker == null
|
||||
? super.hasPermission(sender, node)
|
||||
: this.permissionChecker.apply(node).asBoolean();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@@ -0,0 +1,113 @@
|
||||
/*
|
||||
* 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.standalone.utils;
|
||||
|
||||
import me.lucko.luckperms.standalone.app.integration.StandaloneSender;
|
||||
import me.lucko.luckperms.standalone.app.integration.StandaloneUser;
|
||||
import net.kyori.adventure.text.Component;
|
||||
import net.luckperms.api.util.Tristate;
|
||||
|
||||
import java.util.Locale;
|
||||
import java.util.Set;
|
||||
import java.util.UUID;
|
||||
import java.util.concurrent.CopyOnWriteArraySet;
|
||||
import java.util.function.Consumer;
|
||||
import java.util.function.Function;
|
||||
|
||||
public class TestSender implements StandaloneSender {
|
||||
|
||||
private final Set<Consumer<Component>> messageSinks;
|
||||
|
||||
private String name = "StandaloneUser";
|
||||
private UUID uniqueId = UUID.randomUUID();
|
||||
private boolean isConsole = false;
|
||||
|
||||
private Function<String, Tristate> permissionChecker;
|
||||
|
||||
public TestSender() {
|
||||
this.messageSinks = new CopyOnWriteArraySet<>();
|
||||
this.messageSinks.add(StandaloneUser.INSTANCE::sendMessage);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getName() {
|
||||
return this.name;
|
||||
}
|
||||
|
||||
public void setName(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
@Override
|
||||
public UUID getUniqueId() {
|
||||
return this.uniqueId;
|
||||
}
|
||||
|
||||
public void setUniqueId(UUID uuid) {
|
||||
this.uniqueId = uuid;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void sendMessage(Component component) {
|
||||
for (Consumer<Component> sink : this.messageSinks) {
|
||||
sink.accept(component);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public Tristate getPermissionValue(String permission) {
|
||||
return this.permissionChecker == null
|
||||
? Tristate.TRUE
|
||||
: this.permissionChecker.apply(permission);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean hasPermission(String permission) {
|
||||
return getPermissionValue(permission).asBoolean();
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isConsole() {
|
||||
return this.isConsole;
|
||||
}
|
||||
|
||||
public void setConsole(boolean console) {
|
||||
this.isConsole = console;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Locale getLocale() {
|
||||
return Locale.ENGLISH;
|
||||
}
|
||||
|
||||
public void setPermissionChecker(Function<String, Tristate> permissionChecker) {
|
||||
this.permissionChecker = permissionChecker;
|
||||
}
|
||||
|
||||
public void addMessageSink(Consumer<Component> sink) {
|
||||
this.messageSinks.add(sink);
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user