Skip to content

Commit

Permalink
feat: improve general command execution flow
Browse files Browse the repository at this point in the history
  • Loading branch information
titivermeesch committed Sep 19, 2024
1 parent 30e6619 commit c983d78
Show file tree
Hide file tree
Showing 8 changed files with 61 additions and 69 deletions.
4 changes: 2 additions & 2 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ java {


group = 'me.playbosswar.com'
version = '8.7.5'
version = '8.8.0'
description = 'CommandTimer'

repositories {
Expand Down Expand Up @@ -62,7 +62,7 @@ publishing {
maven(MavenPublication) {
groupId = 'me.playbosswar.com'
artifactId = 'commandtimer-java21'
version = '8.7.5'
version = '8.8.0'

from components.java
}
Expand Down
4 changes: 2 additions & 2 deletions java17-build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ java {


group = 'me.playbosswar.com'
version = '8.7.5'
version = '8.8.0'
description = 'CommandTimer'

repositories {
Expand Down Expand Up @@ -62,7 +62,7 @@ publishing {
maven(MavenPublication) {
groupId = 'me.playbosswar.com'
artifactId = 'commandtimer-java17'
version = '8.7.5'
version = '8.8.0'

from components.java
}
Expand Down
4 changes: 2 additions & 2 deletions java8-build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ java {
}

group = 'me.playbosswar.com'
version = '8.7.5'
version = '8.8.0'
description = 'CommandTimer'

repositories {
Expand Down Expand Up @@ -69,7 +69,7 @@ publishing {
maven(MavenPublication) {
groupId = 'me.playbosswar.com'
artifactId = 'commandtimer-java8'
version = '8.7.5'
version = '8.8.0'

from components.java
}
Expand Down
8 changes: 5 additions & 3 deletions src/main/java/me/playbosswar/com/commands/MainCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ public boolean onCommand(@NotNull CommandSender sender, @NotNull Command cmd, @N
final int[] accumulatedDelaySeconds = {0};
task.getCommands().forEach(command -> {
Bukkit.getScheduler().scheduleSyncDelayedTask(CommandTimerPlugin.getPlugin(),
() -> tasksManager.addTaskCommandExecution(command), 20L * accumulatedDelaySeconds[0]);
() -> tasksManager.processCommandExecution(command), 20L * accumulatedDelaySeconds[0]);
accumulatedDelaySeconds[0] += command.getDelay().toSeconds();
});
Messages.sendMessage(sender, languageManager.get(LanguageKey.TASK_EXECUTION_ONGOING));
Expand All @@ -140,9 +140,11 @@ public boolean onCommand(@NotNull CommandSender sender, @NotNull Command cmd, @N

int selectedCommandIndex = tasksManager.getNextTaskCommandIndex(task);
if(selectedCommandIndex == -1) {
task.getCommands().forEach(tasksManager::addTaskCommandExecution);
Bukkit.getScheduler().runTask(CommandTimerPlugin.getPlugin(), () ->
task.getCommands().forEach(tasksManager::processCommandExecution));
} else {
tasksManager.addTaskCommandExecution(task.getCommands().get(selectedCommandIndex));
Bukkit.getScheduler().runTask(CommandTimerPlugin.getPlugin(), () ->
tasksManager.processCommandExecution(task.getCommands().get(selectedCommandIndex)));
}

Messages.sendMessage(sender, languageManager.get(LanguageKey.TASK_EXECUTED));
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
package me.playbosswar.com.tasks;

import me.playbosswar.com.CommandTimerPlugin;

import java.util.Date;
import org.bukkit.Bukkit;

public class CommandIntervalExecutorRunnable implements Runnable {
private final Task task;
Expand All @@ -20,10 +19,8 @@ public void run() {
return;
}

tasksManager.addTaskCommandExecution(task.getCommands().get(commandIndex));
task.setLastExecuted(new Date());
task.setTimesExecuted(task.getTimesExecuted() + 1);
task.storeInstance();
Bukkit.getScheduler().runTask(CommandTimerPlugin.getInstance(),
() -> tasksManager.processCommandExecution(task.getCommands().get(commandIndex)));
commandIndex++;

if(commandIndex == task.getCommands().size()) {
Expand Down
17 changes: 7 additions & 10 deletions src/main/java/me/playbosswar/com/tasks/TaskRunner.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@
import io.sentry.Sentry;
import me.playbosswar.com.CommandTimerPlugin;
import me.playbosswar.com.enums.CommandExecutionMode;
import me.playbosswar.com.utils.TaskTimeUtils;
import me.playbosswar.com.utils.Tools;
import me.playbosswar.com.utils.Messages;
import me.playbosswar.com.utils.TaskTimeUtils;
import me.playbosswar.com.utils.TaskUtils;
import me.playbosswar.com.utils.Tools;
import org.bukkit.Bukkit;
import org.bukkit.World;

Expand Down Expand Up @@ -148,27 +148,24 @@ private void processTask(Task task) {
final int[] accumulatedDelaySeconds = {0};
task.getCommands().forEach(command -> {
Bukkit.getScheduler().scheduleSyncDelayedTask(CommandTimerPlugin.getPlugin(),
() -> tasksManager.addTaskCommandExecution(command), 20L * accumulatedDelaySeconds[0]);
() -> tasksManager.processCommandExecution(command), 20L * accumulatedDelaySeconds[0]);
accumulatedDelaySeconds[0] += command.getDelay().toSeconds();
});
task.setLastExecuted(new Date());
task.setTimesExecuted(task.getTimesExecuted() + 1);
task.storeInstance();
return;
}

// If it remains -1, that means that all commands should be executed
int selectedCommandIndex = tasksManager.getNextTaskCommandIndex(task);
task.setLastExecuted(new Date());
task.setTimesExecuted(task.getTimesExecuted() + 1);

if(selectedCommandIndex == -1) {
task.setLastExecutedCommandIndex(0);
task.getCommands().forEach(tasksManager::addTaskCommandExecution);
Bukkit.getScheduler().runTask(CommandTimerPlugin.getPlugin(),
() -> task.getCommands().forEach(tasksManager::processCommandExecution));
} else {
TaskCommand taskCommand = task.getCommands().get(selectedCommandIndex);
task.setLastExecutedCommandIndex(task.getCommands().indexOf(taskCommand));
tasksManager.addTaskCommandExecution(taskCommand);
Bukkit.getScheduler().runTask(CommandTimerPlugin.getPlugin(),
() -> tasksManager.processCommandExecution(taskCommand));
}

task.storeInstance();
Expand Down
82 changes: 39 additions & 43 deletions src/main/java/me/playbosswar/com/tasks/TasksManager.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package me.playbosswar.com.tasks;

import me.playbosswar.com.CommandTimerPlugin;
import me.playbosswar.com.conditionsengine.validations.Condition;
import me.playbosswar.com.enums.CommandExecutionMode;
import me.playbosswar.com.enums.Gender;
import me.playbosswar.com.hooks.PAPIHook;
Expand All @@ -14,7 +13,6 @@
import org.bukkit.OfflinePlayer;
import org.bukkit.command.CommandException;
import org.bukkit.entity.Player;
import org.bukkit.scheduler.BukkitRunnable;
import org.jetbrains.annotations.Nullable;

import java.io.IOException;
Expand All @@ -26,15 +24,13 @@
public class TasksManager {
private static final String CONDITION_NO_MATCH = "Conditions did not match";
private List<Task> loadedTasks = new ArrayList<>();
private final List<TaskCommand> scheduledExecutions = new ArrayList<>();
private Thread runnerThread;
public boolean stopRunner = false;
public int executionsSinceLastSync = 0;

public TasksManager() {
loadedTasks.addAll(Files.deserializeJsonFilesIntoCommandTimers());
startRunner();
startCommandExecutor();
}

public Task createTask() {
Expand Down Expand Up @@ -80,7 +76,7 @@ private void startRunner() {
this.runnerThread = thread;
}

private void runConsolePerUserCommand(TaskCommand taskCommand, List<UUID> scopedPlayers) throws CommandException {
private boolean runConsolePerUserCommand(TaskCommand taskCommand, List<UUID> scopedPlayers) throws CommandException {
String command = taskCommand.getCommand();

Collection<Player> affectedPlayers = (Collection<Player>) Bukkit.getOnlinePlayers();
Expand All @@ -91,13 +87,15 @@ private void runConsolePerUserCommand(TaskCommand taskCommand, List<UUID> scoped

boolean delayedExecutions = taskCommand.getInterval().toSeconds() > 0;
int i = 0;
boolean willExecute = false;
for(Player p : affectedPlayers) {
if(taskCommand.getTask().hasCondition()) {
boolean valid = TaskValidationHelpers.processCondition(taskCommand.getTask().getCondition(), p);
if(!valid) {
Messages.sendDebugConsole(CONDITION_NO_MATCH);
continue;
}
willExecute = true;
}

if(delayedExecutions) {
Expand All @@ -111,9 +109,11 @@ private void runConsolePerUserCommand(TaskCommand taskCommand, List<UUID> scoped
}
i++;
}

return willExecute;
}

private void runConsolePerUserOfflineCommand(TaskCommand taskCommand) throws CommandException {
private boolean runConsolePerUserOfflineCommand(TaskCommand taskCommand) throws CommandException {
String command = taskCommand.getCommand();
boolean delayedExecutions = taskCommand.getInterval().toSeconds() > 0;

Expand All @@ -131,27 +131,30 @@ private void runConsolePerUserOfflineCommand(TaskCommand taskCommand) throws Com
}
i++;
}

return true;
}

private void runConsolePerUserCommand(TaskCommand taskCommand) throws CommandException {
runConsolePerUserCommand(taskCommand, new ArrayList<>());
private boolean runConsolePerUserCommand(TaskCommand taskCommand) throws CommandException {
return runConsolePerUserCommand(taskCommand, new ArrayList<>());
}

private void runConsoleCommand(TaskCommand taskCommand) throws CommandException {
private boolean runConsoleCommand(TaskCommand taskCommand) throws CommandException {
if(taskCommand.getTask().hasCondition()) {
boolean valid = TaskValidationHelpers.processCondition(taskCommand.getTask().getCondition(), null);
if(!valid) {
Messages.sendDebugConsole(CONDITION_NO_MATCH);
return;
return false;
}
}

String command = taskCommand.getCommand();
Bukkit.getServer().dispatchCommand(Bukkit.getConsoleSender(), PAPIHook.parsePAPI(command, null));
executionsSinceLastSync++;
return true;
}

private void runPlayerCommand(TaskCommand taskCommand, List<UUID> scopedPlayers) {
private boolean runPlayerCommand(TaskCommand taskCommand, List<UUID> scopedPlayers) {
String command = taskCommand.getCommand();

Collection<Player> affectedPlayers = (Collection<Player>) Bukkit.getOnlinePlayers();
Expand All @@ -162,13 +165,15 @@ private void runPlayerCommand(TaskCommand taskCommand, List<UUID> scopedPlayers)

boolean delayedExecution = taskCommand.getInterval().toSeconds() > 0;
int i = 0;
boolean willExecute = false;
for(Player p : affectedPlayers) {
if(taskCommand.getTask().hasCondition()) {
boolean valid = TaskValidationHelpers.processCondition(taskCommand.getTask().getCondition(), p);
if(!valid) {
Messages.sendDebugConsole(CONDITION_NO_MATCH);
continue;
}
willExecute = true;
}

if(delayedExecution) {
Expand All @@ -179,6 +184,8 @@ private void runPlayerCommand(TaskCommand taskCommand, List<UUID> scopedPlayers)
}
i++;
}

return willExecute;
}

private void runForPlayer(Player p, String command) {
Expand All @@ -193,11 +200,11 @@ private void runForPlayer(Player p, String command) {
executionsSinceLastSync++;
}

private void runPlayerCommand(TaskCommand taskCommand) {
runPlayerCommand(taskCommand, new ArrayList<>());
private boolean runPlayerCommand(TaskCommand taskCommand) {
return runPlayerCommand(taskCommand, new ArrayList<>());
}

private void runOperatorCommand(TaskCommand taskCommand, List<UUID> scopedPlayers) {
private boolean runOperatorCommand(TaskCommand taskCommand, List<UUID> scopedPlayers) {
String command = taskCommand.getCommand();

Collection<Player> affectedPlayers = (Collection<Player>) Bukkit.getOnlinePlayers();
Expand All @@ -208,6 +215,7 @@ private void runOperatorCommand(TaskCommand taskCommand, List<UUID> scopedPlayer

boolean delayedExecutions = taskCommand.getInterval().toSeconds() > 0;
int i = 0;
boolean willExecute = false;
for(Player p : affectedPlayers) {
boolean wasAlreadyOp = p.isOp();

Expand All @@ -223,6 +231,7 @@ private void runOperatorCommand(TaskCommand taskCommand, List<UUID> scopedPlayer
}
continue;
}
willExecute = true;
}

if(delayedExecutions) {
Expand All @@ -242,53 +251,40 @@ private void runOperatorCommand(TaskCommand taskCommand, List<UUID> scopedPlayer
}
i++;
}
}

private void runOperatorCommand(TaskCommand taskCommand) {
runOperatorCommand(taskCommand, new ArrayList<>());
return willExecute;
}

public void addTaskCommandExecution(TaskCommand taskCommand) {
scheduledExecutions.add(taskCommand);
private boolean runOperatorCommand(TaskCommand taskCommand) {
return runOperatorCommand(taskCommand, new ArrayList<>());
}

public void processCommandExecution(TaskCommand taskCommand) {
if(!taskCommand.getTask().isActive()) {
Task task = taskCommand.getTask();
if(!task.isActive()) {
return;
}

Gender gender = taskCommand.getGender();

boolean executed = false;
if(gender.equals(Gender.CONSOLE)) {
runConsoleCommand(taskCommand);
executed = runConsoleCommand(taskCommand);
} else if(gender.equals(Gender.PLAYER)) {
runPlayerCommand(taskCommand);
executed = runPlayerCommand(taskCommand);
} else if(gender.equals(Gender.OPERATOR)) {
runOperatorCommand(taskCommand);
executed = runOperatorCommand(taskCommand);
} else if(gender.equals(Gender.CONSOLE_PER_USER)) {
runConsolePerUserCommand(taskCommand);
executed = runConsolePerUserCommand(taskCommand);
} else if(gender.equals(Gender.CONSOLE_PER_USER_OFFLINE)) {
runConsolePerUserOfflineCommand(taskCommand);
executed = runConsolePerUserOfflineCommand(taskCommand);
}
}

// Executes scheduled commands
private void startCommandExecutor() {
BukkitRunnable runnable = new BukkitRunnable() {

@Override
public void run() {
final List<TaskCommand> tasksToRemove = new ArrayList<>(scheduledExecutions);

tasksToRemove.forEach(taskCommand -> {
processCommandExecution(taskCommand);
scheduledExecutions.remove(taskCommand);
});

}
};

runnable.runTaskTimer(CommandTimerPlugin.getPlugin(), 20L, 20L);
if(executed) {
task.setLastExecuted(new Date());
task.setTimesExecuted(taskCommand.getTask().getTimesExecuted() + 1);
task.storeInstance();
}
}

public int getNextTaskCommandIndex(Task task) {
Expand Down
2 changes: 1 addition & 1 deletion src/main/resources/plugin.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
main: me.playbosswar.com.CommandTimerPlugin
name: "CommandTimer"
version: "8.7.5"
version: "8.8.0"
description: "Schedule commands like you want"
author: PlayBossWar
api-version: 1.13
Expand Down

0 comments on commit c983d78

Please sign in to comment.