1
0
mirror of https://github.com/SquidDev-CC/CC-Tweaked synced 2024-12-14 12:10:30 +00:00

Prevent id map being null when file is empty

Fixes #1030
This commit is contained in:
Jonathan Coates 2022-02-28 11:00:19 +00:00
parent e01895d719
commit e0fcc425c6
No known key found for this signature in database
GPG Key ID: B9E431FF07C98D06

View File

@ -45,21 +45,17 @@ public final class IDAssigner
return ServerLifecycleHooks.getCurrentServer().getWorldPath( FOLDER ).toFile(); return ServerLifecycleHooks.getCurrentServer().getWorldPath( FOLDER ).toFile();
} }
private static MinecraftServer getCachedServer() private static boolean hasServerChanged()
{ {
if( server == null ) return null; if( server == null ) return true;
MinecraftServer currentServer = server.get(); MinecraftServer currentServer = server.get();
if( currentServer == null ) return null; return currentServer == null || currentServer != ServerLifecycleHooks.getCurrentServer();
if( currentServer != ServerLifecycleHooks.getCurrentServer() ) return null;
return currentServer;
} }
public static synchronized int getNextId( String kind ) public static synchronized int getNextId( String kind )
{ {
MinecraftServer currentServer = getCachedServer(); if( hasServerChanged() )
if( currentServer == null )
{ {
// The server has changed, refetch our ID map // The server has changed, refetch our ID map
server = new WeakReference<>( ServerLifecycleHooks.getCurrentServer() ); server = new WeakReference<>( ServerLifecycleHooks.getCurrentServer() );
@ -68,23 +64,22 @@ public final class IDAssigner
dir.mkdirs(); dir.mkdirs();
// Load our ID file from disk // Load our ID file from disk
Map<String, Integer> newIds = null;
idFile = new File( dir, "ids.json" ).toPath(); idFile = new File( dir, "ids.json" ).toPath();
if( Files.isRegularFile( idFile ) ) if( Files.isRegularFile( idFile ) )
{ {
try( Reader reader = Files.newBufferedReader( idFile, StandardCharsets.UTF_8 ) ) try( Reader reader = Files.newBufferedReader( idFile, StandardCharsets.UTF_8 ) )
{ {
ids = GSON.fromJson( reader, ID_TOKEN ); newIds = GSON.fromJson( reader, ID_TOKEN );
} }
catch( Exception e ) catch( Exception e )
{ {
ComputerCraft.log.error( "Cannot load id file '" + idFile + "'", e ); ComputerCraft.log.error( "Cannot load id file '" + idFile + "'", e );
ids = new HashMap<>();
} }
} }
else
{ if( newIds == null ) newIds = new HashMap<>();
ids = new HashMap<>(); ids = newIds;
}
} }
Integer existing = ids.get( kind ); Integer existing = ids.get( kind );