001package net.tnemc.plugincore.core.config; 002 003/* 004 * The New Plugin Core 005 * Copyright (C) 2022 - 2024 Daniel "creatorfromhell" Vidmar 006 * 007 * This program is free software: you can redistribute it and/or modify 008 * it under the terms of the GNU Affero General Public License as published by 009 * the Free Software Foundation, either version 3 of the License, or 010 * (at your option) any later version. 011 * 012 * This program is distributed in the hope that it will be useful, 013 * but WITHOUT ANY WARRANTY; without even the implied warranty of 014 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 015 * GNU Affero General Public License for more details. 016 * 017 * You should have received a copy of the GNU Affero General Public License 018 * along with this program. If not, see <http://www.gnu.org/licenses/>. 019 */ 020 021import dev.dejvokep.boostedyaml.YamlDocument; 022import dev.dejvokep.boostedyaml.settings.Settings; 023import net.tnemc.plugincore.PluginCore; 024import net.tnemc.plugincore.core.compatibility.log.DebugLevel; 025import net.tnemc.plugincore.core.paste.IPasteable; 026import org.jetbrains.annotations.NotNull; 027import org.jetbrains.annotations.Nullable; 028 029import java.io.File; 030import java.io.IOException; 031import java.io.InputStream; 032import java.net.URL; 033import java.net.URLConnection; 034import java.util.ArrayList; 035import java.util.Collections; 036import java.util.List; 037 038/** 039 * Represents a configuration file. 040 * 041 * @author creatorfromhell 042 * @since 0.1.2.0 043 */ 044public abstract class Config implements IPasteable { 045 046 protected final String fileName; 047 protected final File file; 048 protected final String defaults; 049 protected final List<String> nodes = new ArrayList<>(); 050 protected YamlDocument yaml; 051 protected Settings[] settings; 052 053 public Config(final String fileName, final String defaults, final List<String> nodes, final Settings... settings) { 054 055 this.defaults = defaults; 056 this.fileName = fileName; 057 this.nodes.addAll(nodes); 058 file = new File(PluginCore.directory(), fileName); 059 060 this.settings = settings; 061 062 if(!file.exists()) { 063 PluginCore.log().error("Configuration doesn't exist! File Name:" + fileName, DebugLevel.OFF); 064 } 065 } 066 067 public boolean load() { 068 069 try(final InputStream in = getResource(defaults)) { 070 071 if(in != null) { 072 this.yaml = YamlDocument.create(file, in, settings); 073 074 return true; 075 } 076 } catch(final IOException e) { 077 078 PluginCore.log().error("Error while creating config \"" + file.getName() + "\".", e, DebugLevel.OFF); 079 return false; 080 } 081 return false; 082 } 083 084 public YamlDocument getYaml() { 085 086 return yaml; 087 } 088 089 public void setYaml(final YamlDocument yaml) { 090 091 this.yaml = yaml; 092 } 093 094 public boolean save() { 095 096 try { 097 yaml.save(file); 098 return true; 099 } catch(final IOException e) { 100 PluginCore.log().error("Error while saving config \"" + nodes.get(0) + "\".", e, DebugLevel.OFF); 101 return false; 102 } 103 } 104 105 public void setComment(final String route, final List<String> comments) { 106 107 if(yaml.contains(route)) { 108 yaml.getBlock(route).setComments(comments); 109 } 110 } 111 112 public void setComment(final String route, final String comment) { 113 114 setComment(route, Collections.singletonList(comment)); 115 } 116 117 public @Nullable InputStream getResource(@NotNull final String filename) { 118 119 try { 120 final URL url = this.getClass().getClassLoader().getResource(filename); 121 if(url == null) { 122 return null; 123 } else { 124 125 final URLConnection connection = url.openConnection(); 126 connection.setUseCaches(false); 127 128 return connection.getInputStream(); 129 } 130 } catch(final IOException var4) { 131 return null; 132 } 133 } 134 135 //PASTEABLE implementation 136 137 /** 138 * Retrieves the file name associated with this object. 139 * 140 * @return The file name as a String. 141 */ 142 @Override 143 public String fileName() { 144 145 return fileName; 146 } 147 148 /** 149 * Retrieves the extension associated with this object. 150 * 151 * @return The extension as a String. 152 */ 153 @Override 154 public String extension() { 155 156 return "yml"; 157 } 158 159 /** 160 * Retrieves the syntax associated with this object. 161 * 162 * @return The syntax as a String. 163 */ 164 @Override 165 public String syntax() { 166 167 return "YAML"; 168 } 169 170 /** 171 * Retrieves the content of the object. 172 * 173 * @return The content as a String. 174 */ 175 @Override 176 public String content() { 177 178 return getYaml().dump(); 179 } 180}