mirror of
https://github.com/SquidDev-CC/CC-Tweaked
synced 2024-12-14 20:20:30 +00:00
parent
e01895d719
commit
e0fcc425c6
@ -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 );
|
||||||
|
Loading…
Reference in New Issue
Block a user