();
19 |
20 | public FileManager(JavaPlugin plugin)
21 | {
22 | this.plugin = plugin;
23 | }
24 |
25 | /**
26 | * Get the config by the name(Don't forget the .yml)
27 | *
28 | * @param name
29 | * @return
30 | */
31 | public Config getConfig(String name) {
32 | if (!configs.containsKey(name))
33 | configs.put(name, new Config(name));
34 |
35 | return configs.get(name);
36 | }
37 |
38 | /**
39 | * Save the config by the name(Don't forget the .yml)
40 | *
41 | * @param name
42 | * @return
43 | */
44 | public Config saveConfig(String name) {
45 | return getConfig(name).save();
46 | }
47 |
48 | /**
49 | * Reload the config by the name(Don't forget the .yml)
50 | *
51 | * @param name
52 | * @return
53 | */
54 | public Config reloadConfig(String name) {
55 | return getConfig(name).reload();
56 | }
57 |
58 | public class Config {
59 |
60 | private String name;
61 | private File file;
62 | private YamlConfiguration config;
63 |
64 | public Config(String name)
65 | {
66 | this.name = name;
67 | }
68 |
69 | /**
70 | * Saves the config as long as the config isn't empty
71 | *
72 | * @return
73 | */
74 | public Config save() {
75 | if ((this.config == null) || (this.file == null))
76 | return this;
77 | try
78 | {
79 | if (config.getConfigurationSection("").getKeys(true).size() != 0)
80 | config.save(this.file);
81 | }
82 | catch (IOException ex)
83 | {
84 | ex.printStackTrace();
85 | }
86 | return this;
87 | }
88 |
89 | /**
90 | * Gets the config as a YamlConfiguration
91 | *
92 | * @return
93 | */
94 | public YamlConfiguration get() {
95 | if (this.config == null)
96 | reload();
97 |
98 | return this.config;
99 | }
100 |
101 | /**
102 | * Saves the default config(Will overwrite anything in the current config's file)
103 | *
104 | * Don't forget to reload after!
105 | *
106 | * @return
107 | */
108 | public Config saveDefaultConfig() {
109 | file = new File(plugin.getDataFolder(), this.name);
110 |
111 | plugin.saveResource(this.name, false);
112 |
113 | return this;
114 | }
115 |
116 | /**
117 | * Reloads the config
118 | *
119 | * @return
120 | */
121 | public Config reload() {
122 | if (file == null)
123 | this.file = new File(plugin.getDataFolder(), this.name);
124 |
125 | this.config = YamlConfiguration.loadConfiguration(file);
126 |
127 | Reader defConfigStream;
128 | try
129 | {
130 | defConfigStream = new InputStreamReader(plugin.getResource(this.name), "UTF8");
131 |
132 | if (defConfigStream != null)
133 | {
134 | YamlConfiguration defConfig = YamlConfiguration.loadConfiguration(defConfigStream);
135 | this.config.setDefaults(defConfig);
136 | }
137 | }
138 | catch (UnsupportedEncodingException | NullPointerException e)
139 | {
140 |
141 | }
142 | return this;
143 | }
144 |
145 | /**
146 | * Copies the config from the resources to the config's default settings.
147 | *
148 | * Force = true ----> Will add any new values from the default file
149 | *
150 | * Force = false ---> Will NOT add new values from the default file
151 | *
152 | * @param force
153 | * @return
154 | */
155 | public Config copyDefaults(boolean force) {
156 | get().options().copyDefaults(force);
157 | return this;
158 | }
159 |
160 | /**
161 | * An easy way to set a value into the config
162 | *
163 | * @param key
164 | * @param value
165 | * @return
166 | */
167 | public Config set(String key, Object value) {
168 | get().set(key, value);
169 | return this;
170 | }
171 |
172 | /**
173 | * An easy way to get a value from the config
174 | *
175 | * @param key
176 | * @return
177 | */
178 | public String get(String key) {
179 | return get().get(key).toString();
180 | }
181 |
182 | public Object getObject(String key) {
183 | return get().get(key);
184 | }
185 |
186 | public YamlConfiguration getConfig() {
187 | return this.config;
188 | }
189 | }
190 |
191 | }
--------------------------------------------------------------------------------
/src/main/java/eu/ncodes/appwritedatabase/Services/CreateCollectionService.java:
--------------------------------------------------------------------------------
1 | package eu.ncodes.appwritedatabase.Services;
2 |
3 | import com.google.gson.JsonElement;
4 | import com.google.gson.JsonParser;
5 | import eu.ncodes.appwritedatabase.Utils.PluginVariables;
6 | import io.appwrite.exceptions.AppwriteException;
7 | import kotlin.Result;
8 | import kotlin.coroutines.Continuation;
9 | import kotlin.coroutines.CoroutineContext;
10 | import kotlin.coroutines.EmptyCoroutineContext;
11 | import okhttp3.Response;
12 | import org.jetbrains.annotations.NotNull;
13 |
14 | import java.util.ArrayList;
15 | import java.util.HashMap;
16 | import java.util.LinkedHashMap;
17 | import java.util.Map;
18 | import java.util.function.Consumer;
19 |
20 | public class CreateCollectionService {
21 | // TODO: Refactor
22 | public static void CreateCollection(Consumer callback) {
23 |
24 | // Create default rules
25 | ArrayList