1414import org .json .simple .parser .ParseException ;
1515
1616import com .google .gson .JsonParseException ;
17+ import com .google .gson .JsonObject ;
18+ import com .google .gson .JsonParser ;
1719
1820import java .io .File ;
1921import 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
0 commit comments