MCPcopy Create free account
hub / github.com/egg82/fetcharr / loadConfig

Method loadConfig

Webhook/src/main/java/me/egg82/fwebhook/Webhook.java:113–157  ·  view source on GitHub ↗
(@NotNull File configFile)

Source from the content-addressed store, hash-verified

111 }
112
113 private @Nullable CommentedConfigurationNode loadConfig(@NotNull File configFile) {
114 if (!configFile.exists() || !configFile.isFile() || configFile.length() == 0L) {
115 try (InputStream resource = getClass().getResourceAsStream("/config.yaml"); FileWriter out = new FileWriter(configFile); BufferedWriter writer = new BufferedWriter(out)) {
116 if (resource == null) {
117 logger.error("Could not get resource {}", "/config.yaml");
118 return null;
119 }
120
121 File parent = configFile.getParentFile();
122 if (parent.exists() && !parent.isDirectory()) {
123 if (!parent.delete()) {
124 logger.error("Could not delete file {}", parent.getAbsolutePath());
125 return null;
126 }
127 }
128 if (!parent.exists() && !parent.mkdirs()) {
129 logger.error("Could not create directory {}", parent.getAbsolutePath());
130 return null;
131 }
132
133 byte[] buffer = new byte[250];
134 int len;
135 while ((len = resource.read(buffer)) > 0) {
136 char[] chars = new char[len];
137 for (int i = 0; i < len; i++) {
138 chars[i] = (char) buffer[i];
139 }
140 writer.write(chars);
141 }
142 } catch (IOException ex) {
143 logger.error("Could not open resource {} or write file {}", "/config.yaml", configFile.getAbsolutePath(), ex);
144 return null;
145 }
146 }
147
148 try (FileReader file = new FileReader(configFile); BufferedReader reader = new BufferedReader(file)) {
149 YamlConfigurationLoader loader = YamlConfigurationLoader.builder()
150 .source(() -> reader)
151 .build();
152 return loader.load();
153 } catch (IOException ex) {
154 logger.error("Could not read config file at {}", configFile.getAbsolutePath(), ex);
155 return null;
156 }
157 }
158
159 private void fetcharrEvent(@NotNull FetcharrEvent event) {
160 if (!started) {

Callers 1

initMethod · 0.95

Calls 6

existsMethod · 0.80
deleteMethod · 0.45
readMethod · 0.45
writeMethod · 0.45
buildMethod · 0.45
sourceMethod · 0.45

Tested by

no test coverage detected