Skip to content

Commit

Permalink
Created ConfigManager.java
Browse files Browse the repository at this point in the history
  • Loading branch information
dmccoystephenson committed Oct 6, 2021
1 parent dc5a34b commit 4594607
Show file tree
Hide file tree
Showing 3 changed files with 106 additions and 12 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package dansplugins.activitytracker;

import dansplugins.activitytracker.managers.ConfigManager;
import dansplugins.activitytracker.managers.StorageManager;
import org.bukkit.command.Command;
import org.bukkit.command.CommandSender;
Expand Down Expand Up @@ -37,15 +38,11 @@ public String getVersion() {
}

public boolean isDebugEnabled() {
// TODO: implement
return true;
//return ConfigManager.getInstance().getBoolean("debugMode");
return ConfigManager.getInstance().getBoolean("debugMode");
}

private boolean isVersionMismatched() {
// TODO: implement
return false;
// return !getConfig().getString("version").equalsIgnoreCase(getVersion());
return !getConfig().getString("version").equalsIgnoreCase(getVersion());
}

}
105 changes: 99 additions & 6 deletions src/main/java/dansplugins/activitytracker/managers/ConfigManager.java
Original file line number Diff line number Diff line change
@@ -1,14 +1,107 @@
package dansplugins.activitytracker.managers;

import dansplugins.activitytracker.ActivityTracker;
import org.bukkit.ChatColor;
import org.bukkit.command.CommandSender;
import org.bukkit.configuration.file.FileConfiguration;

/*
To add a new config option, the following methods must be altered:
- saveMissingConfigDefaultsIfNotPresent
- setConfigOption()
- sendConfigList()
*/

public class ConfigManager {

private static ConfigManager instance;
private boolean altered = false;

private ConfigManager() {

}

public static ConfigManager getInstance() {
// TODO: implement
return null;
if (instance == null) {
instance = new ConfigManager();
}
return instance;
}

public void saveMissingConfigDefaultsIfNotPresent() {
// set version
if (!getConfig().isString("version")) {
getConfig().addDefault("version", ActivityTracker.getInstance().getVersion());
}
else {
getConfig().set("version", ActivityTracker.getInstance().getVersion());
}

// save config options
if (!getConfig().isSet("debugMode")) {
getConfig().set("debugMode", false);
}
getConfig().options().copyDefaults(true);
ActivityTracker.getInstance().saveConfig();
}

public void setConfigOption(String option, String value, CommandSender sender) {

if (getConfig().isSet(option)) {

if (option.equalsIgnoreCase("version")) {
sender.sendMessage(ChatColor.RED + "Cannot set version.");
return;
} else if (option.equalsIgnoreCase("")) { // no integers yet
getConfig().set(option, Integer.parseInt(value));
sender.sendMessage(ChatColor.GREEN + "Integer set.");
} else if (option.equalsIgnoreCase("debugMode")) {
getConfig().set(option, Boolean.parseBoolean(value));
sender.sendMessage(ChatColor.GREEN + "Boolean set.");
} else if (option.equalsIgnoreCase("")) { // no doubles yet
getConfig().set(option, Double.parseDouble(value));
sender.sendMessage(ChatColor.GREEN + "Double set.");
} else {
getConfig().set(option, value);
sender.sendMessage(ChatColor.GREEN + "String set.");
}

// save
ActivityTracker.getInstance().saveConfig();
altered = true;
} else {
sender.sendMessage(ChatColor.RED + "That config option wasn't found.");
}
}

public boolean getBoolean(String debugMode) {
// TODO: implement
return true;
public void sendConfigList(CommandSender sender) {
sender.sendMessage(ChatColor.AQUA + "=== Config List ===");
sender.sendMessage(ChatColor.AQUA + "version: " + getConfig().getString("version")
+ ", debugMode: " + getString("debugMode"));
}
}

public boolean hasBeenAltered() {
return altered;
}

public FileConfiguration getConfig() {
return ActivityTracker.getInstance().getConfig();
}

public int getInt(String option) {
return getConfig().getInt(option);
}

public boolean getBoolean(String option) {
return getConfig().getBoolean(option);
}

public double getDouble(String option) {
return getConfig().getDouble(option);
}

public String getString(String option) {
return getConfig().getString(option);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import com.google.gson.GsonBuilder;
import com.google.gson.reflect.TypeToken;
import com.google.gson.stream.JsonReader;
import dansplugins.activitytracker.ActivityTracker;
import dansplugins.activitytracker.data.PersistentData;
import dansplugins.activitytracker.objects.ActivityRecord;
import dansplugins.activitytracker.objects.ISession;
Expand Down Expand Up @@ -40,6 +41,9 @@ public static StorageManager getInstance() {
public void save() {
saveActivityRecords();
saveSessions();
if (ConfigManager.getInstance().hasBeenAltered()) {
ActivityTracker.getInstance().saveConfig();
}
}

private void saveActivityRecords() {
Expand Down

0 comments on commit 4594607

Please sign in to comment.