-
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
110 additions
and
24 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
12 changes: 12 additions & 0 deletions
12
common/src/main/java/tocraft/craftedcore/config/annotions/Comment.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
package tocraft.craftedcore.config.annotions; | ||
|
||
import java.lang.annotation.ElementType; | ||
import java.lang.annotation.Retention; | ||
import java.lang.annotation.RetentionPolicy; | ||
import java.lang.annotation.Target; | ||
|
||
@Retention(RetentionPolicy.RUNTIME) | ||
@Target(ElementType.FIELD) | ||
public @interface Comment { | ||
String value() default ""; | ||
} |
33 changes: 33 additions & 0 deletions
33
common/src/main/java/tocraft/craftedcore/util/JsonUtils.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
package tocraft.craftedcore.util; | ||
|
||
import java.util.*; | ||
|
||
public class JsonUtils { | ||
public static String addComments(String json, Map<String, String> keyToComment) { | ||
List<String> lines = new ArrayList<>(json.lines().toList()); | ||
Map<Integer, String> insertions = new TreeMap<>(); | ||
// write comments into new file | ||
for (int i = 0; i < lines.size(); i++) { | ||
String s = lines.get(i); | ||
String indentation = " ".repeat(s.length() - s.trim().length()); | ||
|
||
for (Map.Entry<String, String> entry : keyToComment.entrySet()) { | ||
String comment = entry.getValue(); | ||
if (s.trim().startsWith(String.format("\"%s\"", entry.getKey()))) { | ||
if (comment.contains("\n")) { | ||
comment = indentation + "// " + String.join(String.format("\n%s// ", indentation), comment.split("\n")); | ||
} else { | ||
comment = String.format("%s// %s", indentation, comment); | ||
} | ||
insertions.put(i + insertions.size(), comment); | ||
break; | ||
} | ||
} | ||
} | ||
|
||
StringBuilder sb = new StringBuilder(); | ||
insertions.forEach(lines::add); | ||
lines.forEach(s -> sb.append(String.format("%s%n", s))); | ||
return sb.toString(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters