use of me.lucko.luckperms.common.plugin.scheduler.SchedulerTask in project LuckPerms by lucko.
the class PluginMessageMessenger method sendOutgoingMessage.
@Override
public void sendOutgoingMessage(@NonNull OutgoingMessage outgoingMessage) {
AtomicReference<SchedulerTask> taskRef = new AtomicReference<>();
SchedulerTask task = this.plugin.getBootstrap().getScheduler().asyncRepeating(() -> {
MinecraftServer server = this.plugin.getBootstrap().getServer().orElse(null);
if (server == null) {
return;
}
Collection<ServerPlayerEntity> players = server.getPlayerManager().getPlayerList();
ServerPlayerEntity p = Iterables.getFirst(players, null);
if (p == null) {
return;
}
PacketByteBuf buf = PacketByteBufs.create();
buf.writeString(outgoingMessage.asEncodedString());
ServerPlayNetworking.send(p, CHANNEL, buf);
SchedulerTask t = taskRef.getAndSet(null);
if (t != null) {
t.cancel();
}
}, 10, TimeUnit.SECONDS);
taskRef.set(task);
}
use of me.lucko.luckperms.common.plugin.scheduler.SchedulerTask in project LuckPerms by lucko.
the class CommandManager method executeCommand.
public CompletableFuture<Void> executeCommand(Sender sender, String label, List<String> args) {
SchedulerAdapter scheduler = this.plugin.getBootstrap().getScheduler();
List<String> argsCopy = new ArrayList<>(args);
// if the executingCommand flag is set, there is another command executing at the moment
if (this.executingCommand.get()) {
Message.ALREADY_EXECUTING_COMMAND.send(sender);
}
// a reference to the thread being used to execute the command
AtomicReference<Thread> executorThread = new AtomicReference<>();
// a reference to the timeout task scheduled to catch if this command takes too long to execute
AtomicReference<SchedulerTask> timeoutTask = new AtomicReference<>();
// schedule the actual execution of the command using the command executor service
CompletableFuture<Void> future = CompletableFuture.runAsync(() -> {
// set flags
executorThread.set(Thread.currentThread());
this.executingCommand.set(true);
// actually try to execute the command
try {
execute(sender, label, args);
} catch (Throwable e) {
// catch any exception
this.plugin.getLogger().severe("Exception whilst executing command: " + args, e);
} finally {
// unset flags
this.executingCommand.set(false);
executorThread.set(null);
// cancel the timeout task
SchedulerTask timeout;
if ((timeout = timeoutTask.get()) != null) {
timeout.cancel();
}
}
}, this.executor);
// schedule another task to catch if the command doesn't complete after 10 seconds
timeoutTask.set(scheduler.asyncLater(() -> {
if (!future.isDone()) {
handleCommandTimeout(executorThread, argsCopy);
}
}, 10, TimeUnit.SECONDS));
return future;
}
use of me.lucko.luckperms.common.plugin.scheduler.SchedulerTask in project LuckPerms by lucko.
the class SpongeSchedulerAdapter method submitAsyncTask.
private SchedulerTask submitAsyncTask(Runnable runnable, Consumer<Task.Builder> config) {
Task.Builder builder = Task.builder();
config.accept(builder);
Task task = builder.execute(runnable).plugin(this.pluginContainer).build();
ScheduledTask scheduledTask = this.asyncScheduler.submit(task);
this.tasks.add(scheduledTask);
return scheduledTask::cancel;
}
use of me.lucko.luckperms.common.plugin.scheduler.SchedulerTask in project LuckPerms by lucko.
the class SqlMessenger method close.
@Override
public void close() {
SchedulerTask task = this.pollTask;
if (task != null) {
task.cancel();
}
task = this.housekeepingTask;
if (task != null) {
task.cancel();
}
this.pollTask = null;
this.housekeepingTask = null;
super.close();
}
Aggregations