Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions 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.15.1'
version = '8.16.0'
description = 'CommandTimer'

repositories {
Expand Down Expand Up @@ -74,7 +74,7 @@ publishing {
maven(MavenPublication) {
groupId = 'me.playbosswar.com'
artifactId = 'commandtimer'
version = '8.15.1'
version = '8.16.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.15.1'
version = '8.16.0'
description = 'CommandTimer'

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

from components.java
}
Expand Down
4 changes: 2 additions & 2 deletions java21-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.15.1'
version = '8.16.0'
description = 'CommandTimer'

repositories {
Expand Down Expand Up @@ -67,7 +67,7 @@ publishing {
maven(MavenPublication) {
groupId = 'me.playbosswar.com'
artifactId = 'commandtimer-java21'
version = '8.15.1'
version = '8.16.0'
from components.java
}
}
Expand Down
3 changes: 2 additions & 1 deletion src/main/java/me/playbosswar/com/CommandTimerPlugin.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
import me.playbosswar.com.updater.Updater;
import me.playbosswar.com.utils.Files;
import me.playbosswar.com.utils.Messages;
import me.playbosswar.com.utils.migrations.MigrationManager;
import me.playbosswar.com.utils.Tools;
import org.bukkit.Bukkit;
import org.bukkit.configuration.file.FileConfiguration;
Expand Down Expand Up @@ -87,7 +88,7 @@ public void onEnable() {

Bukkit.getPluginManager().registerEvents(new JoinEvents(), this);

Files.migrateFileNamesToFileUuids();
new MigrationManager(this).runMigrations();
if(getConfig().getBoolean("database.enabled")) {
try {
Class.forName("com.mysql.jdbc.Driver");
Expand Down
28 changes: 27 additions & 1 deletion src/main/java/me/playbosswar/com/commands/MainCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
import me.playbosswar.com.utils.Files;
import me.playbosswar.com.utils.Messages;
import me.playbosswar.com.utils.Tools;
import me.playbosswar.com.utils.migrations.MigrationManager;
import org.bukkit.command.Command;
import org.bukkit.command.CommandExecutor;
import org.bukkit.command.CommandSender;
Expand Down Expand Up @@ -184,8 +185,33 @@ public boolean onCommand(@NotNull CommandSender sender, @NotNull Command cmd, @N

if(args.length == 2) {
String action = args[0];
String taskName = args[1];

if(action.equalsIgnoreCase("rollback")) {
if(!sender.hasPermission("commandtimer.manage")) {
Messages.sendNoPermission(sender);
return true;
}

try {
int targetVersion = Integer.parseInt(args[1]);
MigrationManager migrationManager = new MigrationManager(CommandTimerPlugin.getInstance());

if(targetVersion > migrationManager.getCurrentVersion()) {
Messages.sendMessage(sender, "&cCannot rollback to version " + targetVersion + ". Current version is " + migrationManager.getCurrentVersion());
return true;
}

Messages.sendMessage(sender, "&eStarting rollback to version " + targetVersion + "...");
migrationManager.rollbackToVersion(targetVersion);
Messages.sendMessage(sender, "&aRollback complete. Please restart the server for changes to take effect.");
return true;
} catch(NumberFormatException e) {
Messages.sendMessage(sender, "&cInvalid version number: " + args[1]);
return true;
}
}

String taskName = args[1];
Task task = tasksManager.getTaskByName(taskName);
if(task == null) {
Messages.sendMessage(sender, languageManager.get(LanguageKey.NO_TASK));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,9 +58,7 @@ private void validateConfiguration() throws Exception {
}

if (fileSaveRequired) {
try {
String filePath = getLanguageFilePath(selectedLanguage);
FileWriter jsonFile = new FileWriter(filePath);
try (FileWriter jsonFile = new FileWriter(getLanguageFilePath(selectedLanguage))) {
jsonFile.write(selectedLanguageObject.toJSONString());
jsonFile.flush();
} catch (IOException e) {
Expand Down
14 changes: 10 additions & 4 deletions src/main/java/me/playbosswar/com/tasks/AdHocCommandsManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -124,10 +124,16 @@ private void storeCommand(AdHocCommand command) {
GsonConverter gson = new GsonConverter();
String json = gson.toJson(command);
try {
FileWriter jsonFile = new FileWriter(Files.getAdHocCommandFile(command.getId()));
jsonFile.write(json);
jsonFile.flush();
jsonFile.close();
String path;
try {
path = Files.getAdHocCommandFile(command.getId());
} catch (IllegalStateException e) {
path = Files.getNewAdHocCommandFile(command.getId());
}
try (FileWriter jsonFile = new FileWriter(path)) {
jsonFile.write(json);
jsonFile.flush();
}
} catch (IOException e) {
e.printStackTrace();
}
Expand Down
59 changes: 52 additions & 7 deletions src/main/java/me/playbosswar/com/tasks/Task.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,12 @@
import me.playbosswar.com.tasks.persistors.*;
import me.playbosswar.com.utils.Files;
import me.playbosswar.com.utils.gson.GsonConverter;
import me.playbosswar.com.utils.migrations.MigrationManager;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;

import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.sql.SQLException;
Expand All @@ -40,9 +45,9 @@ public class Task {
private Collection<DayOfWeek> days = new ArrayList<>();
@DatabaseField
private int executionLimit = -1;
private int timesExecuted = 0;
private int lastExecutedCommandIndex = 0;
private Date lastExecuted = new Date();
private transient int timesExecuted = 0;
private transient int lastExecutedCommandIndex = 0;
private transient Date lastExecuted = new Date();
@DatabaseField
private CommandExecutionMode commandExecutionMode = CommandExecutionMode.ALL;
@DatabaseField(persisterClass = TaskIntervalPersistor.class)
Expand Down Expand Up @@ -184,6 +189,7 @@ public int getTimesExecuted() {

public void setTimesExecuted(int timesExecuted) {
this.timesExecuted = timesExecuted;
storeExecutionMetadata();
}

public Date getLastExecuted() {
Expand All @@ -192,6 +198,7 @@ public Date getLastExecuted() {

public void setLastExecuted(Date lastExecuted) {
this.lastExecuted = lastExecuted;
storeExecutionMetadata();
}

public boolean isActive() {
Expand Down Expand Up @@ -264,6 +271,7 @@ public int getLastExecutedCommandIndex() {

public void setLastExecutedCommandIndex(int lastExecutedCommandIndex) {
this.lastExecutedCommandIndex = lastExecutedCommandIndex;
storeExecutionMetadata();
}

public Condition getCondition() {
Expand Down Expand Up @@ -320,6 +328,10 @@ public void setId(UUID id) {
this.id = id;
}

public void storeExecutionMetadata() {
Files.updateLocalTaskMetadata(this);
}

public void storeInstance() {
if(CommandTimerPlugin.getInstance().getConfig().getBoolean("database.enabled")) {
try {
Expand All @@ -336,10 +348,43 @@ public void storeInstance() {
transaction.setContext("task", json);

try {
FileWriter jsonFile = new FileWriter(Files.getTaskFile(id));
jsonFile.write(json);
jsonFile.flush();
} catch(IOException e) {
String path;
File existingFile = null;
try {
path = Files.getTaskFile(id);
existingFile = new File(path);
} catch (IllegalStateException e) {
path = Files.getNewTaskFile(id);
}

JsonObject jsonObject = new JsonParser().parse(json).getAsJsonObject();

if (existingFile != null && existingFile.exists()) {
try (FileReader fr = new FileReader(existingFile)) {
JsonObject existingJson = new JsonParser().parse(fr).getAsJsonObject();
if (existingJson.has("configVersion")) {
jsonObject.addProperty("configVersion", existingJson.get("configVersion").getAsInt());
} else {
jsonObject.addProperty("configVersion", MigrationManager.CURRENT_VERSION);
}
} catch (Exception e) {
if (!jsonObject.has("configVersion")) {
jsonObject.addProperty("configVersion", MigrationManager.CURRENT_VERSION);
}
}
} else {
if (!jsonObject.has("configVersion")) {
jsonObject.addProperty("configVersion", MigrationManager.CURRENT_VERSION);
}
}

json = gson.toJson(jsonObject);

try (FileWriter jsonFile = new FileWriter(path)) {
jsonFile.write(json);
jsonFile.flush();
}
} catch (IOException e) {
e.printStackTrace();
transaction.setThrowable(e);
transaction.setStatus(SpanStatus.INTERNAL_ERROR);
Expand Down
14 changes: 12 additions & 2 deletions src/main/java/me/playbosswar/com/tasks/TaskExecutionMetadata.java
Original file line number Diff line number Diff line change
@@ -1,19 +1,29 @@
package me.playbosswar.com.tasks;

import java.util.Date;
import java.util.UUID;

// Data class only used to store metadata in local json files
public class TaskExecutionMetadata {
private UUID taskId;
private int timesExecuted = 0;
private int lastExecutedCommandIndex = 0;
private Date lastExecuted = new Date();

public TaskExecutionMetadata(int timesExecuted, int lastExecutedCommandIndex, Date lastExecuted) {
public TaskExecutionMetadata(UUID taskId, int timesExecuted, int lastExecutedCommandIndex, Date lastExecuted) {
this.taskId = taskId;
this.timesExecuted = timesExecuted;
this.lastExecutedCommandIndex = lastExecutedCommandIndex;
this.lastExecuted = lastExecuted;
}

public UUID getTaskId() {
return taskId;
}

public void setTaskId(UUID taskId) {
this.taskId = taskId;
}

public int getTimesExecuted() {
return timesExecuted;
}
Expand Down
2 changes: 0 additions & 2 deletions src/main/java/me/playbosswar/com/tasks/TaskRunner.java
Original file line number Diff line number Diff line change
Expand Up @@ -65,8 +65,6 @@ private void processTask(Task task) {
task.setLastExecutedCommandIndex(task.getCommands().indexOf(taskCommand));
CommandTimerPlugin.getScheduler().runTask(() -> tasksManager.processCommandExecution(task, taskCommand));
}

task.storeInstance();
}

@Override
Expand Down
5 changes: 1 addition & 4 deletions src/main/java/me/playbosswar/com/tasks/TasksManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,6 @@ public TasksManager(Plugin plugin) {
if (task.isResetExecutionsAfterRestart()) {
task.setTimesExecuted(0);
task.setLastExecuted(new Date());
task.storeInstance();
}
});

Expand Down Expand Up @@ -104,6 +103,7 @@ public void removeTask(Task task) throws IOException {
try {
loadedTasks.remove(task);
java.nio.file.Files.delete(Paths.get(Files.getTaskFile(task.getId())));
java.nio.file.Files.delete(Paths.get(Files.getTaskLocalExecutionFile(task.getId())));
} catch (IOException e) {
e.printStackTrace();
}
Expand Down Expand Up @@ -159,7 +159,6 @@ public void processCommandExecution(Task task, TaskCommand taskCommand) {
if (executed) {
task.setLastExecuted(new Date());
task.setTimesExecuted(task.getTimesExecuted() + 1);
task.storeInstance();
}
});
}
Expand Down Expand Up @@ -409,8 +408,6 @@ private boolean isTimeInRange(LocalTime time, LocalTime start, LocalTime end) {
}

public void disable() {
List<Task> tasksToStore = loadedTasks.stream().filter(Task::isActive).collect(Collectors.toList());
tasksToStore.forEach(Task::storeInstance);
stopRunner = true;
if (runnerThread != null && runnerThread.isAlive()) {
runnerThread.interrupt();
Expand Down
Loading