19
19
20
20
package com .sk89q .worldedit .cli .data ;
21
21
22
- import com .google .common .io .Resources ;
23
22
import com .google .gson .Gson ;
24
23
import com .google .gson .GsonBuilder ;
25
24
import com .sk89q .worldedit .WorldEdit ;
26
25
import com .sk89q .worldedit .cli .CLIWorldEdit ;
27
- import com .sk89q .worldedit .extension .platform .Capability ;
28
- import com .sk89q .worldedit .util .io .ResourceLoader ;
26
+ import com .sk89q .worldedit .util .io .Closer ;
29
27
28
+ import java .io .FileOutputStream ;
30
29
import java .io .IOException ;
31
30
import java .net .URL ;
32
- import java .nio .charset .StandardCharsets ;
31
+ import java .nio .channels .Channels ;
32
+ import java .nio .channels .ReadableByteChannel ;
33
+ import java .nio .file .Files ;
34
+ import java .nio .file .Path ;
33
35
34
36
public class FileRegistries {
35
37
38
+ private static final int CLI_DATA_VERSION = 1 ;
39
+ private static final String DATA_FILE_DOWNLOAD_URL = "https://services.enginehub.org/cassette-deck/we-cli-data/" ;
40
+
36
41
private final CLIWorldEdit app ;
37
42
private final Gson gson = new GsonBuilder ().create ();
38
43
@@ -43,12 +48,23 @@ public FileRegistries(CLIWorldEdit app) {
43
48
}
44
49
45
50
public void loadDataFiles () {
46
- ResourceLoader resourceLoader = WorldEdit .getInstance ().getPlatformManager ().queryCapability (Capability .CONFIGURATION ).getResourceLoader ();
47
- try {
48
- URL url = resourceLoader .getResource (FileRegistries .class , app .getPlatform ().getDataVersion () + ".json" );
49
- this .dataFile = gson .fromJson (Resources .toString (url , StandardCharsets .UTF_8 ), DataFile .class );
51
+ Path outputFolder = WorldEdit .getInstance ().getWorkingDirectoryPath ("cli-data" );
52
+ Path checkPath = outputFolder .resolve (app .getPlatform ().getDataVersion () + "_" + CLI_DATA_VERSION + ".json" );
53
+
54
+ try (Closer closer = Closer .create ()) {
55
+ Files .createDirectories (outputFolder );
56
+
57
+ if (!Files .exists (checkPath )) {
58
+ URL url = new URL (DATA_FILE_DOWNLOAD_URL + app .getPlatform ().getDataVersion () + "/" + CLI_DATA_VERSION );
59
+ ReadableByteChannel readableByteChannel = Channels .newChannel (url .openStream ());
60
+
61
+ FileOutputStream fileOutputStream = closer .register (new FileOutputStream (checkPath .toFile ()));
62
+ fileOutputStream .getChannel ().transferFrom (readableByteChannel , 0 , Long .MAX_VALUE );
63
+ }
64
+
65
+ this .dataFile = gson .fromJson (Files .readString (checkPath ), DataFile .class );
50
66
} catch (IOException e ) {
51
- throw new RuntimeException ("The provided file is not compatible with this version of WorldEdit-CLI. Please update or report this." );
67
+ throw new RuntimeException ("The provided file is not compatible with this version of WorldEdit-CLI. Please update or report this." , e );
52
68
}
53
69
}
54
70
0 commit comments