Guest User

Untitled

a guest
Feb 22nd, 2018
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.92 KB | None | 0 0
  1. import ch.njol.skript.classes.ClassInfo;
  2. import ch.njol.skript.classes.Parser;
  3. import ch.njol.skript.classes.Serializer;
  4. import ch.njol.skript.lang.ParseContext;
  5. import ch.njol.util.Pair;
  6. import ch.njol.yggdrasil.Fields;
  7.  
  8. import java.io.StreamCorruptedException;
  9. import java.util.ArrayList;
  10. import java.util.List;
  11. import java.util.Map;
  12.  
  13. // A small class for enum type registration.
  14.  
  15. // Classes.registerClass(new EnumClassInfo<>(myClass.class, "[the] code name").getClassInfo())
  16. public class EnumClassInfo<E extends Enum<?>> {
  17. private Class<E> eClass;
  18. private String codeName;
  19. private List<Map.Entry<String, E>> pairings = new ArrayList<>();
  20.  
  21. public EnumClassInfo(Class<E> eClass, String codeName) {
  22. this.eClass = eClass;
  23. this.codeName = codeName;
  24. for (E value : eClass.getEnumConstants()) {
  25. pairings.add(0, new Pair<>(value.toString(), value));
  26. }
  27. }
  28.  
  29. public ClassInfo<E> getClassInfo() {
  30. return new ClassInfo<>(eClass, codeName)
  31. .user(codeName + "s?")
  32. .parser(getParser(true))
  33. .serializer(getSerializer());
  34. }
  35.  
  36. private Parser<E> getParser(Boolean stringEdited) {
  37. if (stringEdited) {
  38. return new Parser<E>() {
  39. @Override
  40. public E parse(String s, ParseContext context) {
  41. s = s.toUpperCase().replace(" ", "_").replace("İ", "I");
  42. for (Map.Entry<String, E> pairing : pairings) {
  43. if (s.equals(pairing.getKey())) {
  44. return pairing.getValue();
  45. }
  46. }
  47. return null;
  48. }
  49.  
  50. @Override
  51. public String toString(E e, int i) {
  52. return e.toString();
  53. }
  54.  
  55. @Override
  56. public String toVariableNameString(E e) {
  57. return e.toString();
  58. }
  59.  
  60. @Override
  61. public String getVariableNamePattern() {
  62. return ".+";
  63. }
  64. };
  65. }
  66. return new Parser<E>() {
  67. @Override
  68. public E parse(String s, ParseContext context) {
  69. for (Map.Entry<String, E> pairing : pairings) {
  70. if (s.equals(pairing.getKey())) {
  71. return pairing.getValue();
  72. }
  73. }
  74. return null;
  75. }
  76.  
  77. @Override
  78. public String toString(E e, int i) {
  79. return e.toString();
  80. }
  81.  
  82. @Override
  83. public String toVariableNameString(E e) {
  84. return e.toString();
  85. }
  86.  
  87. @Override
  88. public String getVariableNamePattern() {
  89. return ".+";
  90. }
  91. };
  92.  
  93. }
  94.  
  95. private Serializer<E> getSerializer() {
  96. return new Serializer<E>() {
  97. @Override
  98. public Fields serialize(E e) {
  99. Fields fields = new Fields();
  100. fields.putObject("value", EnumClassInfo.this.toString());
  101. return fields;
  102. }
  103.  
  104. @Override
  105. public void deserialize(E e, Fields fields) {
  106. throw new UnsupportedOperationException();
  107. }
  108.  
  109. @Override
  110. public boolean mustSyncDeserialization() {
  111. return false;
  112. }
  113.  
  114. @Override
  115. protected boolean canBeInstantiated() {
  116. return false;
  117. }
  118.  
  119. @Override
  120. public E deserialize(Fields fields) throws StreamCorruptedException {
  121. for (Map.Entry<String, E> pairing : pairings) {
  122. if (fields.getObject("value").equals(pairing.getKey())) {
  123. return pairing.getValue();
  124. }
  125. }
  126. return null;
  127. }
  128. };
  129. }
  130. }
Add Comment
Please, Sign In to add comment