Java Code Examples for org.spongepowered.api.scheduler.Task#Builder

The following examples show how to use org.spongepowered.api.scheduler.Task#Builder . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: HomeCommand.java    From EagleFactions with MIT License 6 votes vote down vote up
private void startHomeCooldown(UUID playerUUID)
{
    EagleFactionsPlugin.HOME_COOLDOWN_PLAYERS.put(playerUUID, this.factionsConfig.getHomeCooldown());

    final Task.Builder taskBuilder = Sponge.getScheduler().createTaskBuilder();

    taskBuilder.interval(1, TimeUnit.SECONDS).execute(task ->
    {
        if (EagleFactionsPlugin.HOME_COOLDOWN_PLAYERS.containsKey(playerUUID))
        {
            int seconds = EagleFactionsPlugin.HOME_COOLDOWN_PLAYERS.get(playerUUID);

            if (seconds < 1)
            {
                EagleFactionsPlugin.HOME_COOLDOWN_PLAYERS.remove(playerUUID);
                task.cancel();
            }
            EagleFactionsPlugin.HOME_COOLDOWN_PLAYERS.replace(playerUUID, seconds, seconds - 1);
        }
    }).submit(super.getPlugin());
}
 
Example 2
Source File: AllyCommand.java    From EagleFactions with MIT License 6 votes vote down vote up
private void sendInvite(final Player player, final Faction playerFaction, final Faction targetFaction) throws CommandException
{
	final AllyRequest invite = new AllyRequest(playerFaction.getName(), targetFaction.getName());
	if(EagleFactionsPlugin.ALLY_INVITE_LIST.contains(invite))
		throw new CommandException(Text.of(PluginInfo.ERROR_PREFIX, Messages.YOU_HAVE_ALREADY_INVITED_THIS_FACTION_TO_THE_ALLIANCE));

	EagleFactionsPlugin.ALLY_INVITE_LIST.add(invite);

	final Optional<Player> optionalInvitedFactionLeader = super.getPlugin().getPlayerManager().getPlayer(targetFaction.getLeader());

	optionalInvitedFactionLeader.ifPresent(x-> optionalInvitedFactionLeader.get().sendMessage(getInviteGetMessage(playerFaction)));
	targetFaction.getOfficers().forEach(x-> super.getPlugin().getPlayerManager().getPlayer(x).ifPresent(y-> getInviteGetMessage(playerFaction)));

	player.sendMessage(Text.of(PluginInfo.PLUGIN_PREFIX, MessageLoader.parseMessage(Messages.YOU_HAVE_INVITED_FACTION_TO_THE_ALLIANCE, TextColors.GREEN, ImmutableMap.of(Placeholders.FACTION_NAME, Text.of(TextColors.GOLD, targetFaction.getName())))));

	final Task.Builder taskBuilder = Sponge.getScheduler().createTaskBuilder();
	taskBuilder.execute(() -> EagleFactionsPlugin.ALLY_INVITE_LIST.remove(invite)).delay(2, TimeUnit.MINUTES).name("EagleFaction - Remove Invite").submit(super.getPlugin());
}
 
Example 3
Source File: Metrics.java    From EagleFactions with MIT License 6 votes vote down vote up
private void startSubmitting()
{
    // We use a timer cause want to be independent from the server tps
    final Timer timer = new Timer(true);
    timerTask = new TimerTask() {
        @Override
        public void run() {
            // The data collection (e.g. for custom graphs) is done sync
            // Don't be afraid! The connection to the bStats server is still async, only the stats collection is sync ;)
            Scheduler scheduler = Sponge.getScheduler();
            Task.Builder taskBuilder = scheduler.createTaskBuilder();
            taskBuilder.execute(() -> submitData()).submit(plugin);
        }
    };
    timer.scheduleAtFixedRate(timerTask, 1000 * 60 * 5, 1000 * 60 * 30);
    // Submit the data every 30 minutes, first time after 5 minutes to give other plugins enough time to start
    // WARNING: Changing the frequency has no effect but your plugin WILL be blocked/deleted!
    // WARNING: Just don't do it!
}
 
Example 4
Source File: AttackLogicImpl.java    From EagleFactions with MIT License 6 votes vote down vote up
@Override
public void runClaimingRestorer(String factionName)
{

    Task.Builder taskBuilder = Sponge.getScheduler().createTaskBuilder();

    taskBuilder.interval(1, TimeUnit.SECONDS).execute(task ->
    {
        if(EagleFactionsPlugin.ATTACKED_FACTIONS.containsKey(factionName))
        {
            int seconds = EagleFactionsPlugin.ATTACKED_FACTIONS.get(factionName);

            if (seconds <= 0)
            {
                EagleFactionsPlugin.ATTACKED_FACTIONS.remove(factionName);
                task.cancel();
            }
            else
            {
                EagleFactionsPlugin.ATTACKED_FACTIONS.replace(factionName, seconds, seconds - 1);
            }
        }
    }).submit(EagleFactionsPlugin.getPlugin());
}
 
Example 5
Source File: AttackLogicImpl.java    From EagleFactions with MIT License 6 votes vote down vote up
@Override
public void runHomeUsageRestorer(final UUID playerUUID)
{
    Task.Builder taskBuilder = Sponge.getScheduler().createTaskBuilder();

    taskBuilder.interval(1, TimeUnit.SECONDS).execute(task ->
    {
        if (EagleFactionsPlugin.BLOCKED_HOME.containsKey(playerUUID))
        {
            int seconds = EagleFactionsPlugin.BLOCKED_HOME.get(playerUUID);

            if (seconds <= 0)
            {
                EagleFactionsPlugin.BLOCKED_HOME.remove(playerUUID);
                task.cancel();
            }
            else
            {
                EagleFactionsPlugin.BLOCKED_HOME.replace(playerUUID, seconds, seconds - 1);
            }
        }
    }).submit(EagleFactionsPlugin.getPlugin());
}
 
Example 6
Source File: SpongeTaskMan.java    From FastAsyncWorldedit with GNU General Public License v3.0 5 votes vote down vote up
@Override
public int repeat(Runnable runnable, int interval) {
    int val = this.i.incrementAndGet();
    Task.Builder builder = Sponge.getGame().getScheduler().createTaskBuilder();
    Task.Builder built = builder.delayTicks(interval).intervalTicks(interval).execute(runnable);
    Task task = built.submit(plugin);
    this.tasks.put(val, task);
    return val;
}
 
Example 7
Source File: EagleFactionsScheduler.java    From EagleFactions with MIT License 5 votes vote down vote up
public void scheduleWithDelayedIntervalAsync(EagleFactionsRunnableTask task, long delay, TimeUnit delayUnit, long interval, TimeUnit intervalUnit)
{
    Task.Builder taskBuilder = Sponge.getScheduler().createTaskBuilder();
    taskBuilder.delay(delay, delayUnit)
            .interval(interval, intervalUnit)
            .execute(task).async().submit(EagleFactionsPlugin.getPlugin());
}
 
Example 8
Source File: SpongeTaskMan.java    From FastAsyncWorldedit with GNU General Public License v3.0 5 votes vote down vote up
@Override
public int repeat(Runnable runnable, int interval) {
    int val = this.i.incrementAndGet();
    Task.Builder builder = Sponge.getGame().getScheduler().createTaskBuilder();
    Task.Builder built = builder.delayTicks(interval).intervalTicks(interval).execute(runnable);
    Task task = built.submit(plugin);
    this.tasks.put(val, task);
    return val;
}
 
Example 9
Source File: SpongeTaskMan.java    From FastAsyncWorldedit with GNU General Public License v3.0 5 votes vote down vote up
@Override
public int repeatAsync(Runnable runnable, int interval) {
    int val = this.i.incrementAndGet();
    Task.Builder builder = Sponge.getGame().getScheduler().createTaskBuilder();
    Task.Builder built = builder.delayTicks(interval).async().intervalTicks(interval).execute(runnable);
    Task task = built.submit(plugin);
    this.tasks.put(val, task);
    return val;
}
 
Example 10
Source File: AttackLogicImpl.java    From EagleFactions with MIT License 5 votes vote down vote up
@Override
public void attack(final Player player, final Vector3i attackedChunk)
{
    Task.Builder taskBuilder = Sponge.getScheduler().createTaskBuilder();

    taskBuilder.interval(1, TimeUnit.SECONDS).execute(new Consumer<Task>()
    {
        int seconds = 1;

        @Override
        public void accept(Task task)
        {
            if(attackedChunk.toString().equals(player.getLocation().getChunkPosition().toString()))
            {
                if(seconds == factionsConfig.getAttackTime())
                {
                    //Because it is not possible to attack territory that is not claimed then we can safely get faction here.
                    Faction chunkFaction = factionLogic.getFactionByChunk(player.getWorld().getUniqueId(), attackedChunk).get();

                    informAboutDestroying(chunkFaction);
                    player.sendMessage(Text.of(PluginInfo.PLUGIN_PREFIX, TextColors.GREEN, Messages.CLAIM_DESTROYED));

                    final Claim claim = new Claim(player.getWorld().getUniqueId(), attackedChunk);
                    factionLogic.destroyClaim(chunkFaction, claim);
                    task.cancel();
                }
                else
                {
                    player.sendMessage(Text.of(PluginInfo.PLUGIN_PREFIX, TextColors.RESET, seconds));
                    seconds++;
                }
            }
            else
            {
                player.sendMessage(Text.of(PluginInfo.ERROR_PREFIX, TextColors.RED, Messages.YOU_MOVED_FROM_THE_CHUNK));
                task.cancel();
            }
        }
    }).submit(EagleFactionsPlugin.getPlugin());
}
 
Example 11
Source File: SpongeMetrics.java    From FastAsyncWorldedit with GNU General Public License v3.0 4 votes vote down vote up
/**
 * Start measuring statistics. This will immediately create an async repeating task as the plugin and send the
 * initial data to the metrics backend, and then after that it will post in increments of PING_INTERVAL * 1200
 * ticks.
 *
 * @return True if statistics measuring is running, otherwise false.
 */
public boolean start() {
    synchronized (optOutLock) {
        // Did we opt out?
        if (isOptOut()) {
            return false;
        }

        // Is metrics already running?
        if (task != null) {
            return true;
        }

        // Begin hitting the server with glorious data
        final Task.Builder builder = game.getScheduler().createTaskBuilder();
        builder.async().interval(PING_INTERVAL, TimeUnit.MINUTES).execute(new Runnable() {

            private boolean firstPost = true;

            @Override
            public void run() {
                try {
                    // This has to be synchronized or it can collide with the disable method.
                    synchronized (optOutLock) {
                        // Disable Task, if it is running and the server owner decided to opt-out
                        if (isOptOut() && (task != null)) {
                            task.cancel();
                            task = null;
                        }
                    }

                    // We use the inverse of firstPost because if it is the first time we are posting,
                    // it is not a interval ping, so it evaluates to FALSE
                    // Each time thereafter it will evaluate to TRUE, i.e PING!
                    postPlugin(!firstPost);

                    // After the first post we set firstPost to false
                    // Each post thereafter will be a ping
                    firstPost = false;
                } catch (final IOException e) {
                    if (debug) {
                        Fawe.debug("[Metrics] " + e.getMessage());
                    }
                }
            }
        });
        return true;
    }
}
 
Example 12
Source File: EagleFactionsScheduler.java    From EagleFactions with MIT License 4 votes vote down vote up
public void scheduleWithDelayAsync(EagleFactionsRunnableTask task, long delay)
{
    Task.Builder taskBuilder = Sponge.getScheduler().createTaskBuilder();
    taskBuilder.delay(delay, TimeUnit.SECONDS).execute(task).async().submit(EagleFactionsPlugin.getPlugin());
}
 
Example 13
Source File: SpongeScheduler.java    From NuVotifier with GNU General Public License v3.0 4 votes vote down vote up
private Task.Builder taskBuilder(Runnable runnable) {
    return Sponge.getScheduler().createTaskBuilder().execute(runnable);
}
 
Example 14
Source File: VirtualChestActions.java    From VirtualChest with GNU Lesser General Public License v3.0 4 votes vote down vote up
private static void enable(Object plugin)
{
    Optional.ofNullable(task).ifPresent(Task::cancel);
    Task.Builder builder = Sponge.getScheduler().createTaskBuilder().intervalTicks(1);
    task = builder.name("VirtualChestTitleManager").execute(TitleManager::sendTitle).submit(plugin);
}
 
Example 15
Source File: SpongeMetrics.java    From FastAsyncWorldedit with GNU General Public License v3.0 4 votes vote down vote up
/**
 * Start measuring statistics. This will immediately create an async repeating task as the plugin and send the
 * initial data to the metrics backend, and then after that it will post in increments of PING_INTERVAL * 1200
 * ticks.
 *
 * @return True if statistics measuring is running, otherwise false.
 */
public boolean start() {
    synchronized (optOutLock) {
        // Did we opt out?
        if (isOptOut()) {
            return false;
        }

        // Is metrics already running?
        if (task != null) {
            return true;
        }

        // Begin hitting the server with glorious data
        final Task.Builder builder = game.getScheduler().createTaskBuilder();
        builder.async().interval(PING_INTERVAL, TimeUnit.MINUTES).execute(new Runnable() {

            private boolean firstPost = true;

            @Override
            public void run() {
                try {
                    // This has to be synchronized or it can collide with the disable method.
                    synchronized (optOutLock) {
                        // Disable Task, if it is running and the server owner decided to opt-out
                        if (isOptOut() && (task != null)) {
                            task.cancel();
                            task = null;
                        }
                    }

                    // We use the inverse of firstPost because if it is the first time we are posting,
                    // it is not a interval ping, so it evaluates to FALSE
                    // Each time thereafter it will evaluate to TRUE, i.e PING!
                    postPlugin(!firstPost);

                    // After the first post we set firstPost to false
                    // Each post thereafter will be a ping
                    firstPost = false;
                } catch (final IOException e) {
                    if (debug) {
                        Fawe.debug("[Metrics] " + e.getMessage());
                    }
                }
            }
        });
        return true;
    }
}
 
Example 16
Source File: InviteCommand.java    From EagleFactions with MIT License 4 votes vote down vote up
@Override
public CommandResult execute(final CommandSource source, final CommandContext context) throws CommandException
{
    final Player invitedPlayer = context.requireOne("player");

    if(!(source instanceof Player))
        throw new CommandException(Text.of(PluginInfo.ERROR_PREFIX, TextColors.RED, Messages.ONLY_IN_GAME_PLAYERS_CAN_USE_THIS_COMMAND));

    final Player senderPlayer = (Player)source;
    final Optional<Faction> optionalSenderFaction = getPlugin().getFactionLogic().getFactionByPlayerUUID(senderPlayer.getUniqueId());

    if(!optionalSenderFaction.isPresent())
        throw new CommandException(Text.of(PluginInfo.ERROR_PREFIX, TextColors.RED, Messages.YOU_MUST_BE_IN_FACTION_IN_ORDER_TO_USE_THIS_COMMAND));

    final Faction senderFaction = optionalSenderFaction.get();

    if (!super.getPlugin().getPermsManager().canInvite(senderPlayer.getUniqueId(), senderFaction))
        throw new CommandException(Text.of(PluginInfo.ERROR_PREFIX, TextColors.RED, Messages.PLAYERS_WITH_YOUR_RANK_CANT_INVITE_PLAYERS_TO_FACTION));

    if(this.factionsConfig.isPlayerLimit())
    {
        int playerCount = 0;
        playerCount += senderFaction.getLeader().toString().equals("") ? 0 : 1;
        playerCount += senderFaction.getOfficers().isEmpty() ? 0 : senderFaction.getOfficers().size();
        playerCount += senderFaction.getMembers().isEmpty() ? 0 : senderFaction.getMembers().size();
        playerCount += senderFaction.getRecruits().isEmpty() ? 0 : senderFaction.getRecruits().size();

        if(playerCount >= this.factionsConfig.getPlayerLimit())
        {
            senderPlayer.sendMessage(Text.of(PluginInfo.ERROR_PREFIX, TextColors.RED, Messages.YOU_CANT_INVITE_MORE_PLAYERS_TO_YOUR_FACTION + " " + Messages.FACTIONS_PLAYER_LIMIT_HAS_BEEN_REACHED));
            return CommandResult.success();
        }
    }

    if(super.getPlugin().getFactionLogic().getFactionByPlayerUUID(invitedPlayer.getUniqueId()).isPresent())
        throw new CommandException(Text.of(PluginInfo.ERROR_PREFIX, TextColors.RED, Messages.PLAYER_IS_ALREADY_IN_A_FACTION));

    final Invite invite = new Invite(senderFaction.getName(), invitedPlayer.getUniqueId());
    EagleFactionsPlugin.INVITE_LIST.add(invite);

    invitedPlayer.sendMessage(getInviteGetMessage(senderFaction));
    senderPlayer.sendMessage(Text.of(PluginInfo.PLUGIN_PREFIX,TextColors.GREEN, Messages.YOU_INVITED + " ", TextColors.GOLD, invitedPlayer.getName(), TextColors.GREEN, " " + Messages.TO_YOUR_FACTION));

    final Task.Builder taskBuilder = Sponge.getScheduler().createTaskBuilder();

    taskBuilder.execute(() -> EagleFactionsPlugin.INVITE_LIST.remove(invite)).delay(2, TimeUnit.MINUTES).name("EagleFaction - Remove Invite").submit(EagleFactionsPlugin.getPlugin());
    return CommandResult.success();
}
 
Example 17
Source File: SpongeTaskMan.java    From FastAsyncWorldedit with GNU General Public License v3.0 4 votes vote down vote up
@Override
public void async(Runnable runnable) {
    Task.Builder builder = Sponge.getGame().getScheduler().createTaskBuilder();
    builder.async().execute(runnable).submit(plugin);
}
 
Example 18
Source File: SpongeTaskMan.java    From FastAsyncWorldedit with GNU General Public License v3.0 4 votes vote down vote up
@Override
public void later(Runnable runnable, int delay) {
    Task.Builder builder = Sponge.getGame().getScheduler().createTaskBuilder();
    builder.delayTicks(delay).execute(runnable).submit(plugin);
}
 
Example 19
Source File: EagleFactionsScheduler.java    From EagleFactions with MIT License 4 votes vote down vote up
public void scheduleWithDelay(EagleFactionsRunnableTask task, long delay)
{
    Task.Builder taskBuilder = Sponge.getScheduler().createTaskBuilder();
    taskBuilder.delay(delay, TimeUnit.SECONDS).execute(task).submit(EagleFactionsPlugin.getPlugin());
}
 
Example 20
Source File: EagleFactionsScheduler.java    From EagleFactions with MIT License 4 votes vote down vote up
public void scheduleWithDelayAsync(EagleFactionsConsumerTask<Task> task, long delay)
{
    Task.Builder taskBuilder = Sponge.getScheduler().createTaskBuilder();
    taskBuilder.delay(delay, TimeUnit.SECONDS).execute(task).async().submit(EagleFactionsPlugin.getPlugin());
}