Search in sources :

Example 1 with SchedulerTask

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);
}
Also used : PacketByteBuf(net.minecraft.network.PacketByteBuf) AtomicReference(java.util.concurrent.atomic.AtomicReference) ServerPlayerEntity(net.minecraft.server.network.ServerPlayerEntity) SchedulerTask(me.lucko.luckperms.common.plugin.scheduler.SchedulerTask) MinecraftServer(net.minecraft.server.MinecraftServer)

Example 2 with SchedulerTask

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;
}
Also used : SchedulerAdapter(me.lucko.luckperms.common.plugin.scheduler.SchedulerAdapter) ArrayList(java.util.ArrayList) AtomicReference(java.util.concurrent.atomic.AtomicReference) SchedulerTask(me.lucko.luckperms.common.plugin.scheduler.SchedulerTask)

Example 3 with SchedulerTask

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;
}
Also used : ScheduledTask(org.spongepowered.api.scheduler.ScheduledTask) SchedulerTask(me.lucko.luckperms.common.plugin.scheduler.SchedulerTask) Task(org.spongepowered.api.scheduler.Task) ScheduledTask(org.spongepowered.api.scheduler.ScheduledTask)

Example 4 with SchedulerTask

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();
}
Also used : SchedulerTask(me.lucko.luckperms.common.plugin.scheduler.SchedulerTask)

Aggregations

SchedulerTask (me.lucko.luckperms.common.plugin.scheduler.SchedulerTask)4 AtomicReference (java.util.concurrent.atomic.AtomicReference)2 ArrayList (java.util.ArrayList)1 SchedulerAdapter (me.lucko.luckperms.common.plugin.scheduler.SchedulerAdapter)1 PacketByteBuf (net.minecraft.network.PacketByteBuf)1 MinecraftServer (net.minecraft.server.MinecraftServer)1 ServerPlayerEntity (net.minecraft.server.network.ServerPlayerEntity)1 ScheduledTask (org.spongepowered.api.scheduler.ScheduledTask)1 Task (org.spongepowered.api.scheduler.Task)1