Add support for delaying commands.

This commit is contained in:
DNx 2018-01-26 14:28:02 +07:00
parent dfb2d72d28
commit f7e9ac9685
3 changed files with 40 additions and 8 deletions

View File

@ -9,6 +9,8 @@ public class Command {
private String command;
/** The executor of the command. */
private Executor executor = Executor.PLAYER;
/** Delay before executing the command (in ticks) */
private long delay = 0;
/**
* Default constructor (for bean mapping).
@ -27,6 +29,19 @@ public class Command {
this.executor = executor;
}
/**
* Constructor.
*
* @param command the command
* @param executor the executor of the command
* @param delay the delay (in ticks) before executing command
*/
public Command(String command, Executor executor, long delay) {
this.command = command;
this.executor = executor;
this.delay = delay;
}
public String getCommand() {
return command;
}
@ -43,6 +58,14 @@ public class Command {
this.executor = executor;
}
public long getDelay() {
return delay;
}
public void setDelay(int delay) {
this.delay = delay;
}
@Override
public String toString() {
return command + " (" + executor + ")";

View File

@ -127,11 +127,13 @@ public class CommandManager implements Reloadable {
for (T command : commands) {
if (predicate.test(command)) {
final String execution = command.getCommand();
if (Executor.CONSOLE.equals(command.getExecutor())) {
bukkitService.dispatchConsoleCommand(execution);
} else {
bukkitService.dispatchCommand(player, execution);
}
bukkitService.scheduleSyncDelayedTask(() -> {
if (Executor.CONSOLE.equals(command.getExecutor())) {
bukkitService.dispatchConsoleCommand(execution);
} else {
bukkitService.dispatchCommand(player, execution);
}
}, command.getDelay());
}
}
}
@ -175,9 +177,9 @@ public class CommandManager implements Reloadable {
private List<Tag<Player>> buildAvailableTags() {
return Arrays.asList(
createTag("%p", pl -> pl.getName()),
createTag("%nick", pl -> pl.getDisplayName()),
createTag("%ip", pl -> PlayerUtils.getPlayerIp(pl)),
createTag("%p", Player::getName),
createTag("%nick", Player::getDisplayName),
createTag("%ip", PlayerUtils::getPlayerIp),
createTag("%country", pl -> geoIpService.getCountryName(PlayerUtils.getPlayerIp(pl))));
}
}

View File

@ -48,6 +48,13 @@ public final class CommandSettingsHolder implements SettingsHolder {
" command: 'broadcast %p has joined, welcome back!'",
" executor: CONSOLE",
"",
"You can also add delay to command. It will run after the specified ticks. Example:",
"onLogin:",
" rules:",
" command: 'rules'",
" executor: PLAYER",
" delay: 200",
"",
"Supported command events: onLogin, onSessionLogin, onFirstLogin, onJoin, onLogout, onRegister, "
+ "onUnregister",
"",