diff --git a/build.gradle b/build.gradle index 49e7ae6..3764ea9 100644 --- a/build.gradle +++ b/build.gradle @@ -10,7 +10,7 @@ java { group = 'me.playbosswar.com' -version = '8.7.5' +version = '8.8.0' description = 'CommandTimer' repositories { @@ -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 } diff --git a/java17-build.gradle b/java17-build.gradle index edd429e..7fbda39 100644 --- a/java17-build.gradle +++ b/java17-build.gradle @@ -10,7 +10,7 @@ java { group = 'me.playbosswar.com' -version = '8.7.5' +version = '8.8.0' description = 'CommandTimer' repositories { @@ -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 } diff --git a/java8-build.gradle b/java8-build.gradle index 0599c83..b43fd56 100644 --- a/java8-build.gradle +++ b/java8-build.gradle @@ -9,7 +9,7 @@ java { } group = 'me.playbosswar.com' -version = '8.7.5' +version = '8.8.0' description = 'CommandTimer' repositories { @@ -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 } diff --git a/src/main/java/me/playbosswar/com/commands/MainCommand.java b/src/main/java/me/playbosswar/com/commands/MainCommand.java index daf3220..6f08f00 100644 --- a/src/main/java/me/playbosswar/com/commands/MainCommand.java +++ b/src/main/java/me/playbosswar/com/commands/MainCommand.java @@ -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)); @@ -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)); diff --git a/src/main/java/me/playbosswar/com/tasks/CommandIntervalExecutorRunnable.java b/src/main/java/me/playbosswar/com/tasks/CommandIntervalExecutorRunnable.java index 483e47a..cf03c74 100644 --- a/src/main/java/me/playbosswar/com/tasks/CommandIntervalExecutorRunnable.java +++ b/src/main/java/me/playbosswar/com/tasks/CommandIntervalExecutorRunnable.java @@ -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; @@ -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()) { diff --git a/src/main/java/me/playbosswar/com/tasks/TaskRunner.java b/src/main/java/me/playbosswar/com/tasks/TaskRunner.java index 81124c4..4ee92de 100644 --- a/src/main/java/me/playbosswar/com/tasks/TaskRunner.java +++ b/src/main/java/me/playbosswar/com/tasks/TaskRunner.java @@ -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; @@ -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(); diff --git a/src/main/java/me/playbosswar/com/tasks/TasksManager.java b/src/main/java/me/playbosswar/com/tasks/TasksManager.java index 44283e4..ef22cdb 100644 --- a/src/main/java/me/playbosswar/com/tasks/TasksManager.java +++ b/src/main/java/me/playbosswar/com/tasks/TasksManager.java @@ -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; @@ -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; @@ -26,7 +24,6 @@ public class TasksManager { private static final String CONDITION_NO_MATCH = "Conditions did not match"; private List loadedTasks = new ArrayList<>(); - private final List scheduledExecutions = new ArrayList<>(); private Thread runnerThread; public boolean stopRunner = false; public int executionsSinceLastSync = 0; @@ -34,7 +31,6 @@ public class TasksManager { public TasksManager() { loadedTasks.addAll(Files.deserializeJsonFilesIntoCommandTimers()); startRunner(); - startCommandExecutor(); } public Task createTask() { @@ -80,7 +76,7 @@ private void startRunner() { this.runnerThread = thread; } - private void runConsolePerUserCommand(TaskCommand taskCommand, List scopedPlayers) throws CommandException { + private boolean runConsolePerUserCommand(TaskCommand taskCommand, List scopedPlayers) throws CommandException { String command = taskCommand.getCommand(); Collection affectedPlayers = (Collection) Bukkit.getOnlinePlayers(); @@ -91,6 +87,7 @@ private void runConsolePerUserCommand(TaskCommand taskCommand, List 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); @@ -98,6 +95,7 @@ private void runConsolePerUserCommand(TaskCommand taskCommand, List scoped Messages.sendDebugConsole(CONDITION_NO_MATCH); continue; } + willExecute = true; } if(delayedExecutions) { @@ -111,9 +109,11 @@ private void runConsolePerUserCommand(TaskCommand taskCommand, List 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; @@ -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 scopedPlayers) { + private boolean runPlayerCommand(TaskCommand taskCommand, List scopedPlayers) { String command = taskCommand.getCommand(); Collection affectedPlayers = (Collection) Bukkit.getOnlinePlayers(); @@ -162,6 +165,7 @@ private void runPlayerCommand(TaskCommand taskCommand, List 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); @@ -169,6 +173,7 @@ private void runPlayerCommand(TaskCommand taskCommand, List scopedPlayers) Messages.sendDebugConsole(CONDITION_NO_MATCH); continue; } + willExecute = true; } if(delayedExecution) { @@ -179,6 +184,8 @@ private void runPlayerCommand(TaskCommand taskCommand, List scopedPlayers) } i++; } + + return willExecute; } private void runForPlayer(Player p, String command) { @@ -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 scopedPlayers) { + private boolean runOperatorCommand(TaskCommand taskCommand, List scopedPlayers) { String command = taskCommand.getCommand(); Collection affectedPlayers = (Collection) Bukkit.getOnlinePlayers(); @@ -208,6 +215,7 @@ private void runOperatorCommand(TaskCommand taskCommand, List scopedPlayer boolean delayedExecutions = taskCommand.getInterval().toSeconds() > 0; int i = 0; + boolean willExecute = false; for(Player p : affectedPlayers) { boolean wasAlreadyOp = p.isOp(); @@ -223,6 +231,7 @@ private void runOperatorCommand(TaskCommand taskCommand, List scopedPlayer } continue; } + willExecute = true; } if(delayedExecutions) { @@ -242,53 +251,40 @@ private void runOperatorCommand(TaskCommand taskCommand, List 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 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) { diff --git a/src/main/resources/plugin.yml b/src/main/resources/plugin.yml index dfbdc13..e6b0b9b 100644 --- a/src/main/resources/plugin.yml +++ b/src/main/resources/plugin.yml @@ -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