|
10 | 10 | import org.broken.arrow.library.itemcreator.serialization.AttributeModifierWrapper; |
11 | 11 | import org.broken.arrow.library.itemcreator.serialization.typeadapter.*; |
12 | 12 | import org.broken.arrow.library.itemcreator.meta.potion.PotionTypeWrapper; |
| 13 | +import org.broken.arrow.library.itemcreator.utility.FormatString; |
13 | 14 | import org.bukkit.*; |
14 | 15 | import org.bukkit.attribute.Attribute; |
15 | 16 | import org.bukkit.attribute.AttributeModifier; |
@@ -513,98 +514,28 @@ private void setAttributeModifier(final ItemMeta meta) { |
513 | 514 |
|
514 | 515 | @Override |
515 | 516 | 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 | + |
610 | 541 | } |
0 commit comments