44import org .jetbrains .annotations .NotNull ;
55
66import java .io .*;
7+ import java .nio .file .Path ;
78import java .util .function .Function ;
89
10+ import static java .nio .file .Files .move ;
11+ import static java .nio .file .StandardCopyOption .ATOMIC_MOVE ;
12+
913
1014public class PluginConfigManipulator {
1115 private final Plugin plugin ;
@@ -42,11 +46,11 @@ public void addLineBelow(String needle, String content) {
4246 }
4347
4448 private void transformLinesContaining (String needle , Function <@ NotNull String , @ NotNull String []> transformer ) {
45- File inputFile = new File ( this .plugin .getDataFolder () + File . separator + "config.yml" );
46- File tempFile = new File ( this .plugin .getDataFolder () + File . separator + "temp.yml" );
49+ Path inputFile = this .plugin .getDataPath (). resolve ( "config.yml" );
50+ Path tempFile = this .plugin .getDataPath (). resolve ( "temp.yml" );
4751 try (
48- BufferedReader reader = new BufferedReader (new FileReader (inputFile ));
49- BufferedWriter writer = new BufferedWriter (new FileWriter (tempFile ))
52+ BufferedReader reader = new BufferedReader (new FileReader (inputFile . toFile () ));
53+ BufferedWriter writer = new BufferedWriter (new FileWriter (tempFile . toFile () ))
5054 ) {
5155
5256 String currentLine ;
@@ -61,11 +65,7 @@ private void transformLinesContaining(String needle, Function<@NotNull String, @
6165 }
6266 }
6367
64- boolean deleted = inputFile .delete ();
65- boolean renamed = tempFile .renameTo (inputFile );
66- if (!deleted || !renamed ) {
67- this .plugin .getComponentLogger ().warn ("Failed to swap temporary file with config" );
68- }
68+ move (tempFile , inputFile , ATOMIC_MOVE );
6969 } catch (IOException e ) {
7070 this .plugin .getComponentLogger ().error ("Failed to transform config file {} with needle '{}'" , inputFile , needle , e );
7171 }
0 commit comments