Skip to content

Commit cc0c5fb

Browse files
committed
feat: remove requirement that file name should be the task uuid
1 parent d3909f9 commit cc0c5fb

9 files changed

Lines changed: 167 additions & 62 deletions

File tree

build.gradle

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ java {
99
}
1010

1111
group = 'me.playbosswar.com'
12-
version = '8.15.1'
12+
version = '8.16.0'
1313
description = 'CommandTimer'
1414

1515
repositories {
@@ -74,7 +74,7 @@ publishing {
7474
maven(MavenPublication) {
7575
groupId = 'me.playbosswar.com'
7676
artifactId = 'commandtimer'
77-
version = '8.15.1'
77+
version = '8.16.0'
7878

7979
from components.java
8080
}

java17-build.gradle

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ java {
1010

1111

1212
group = 'me.playbosswar.com'
13-
version = '8.15.1'
13+
version = '8.16.0'
1414
description = 'CommandTimer'
1515

1616
repositories {
@@ -63,7 +63,7 @@ publishing {
6363
maven(MavenPublication) {
6464
groupId = 'me.playbosswar.com'
6565
artifactId = 'commandtimer-java17'
66-
version = '8.15.1'
66+
version = '8.16.0'
6767

6868
from components.java
6969
}

java21-build.gradle

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ java {
1010

1111

1212
group = 'me.playbosswar.com'
13-
version = '8.15.1'
13+
version = '8.16.0'
1414
description = 'CommandTimer'
1515

1616
repositories {
@@ -67,7 +67,7 @@ publishing {
6767
maven(MavenPublication) {
6868
groupId = 'me.playbosswar.com'
6969
artifactId = 'commandtimer-java21'
70-
version = '8.15.1'
70+
version = '8.16.0'
7171
from components.java
7272
}
7373
}

src/main/java/me/playbosswar/com/CommandTimerPlugin.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,6 @@ public void onEnable() {
8888

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

91-
Files.migrateFileNamesToFileUuids();
9291
new MigrationManager(this).runMigrations();
9392
if(getConfig().getBoolean("database.enabled")) {
9493
try {

src/main/java/me/playbosswar/com/tasks/AdHocCommandsManager.java

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -123,9 +123,17 @@ private List<AdHocCommand> loadAdHocCommandsFromFiles() {
123123
private void storeCommand(AdHocCommand command) {
124124
GsonConverter gson = new GsonConverter();
125125
String json = gson.toJson(command);
126-
try (FileWriter jsonFile = new FileWriter(Files.getAdHocCommandFile(command.getId()))) {
127-
jsonFile.write(json);
128-
jsonFile.flush();
126+
try {
127+
String path;
128+
try {
129+
path = Files.getAdHocCommandFile(command.getId());
130+
} catch (IllegalStateException e) {
131+
path = Files.getNewAdHocCommandFile(command.getId());
132+
}
133+
try (FileWriter jsonFile = new FileWriter(path)) {
134+
jsonFile.write(json);
135+
jsonFile.flush();
136+
}
129137
} catch (IOException e) {
130138
e.printStackTrace();
131139
}

src/main/java/me/playbosswar/com/tasks/Task.java

Lines changed: 42 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,12 @@
1414
import me.playbosswar.com.tasks.persistors.*;
1515
import me.playbosswar.com.utils.Files;
1616
import me.playbosswar.com.utils.gson.GsonConverter;
17+
import me.playbosswar.com.utils.migrations.MigrationManager;
18+
import com.google.gson.JsonObject;
19+
import com.google.gson.JsonParser;
1720

21+
import java.io.File;
22+
import java.io.FileReader;
1823
import java.io.FileWriter;
1924
import java.io.IOException;
2025
import java.sql.SQLException;
@@ -342,9 +347,43 @@ public void storeInstance() {
342347
String json = gson.toJson(this);
343348
transaction.setContext("task", json);
344349

345-
try (FileWriter jsonFile = new FileWriter(Files.getTaskFile(id))) {
346-
jsonFile.write(json);
347-
jsonFile.flush();
350+
try {
351+
String path;
352+
File existingFile = null;
353+
try {
354+
path = Files.getTaskFile(id);
355+
existingFile = new File(path);
356+
} catch (IllegalStateException e) {
357+
path = Files.getNewTaskFile(id);
358+
}
359+
360+
JsonObject jsonObject = new JsonParser().parse(json).getAsJsonObject();
361+
362+
if (existingFile != null && existingFile.exists()) {
363+
try (FileReader fr = new FileReader(existingFile)) {
364+
JsonObject existingJson = new JsonParser().parse(fr).getAsJsonObject();
365+
if (existingJson.has("configVersion")) {
366+
jsonObject.addProperty("configVersion", existingJson.get("configVersion").getAsInt());
367+
} else {
368+
jsonObject.addProperty("configVersion", MigrationManager.CURRENT_VERSION);
369+
}
370+
} catch (Exception e) {
371+
if (!jsonObject.has("configVersion")) {
372+
jsonObject.addProperty("configVersion", MigrationManager.CURRENT_VERSION);
373+
}
374+
}
375+
} else {
376+
if (!jsonObject.has("configVersion")) {
377+
jsonObject.addProperty("configVersion", MigrationManager.CURRENT_VERSION);
378+
}
379+
}
380+
381+
json = gson.toJson(jsonObject);
382+
383+
try (FileWriter jsonFile = new FileWriter(path)) {
384+
jsonFile.write(json);
385+
jsonFile.flush();
386+
}
348387
} catch (IOException e) {
349388
e.printStackTrace();
350389
transaction.setThrowable(e);

src/main/java/me/playbosswar/com/utils/Files.java

Lines changed: 98 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@
1414
import org.json.simple.parser.ParseException;
1515

1616
import com.google.gson.JsonParseException;
17+
import com.google.gson.JsonObject;
18+
import com.google.gson.JsonParser;
1719

1820
import java.io.File;
1921
import java.io.FileReader;
@@ -53,14 +55,90 @@ public static void createDataFolders() {
5355
CommandTimerPlugin.getPlugin().saveResource("languages/default.json", true);
5456
}
5557

56-
/**
57-
* Returns timer json file
58-
*/
58+
private static File findTaskFileByUuid(UUID id) {
59+
File dir = new File(pluginFolderPath + "/timers");
60+
File[] files = dir.listFiles(file -> file.getName().endsWith(".json"));
61+
if (files == null) return null;
62+
63+
for (File file : files) {
64+
try (FileReader fr = new FileReader(file)) {
65+
JsonObject json = new JsonParser().parse(fr).getAsJsonObject();
66+
if (json.has("id")) {
67+
UUID fileId = UUID.fromString(json.get("id").getAsString());
68+
if (fileId.equals(id)) {
69+
return file;
70+
}
71+
}
72+
} catch (Exception e) {
73+
continue;
74+
}
75+
}
76+
return null;
77+
}
78+
79+
private static File findMetadataFileByUuid(UUID id) {
80+
File dir = new File(pluginFolderPath + "/execution-data");
81+
File[] files = dir.listFiles(file -> file.getName().endsWith(".json"));
82+
if (files == null) return null;
83+
84+
for (File file : files) {
85+
try (FileReader fr = new FileReader(file)) {
86+
JsonObject json = new JsonParser().parse(fr).getAsJsonObject();
87+
if (json.has("taskId")) {
88+
UUID fileId = UUID.fromString(json.get("taskId").getAsString());
89+
if (fileId.equals(id)) {
90+
return file;
91+
}
92+
}
93+
} catch (Exception e) {
94+
continue;
95+
}
96+
}
97+
return null;
98+
}
99+
100+
private static File findAdHocCommandFileByUuid(UUID id) {
101+
File dir = new File(pluginFolderPath + "/ad-hoc-commands");
102+
File[] files = dir.listFiles(file -> file.getName().endsWith(".json"));
103+
if (files == null) return null;
104+
105+
for (File file : files) {
106+
try (FileReader fr = new FileReader(file)) {
107+
JsonObject json = new JsonParser().parse(fr).getAsJsonObject();
108+
if (json.has("id")) {
109+
UUID fileId = UUID.fromString(json.get("id").getAsString());
110+
if (fileId.equals(id)) {
111+
return file;
112+
}
113+
}
114+
} catch (Exception e) {
115+
continue;
116+
}
117+
}
118+
return null;
119+
}
120+
59121
public static String getTaskFile(UUID id) {
60-
return pluginFolderPath + "/timers/" + id + ".json";
122+
File file = findTaskFileByUuid(id);
123+
if (file != null) {
124+
return file.getAbsolutePath();
125+
}
126+
throw new IllegalStateException("Task file not found for UUID: " + id);
61127
}
62128

63129
public static String getTaskLocalExecutionFile(UUID id) {
130+
File file = findMetadataFileByUuid(id);
131+
if (file != null) {
132+
return file.getAbsolutePath();
133+
}
134+
throw new IllegalStateException("Task metadata file not found for UUID: " + id);
135+
}
136+
137+
public static String getNewTaskFile(UUID id) {
138+
return pluginFolderPath + "/timers/" + id + ".json";
139+
}
140+
141+
public static String getNewTaskLocalExecutionFile(UUID id) {
64142
return pluginFolderPath + "/execution-data/" + id + ".json";
65143
}
66144

@@ -69,6 +147,14 @@ public static String getAdHocCommandsDirectory() {
69147
}
70148

71149
public static String getAdHocCommandFile(UUID id) {
150+
File file = findAdHocCommandFileByUuid(id);
151+
if (file != null) {
152+
return file.getAbsolutePath();
153+
}
154+
throw new IllegalStateException("Ad-hoc command file not found for UUID: " + id);
155+
}
156+
157+
public static String getNewAdHocCommandFile(UUID id) {
72158
return pluginFolderPath + "/ad-hoc-commands/" + id + ".json";
73159
}
74160

@@ -91,61 +177,23 @@ private static void healTask(Task task) {
91177
}
92178
}
93179

94-
public static void migrateFileNamesToFileUuids() {
95-
File dir = new File(pluginFolderPath + "/timers");
96-
File[] directoryListing = dir.listFiles(file -> file.getName().endsWith(".json"));
97-
98-
if(directoryListing != null) {
99-
for(File file : directoryListing) {
100-
if(!file.exists() || !file.getName().contains("json")) {
101-
continue;
102-
}
103-
104-
try {
105-
UUID.fromString(file.getName().replace(".json", ""));
106-
} catch(IllegalArgumentException e) {
107-
try {
108-
UUID uuid = UUID.randomUUID();
109-
110-
FileReader fr = new FileReader(file.getPath());
111-
JSONParser jsonParser = new JSONParser();
112-
Task task = new GsonConverter().fromJson(jsonParser.parse(fr).toString(), Task.class);
113-
task.setId(uuid);
114-
115-
GsonConverter gson = new GsonConverter();
116-
String json = gson.toJson(task);
117-
try (FileWriter jsonFile = new FileWriter(pluginFolderPath + "/timers/" + uuid + ".json")) {
118-
jsonFile.write(json);
119-
jsonFile.flush();
120-
}
121-
122-
file.delete();
123-
Messages.sendConsole("Migrated " + file.getName() + " to " + uuid + ".json");
124-
} catch(IOException | ParseException ex) {
125-
throw new RuntimeException(ex);
126-
}
127-
}
128-
}
129-
}
130-
}
131-
132180
public static TaskExecutionMetadata getOrCreateTaskMetadata(Task task) {
133181
try {
134-
String path = getTaskLocalExecutionFile(task.getId());
135-
File file = new File(path);
136-
if (!file.exists()) {
182+
File file = findMetadataFileByUuid(task.getId());
183+
if (file == null || !file.exists()) {
137184
TaskExecutionMetadata metadata = new TaskExecutionMetadata(task.getId(),
138185
task.getTimesExecuted(), task.getLastExecutedCommandIndex(), task.getLastExecuted());
139186
GsonConverter gson = new GsonConverter();
140187
String json = gson.toJson(metadata);
188+
String path = getNewTaskLocalExecutionFile(task.getId());
141189
try (FileWriter jsonFile = new FileWriter(path)) {
142190
jsonFile.write(json);
143191
jsonFile.flush();
144192
}
145193
return metadata;
146194
}
147195

148-
FileReader fr = new FileReader(getTaskLocalExecutionFile(task.getId()));
196+
FileReader fr = new FileReader(file);
149197
JSONParser jsonParser = new JSONParser();
150198
TaskExecutionMetadata metadata = new GsonConverter().fromJson(jsonParser.parse(fr).toString(),
151199
TaskExecutionMetadata.class);
@@ -162,7 +210,9 @@ public static void updateLocalTaskMetadata(Task task) {
162210

163211
GsonConverter gson = new GsonConverter();
164212
String json = gson.toJson(metadata);
165-
try (FileWriter jsonFile = new FileWriter(getTaskLocalExecutionFile(task.getId()))) {
213+
File file = findMetadataFileByUuid(task.getId());
214+
String path = file != null ? file.getAbsolutePath() : getNewTaskLocalExecutionFile(task.getId());
215+
try (FileWriter jsonFile = new FileWriter(path)) {
166216
jsonFile.write(json);
167217
jsonFile.flush();
168218
} catch (IOException e) {
@@ -181,7 +231,7 @@ public static List<Task> deserializeJsonFilesIntoCommandTimers() {
181231
try {
182232
if(directoryListing != null) {
183233
for(File file : directoryListing) {
184-
if(!file.exists() || !file.getName().contains("json")) {
234+
if(!file.exists() || !file.getName().contains(".json")) {
185235
continue;
186236
}
187237

src/main/java/me/playbosswar/com/utils/migrations/MigrationManager.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ public void runMigrations() {
4646

4747
Messages.sendConsole("Migrating task file: " + file.getName() + " (v" + fileVersion + " -> v" + CURRENT_VERSION + ")");
4848

49+
boolean failed = false;
4950
for (Migration migration : migrations.stream()
5051
.filter(m -> m.getVersion() > fileVersion)
5152
.sorted(Comparator.comparingInt(Migration::getVersion))
@@ -55,9 +56,17 @@ public void runMigrations() {
5556
} catch (Exception e) {
5657
Messages.sendConsole("Migration v" + migration.getVersion() + " failed for " + file.getName() + ": " + e.getMessage());
5758
e.printStackTrace();
59+
failed = true;
60+
break;
5861
}
5962
}
6063

64+
if (failed) {
65+
Messages.sendConsole("Migration failed for " + file.getName() + ". Disabling plugin...");
66+
CommandTimerPlugin.getInstance().getServer().getPluginManager().disablePlugin(CommandTimerPlugin.getInstance());
67+
return;
68+
}
69+
6170
Messages.sendConsole("Migration complete for " + file.getName() + " (v" + fileVersion + " -> v" + CURRENT_VERSION + ")");
6271

6372
json.addProperty("configVersion", CURRENT_VERSION);

src/main/resources/plugin.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
main: me.playbosswar.com.CommandTimerPlugin
22
name: "CommandTimer"
3-
version: "8.15.1"
3+
version: "8.16.0"
44
description: "Schedule commands like you want"
55
author: PlayBossWar
66
api-version: 1.13

0 commit comments

Comments
 (0)