Advertisement
Guest User

Updated Config

a guest
Oct 12th, 2019
140
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.79 KB | None | 0 0
  1. package org.example;
  2.  
  3. import com.google.common.reflect.TypeToken;
  4. import ninja.leaping.configurate.ConfigurationNode;
  5. import ninja.leaping.configurate.commented.CommentedConfigurationNode;
  6. import ninja.leaping.configurate.hocon.HoconConfigurationLoader;
  7. import ninja.leaping.configurate.loader.ConfigurationLoader;
  8. import ninja.leaping.configurate.objectmapping.ObjectMappingException;
  9.  
  10. import java.io.File;
  11. import java.io.IOException;
  12. import java.util.Collection;
  13. import java.util.List;
  14. import java.util.function.Function;
  15. import java.util.stream.Collector;
  16. import java.util.stream.Collectors;
  17.  
  18. public class Config {
  19.  
  20. private ConfigurationLoader<CommentedConfigurationNode> loader;
  21. private CommentedConfigurationNode root;
  22.  
  23. public Config(ConfigurationLoader<CommentedConfigurationNode> loader, CommentedConfigurationNode root){
  24. this.loader = loader;
  25. this.root = root;
  26. }
  27.  
  28. public Config(File file) {
  29. this.loader = HoconConfigurationLoader.builder().setFile(file).build();
  30. try {
  31. root = this.loader.load();
  32. } catch (IOException e) {
  33. root = this.loader.createEmptyNode();
  34. }
  35. }
  36.  
  37. /**
  38. * Removes a node from the config file, this includes child nodes
  39. * @param node node to remove
  40. * @return itself for chaining
  41. */
  42. public Config remove(Object... node){
  43. return set(null, node);
  44. }
  45.  
  46. /**
  47. * Sets a object directly into the node
  48. * @param object the object to set
  49. * @param node the location
  50. * @return itself for chaining
  51. */
  52. public Config set(Object object, Object... node) {
  53. this.root.getNode(node).setValue(object);
  54. return this;
  55. }
  56.  
  57. /**
  58. * Sets the object directly into the node
  59. * @param type the type the object is
  60. * @param obj the object to set
  61. * @param node the location
  62. * @param <T> the object type
  63. * @return itself for chaining
  64. * @throws ObjectMappingException
  65. */
  66. public <T extends Object> Config set(TypeToken<T> type, T obj, Object... node) throws ObjectMappingException {
  67. this.root.getNode(node).setValue(type, obj);
  68. return this;
  69. }
  70.  
  71. /**
  72. * Gets the value in the location however if none is found then the other is used
  73. * @param function the function to get the value
  74. * @param other the failsafe object
  75. * @param node the location
  76. * @param <T> the type of what to get
  77. * @return the value at the location or other if no value is found
  78. */
  79. public <T extends Object> T get(Function<ConfigurationNode, T> function, T other, Object... node) {
  80. T value = function.apply(this.root.getNode(node));
  81. if (value == null) {
  82. return other;
  83. }
  84. return value;
  85. }
  86.  
  87. /**
  88. * Gets a specified value from the node
  89. *
  90. * @param function the conversion from node to T
  91. * @param node the location
  92. * @param <T> the type
  93. * @return the value
  94. */
  95. public <T extends Object> T get(Function<ConfigurationNode, T> function, Object... node) {
  96. return function.apply(this.root.getNode(node));
  97. }
  98.  
  99. /**
  100. * Checks if the location is real or not
  101. * @param node the location to check
  102. * @return if the location is not real
  103. */
  104. public boolean isVirtual(Object... node){
  105. return get(ConfigurationNode::isVirtual, node);
  106. }
  107.  
  108. /**
  109. * Gets a integer value from the node
  110. *
  111. * @param node the location
  112. * @return the value
  113. */
  114. public int getInteger(Object... node) {
  115. return get(ConfigurationNode::getInt, node);
  116. }
  117.  
  118. /**
  119. * Gets a double value from the node
  120. *
  121. * @param node the location
  122. * @return the value
  123. */
  124. public double getDouble(Object... node) {
  125. return get(ConfigurationNode::getDouble, node);
  126. }
  127.  
  128. /**
  129. * Gets a string value from the node
  130. *
  131. * @param node the location
  132. * @return the value
  133. */
  134. public String getString(Object... node) {
  135. return get(ConfigurationNode::getString, node);
  136. }
  137.  
  138. /**
  139. * Gets a boolean value from the node
  140. *
  141. * @param node the location
  142. * @return the value
  143. */
  144. public boolean getBoolean(Object... node) {
  145. return get(ConfigurationNode::getBoolean, node);
  146. }
  147.  
  148. /**
  149. * Gets a specified type of collection from the configuration file
  150. *
  151. * @param collector The type of collection @see Collectors
  152. * @param function the conversion from node to T
  153. * @param node the location of the position
  154. * @param <T> the class type of the collection
  155. * @param <A> the type of collection
  156. * @return A specified type of collection with all values from the node
  157. */
  158. public <T, A extends Collection<T>> A getCollection(Collector<T, ?, A> collector, Function<ConfigurationNode, T> function, Object... node) {
  159. return this.root.getNode(node).getChildrenList().stream().map(c -> function.apply(c)).collect(collector);
  160. }
  161.  
  162. /**
  163. * Gets a list of T from the configuration file
  164. *
  165. * @param function The conversion from node to T
  166. * @param node The location of the position
  167. * @param <T> The class type of the list
  168. * @return The list with all values
  169. */
  170. public <T> List<T> getList(Function<ConfigurationNode, T> function, Object... node) {
  171. return getCollection(Collectors.toList(), function, node);
  172. }
  173.  
  174. /**
  175. * Saves the changes you have made to the config
  176. *
  177. * @return itself for chaining
  178. * @throws IOException if it can not save for X reason
  179. */
  180. public Config save() throws IOException {
  181. this.loader.save(this.root);
  182. return this;
  183. }
  184. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement