From 2b71476cfee29239470d11cf335ba8ec8c385686 Mon Sep 17 00:00:00 2001 From: Luck Date: Thu, 25 Aug 2016 18:07:32 +0100 Subject: [PATCH] Implement ConsecutiveExecutor --- .../me/lucko/luckperms/LPBukkitPlugin.java | 4 ++ .../me/lucko/luckperms/LPBungeePlugin.java | 4 ++ .../me/lucko/luckperms/LuckPermsPlugin.java | 2 + .../luckperms/commands/CommandManager.java | 6 +- .../commands/ConsecutiveExecutor.java | 72 +++++++++++++++++++ .../luckperms/commands/misc/QueueCommand.java | 55 ++++++++++++++ .../me/lucko/luckperms/data/Importer.java | 2 +- .../me/lucko/luckperms/LPSpongePlugin.java | 4 ++ 8 files changed, 144 insertions(+), 5 deletions(-) create mode 100644 common/src/main/java/me/lucko/luckperms/commands/ConsecutiveExecutor.java create mode 100644 common/src/main/java/me/lucko/luckperms/commands/misc/QueueCommand.java diff --git a/bukkit/src/main/java/me/lucko/luckperms/LPBukkitPlugin.java b/bukkit/src/main/java/me/lucko/luckperms/LPBukkitPlugin.java index cc2bb9d65..8349470ff 100644 --- a/bukkit/src/main/java/me/lucko/luckperms/LPBukkitPlugin.java +++ b/bukkit/src/main/java/me/lucko/luckperms/LPBukkitPlugin.java @@ -27,6 +27,7 @@ import me.lucko.luckperms.api.Logger; import me.lucko.luckperms.api.LuckPermsApi; import me.lucko.luckperms.api.implementation.ApiProvider; import me.lucko.luckperms.api.vault.VaultHook; +import me.lucko.luckperms.commands.ConsecutiveExecutor; import me.lucko.luckperms.commands.Sender; import me.lucko.luckperms.constants.Message; import me.lucko.luckperms.core.LPConfiguration; @@ -63,6 +64,7 @@ public class LPBukkitPlugin extends JavaPlugin implements LuckPermsPlugin { private ApiProvider apiProvider; private Logger log; private Importer importer; + private ConsecutiveExecutor consecutiveExecutor; @Override public void onEnable() { @@ -91,6 +93,7 @@ public class LPBukkitPlugin extends JavaPlugin implements LuckPermsPlugin { groupManager = new GroupManager(this); trackManager = new TrackManager(); importer = new Importer(commandManager); + consecutiveExecutor = new ConsecutiveExecutor(commandManager); int mins = getConfiguration().getSyncTime(); if (mins > 0) { @@ -100,6 +103,7 @@ public class LPBukkitPlugin extends JavaPlugin implements LuckPermsPlugin { getServer().getScheduler().runTaskTimer(this, BukkitSenderFactory.get(), 1L, 1L); getServer().getScheduler().runTaskTimerAsynchronously(this, new ExpireTemporaryTask(this), 60L, 60L); + getServer().getScheduler().runTaskTimerAsynchronously(this, consecutiveExecutor, 20L, 20L); // Provide vault support getLog().info("Attempting to hook into Vault..."); diff --git a/bungee/src/main/java/me/lucko/luckperms/LPBungeePlugin.java b/bungee/src/main/java/me/lucko/luckperms/LPBungeePlugin.java index 4b1b71f85..cab8110eb 100644 --- a/bungee/src/main/java/me/lucko/luckperms/LPBungeePlugin.java +++ b/bungee/src/main/java/me/lucko/luckperms/LPBungeePlugin.java @@ -26,6 +26,7 @@ import lombok.Getter; import me.lucko.luckperms.api.Logger; import me.lucko.luckperms.api.implementation.ApiProvider; import me.lucko.luckperms.commands.CommandManager; +import me.lucko.luckperms.commands.ConsecutiveExecutor; import me.lucko.luckperms.commands.Sender; import me.lucko.luckperms.constants.Message; import me.lucko.luckperms.core.LPConfiguration; @@ -64,6 +65,7 @@ public class LPBungeePlugin extends Plugin implements LuckPermsPlugin { private ApiProvider apiProvider; private Logger log; private Importer importer; + private ConsecutiveExecutor consecutiveExecutor; @Override public void onEnable() { @@ -91,6 +93,7 @@ public class LPBungeePlugin extends Plugin implements LuckPermsPlugin { groupManager = new GroupManager(this); trackManager = new TrackManager(); importer = new Importer(commandManager); + consecutiveExecutor = new ConsecutiveExecutor(commandManager); int mins = getConfiguration().getSyncTime(); if (mins > 0) { @@ -100,6 +103,7 @@ public class LPBungeePlugin extends Plugin implements LuckPermsPlugin { // 20 times per second (once per "tick") getProxy().getScheduler().schedule(this, BungeeSenderFactory.get(), 50L, 50L, TimeUnit.MILLISECONDS); getProxy().getScheduler().schedule(this, new ExpireTemporaryTask(this), 3L, 3L, TimeUnit.SECONDS); + getProxy().getScheduler().schedule(this, consecutiveExecutor, 1L, 1L, TimeUnit.SECONDS); getLog().info("Registering API..."); apiProvider = new ApiProvider(this); diff --git a/common/src/main/java/me/lucko/luckperms/LuckPermsPlugin.java b/common/src/main/java/me/lucko/luckperms/LuckPermsPlugin.java index 85cfda786..9fd0afab5 100644 --- a/common/src/main/java/me/lucko/luckperms/LuckPermsPlugin.java +++ b/common/src/main/java/me/lucko/luckperms/LuckPermsPlugin.java @@ -24,6 +24,7 @@ package me.lucko.luckperms; import me.lucko.luckperms.api.Logger; import me.lucko.luckperms.api.implementation.ApiProvider; +import me.lucko.luckperms.commands.ConsecutiveExecutor; import me.lucko.luckperms.commands.Sender; import me.lucko.luckperms.constants.Message; import me.lucko.luckperms.core.LPConfiguration; @@ -57,6 +58,7 @@ public interface LuckPermsPlugin { UuidCache getUuidCache(); ApiProvider getApiProvider(); Importer getImporter(); + ConsecutiveExecutor getConsecutiveExecutor(); /** * @return the version of the plugin diff --git a/common/src/main/java/me/lucko/luckperms/commands/CommandManager.java b/common/src/main/java/me/lucko/luckperms/commands/CommandManager.java index d85f39c60..02036c890 100644 --- a/common/src/main/java/me/lucko/luckperms/commands/CommandManager.java +++ b/common/src/main/java/me/lucko/luckperms/commands/CommandManager.java @@ -33,10 +33,7 @@ import me.lucko.luckperms.commands.group.GroupMainCommand; import me.lucko.luckperms.commands.group.ListGroups; import me.lucko.luckperms.commands.log.LogMainCommand; import me.lucko.luckperms.commands.migration.MigrationMainCommand; -import me.lucko.luckperms.commands.misc.DebugCommand; -import me.lucko.luckperms.commands.misc.ImportCommand; -import me.lucko.luckperms.commands.misc.InfoCommand; -import me.lucko.luckperms.commands.misc.SyncCommand; +import me.lucko.luckperms.commands.misc.*; import me.lucko.luckperms.commands.track.CreateTrack; import me.lucko.luckperms.commands.track.DeleteTrack; import me.lucko.luckperms.commands.track.ListTracks; @@ -64,6 +61,7 @@ public class CommandManager { .add(new InfoCommand()) .add(new DebugCommand()) .add(new ImportCommand()) + .add(new QueueCommand()) .add(new MigrationMainCommand()) .add(new CreateGroup()) .add(new DeleteGroup()) diff --git a/common/src/main/java/me/lucko/luckperms/commands/ConsecutiveExecutor.java b/common/src/main/java/me/lucko/luckperms/commands/ConsecutiveExecutor.java new file mode 100644 index 000000000..b2b57ee11 --- /dev/null +++ b/common/src/main/java/me/lucko/luckperms/commands/ConsecutiveExecutor.java @@ -0,0 +1,72 @@ +/* + * Copyright (c) 2016 Lucko (Luck) + * + * 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.commands; + +import lombok.AllArgsConstructor; +import lombok.Getter; +import lombok.RequiredArgsConstructor; + +import java.util.ArrayList; +import java.util.List; + +/** + * Executes commands consecutively + */ +@RequiredArgsConstructor +public class ConsecutiveExecutor implements Runnable { + private final CommandManager manager; + + private final List commands = new ArrayList<>(); + + public void queueCommand(QueuedCommand command) { + synchronized (commands) { + commands.add(command); + } + } + + @Override + public void run() { + final List toExecute = new ArrayList<>(); + synchronized (commands) { + toExecute.addAll(commands); + commands.clear(); + } + + if (toExecute.isEmpty()) { + return; + } + + for (QueuedCommand command : toExecute) { + manager.onCommand(command.getSender(), "perms", command.getArgs()); + } + + } + + @Getter + @AllArgsConstructor + public static class QueuedCommand { + private final Sender sender; + private final List args; + } + +} diff --git a/common/src/main/java/me/lucko/luckperms/commands/misc/QueueCommand.java b/common/src/main/java/me/lucko/luckperms/commands/misc/QueueCommand.java new file mode 100644 index 000000000..c0dbe8730 --- /dev/null +++ b/common/src/main/java/me/lucko/luckperms/commands/misc/QueueCommand.java @@ -0,0 +1,55 @@ +/* + * Copyright (c) 2016 Lucko (Luck) + * + * 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.commands.misc; + +import me.lucko.luckperms.LuckPermsPlugin; +import me.lucko.luckperms.commands.CommandResult; +import me.lucko.luckperms.commands.ConsecutiveExecutor; +import me.lucko.luckperms.commands.Sender; +import me.lucko.luckperms.commands.SingleMainCommand; +import me.lucko.luckperms.constants.Constants; +import me.lucko.luckperms.constants.Permission; + +import java.util.List; + +public class QueueCommand extends SingleMainCommand { + public QueueCommand() { + super("QueueCommand", "/%s queuecommand ", 1, Permission.MIGRATION); + } + + @Override + protected CommandResult execute(LuckPermsPlugin plugin, Sender sender, List args, String label) { + if (args.get(0).equalsIgnoreCase(getName())) { + // Prevent infinite loops + return CommandResult.FAILURE; + } + + plugin.getConsecutiveExecutor().queueCommand(new ConsecutiveExecutor.QueuedCommand(sender, args)); + return CommandResult.SUCCESS; + } + + @Override + protected boolean isAuthorized(Sender sender) { + return sender.getUuid().equals(Constants.getConsoleUUID()); + } +} diff --git a/common/src/main/java/me/lucko/luckperms/data/Importer.java b/common/src/main/java/me/lucko/luckperms/data/Importer.java index f5ec21c7f..7c83ac589 100644 --- a/common/src/main/java/me/lucko/luckperms/data/Importer.java +++ b/common/src/main/java/me/lucko/luckperms/data/Importer.java @@ -38,7 +38,7 @@ import java.util.*; import java.util.stream.Collectors; /** - * Executes a list of commands sequentially in a single thread. + * Class to handle import operations */ @RequiredArgsConstructor public class Importer { diff --git a/sponge/src/main/java/me/lucko/luckperms/LPSpongePlugin.java b/sponge/src/main/java/me/lucko/luckperms/LPSpongePlugin.java index 51e6c509d..6273681f7 100644 --- a/sponge/src/main/java/me/lucko/luckperms/LPSpongePlugin.java +++ b/sponge/src/main/java/me/lucko/luckperms/LPSpongePlugin.java @@ -26,6 +26,7 @@ import com.google.inject.Inject; import lombok.Getter; import me.lucko.luckperms.api.LuckPermsApi; import me.lucko.luckperms.api.implementation.ApiProvider; +import me.lucko.luckperms.commands.ConsecutiveExecutor; import me.lucko.luckperms.commands.Sender; import me.lucko.luckperms.constants.Message; import me.lucko.luckperms.constants.Permission; @@ -90,6 +91,7 @@ public class LPSpongePlugin implements LuckPermsPlugin { private ApiProvider apiProvider; private me.lucko.luckperms.api.Logger log; private Importer importer; + private ConsecutiveExecutor consecutiveExecutor; @Listener public void onEnable(GamePreInitializationEvent event) { @@ -115,6 +117,7 @@ public class LPSpongePlugin implements LuckPermsPlugin { groupManager = new GroupManager(this); trackManager = new TrackManager(); importer = new Importer(commandManager); + consecutiveExecutor = new ConsecutiveExecutor(commandManager); getLog().info("Registering API..."); apiProvider = new ApiProvider(this); @@ -132,6 +135,7 @@ public class LPSpongePlugin implements LuckPermsPlugin { scheduler.createTaskBuilder().intervalTicks(1L).execute(SpongeSenderFactory.get()).submit(this); scheduler.createTaskBuilder().async().intervalTicks(60L).execute(new ExpireTemporaryTask(this)).submit(this); + scheduler.createTaskBuilder().async().intervalTicks(20L).execute(consecutiveExecutor).submit(this); getLog().info("Successfully loaded."); }