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 YamlDocument yaml;
047
048  protected final String fileName;
049  protected final File file;
050  protected final String defaults;
051
052  protected final List<String> nodes = new ArrayList<>();
053
054  protected Settings[] settings;
055
056  public Config(final String fileName, final String defaults, final List<String> nodes, final Settings... settings) {
057    this.defaults = defaults;
058    this.fileName = fileName;
059    this.nodes.addAll(nodes);
060    file = new File(PluginCore.directory(), fileName);
061
062    this.settings = settings;
063
064    if(!file.exists()) {
065      PluginCore.log().error("Configuration doesn't exist! File Name:" + fileName, DebugLevel.OFF);
066    }
067  }
068
069  public boolean load() {
070
071    try(final InputStream in = getResource(defaults)) {
072
073      if(in != null) {
074        this.yaml = YamlDocument.create(file, in, settings);
075
076        return true;
077      }
078    } catch (final IOException e) {
079
080      PluginCore.log().error("Error while creating config \"" + file.getName() + "\".", e, DebugLevel.OFF);
081      return false;
082    }
083    return false;
084  }
085
086  public YamlDocument getYaml() {
087    return yaml;
088  }
089
090  public void setYaml(final YamlDocument yaml) {
091    this.yaml = yaml;
092  }
093
094  public boolean save() {
095    try {
096      yaml.save(file);
097      return true;
098    } catch(final IOException e) {
099      PluginCore.log().error("Error while saving config \"" + nodes.get(0) + "\".", e, DebugLevel.OFF);
100      return false;
101    }
102  }
103
104  public void setComment(final String route, final List<String> comments) {
105
106    if(yaml.contains(route)) {
107      yaml.getBlock(route).setComments(comments);
108    }
109  }
110
111  public void setComment(final String route, final String comment) {
112
113    setComment(route, Collections.singletonList(comment));
114  }
115
116  public @Nullable InputStream getResource(@NotNull final String filename) {
117
118    try {
119      final URL url = this.getClass().getClassLoader().getResource(filename);
120      if (url == null) {
121        return null;
122      } else {
123
124        final URLConnection connection = url.openConnection();
125        connection.setUseCaches(false);
126
127        return connection.getInputStream();
128      }
129    } catch (final IOException var4) {
130      return null;
131    }
132  }
133
134  //PASTEABLE implementation
135
136  /**
137   * Retrieves the file name associated with this object.
138   *
139   * @return The file name as a String.
140   */
141  @Override
142  public String fileName() {
143    return fileName;
144  }
145
146  /**
147   * Retrieves the extension associated with this object.
148   *
149   * @return The extension as a String.
150   */
151  @Override
152  public String extension() {
153    return "yml";
154  }
155
156  /**
157   * Retrieves the syntax associated with this object.
158   *
159   * @return The syntax as a String.
160   */
161  @Override
162  public String syntax() {
163    return "YAML";
164  }
165
166  /**
167   * Retrieves the content of the object.
168   *
169   * @return The content as a String.
170   */
171  @Override
172  public String content() {
173    return getYaml().dump();
174  }
175}