Skip to content

Commit 76a2f6e

Browse files
committed
Improve the debug option.
1 parent 72ee0ec commit 76a2f6e

File tree

2 files changed

+137
-94
lines changed

2 files changed

+137
-94
lines changed

Item Creator/src/main/java/org/broken/arrow/library/itemcreator/serialization/itemstack/SerializeItem.java

Lines changed: 25 additions & 94 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
import org.broken.arrow.library.itemcreator.serialization.AttributeModifierWrapper;
1111
import org.broken.arrow.library.itemcreator.serialization.typeadapter.*;
1212
import org.broken.arrow.library.itemcreator.meta.potion.PotionTypeWrapper;
13+
import org.broken.arrow.library.itemcreator.utility.FormatString;
1314
import org.bukkit.*;
1415
import org.bukkit.attribute.Attribute;
1516
import org.bukkit.attribute.AttributeModifier;
@@ -513,98 +514,28 @@ private void setAttributeModifier(final ItemMeta meta) {
513514

514515
@Override
515516
public String toString() {
516-
StringBuilder sb = new StringBuilder("SerializeItem {\n");
517-
518-
appendFieldRecursive(sb, "itemFlags", itemFlags, 1);
519-
appendFieldRecursive(sb, "enchantments", enchantments, 1);
520-
appendFieldRecursive(sb, "attributeModifiers", attributeModifiers, 1);
521-
appendFieldRecursive(sb, "patterns", patterns, 1);
522-
appendFieldRecursive(sb, "type", type, 1);
523-
appendFieldRecursive(sb, "armorColor", armorColor, 1);
524-
appendFieldRecursive(sb, "name", name, 1);
525-
appendFieldRecursive(sb, "lore", lore, 1);
526-
appendFieldRecursive(sb, "customModelData", customModelData, 1);
527-
appendFieldRecursive(sb, "skullOwner", skullOwner, 1);
528-
appendFieldRecursive(sb, "skinPlayerId", skinPlayerId, 1);
529-
appendFieldRecursive(sb, "potionEffects", potionEffects, 1);
530-
appendFieldRecursive(sb, "fireworkMeta", fireworkMeta, 1);
531-
appendFieldRecursive(sb, "bookMenta", bookMenta, 1);
532-
appendFieldRecursive(sb, "mapViewMeta", mapViewMeta, 1);
533-
appendFieldRecursive(sb, "skullUrl", skullUrl, 1);
534-
if (amount > 0) appendFieldRecursive(sb, "amount", amount, 1);
535-
if (unbreakable) appendFieldRecursive(sb, "unbreakable", unbreakable, 1);
536-
537-
trimTrailingComma(sb);
538-
sb.append("}");
539-
return sb.toString();
540-
}
541-
542-
private void appendFieldRecursive(StringBuilder sb, String key, Object value, int indent) {
543-
if (value == null) return;
544-
if (checkIfCollectionEmpty(value)) return;
545-
546-
String prefix = indent(indent);
547-
548-
if (key != null) {
549-
sb.append(prefix).append("\"").append(key).append("\": ");
550-
} else {
551-
sb.append(prefix);
552-
}
553-
554-
if (value instanceof Map<?, ?>) {
555-
Map<?, ?> map = (Map<?, ?>) value;
556-
sb.append("{\n");
557-
for (Map.Entry<?, ?> entry : map.entrySet()) {
558-
appendFieldRecursive(sb, String.valueOf(entry.getKey()), entry.getValue(), indent + 1);
559-
}
560-
trimTrailingComma(sb);
561-
sb.append(prefix).append("},\n");
562-
563-
} else if (value instanceof Collection<?>) {
564-
Collection<?> collection = (Collection<?>) value;
565-
sb.append("[\n");
566-
for (Object obj : collection) {
567-
appendFieldRecursive(sb, null, obj, indent + 1);
568-
}
569-
trimTrailingComma(sb);
570-
sb.append(prefix).append("],\n");
571-
572-
} else if (value instanceof String) {
573-
sb.append("\"").append(value).append("\",\n");
574-
575-
} else {
576-
String text = value.toString();
577-
if (text.contains("\n")) {
578-
sb.append("{\n");
579-
String[] lines = text.split("\n");
580-
for (String line : lines) {
581-
sb.append(indent(indent + 1)).append(line.trim()).append("\n");
582-
}
583-
sb.append(prefix).append("},\n");
584-
} else {
585-
sb.append(value).append(",\n");
586-
}
587-
}
588-
}
589-
590-
private void trimTrailingComma(StringBuilder sb) {
591-
int len = sb.length();
592-
if (len > 2 && sb.substring(len - 2).equals(",\n")) {
593-
sb.setLength(len - 2);
594-
sb.append('\n');
595-
}
596-
}
597-
598-
private String indent(int level) {
599-
StringBuilder spaces = new StringBuilder();
600-
for (int i = 0; i < level * 2; i++) {
601-
spaces.append(' ');
602-
}
603-
return spaces.toString();
604-
}
605-
606-
private static boolean checkIfCollectionEmpty(final Object value) {
607-
return (value instanceof Collection && ((Collection<?>) value).isEmpty())
608-
|| (value instanceof Map && ((Map<?, ?>) value).isEmpty());
609-
}
517+
FormatString string = new FormatString(new StringBuilder("SerializeItem {\n"));
518+
519+
string.appendFieldRecursive("itemFlags", itemFlags, 1);
520+
string.appendFieldRecursive("enchantments", enchantments, 1);
521+
string.appendFieldRecursive("attributeModifiers", attributeModifiers, 1);
522+
string.appendFieldRecursive("patterns", patterns, 1);
523+
string.appendFieldRecursive("type", type, 1);
524+
string.appendFieldRecursive("armorColor", armorColor, 1);
525+
string.appendFieldRecursive("name", name, 1);
526+
string.appendFieldRecursive("lore", lore, 1);
527+
string.appendFieldRecursive("customModelData", customModelData, 1);
528+
string.appendFieldRecursive("skullOwner", skullOwner, 1);
529+
string.appendFieldRecursive("skinPlayerId", skinPlayerId, 1);
530+
string.appendFieldRecursive("potionEffects", potionEffects, 1);
531+
string.appendFieldRecursive("fireworkMeta", fireworkMeta, 1);
532+
string.appendFieldRecursive("bookMenta", bookMenta, 1);
533+
string.appendFieldRecursive("mapViewMeta", mapViewMeta, 1);
534+
string.appendFieldRecursive("skullUrl", skullUrl, 1);
535+
if (amount > 0) string.appendFieldRecursive("amount", amount, 1);
536+
if (unbreakable) string.appendFieldRecursive("unbreakable", unbreakable, 1);
537+
538+
return string.finalizeString().toString();
539+
}
540+
610541
}
Lines changed: 112 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,112 @@
1+
package org.broken.arrow.library.itemcreator.utility;
2+
3+
import javax.annotation.Nonnull;
4+
import java.util.Collection;
5+
import java.util.Map;
6+
7+
public class FormatString {
8+
9+
private final StringBuilder formatedString;
10+
11+
/**
12+
* Create a formated string in a json style format.
13+
*
14+
* @param builder the string builder instance you want to use.
15+
*/
16+
public FormatString(@Nonnull final StringBuilder builder) {
17+
this.formatedString = builder;
18+
}
19+
20+
/**
21+
* Append your value for a string, map or a collection.
22+
*
23+
* @param key the key you want to attach to the value.
24+
* @param value the value to format.
25+
* @param indent amount of spaces to add.
26+
*/
27+
public void appendFieldRecursive(String key, Object value, int indent) {
28+
if (value == null) return;
29+
if (checkIfCollectionEmpty(value)) return;
30+
31+
String prefix = indent(indent);
32+
33+
if (key != null) {
34+
formatedString.append(prefix).append("\"").append(key).append("\": ");
35+
} else {
36+
formatedString.append(prefix);
37+
}
38+
39+
if (value instanceof Map<?, ?>) {
40+
Map<?, ?> map = (Map<?, ?>) value;
41+
formatedString.append("{\n");
42+
for (Map.Entry<?, ?> entry : map.entrySet()) {
43+
appendFieldRecursive(String.valueOf(entry.getKey()), entry.getValue(), indent + 1);
44+
}
45+
trimTrailingComma();
46+
formatedString.append(prefix).append("},\n");
47+
48+
} else if (value instanceof Collection<?>) {
49+
Collection<?> collection = (Collection<?>) value;
50+
formatedString.append("[\n");
51+
for (Object obj : collection) {
52+
appendFieldRecursive(null, obj, indent + 1);
53+
}
54+
trimTrailingComma();
55+
formatedString.append(prefix).append("],\n");
56+
57+
} else if (value instanceof String) {
58+
formatedString.append("\"").append(value).append("\",\n");
59+
60+
} else {
61+
addNewline(value, indent, prefix);
62+
}
63+
}
64+
65+
/**
66+
* Finalize the string created.
67+
*
68+
* @return the string builder instance with your set data.
69+
*/
70+
public StringBuilder finalizeString() {
71+
trimTrailingComma();
72+
formatedString.append("}");
73+
return formatedString;
74+
}
75+
76+
private void addNewline(final Object value, final int indent, final String prefix) {
77+
String text = value.toString();
78+
if (text.contains("\n")) {
79+
formatedString.append("{\n");
80+
String[] lines = text.split("\n");
81+
for (String line : lines) {
82+
formatedString.append(indent(indent + 1)).append(line.trim()).append("\n");
83+
}
84+
formatedString.append(prefix).append("},\n");
85+
} else {
86+
formatedString.append(value).append(",\n");
87+
}
88+
}
89+
90+
private void trimTrailingComma() {
91+
int len = formatedString.length();
92+
if (len > 2 && formatedString.substring(len - 2).equals(",\n")) {
93+
formatedString.setLength(len - 2);
94+
formatedString.append('\n');
95+
}
96+
}
97+
98+
private String indent(int level) {
99+
StringBuilder spaces = new StringBuilder();
100+
for (int i = 0; i < level * 2; i++) {
101+
spaces.append(' ');
102+
}
103+
return spaces.toString();
104+
}
105+
106+
private static boolean checkIfCollectionEmpty(final Object value) {
107+
return (value instanceof Collection && ((Collection<?>) value).isEmpty())
108+
|| (value instanceof Map && ((Map<?, ?>) value).isEmpty());
109+
}
110+
111+
112+
}

0 commit comments

Comments
 (0)