2015-05-16 20:00:55 +00:00
package me.ryanhamshire.GriefPrevention ;
2020-06-09 04:57:55 +00:00
import com.google.common.io.Files ;
2015-05-16 20:00:55 +00:00
import java.io.File ;
import java.nio.charset.Charset ;
import java.util.List ;
import java.util.UUID ;
import java.util.concurrent.ConcurrentHashMap ;
//loads ignore data from file into a hash map
class IgnoreLoaderThread extends Thread
{
private UUID playerToLoad ;
private ConcurrentHashMap < UUID , Boolean > destinationMap ;
2020-06-09 04:57:55 +00:00
2015-05-16 20:00:55 +00:00
IgnoreLoaderThread ( UUID playerToLoad , ConcurrentHashMap < UUID , Boolean > destinationMap )
{
this . playerToLoad = playerToLoad ;
this . destinationMap = destinationMap ;
this . setPriority ( MIN_PRIORITY ) ;
}
2020-06-09 04:57:55 +00:00
2015-05-16 20:00:55 +00:00
@Override
public void run ( )
{
File ignoreFile = new File ( DataStore . playerDataFolderPath + File . separator + this . playerToLoad + " .ignore " ) ;
2020-06-09 04:57:55 +00:00
2015-05-16 20:00:55 +00:00
//if the file doesn't exist, there's nothing to do here
2020-06-09 04:57:55 +00:00
if ( ! ignoreFile . exists ( ) ) return ;
2015-05-16 20:00:55 +00:00
boolean needRetry = false ;
int retriesRemaining = 5 ;
Exception latestException = null ;
do
{
try
2020-06-09 04:57:55 +00:00
{
2015-05-16 20:00:55 +00:00
needRetry = false ;
2020-06-09 04:57:55 +00:00
2015-05-16 20:00:55 +00:00
//read the file content and immediately close it
List < String > lines = Files . readLines ( ignoreFile , Charset . forName ( " UTF-8 " ) ) ;
2020-06-09 04:57:55 +00:00
2015-05-16 20:00:55 +00:00
//each line is one ignore. asterisks indicate administrative ignores
2020-06-09 04:57:55 +00:00
for ( String line : lines )
2015-05-16 20:00:55 +00:00
{
boolean adminIgnore = false ;
2020-06-09 04:57:55 +00:00
if ( line . startsWith ( " * " ) )
2015-05-16 20:00:55 +00:00
{
adminIgnore = true ;
line = line . substring ( 1 ) ;
}
try
{
UUID ignoredUUID = UUID . fromString ( line ) ;
this . destinationMap . put ( ignoredUUID , adminIgnore ) ;
}
2020-06-09 04:57:55 +00:00
catch ( IllegalArgumentException e ) { } //if a bad UUID, ignore the line
2015-05-16 20:00:55 +00:00
}
}
2020-06-09 04:57:55 +00:00
2015-05-16 20:00:55 +00:00
//if there's any problem with the file's content, retry up to 5 times with 5 milliseconds between
2020-06-09 04:57:55 +00:00
catch ( Exception e )
2015-05-16 20:00:55 +00:00
{
latestException = e ;
needRetry = true ;
retriesRemaining - - ;
}
2020-06-09 04:57:55 +00:00
2015-05-16 20:00:55 +00:00
try
{
2020-06-09 04:57:55 +00:00
if ( needRetry ) Thread . sleep ( 5 ) ;
2015-05-16 20:00:55 +00:00
}
2020-06-09 04:57:55 +00:00
catch ( InterruptedException exception ) { }
} while ( needRetry & & retriesRemaining > = 0 ) ;
2015-05-16 20:00:55 +00:00
//if last attempt failed, log information about the problem
2020-06-09 04:57:55 +00:00
if ( needRetry )
2015-05-16 20:00:55 +00:00
{
GriefPrevention . AddLogEntry ( " Retry attempts exhausted. Unable to load ignore data for player \" " + playerToLoad . toString ( ) + " \" : " + latestException . toString ( ) ) ;
latestException . printStackTrace ( ) ;
}
}
}