11package xyz .jpenilla .squaremap .common .data ;
22
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 ;
123import java .nio .file .Path ;
134import java .util .HashMap ;
145import java .util .Iterator ;
15- import java .util .LinkedHashMap ;
16- import java .util .List ;
176import java .util .Map ;
187import java .util .Set ;
198import java .util .concurrent .ConcurrentHashMap ;
3221import xyz .jpenilla .squaremap .api .Registry ;
3322import xyz .jpenilla .squaremap .api .WorldIdentifier ;
3423import xyz .jpenilla .squaremap .common .LayerRegistry ;
35- import xyz .jpenilla .squaremap .common .Logging ;
36- import xyz .jpenilla .squaremap .common .SquaremapCommon ;
3724import xyz .jpenilla .squaremap .common .config .WorldAdvanced ;
3825import xyz .jpenilla .squaremap .common .config .WorldConfig ;
26+ import xyz .jpenilla .squaremap .common .data .facilities .DataFacility ;
27+ import xyz .jpenilla .squaremap .common .data .facilities .DataFacilityFactory ;
3928import xyz .jpenilla .squaremap .common .layer .SpawnIconProvider ;
4029import xyz .jpenilla .squaremap .common .layer .WorldBorderProvider ;
4130import xyz .jpenilla .squaremap .common .task .render .AbstractRender ;
4231import xyz .jpenilla .squaremap .common .task .render .BackgroundRender ;
4332import xyz .jpenilla .squaremap .common .task .render .FullRender ;
4433import xyz .jpenilla .squaremap .common .util .Colors ;
4534import xyz .jpenilla .squaremap .common .util .FileUtil ;
46- import xyz .jpenilla .squaremap .common .util .RecordTypeAdapterFactory ;
4735import xyz .jpenilla .squaremap .common .util .Util ;
4836import xyz .jpenilla .squaremap .common .visibilitylimit .VisibilityLimitImpl ;
4937
5038@ DefaultQualifier (NonNull .class )
5139public 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 ();
5840 private static final Map <WorldIdentifier , LayerRegistry > LAYER_REGISTRIES = new HashMap <>();
5941
6042 private final ServerLevel level ;
61- private final Path dataPath ;
43+ private final DataFacility dataFacility ;
6244 private final Path tilesPath ;
6345 private final ExecutorService imageIOexecutor ;
6446 private final ScheduledExecutorService executor ;
@@ -90,16 +72,7 @@ protected MapWorldInternal(final ServerLevel level) {
9072 this .blockColors = new BlockColors (this );
9173 this .levelBiomeColorData = LevelBiomeColorData .create (this );
9274
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- }
75+ this .dataFacility = DataFacilityFactory .getDataFacility (this .identifier (), Util .levelWebName (this .level ));
10376
10477 this .tilesPath = FileUtil .getAndCreateTilesDirectory (this .serverLevel ());
10578
@@ -124,53 +97,19 @@ protected MapWorldInternal(final ServerLevel level) {
12497 }
12598
12699 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 ;
100+ return this .dataFacility .getRenderProgress ();
140101 }
141102
142103 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+ this .dataFacility .saveRenderProgress (regions );
148105 }
149106
150107 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+ this .dataFacility .saveDirtyChunks (this .modifiedChunks );
156109 }
157110
158111 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 (this .dataFacility .getDirtyChunks ());
174113 }
175114
176115 private void startBackgroundRender () {
@@ -255,11 +194,7 @@ public void pauseRenders(boolean pauseRenders) {
255194 }
256195
257196 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+ this .dataFacility .deleteRenderProgress ();
263198 }
264199
265200 public void stopRender () {
0 commit comments