-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathConfigService.java
More file actions
144 lines (125 loc) · 6.06 KB
/
ConfigService.java
File metadata and controls
144 lines (125 loc) · 6.06 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
package dansplugins.activitytracker.services;
import org.bukkit.ChatColor;
import org.bukkit.command.CommandSender;
import org.bukkit.configuration.file.FileConfiguration;
import dansplugins.activitytracker.ActivityTracker;
/*
To add a new config option, the following methods must be altered:
- saveMissingConfigDefaultsIfNotPresent
- setConfigOption()
- sendConfigList()
*/
/**
* @author Daniel McCoy Stephenson
*/
public class ConfigService {
private final ActivityTracker activityTracker;
private boolean altered = false;
public ConfigService(ActivityTracker activityTracker) {
this.activityTracker = activityTracker;
}
public void saveMissingConfigDefaultsIfNotPresent() {
// set version
if (!getConfig().isString("version")) {
getConfig().addDefault("version", activityTracker.getVersion());
}
else {
getConfig().set("version", activityTracker.getVersion());
}
// save config options
if (!getConfig().isSet("debugMode")) {
getConfig().set("debugMode", false);
}
if (!getConfig().isSet("restApiEnabled")) {
getConfig().set("restApiEnabled", false);
}
if (!getConfig().isSet("restApiPort")) {
getConfig().set("restApiPort", 8080);
}
if (!getConfig().isSet("discordWebhookEnabled")) {
getConfig().set("discordWebhookEnabled", false);
}
if (!getConfig().isSet("discordWebhookUrl")) {
getConfig().set("discordWebhookUrl", "");
}
if (!getConfig().isSet("discordWebhookStaffOnly")) {
getConfig().set("discordWebhookStaffOnly", false);
}
if (!getConfig().isSet("discordWebhookJoinMessage")) {
getConfig().set("discordWebhookJoinMessage", "\u2694\uFE0F **{player}** has joined the server!");
}
if (!getConfig().isSet("discordWebhookQuitMessage")) {
getConfig().set("discordWebhookQuitMessage", "\uD83D\uDC4B **{player}** has left the server.");
}
getConfig().options().copyDefaults(true);
activityTracker.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("restApiPort")) {
getConfig().set(option, Integer.parseInt(value));
sender.sendMessage(ChatColor.GREEN + "Integer set.");
} else if (option.equalsIgnoreCase("debugMode") || option.equalsIgnoreCase("restApiEnabled")
|| option.equalsIgnoreCase("discordWebhookEnabled") || option.equalsIgnoreCase("discordWebhookStaffOnly")) {
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.saveConfig();
altered = true;
} else {
sender.sendMessage(ChatColor.RED + "That config option wasn't found.");
}
}
public void sendConfigList(CommandSender sender) {
sender.sendMessage("");
sender.sendMessage(ChatColor.GOLD + "┌─ " + ChatColor.YELLOW + "" + ChatColor.BOLD + "Activity Tracker" +
ChatColor.RESET + ChatColor.GOLD + " ─ Config");
sender.sendMessage(ChatColor.GOLD + "│ " + ChatColor.GRAY + "version: " +
ChatColor.WHITE + getConfig().getString("version"));
sender.sendMessage(ChatColor.GOLD + "│ " + ChatColor.GRAY + "debugMode: " +
ChatColor.WHITE + getString("debugMode"));
sender.sendMessage(ChatColor.GOLD + "│ " + ChatColor.GRAY + "restApiEnabled: " +
ChatColor.WHITE + getString("restApiEnabled"));
sender.sendMessage(ChatColor.GOLD + "│ " + ChatColor.GRAY + "restApiPort: " +
ChatColor.WHITE + getString("restApiPort"));
sender.sendMessage(ChatColor.GOLD + "│ " + ChatColor.GRAY + "discordWebhookEnabled: " +
ChatColor.WHITE + getString("discordWebhookEnabled"));
sender.sendMessage(ChatColor.GOLD + "│ " + ChatColor.GRAY + "discordWebhookUrl: " +
ChatColor.WHITE + getString("discordWebhookUrl"));
sender.sendMessage(ChatColor.GOLD + "│ " + ChatColor.GRAY + "discordWebhookStaffOnly: " +
ChatColor.WHITE + getString("discordWebhookStaffOnly"));
sender.sendMessage(ChatColor.GOLD + "│ " + ChatColor.GRAY + "discordWebhookJoinMessage:" +
ChatColor.WHITE + " " + getString("discordWebhookJoinMessage"));
sender.sendMessage(ChatColor.GOLD + "│ " + ChatColor.GRAY + "discordWebhookQuitMessage:" +
ChatColor.WHITE + " " + getString("discordWebhookQuitMessage"));
sender.sendMessage(ChatColor.GOLD + "└─────────────────────────");
}
public boolean hasBeenAltered() {
return altered;
}
public FileConfiguration getConfig() {
return activityTracker.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);
}
}