1
1
package xyz .jpenilla .squaremap .common .data ;
2
2
3
- import com .google .gson .Gson ;
4
- import com .google .gson .GsonBuilder ;
5
- import com .google .gson .JsonIOException ;
6
- import com .google .gson .JsonSyntaxException ;
7
- import com .google .gson .reflect .TypeToken ;
8
- import java .io .BufferedReader ;
9
- import java .io .IOException ;
10
- import java .lang .reflect .Type ;
11
- import java .nio .file .Files ;
12
3
import java .nio .file .Path ;
13
4
import java .util .HashMap ;
14
5
import java .util .Iterator ;
15
- import java .util .LinkedHashMap ;
16
- import java .util .List ;
17
6
import java .util .Map ;
18
7
import java .util .Set ;
19
8
import java .util .concurrent .ConcurrentHashMap ;
32
21
import xyz .jpenilla .squaremap .api .Registry ;
33
22
import xyz .jpenilla .squaremap .api .WorldIdentifier ;
34
23
import xyz .jpenilla .squaremap .common .LayerRegistry ;
35
- import xyz .jpenilla .squaremap .common .Logging ;
36
- import xyz .jpenilla .squaremap .common .SquaremapCommon ;
37
24
import xyz .jpenilla .squaremap .common .config .WorldAdvanced ;
38
25
import xyz .jpenilla .squaremap .common .config .WorldConfig ;
26
+ import xyz .jpenilla .squaremap .common .data .storage .AdditionalParameters ;
27
+ import xyz .jpenilla .squaremap .common .data .storage .DataStorageHolder ;
39
28
import xyz .jpenilla .squaremap .common .layer .SpawnIconProvider ;
40
29
import xyz .jpenilla .squaremap .common .layer .WorldBorderProvider ;
41
30
import xyz .jpenilla .squaremap .common .task .render .AbstractRender ;
42
31
import xyz .jpenilla .squaremap .common .task .render .BackgroundRender ;
43
32
import xyz .jpenilla .squaremap .common .task .render .FullRender ;
44
33
import xyz .jpenilla .squaremap .common .util .Colors ;
45
34
import xyz .jpenilla .squaremap .common .util .FileUtil ;
46
- import xyz .jpenilla .squaremap .common .util .RecordTypeAdapterFactory ;
47
35
import xyz .jpenilla .squaremap .common .util .Util ;
48
36
import xyz .jpenilla .squaremap .common .visibilitylimit .VisibilityLimitImpl ;
49
37
50
38
@ DefaultQualifier (NonNull .class )
51
39
public abstract class MapWorldInternal implements MapWorld {
52
- private static final String DIRTY_CHUNKS_FILE_NAME = "dirty_chunks.json" ;
53
- private static final String RENDER_PROGRESS_FILE_NAME = "resume_render.json" ;
54
- private static final Gson GSON = new GsonBuilder ()
55
- .registerTypeAdapterFactory (new RecordTypeAdapterFactory ())
56
- .enableComplexMapKeySerialization ()
57
- .create ();
58
40
private static final Map <WorldIdentifier , LayerRegistry > LAYER_REGISTRIES = new HashMap <>();
59
41
60
42
private final ServerLevel level ;
61
- private final Path dataPath ;
62
43
private final Path tilesPath ;
63
44
private final ExecutorService imageIOexecutor ;
64
45
private final ScheduledExecutorService executor ;
@@ -90,17 +71,6 @@ protected MapWorldInternal(final ServerLevel level) {
90
71
this .blockColors = new BlockColors (this );
91
72
this .levelBiomeColorData = LevelBiomeColorData .create (this );
92
73
93
- this .dataPath = SquaremapCommon .instance ().platform ().dataDirectory ().resolve ("data" ).resolve (
94
- Util .levelWebName (this .level )
95
- );
96
- try {
97
- if (!Files .exists (this .dataPath )) {
98
- Files .createDirectories (this .dataPath );
99
- }
100
- } catch (final IOException e ) {
101
- throw this .failedToCreateDataDirectory (e );
102
- }
103
-
104
74
this .tilesPath = FileUtil .getAndCreateTilesDirectory (this .serverLevel ());
105
75
106
76
this .startBackgroundRender ();
@@ -124,53 +94,22 @@ protected MapWorldInternal(final ServerLevel level) {
124
94
}
125
95
126
96
public @ Nullable Map <RegionCoordinate , Boolean > getRenderProgress () {
127
- try {
128
- final Path file = this .dataPath .resolve (RENDER_PROGRESS_FILE_NAME );
129
- if (Files .isRegularFile (file )) {
130
- final Type type = new TypeToken <LinkedHashMap <RegionCoordinate , Boolean >>() {
131
- }.getType ();
132
- try (final BufferedReader reader = Files .newBufferedReader (file )) {
133
- return GSON .fromJson (reader , type );
134
- }
135
- }
136
- } catch (JsonIOException | JsonSyntaxException | IOException e ) {
137
- Logging .logger ().warn ("Failed to deserialize render progress for world '{}'" , this .identifier ().asString (), e );
138
- }
139
- return null ;
97
+ return DataStorageHolder .getDataStorage ().getRenderProgress (
98
+ this .identifier (),
99
+ new AdditionalParameters ().put ("levelWebName" , Util .levelWebName (this .level ))
100
+ ).join ();
140
101
}
141
102
142
103
public void saveRenderProgress (Map <RegionCoordinate , Boolean > regions ) {
143
- try {
144
- Files .writeString (this .dataPath .resolve (RENDER_PROGRESS_FILE_NAME ), GSON .toJson (regions ));
145
- } catch (IOException e ) {
146
- Logging .logger ().warn ("Failed to serialize render progress for world '{}'" , this .identifier ().asString (), e );
147
- }
104
+ DataStorageHolder .getDataStorage ().storeRenderProgress (this .identifier (), regions , new AdditionalParameters ().put ("levelWebName" , Util .levelWebName (this .level )));
148
105
}
149
106
150
107
private void serializeDirtyChunks () {
151
- try {
152
- Files .writeString (this .dataPath .resolve (DIRTY_CHUNKS_FILE_NAME ), GSON .toJson (this .modifiedChunks ));
153
- } catch (IOException e ) {
154
- Logging .logger ().warn ("Failed to serialize dirty chunks for world '{}'" , this .identifier ().asString (), e );
155
- }
108
+ DataStorageHolder .getDataStorage ().storeDirtyChunks (this .identifier (), this .modifiedChunks , new AdditionalParameters ().put ("levelWebName" , Util .levelWebName (this .level )));
156
109
}
157
110
158
111
private void deserializeDirtyChunks () {
159
- try {
160
- final Path file = this .dataPath .resolve (DIRTY_CHUNKS_FILE_NAME );
161
- if (Files .isRegularFile (file )) {
162
- try (final BufferedReader reader = Files .newBufferedReader (file )) {
163
- this .modifiedChunks .addAll (
164
- GSON .fromJson (
165
- reader ,
166
- TypeToken .getParameterized (List .class , ChunkCoordinate .class ).getType ()
167
- )
168
- );
169
- }
170
- }
171
- } catch (JsonIOException | JsonSyntaxException | IOException e ) {
172
- Logging .logger ().warn ("Failed to deserialize dirty chunks for world '{}'" , this .identifier ().asString (), e );
173
- }
112
+ this .modifiedChunks .addAll (DataStorageHolder .getDataStorage ().getDirtyChunks (this .identifier (), new AdditionalParameters ().put ("levelWebName" , Util .levelWebName (this .level ))).join ());
174
113
}
175
114
176
115
private void startBackgroundRender () {
@@ -255,11 +194,7 @@ public void pauseRenders(boolean pauseRenders) {
255
194
}
256
195
257
196
public void finishedRender () {
258
- try {
259
- Files .deleteIfExists (this .dataPath .resolve (RENDER_PROGRESS_FILE_NAME ));
260
- } catch (IOException e ) {
261
- Logging .logger ().warn ("Failed to delete render progress data for world '{}'" , this .identifier ().asString (), e );
262
- }
197
+ DataStorageHolder .getDataStorage ().deleteRenderProgress (this .identifier (), new AdditionalParameters ().put ("levelWebName" , Util .levelWebName (this .level )));
263
198
}
264
199
265
200
public void stopRender () {
0 commit comments