1
0
mirror of https://github.com/SquidDev-CC/CC-Tweaked synced 2024-12-13 19:50:31 +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();
}
private static MinecraftServer getCachedServer()
private static boolean hasServerChanged()
{
if( server == null ) return null;
if( server == null ) return true;
MinecraftServer currentServer = server.get();
if( currentServer == null ) return null;
if( currentServer != ServerLifecycleHooks.getCurrentServer() ) return null;
return currentServer;
return currentServer == null || currentServer != ServerLifecycleHooks.getCurrentServer();
}
public static synchronized int getNextId( String kind )
{
MinecraftServer currentServer = getCachedServer();
if( currentServer == null )
if( hasServerChanged() )
{
// The server has changed, refetch our ID map
server = new WeakReference<>( ServerLifecycleHooks.getCurrentServer() );
@ -68,23 +64,22 @@ public final class IDAssigner
dir.mkdirs();
// Load our ID file from disk
Map<String, Integer> newIds = null;
idFile = new File( dir, "ids.json" ).toPath();
if( Files.isRegularFile( idFile ) )
{
try( Reader reader = Files.newBufferedReader( idFile, StandardCharsets.UTF_8 ) )
{
ids = GSON.fromJson( reader, ID_TOKEN );
newIds = GSON.fromJson( reader, ID_TOKEN );
}
catch( Exception e )
{
ComputerCraft.log.error( "Cannot load id file '" + idFile + "'", e );
ids = new HashMap<>();
}
}
else
{
ids = new HashMap<>();
}
if( newIds == null ) newIds = new HashMap<>();
ids = newIds;
}
Integer existing = ids.get( kind );