1010import io .github .syst3ms .skriptparser .log .SkriptLogger ;
1111import org .jetbrains .annotations .Nullable ;
1212
13+ import java .io .BufferedReader ;
14+ import java .io .BufferedWriter ;
15+ import java .io .IOException ;
16+ import java .io .InputStream ;
17+ import java .io .InputStreamReader ;
18+ import java .nio .file .Files ;
1319import java .nio .file .Path ;
20+ import java .nio .file .StandardOpenOption ;
21+ import java .util .ArrayList ;
22+ import java .util .List ;
23+ import java .util .Set ;
24+ import java .util .stream .Collectors ;
1425
1526/**
1627 * Config for Skript
1728 */
1829public class SkriptConfig {
1930
20- private final Config config ;
31+ private Config config ;
2132 private final boolean debug ;
2233 private final int maxTargetBlockDistance ;
2334 private final ConfigSection effectCommands ;
@@ -40,8 +51,15 @@ public SkriptConfig(Skript skript) {
4051 logger .debug ("Checking for update from: " + configVersion );
4152 Semver hySkriptVersion = skript .getPlugin ().getManifest ().getVersion ();
4253 if (configVersion .compareTo (hySkriptVersion ) < 0 ) {
43- logger .debug ("Updating config to version: " + hySkriptVersion );
44- updateConfig ();
54+ logger .info ("Updating config to version: " + hySkriptVersion );
55+ try {
56+ // Update the config from the default config
57+ updateConfig (skriptConfigPath , hySkriptVersion .toString ());
58+ // Reload the config so we have updated values
59+ this .config = new Config (skriptConfigPath , "/config.sk" , logger );
60+ } catch (IOException e ) {
61+ throw new RuntimeException (e );
62+ }
4563 } else {
4664 logger .debug ("Config is up to date" );
4765 }
@@ -51,25 +69,20 @@ public SkriptConfig(Skript skript) {
5169
5270 // Set up max-target-block-distance
5371 this .maxTargetBlockDistance = this .config .getInt ("max-target-block-distance" );
54- if (this .maxTargetBlockDistance == -1 ) {
55- // This would happen if the config is missing this value
56- // TODO update config
57- } else if (this .maxTargetBlockDistance < 0 ) {
72+ if (this .maxTargetBlockDistance < 0 ) {
5873 logger .error ("max-target-block-distance must be greater than or equal to 0" , ErrorType .STRUCTURE_ERROR );
5974 }
6075
6176 // Set up effect commands
6277 this .effectCommands = this .config .getConfigSection ("effect-commands" );
6378 if (this .effectCommands == null ) {
6479 logger .error ("Effect commands section not found in config.sk" , ErrorType .STRUCTURE_ERROR );
65- // TODO update config
6680 }
6781
6882 // Set up databases
6983 this .databases = this .config .getConfigSection ("databases" );
7084 if (this .databases == null ) {
7185 logger .error ("Databases section not found in config.sk" , ErrorType .STRUCTURE_ERROR );
72- // TODO update config
7386 }
7487
7588 // Set up commands generate permissions
@@ -131,8 +144,71 @@ public boolean getCommandsGeneratePermissions() {
131144 return this .commandsGeneratePermissions ;
132145 }
133146
134- private void updateConfig () {
135- // TODO update config
147+ @ SuppressWarnings ("resource" )
148+ private void updateConfig (Path userPath , String version ) throws IOException {
149+ Set <String > userKeys = Files .lines (userPath )
150+ .map (String ::trim )
151+ .filter (l -> !l .startsWith ("#" ) && l .contains (":" ))
152+ .map (l -> l .split (":" )[0 ].trim ())
153+ .collect (Collectors .toSet ());
154+
155+ InputStream stream = SkriptConfig .class .getResourceAsStream ("/config.sk" );
156+ assert stream != null ;
157+ try (BufferedReader reader = new BufferedReader (new InputStreamReader (stream ));
158+ BufferedWriter writer = Files .newBufferedWriter (userPath , StandardOpenOption .APPEND )) {
159+
160+ List <String > commentBuffer = new ArrayList <>();
161+ String line ;
162+
163+ while ((line = reader .readLine ()) != null ) {
164+ String trimmed = line .trim ();
165+
166+ if (trimmed .startsWith ("#" )) {
167+ // It's a comment, save it for the next key we find
168+ commentBuffer .add (line );
169+ } else if (trimmed .contains (":" )) {
170+ String key = trimmed .split (":" )[0 ].trim ();
171+
172+ if (!userKeys .contains (key )) {
173+ // User is missing this key!
174+ writer .newLine (); // Add spacing for readability
175+
176+ // Write the buffered comments first
177+ for (String comment : commentBuffer ) {
178+ writer .write (comment );
179+ writer .newLine ();
180+ }
181+ // Write the actual key-value pair
182+ writer .write (line );
183+ writer .newLine ();
184+ }
185+ // Clear buffer regardless of whether we wrote it or not
186+ commentBuffer .clear ();
187+ } else if (trimmed .isEmpty ()) {
188+ commentBuffer .clear ();
189+ }
190+ }
191+ }
192+
193+ updateConfigVersion (userPath , version );
194+ }
195+
196+ private void updateConfigVersion (Path userPath , String newVersion ) throws IOException {
197+ List <String > lines = Files .readAllLines (userPath );
198+ boolean updated = false ;
199+
200+ for (int i = 0 ; i < lines .size (); i ++) {
201+ String line = lines .get (i ).trim ();
202+ if (line .startsWith ("hyskript-version:" )) {
203+ lines .set (i , "hyskript-version: " + newVersion );
204+ updated = true ;
205+ break ;
206+ }
207+ }
208+
209+ if (updated ) {
210+ Files .write (userPath , lines );
211+ }
136212 }
137213
138214}
0 commit comments