-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathAutoSaveThread.java
124 lines (111 loc) · 3.47 KB
/
AutoSaveThread.java
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
/**
*
* Copyright 2011 MilkBowl (https://github.com/MilkBowl)
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*
*/
package net.milkbowl.autosave;
import java.util.Date;
import java.util.logging.Logger;
public class AutoSaveThread extends Thread {
protected final Logger log = Logger.getLogger("Minecraft");
private boolean run = true;
private AutoSave plugin = null;
private AutoSaveConfig config = null;
// Constructor to define number of seconds to sleep
AutoSaveThread(AutoSave plugin, AutoSaveConfig config) {
this.plugin = plugin;
this.config = config;
}
// Allows for the thread to naturally exit if value is false
public void setRun(boolean run) {
this.run = run;
}
// The code to run...weee
public void run() {
if (config == null) {
return;
}
log.info(String
.format("[%s] AutoSaveThread Started: Interval is %d seconds, Warn Times are %s",
plugin.getDescription().getName(), config.varInterval,
Generic.join(",", config.varWarnTimes)));
while (run) {
// Prevent AutoSave from never sleeping
// If interval is 0, sleep for 5 seconds and skip saving
if(config.varInterval == 0) {
try {
Thread.sleep(5000);
} catch(InterruptedException e) {
// care
}
continue;
}
// Do our Sleep stuff!
for (int i = 0; i < config.varInterval; i++) {
try {
if (!run) {
if (config.varDebug) {
log.info(String.format("[%s] Graceful quit of AutoSaveThread", plugin.getDescription().getName()));
}
return;
}
boolean warn = false;
for (int w : config.varWarnTimes) {
if (w != 0 && w + i == config.varInterval) {
warn = true;
}
}
if (warn) {
// Perform warning
if (config.varDebug) {
log.info(String.format("[%s] Warning Time Reached: %d seconds to go.", plugin.getDescription().getName(), config.varInterval - i));
}
plugin.getServer().broadcastMessage(Generic.parseColor(config.messageWarning));
log.info(String.format("[%s] %s", plugin.getDescription().getName(), config.messageWarning));
}
Thread.sleep(1000);
} catch (InterruptedException e) {
log.info("Could not sleep!");
}
}
switch (config.varMode) {
case ASYNCHRONOUS:
plugin.getServer().getScheduler()
.scheduleAsyncDelayedTask(plugin, new Runnable() {
public void run() {
plugin.performSave();
plugin.lastSave = new Date();
}
});
break;
case SYNCHRONOUS:
plugin.getServer().getScheduler()
.scheduleSyncDelayedTask(plugin, new Runnable() {
public void run() {
plugin.performSave();
plugin.lastSave = new Date();
}
});
break;
default:
log.warning(String.format("[%s] Invalid configuration mode!",
plugin.getDescription().getName()));
break;
}
}
}
}