Skip to content

Commit fcace7c

Browse files
committed
Cleanup configuration somewhat
1 parent f16b098 commit fcace7c

File tree

2 files changed

+170
-63
lines changed

2 files changed

+170
-63
lines changed
Lines changed: 117 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,117 @@
1+
/**
2+
* Copyright 2013-2014 by UnoModding, ATLauncher and Contributors
3+
*
4+
* This work is licensed under the Creative Commons Attribution-ShareAlike 3.0 Unported License.
5+
* To view a copy of this license, visit http://creativecommons.org/licenses/by-sa/3.0/.
6+
*/
7+
package unomodding.bukkit.playtimelimiter;
8+
9+
public class Configuration {
10+
11+
private final PlayTimeLimiter plugin;
12+
13+
public Configuration(final PlayTimeLimiter plugin) {
14+
this.plugin = plugin;
15+
}
16+
17+
/**
18+
* Ensures that the plugin configuration has been populated
19+
* with default values.
20+
*/
21+
public void ensureDefaults() {
22+
if (!this.plugin.getConfig().isSet(Options.INITIAL_TIME)) {
23+
this.plugin.getConfig().set(Options.INITIAL_TIME, 28800);
24+
this.plugin.saveConfig();
25+
}
26+
27+
if (!this.plugin.getConfig().isSet(Options.TIME_PER_DAY)) {
28+
this.plugin.getConfig().set(Options.TIME_PER_DAY, 3600);
29+
this.plugin.saveConfig();
30+
}
31+
32+
if (!this.plugin.getConfig().isSet(Options.SECONDS_BETWEEN_CHECKS)) {
33+
this.plugin.getConfig().set(Options.SECONDS_BETWEEN_CHECKS, 10);
34+
this.plugin.saveConfig();
35+
}
36+
37+
if (!this.plugin.getConfig().isSet(Options.SECONDS_BETWEEN_SAVES)) {
38+
this.plugin.getConfig().set(Options.SECONDS_BETWEEN_SAVES, 600);
39+
this.plugin.saveConfig();
40+
}
41+
42+
if (!this.plugin.getConfig().isSet(Options.TIME_TRAVELS)) {
43+
this.plugin.getConfig().set(Options.TIME_TRAVELS, false);
44+
this.plugin.saveConfig();
45+
}
46+
47+
/*if (!getConfig().isSet("timeCap")) {
48+
getConfig().set("timeCap", true);
49+
saveConfig();
50+
}
51+
52+
if (!getConfig().isSet("timeCapValue")) {
53+
getConfig().set("timeCapValue", 18000);
54+
saveConfig();
55+
}*/
56+
}
57+
58+
/**
59+
* Gets the time playtime was started on the server.
60+
*
61+
* @return The time playtime was started
62+
*/
63+
public int getTimeStarted() {
64+
return this.plugin.getConfig().getInt(Options.TIME_STARTED);
65+
}
66+
67+
/**
68+
* A psuedo-enum of all the configuration options in PlayTimeLimiter.
69+
*/
70+
public static final class Options {
71+
72+
/**
73+
* Time playtime was enabled (in seconds past Epoch).
74+
*/
75+
public static final String TIME_STARTED = "timeStarted";
76+
77+
/**
78+
* Initial playtime given (in seconds).
79+
*
80+
* <strong>Default</strong>: <em>28800</em>
81+
*/
82+
public static final String INITIAL_TIME = "initialTime";
83+
84+
/**
85+
* Daily playtime increment/reset (in seconds).
86+
*
87+
* <strong>Default</strong>: <em>3600</em>
88+
*/
89+
public static final String TIME_PER_DAY = "timePerDay";
90+
91+
/**
92+
* Time between each playtime check (in seconds).
93+
*
94+
* <strong>Default</strong>: <em>10</em>
95+
*/
96+
public static final String SECONDS_BETWEEN_CHECKS = "secondsBetweenPlayTimeChecks";
97+
98+
/**
99+
* Time between each playtime save (in seconds).
100+
*
101+
* <strong>Default</strong>: <em>600</em>
102+
*/
103+
public static final String SECONDS_BETWEEN_SAVES = "secondsBetweenPlayTimeSaving";
104+
105+
/**
106+
* Whether playtime should increment, rather than reset.
107+
*
108+
* <strong>Default</strong>: <em>false</em>
109+
*/
110+
public static final String TIME_TRAVELS = "timeTravels";
111+
112+
private Options() {
113+
}
114+
115+
}
116+
117+
}

src/main/java/unomodding/bukkit/playtimelimiter/PlayTimeLimiter.java

Lines changed: 53 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,15 @@
66
*/
77
package unomodding.bukkit.playtimelimiter;
88

9+
import static unomodding.bukkit.playtimelimiter.Configuration.Options.INITIAL_TIME;
10+
import static unomodding.bukkit.playtimelimiter.Configuration.Options.SECONDS_BETWEEN_CHECKS;
11+
import static unomodding.bukkit.playtimelimiter.Configuration.Options.SECONDS_BETWEEN_SAVES;
12+
import static unomodding.bukkit.playtimelimiter.Configuration.Options.TIME_PER_DAY;
13+
import static unomodding.bukkit.playtimelimiter.Configuration.Options.TIME_TRAVELS;
14+
15+
import com.google.common.primitives.Ints;
916
import com.google.gson.Gson;
17+
import com.google.gson.GsonBuilder;
1018
import com.google.gson.reflect.TypeToken;
1119
import org.bukkit.ChatColor;
1220
import org.bukkit.plugin.PluginDescriptionFile;
@@ -24,9 +32,11 @@
2432
import java.io.FileReader;
2533
import java.io.FileWriter;
2634
import java.io.IOException;
35+
import java.time.Instant;
2736
import java.util.HashMap;
2837
import java.util.Map;
2938
import java.util.UUID;
39+
import java.util.logging.Level;
3040

3141
/**
3242
* PlayTimeLimiter plugin for Bukkit
@@ -35,14 +45,19 @@
3545
* @author Jamie Mansfield <https://github.com/lexware>
3646
*/
3747
public class PlayTimeLimiter extends JavaPlugin {
48+
49+
private static final Gson GSON = new GsonBuilder()
50+
.setPrettyPrinting()
51+
.create();
52+
3853
private final PlayTimeListener playerListener = new PlayTimeListener(this);
39-
private Map<String, Integer> timePlayed = new HashMap<String, Integer>();
40-
private Map<String, Integer> timeLoggedIn = new HashMap<String, Integer>();
41-
private Map<String, Boolean> seenWarningMessages = new HashMap<String, Boolean>();
54+
private final Configuration configuration = new Configuration(this);
55+
private Map<String, Integer> timePlayed = new HashMap<>();
56+
private Map<String, Integer> timeLoggedIn = new HashMap<>();
57+
private Map<String, Boolean> seenWarningMessages = new HashMap<>();
4258

4359
private boolean shutdownHookAdded = false;
4460
private boolean started = false;
45-
private final Gson GSON = new Gson();
4661

4762
@Override
4863
public void onDisable() {
@@ -72,71 +87,46 @@ public void onEnable() {
7287
getCommand("playtime").setExecutor(playTimeCommand);
7388
getCommand("playtime").setTabCompleter(playTimeCommand);
7489

75-
if (getConfig().isSet("timeStarted")) {
76-
this.started = true;
77-
}
78-
if (!getConfig().isSet("initialTime")) {
79-
getConfig().set("initialTime", 28800);
80-
saveConfig();
81-
}
82-
if (!getConfig().isSet("timePerDay")) {
83-
getConfig().set("timePerDay", 3600);
84-
saveConfig();
85-
}
86-
if (!getConfig().isSet("secondsBetweenPlayTimeChecks")) {
87-
getConfig().set("secondsBetweenPlayTimeChecks", 10);
88-
saveConfig();
89-
}
90-
if (!getConfig().isSet("secondsBetweenPlayTimeSaving")) {
91-
getConfig().set("secondsBetweenPlayTimeSaving", 600);
92-
saveConfig();
93-
}
94-
if (!getConfig().isSet("timeTravels")) {
95-
getConfig().set("timeTravels", true);
96-
saveConfig();
97-
}
98-
/*if (!getConfig().isSet("timeCap")) {
99-
getConfig().set("timeCap", true);
100-
saveConfig();
101-
}
102-
if (!getConfig().isSet("timeCapValue")) {
103-
getConfig().set("timeCapValue", 18000);
104-
saveConfig();
105-
}*/
90+
// Config
91+
this.started = this.getConfig().isSet(Configuration.Options.TIME_STARTED);
92+
this.configuration.ensureDefaults();
10693

107-
getLogger().info(
108-
String.format("Server started at %s which was %s seconds ago!",
109-
getConfig().get("timeStarted"),
110-
this.secondsToDaysHoursSecondsString((int) ((System
111-
.currentTimeMillis() / 1000) - getConfig()
112-
.getInt("timeStarted")))));
94+
// The server started log message
95+
this.getLogger().info(
96+
String.format("Server started at %s which was %s ago!",
97+
this.configuration.getTimeStarted(),
98+
this.secondsToDaysHoursSecondsString(
99+
Ints.checkedCast(Instant.now().getEpochSecond()) - this.configuration.getTimeStarted()
100+
)
101+
)
102+
);
113103

114-
PluginDescriptionFile pdfFile = this.getDescription();
115-
getLogger().info(
116-
pdfFile.getName() + " version " + pdfFile.getVersion()
117-
+ " is enabled!");
104+
// PlayTimeLimiter v{} is enabled!
105+
final PluginDescriptionFile descriptor = this.getDescription();
106+
getLogger().info("PlayTimeLimiter v" + descriptor.getVersion() + " is enabled!");
118107

119108
// Load the playtime from file
120109
this.loadPlayTime();
121110

111+
// Tasks
122112
this.getServer().getScheduler().scheduleSyncRepeatingTask(this,
123113
new PlayTimeSaverTask(this), 30000,
124-
getConfig().getInt("secondsBetweenPlayTimeSaving") * 1000);
114+
getConfig().getInt(SECONDS_BETWEEN_SAVES) * 1000);
125115
this.getServer().getScheduler().scheduleSyncRepeatingTask(this,
126116
new PlayTimeCheckerTask(this), 30000,
127-
getConfig().getInt("secondsBetweenPlayTimeChecks") * 1000);
128-
117+
getConfig().getInt(SECONDS_BETWEEN_CHECKS) * 1000);
129118

119+
// Metrics
130120
try {
131-
MetricsLite metrics = new MetricsLite(this);
121+
final MetricsLite metrics = new MetricsLite(this);
132122
metrics.start();
133-
} catch (IOException e) {
134-
getLogger().info("Couldn't send Metrics data.");
123+
} catch (final IOException ex) {
124+
this.getLogger().log(Level.INFO, "Failed to send Metrics data!", ex);
135125
}
136126
}
137127

138128
public int secondsUntilNextDay() {
139-
int timeStarted = getConfig().getInt("timeStarted");
129+
final int timeStarted = this.configuration.getTimeStarted();
140130
int secondsSince = (int) ((System.currentTimeMillis() / 1000) - timeStarted);
141131

142132
while (secondsSince >= 86400) {
@@ -147,29 +137,29 @@ public int secondsUntilNextDay() {
147137
}
148138

149139
public String secondsToDaysHoursSecondsString(int secondsToConvert) {
150-
int hours = secondsToConvert / 3600;
151-
int minutes = (secondsToConvert % 3600) / 60;
152-
int seconds = secondsToConvert % 60;
140+
final int hours = secondsToConvert / 3600;
141+
final int minutes = (secondsToConvert % 3600) / 60;
142+
final int seconds = secondsToConvert % 60;
153143
return String.format("%02d hours, %02d minutes & %02d seconds", hours,
154144
minutes, seconds);
155145
}
156146

157147
public int getTimeAllowedInSeconds() {
158-
int timeStarted = getConfig().getInt("timeStarted");
148+
int timeStarted = this.configuration.getTimeStarted();
159149
int secondsSince = (int) ((System.currentTimeMillis() / 1000) - timeStarted);
160150
int secondsAllowed = 0;
161151

162152
// Add the initial time we give the player at the beginning
163-
secondsAllowed += getConfig().getInt("initialTime");
153+
secondsAllowed += getConfig().getInt(INITIAL_TIME);
164154

165155
// Then for each day including the first day (24 hours realtime) add the
166156
// set amount of
167157
// seconds to the time allowed
168158
while (secondsSince >= 0) {
169-
if (getConfig().getBoolean("timeTravels")) {
170-
secondsAllowed += getConfig().getInt("timePerDay");
159+
if (getConfig().getBoolean(TIME_TRAVELS)) {
160+
secondsAllowed += getConfig().getInt(TIME_PER_DAY);
171161
} else {
172-
secondsAllowed = getConfig().getInt("timePerDay");
162+
secondsAllowed = getConfig().getInt(TIME_PER_DAY);
173163
}
174164
secondsSince -= 86400;
175165
}
@@ -285,14 +275,14 @@ public boolean start() {
285275
return false;
286276
} else {
287277
this.started = true;
288-
String initial = (getConfig().getInt("initialTime") / 60 / 60) + "";
289-
String perday = (getConfig().getInt("timePerDay") / 60 / 60) + "";
278+
String initial = (getConfig().getInt(INITIAL_TIME) / 60 / 60) + "";
279+
String perday = (getConfig().getInt(TIME_PER_DAY) / 60 / 60) + "";
290280
getServer().broadcastMessage(
291281
ChatColor.GREEN + "Playtime has now started! You have "
292282
+ initial
293283
+ " hour/s of playtime to start with and " + perday
294284
+ " hour/s of playtime added per day!");
295-
getConfig().set("timeStarted", (System.currentTimeMillis() / 1000));
285+
getConfig().set("timeStarted", Ints.checkedCast(Instant.now().getEpochSecond()));
296286
saveConfig();
297287
return true;
298288
}

0 commit comments

Comments
 (0)