4545import java .net .MalformedURLException ;
4646import java .net .URISyntaxException ;
4747import java .net .URL ;
48+ import java .nio .file .Files ;
49+ import java .nio .file .Path ;
4850import java .text .DecimalFormat ;
4951import java .text .DecimalFormatSymbols ;
5052import java .text .NumberFormat ;
5153import java .util .*;
54+ import java .util .concurrent .ConcurrentHashMap ;
5255import java .util .concurrent .ThreadLocalRandom ;
5356import java .util .function .BiConsumer ;
5457import java .util .regex .Matcher ;
@@ -62,6 +65,7 @@ public abstract class ZUtils extends MessageUtils {
6265 private static final Timer TIMER = new Timer ();
6366 private static final UUID RANDOM_UUID = UUID .fromString ("92864445-51c5-4c3b-9039-517c9927d1b4" ); // We reuse the same "random" UUID all the time
6467 private static final SimpleCache <String , Object > cache = new SimpleCache <>();
68+ private static final ConcurrentHashMap <Path , CachedLines > CONFIGURATION_CACHE = new ConcurrentHashMap <>();
6569 // For plugin support from 1.8 to 1.12
6670 private static Material [] byId ;
6771
@@ -786,46 +790,42 @@ protected YamlConfiguration loadAndReplaceConfiguration(File file, Map<String, O
786790 YamlConfiguration loadConfiguration = new YamlConfiguration ();
787791
788792 try {
789- FileInputStream stream = new FileInputStream (file );
790- Reader reader = new InputStreamReader (stream , Charsets .UTF_8 );
791-
792- BufferedReader input = new BufferedReader (reader );
793+ CachedLines cachedLines = getCachedLines (file );
793794 StringBuilder builder = new StringBuilder ();
794795 Placeholders placeholders = new Placeholders ();
796+ Map <String , Object > placeholdersMap = mapPlaceholders != null ? mapPlaceholders : Collections .emptyMap ();
795797
796- String line ;
797- try {
798- while ((line = input .readLine ()) != null ) {
799-
800- for (Map .Entry <String , Object > replacement : mapPlaceholders .entrySet ()) {
801- String key = replacement .getKey ();
802- Object value = replacement .getValue ();
803-
804- if (line != null ) {
805- if (value instanceof List <?> && line .contains ("%" + key + "%" )) {
806- int index = line .indexOf ("%" + key + "%" );
807- String prefix = line .substring (0 , index );
808- String finalLine = line .substring (index );
809- ((List <?>) value ).forEach (currentValue -> {
810- String currentElement = placeholders .parse (finalLine , key , currentValue .toString ());
811- builder .append (placeholders .parse (prefix , key , currentValue .toString ())).append (currentElement );
812- builder .append ('\n' );
813- });
814-
815- line = null ;
816- } else {
817- line = placeholders .parse (line , key , value .toString ());
818- }
819- }
820- }
798+ for (String originalLine : cachedLines .lines ()) {
799+ String line = originalLine ;
800+
801+ for (Map .Entry <String , Object > replacement : placeholdersMap .entrySet ()) {
802+ String key = replacement .getKey ();
803+ Object value = replacement .getValue ();
821804
822805 if (line != null ) {
823- builder .append (line );
824- builder .append ('\n' );
806+ if (value instanceof List <?> && line .contains ("%" + key + "%" )) {
807+ int index = line .indexOf ("%" + key + "%" );
808+ String prefix = line .substring (0 , index );
809+ String finalLine = line .substring (index );
810+ ((List <?>) value ).forEach (currentValue -> {
811+ String replacementValue = currentValue != null ? currentValue .toString () : "" ;
812+ String currentElement = placeholders .parse (finalLine , key , replacementValue );
813+ builder .append (placeholders .parse (prefix , key , replacementValue )).append (currentElement );
814+ builder .append ('\n' );
815+ });
816+
817+ line = null ;
818+ } else {
819+ String replacementValue = value != null ? value .toString () : "" ;
820+ line = placeholders .parse (line , key , replacementValue );
821+ }
825822 }
826823 }
827- } finally {
828- input .close ();
824+
825+ if (line != null ) {
826+ builder .append (line );
827+ builder .append ('\n' );
828+ }
829829 }
830830
831831 loadConfiguration .loadFromString (builder .toString ());
@@ -837,4 +837,28 @@ protected YamlConfiguration loadAndReplaceConfiguration(File file, Map<String, O
837837 return loadConfiguration ;
838838 }
839839
840+ protected static void clearConfigurationCache () {
841+ CONFIGURATION_CACHE .clear ();
842+ }
843+
844+ private static CachedLines getCachedLines (File file ) throws IOException {
845+ Path path = file .toPath ();
846+ long lastModified = file .lastModified ();
847+ CachedLines cachedLines = CONFIGURATION_CACHE .get (path );
848+
849+ if (cachedLines == null || cachedLines .lastModified () != lastModified ) {
850+ List <String > lines = Files .readAllLines (path , Charsets .UTF_8 );
851+ cachedLines = new CachedLines (lines , lastModified );
852+ CONFIGURATION_CACHE .put (path , cachedLines );
853+ }
854+
855+ return cachedLines ;
856+ }
857+
858+ private record CachedLines (List <String > lines , long lastModified ) {
859+ private CachedLines {
860+ lines = List .copyOf (lines );
861+ }
862+ }
863+
840864}
0 commit comments