Advertisement
Guest User

Config

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