Skip to content

Commit

Permalink
Bungee Config Rework
Browse files Browse the repository at this point in the history
  • Loading branch information
VedantMulay committed Feb 19, 2024
1 parent 9c71431 commit 705e51f
Show file tree
Hide file tree
Showing 4 changed files with 109 additions and 67 deletions.
13 changes: 1 addition & 12 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

<groupId>io.github.vedantmulay</groupId>
<artifactId>NeptuneAPI</artifactId>
<version>1.0.3</version>
<version>1.0.5</version>
<packaging>jar</packaging>

<name>NeptuneAPI</name>
Expand Down Expand Up @@ -67,17 +67,6 @@
</executions>
</plugin>

<plugin>
<groupId>org.sonatype.plugins</groupId>
<artifactId>nexus-staging-maven-plugin</artifactId>
<version>1.6.7</version>
<extensions>true</extensions>
<configuration>
<serverId>ossrh</serverId>
<nexusUrl>https://s01.oss.sonatype.org/</nexusUrl>
<autoReleaseAfterClose>true</autoReleaseAfterClose>
</configuration>
</plugin>

<plugin>
<groupId>org.apache.maven.plugins</groupId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@
import org.bukkit.plugin.java.JavaPlugin;

import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.nio.file.Files;
import java.nio.file.Path;

Expand Down Expand Up @@ -75,6 +77,24 @@ public NeptuneConfig(JavaPlugin plugin, String name) {
}
}

public void checkDefaults(JavaPlugin plugin, String name) {
// Load default configuration from resources
InputStream defaultConfigStream = plugin.getResource(name);
if (defaultConfigStream != null) {
YamlConfiguration defaultConfig = YamlConfiguration.loadConfiguration(new InputStreamReader(defaultConfigStream));

// Check if each default value exists in the current configuration
for (String key : defaultConfig.getKeys(true)) {
if (!yaml.contains(key)) {
// Copy default value to current configuration
yaml.set(key, defaultConfig.get(key));
}
}
save();
} else {
plugin.getLogger().warning("Unable to find default configuration file: " + name);
}
}

// Saves the configuration data
public void save() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,78 +23,44 @@

package io.github.vedantmulay.neptuneapi.bungee.config;

import org.yaml.snakeyaml.DumperOptions;
import org.yaml.snakeyaml.Yaml;
import net.md_5.bungee.api.plugin.Plugin;

import java.io.*;
import java.nio.file.*;

import java.util.HashMap;
import java.util.Map;

public class NeptuneBungeeConfig {

private final File configFile;
private final Plugin plugin;
private Map<String, Object> configData;

public NeptuneBungeeConfig(String configFileName) {
this.configFile = new File("plugins/NeptuneBungee/" + configFileName);
this.configData = new HashMap<>();
loadConfig();
}

public void loadConfig() {
try {
if (!configFile.exists()) {
copyDefaultConfig();
}

try (FileReader reader = new FileReader(configFile)) {
Yaml yaml = new Yaml();
configData = yaml.load(reader);
public NeptuneBungeeConfig(String name, Plugin plugin) {
this.plugin = plugin;
// Check if the config file exists, if not, create it
File configFile = new File(plugin.getDataFolder(), name + ".yml");
if (!configFile.exists()) {
plugin.getDataFolder().mkdirs();
try (InputStream inputStream = plugin.getResourceAsStream("config.yml")) {
Files.copy(inputStream, configFile.toPath(), StandardCopyOption.REPLACE_EXISTING);
} catch (IOException e) {
plugin.getLogger().severe("Failed to create config file: " + e.getMessage());
}
} catch (IOException e) {
e.printStackTrace();
}
copyDefaults(configFile);
}

public void saveConfig() {
try (FileWriter writer = new FileWriter(configFile)) {
DumperOptions options = new DumperOptions();
options.setDefaultFlowStyle(DumperOptions.FlowStyle.BLOCK);

Yaml yaml = new Yaml(options);
yaml.dump(configData, writer);
} catch (IOException e) {
e.printStackTrace();
private void copyDefaults(File configFile) {
// Load default config from resources
InputStream defaultConfigStream = plugin.getResourceAsStream("config.yml");
if (defaultConfigStream == null) {
return;
}
}

private void copyDefaultConfig() {
// Copy default config if absent in the plugin folder
try {
Path pluginDirectory = Paths.get(configFile.getParent());
if (!Files.exists(pluginDirectory)) {
Files.createDirectories(pluginDirectory);
}

InputStream defaultConfigStream = getClass().getResourceAsStream("/" + configFile.getName());

if (defaultConfigStream != null) {
Files.copy(defaultConfigStream, Paths.get(configFile.toURI()));
} else {
throw new FileNotFoundException("Default " + configFile.getName() + " not found in resources.");
}
Files.copy(defaultConfigStream, configFile.toPath(), StandardCopyOption.REPLACE_EXISTING);
} catch (IOException e) {
e.printStackTrace();
plugin.getLogger().severe("Failed to copy default config file: " + e.getMessage());
}
}

public Object getConfigValue(String key) {
return configData.get(key);
}

public void setConfigValue(String key, Object value) {
configData.put(key, value);
saveConfig();
}
}
67 changes: 67 additions & 0 deletions src/main/java/io/github/vedantmulay/neptuneapi/common/NTime.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
/*
* Copyright (c) 2024 Lumina Games
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NON INFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*
*/

package io.github.vedantmulay.neptuneapi.common;

import java.util.concurrent.TimeUnit;

public class NTime {

public String convertTime(long time) {
long currentTime = System.currentTimeMillis() / 1000;

if (time <= currentTime) {
return null;
}

long durationInSeconds = time - currentTime;

long weeks = TimeUnit.SECONDS.toDays(durationInSeconds) / 7;
long days = TimeUnit.SECONDS.toDays(durationInSeconds) % 7;
long hours = TimeUnit.SECONDS.toHours(durationInSeconds) % 24;
long minutes = TimeUnit.SECONDS.toMinutes(durationInSeconds) % 60;
long seconds = durationInSeconds % 60;

StringBuilder sb = new StringBuilder();
if (weeks > 0) {
sb.append(weeks).append("w");
}
if (days > 0) {
sb.append(days).append("d");
}
if (hours > 0) {
sb.append(hours).append("h");
}
if (minutes > 0) {
sb.append(minutes).append("m");
}
if (seconds > 0) {
sb.append(seconds).append("s");
}

return sb.toString();
}



}

0 comments on commit 705e51f

Please sign in to comment.