2222import se .llbit .chunky .ui .ProgressTracker ;
2323import se .llbit .chunky .world .region .MCRegion ;
2424import se .llbit .log .Log ;
25+ import se .llbit .math .Vector3i ;
2526import se .llbit .nbt .NamedTag ;
2627import se .llbit .nbt .Tag ;
2728import se .llbit .util .MinecraftText ;
@@ -70,26 +71,48 @@ public class JavaWorld extends World {
7071 new String [] { NETHER_DIMENSION_ID , OVERWORLD_DIMENSION_ID , END_DIMENSION_ID }
7172 ));
7273
73- int versionId ;
74+ protected int versionId ;
75+
76+ /**
77+ * In a java world player data is per-world and not per-dimension, so we store it here.
78+ */
79+ protected final Set <PlayerEntityData > playerEntities ;
80+
81+ /**
82+ * In a java world spawn position is per-world and not per-dimension, so we store it here.
83+ */
84+ protected final Vector3i spawnPos ;
7485
7586 /**
7687 * @param levelName name of the world (not the world directory).
7788 * @param worldDirectory Minecraft world directory.
7889 * @param seed
7990 * @param timestamp
8091 */
81- protected JavaWorld (String levelName , File worldDirectory , long seed , long timestamp ) {
92+ protected JavaWorld (String levelName , File worldDirectory , long seed , long timestamp , Set < PlayerEntityData > playerEntities , Vector3i spawnPos ) {
8293 super (levelName , worldDirectory , seed , timestamp );
94+ this .playerEntities = playerEntities ;
95+ this .spawnPos = spawnPos ;
8396 }
8497
8598 @ Override
86- public Set <String > listDimensions () {
99+ public Set <String > availableDimensions () {
87100 return new ObjectArraySet <>(VANILLA_DIMENSION_ID_TO_IDX .keySet ());
88101 }
89102
103+ @ Override
104+ public Optional <String > defaultDimension () {
105+ return Optional .of (OVERWORLD_DIMENSION_ID );
106+ }
107+
90108 public Dimension loadDimension (String dimension ) {
91- currentDimension = loadDimension (this , this .worldDirectory , dimension , -1 , Collections .emptySet ());
92- currentDimension .reloadPlayerData ();
109+ currentDimension = loadDimension (
110+ this ,
111+ this .worldDirectory ,
112+ dimension ,
113+ -1 ,
114+ this .playerEntities .stream ().filter (player -> player .dimension .equals (dimension )).collect (Collectors .toSet ())
115+ );
93116 return currentDimension ;
94117 }
95118
@@ -122,7 +145,7 @@ public static World loadWorld(File worldDirectory, LoggedWarnings warnings) {
122145 }
123146 Tag versionId = result .get (".Data.Version.Id" );
124147 Tag player = result .get (".Data.Player" );
125- Tag spawnX = player .get ("SpawnX" );
148+ Tag spawnX = player .get ("SpawnX" ); // TODO: not sure what to do with spawn location now. I guess for java worlds: the world should store it and the dimension should set it in the map view when loaded...?
126149 Tag spawnY = player .get ("SpawnY" );
127150 Tag spawnZ = player .get ("SpawnZ" );
128151 Tag gameType = result .get (".Data.GameType" );
@@ -131,20 +154,19 @@ public static World loadWorld(File worldDirectory, LoggedWarnings warnings) {
131154
132155 long seed = randomSeed .longValue (0 );
133156
134- Set <PlayerEntityData > playerEntities = getPlayerEntityData (worldDirectory , dimensionId , player );
135-
136- JavaWorld world = new JavaWorld (levelName , worldDirectory , seed , modtime );
137- world .gameMode = gameType .intValue (0 );
138- world .versionId = versionId .intValue ();
139-
140- Dimension dimension = loadDimension (world , worldDirectory , dimensionId , modtime , playerEntities );
157+ Set <PlayerEntityData > playerEntities = getPlayerEntityData (worldDirectory , player );
141158
142159 boolean haveSpawnPos = !(spawnX .isError () || spawnY .isError () || spawnZ .isError ());
160+ Vector3i spawnPos ;
143161 if (haveSpawnPos ) {
144- dimension .setSpawnPos (new Vector3i (spawnX .intValue (0 ), spawnY .intValue (0 ), spawnZ .intValue (0 )));
162+ spawnPos = new Vector3i (spawnX .intValue (0 ), spawnY .intValue (0 ), spawnZ .intValue (0 ));
163+ } else {
164+ spawnPos = new Vector3i (0 , 0 , 0 );
145165 }
146166
147- world .currentDimension = dimension ;
167+ JavaWorld world = new JavaWorld (levelName , worldDirectory , seed , modtime , playerEntities , spawnPos );
168+ world .gameMode = gameType .intValue (0 );
169+ world .versionId = versionId .intValue ();
148170
149171 return world ;
150172 } catch (FileNotFoundException e ) {
@@ -172,14 +194,12 @@ protected static JavaDimension loadDimension(JavaWorld world, File worldDirector
172194 }
173195
174196 @ NotNull
175- static Set <PlayerEntityData > getPlayerEntityData (File worldDirectory , String dimensionId , Tag player ) {
197+ static Set <PlayerEntityData > getPlayerEntityData (File worldDirectory , Tag player ) {
176198 Set <PlayerEntityData > playerEntities = new HashSet <>();
177199 if (!player .isError ()) {
178200 playerEntities .add (new PlayerEntityData (player ));
179201 }
180202 loadAdditionalPlayers (worldDirectory , playerEntities );
181- // Filter for the players only within the requested dimension
182- playerEntities = playerEntities .stream ().filter (playerData -> playerData .dimension .equals (dimensionId )).collect (Collectors .toSet ());
183203 return playerEntities ;
184204 }
185205
@@ -206,8 +226,8 @@ synchronized boolean reloadPlayerData() {
206226 request .add (".Data.Player" );
207227 Map <String , Tag > result = NamedTag .quickParse (in , request );
208228 Tag player = result .get (".Data.Player" );
209-
210- currentDimension . setPlayerEntities (getPlayerEntityData (worldDirectory , currentDimension . id () , player ));
229+ this . playerEntities . clear ();
230+ this . playerEntities . addAll (getPlayerEntityData (worldDirectory , player ));
211231 } catch (IOException e ) {
212232 Log .infof ("Could not read the level.dat file for world %s while trying to reload player data!" , levelName );
213233 return false ;
@@ -236,25 +256,12 @@ private static void loadPlayerData(File playerdata, Set<PlayerEntityData> player
236256 }
237257 }
238258
239- @ Override
240- public synchronized JavaDimension currentDimension () {
241- return (JavaDimension ) this .currentDimension ;
242- }
243-
244- /**
245- * @deprecated Use {@link JavaWorld#currentDimension()} -> {@link Dimension#getDimensionDirectory()} ()}. Removed once there are no more usages
246- */
247- @ Deprecated
248259 protected synchronized File getDataDirectory (int dimension ) {
249260 return dimension == 0 ?
250261 worldDirectory :
251262 new File (worldDirectory , "DIM" + dimension );
252263 }
253264
254- /**
255- @deprecated Use {@link JavaWorld#currentDimension()} -> {@link JavaDimension#getRegionDirectory()}. Removed once there are no more usages
256- */
257- @ Deprecated
258265 protected synchronized File getRegionDirectory (int dimension ) {
259266 return new File (getDataDirectory (dimension ), "region" );
260267 }
@@ -399,7 +406,4 @@ public static boolean isWorldDir(File worldDir) {
399406 return false ;
400407 }
401408
402- public Date getLastModified () {
403- return new Date (this .worldDirectory .lastModified ());
404- }
405409}
0 commit comments