66 */
77package 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 ;
916import com .google .gson .Gson ;
17+ import com .google .gson .GsonBuilder ;
1018import com .google .gson .reflect .TypeToken ;
1119import org .bukkit .ChatColor ;
1220import org .bukkit .plugin .PluginDescriptionFile ;
2432import java .io .FileReader ;
2533import java .io .FileWriter ;
2634import java .io .IOException ;
35+ import java .time .Instant ;
2736import java .util .HashMap ;
2837import java .util .Map ;
2938import java .util .UUID ;
39+ import java .util .logging .Level ;
3040
3141/**
3242 * PlayTimeLimiter plugin for Bukkit
3545 * @author Jamie Mansfield <https://github.com/lexware>
3646 */
3747public 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