1 |
package dk.thoerup.bukkit.hoeruputils; |
2 |
|
3 |
import java.io.File; |
4 |
import java.io.IOException; |
5 |
|
6 |
import org.bukkit.configuration.Configuration; |
7 |
import org.bukkit.configuration.file.FileConfiguration; |
8 |
import org.bukkit.configuration.file.YamlConfiguration; |
9 |
import org.bukkit.event.EventHandler; |
10 |
import org.bukkit.event.Listener; |
11 |
import org.bukkit.event.player.PlayerLoginEvent; |
12 |
import org.bukkit.event.player.PlayerQuitEvent; |
13 |
|
14 |
|
15 |
public class LogintimeHandler implements Listener { |
16 |
|
17 |
static final String FILENAME = "logintime.yml"; |
18 |
|
19 |
FileConfiguration config = new YamlConfiguration(); |
20 |
|
21 |
File pluginFolder; |
22 |
|
23 |
public LogintimeHandler( File pluginFolder) { |
24 |
this.pluginFolder = pluginFolder; |
25 |
|
26 |
loadTimes(); |
27 |
} |
28 |
|
29 |
Configuration getConfig() { |
30 |
return config; |
31 |
} |
32 |
|
33 |
@EventHandler |
34 |
public void onPlayerLogin(PlayerLoginEvent event) { |
35 |
config.set( event.getPlayer().getName() + ".logintime", System.currentTimeMillis() ); |
36 |
saveTimes(); |
37 |
} |
38 |
|
39 |
@EventHandler |
40 |
public void onPlayerQuit(PlayerQuitEvent event) { |
41 |
config.set( event.getPlayer().getName() + ".logintime", null); |
42 |
saveTimes(); |
43 |
} |
44 |
|
45 |
protected void loadTimes() { |
46 |
File file = new File(pluginFolder, FILENAME); |
47 |
if (file.exists() ) { |
48 |
try { |
49 |
config.load(file); |
50 |
} catch (Exception e) { |
51 |
System.out.println("[HoerupUtils] Could not load logintime.yml " + e.getMessage() ); |
52 |
} |
53 |
} |
54 |
} |
55 |
|
56 |
protected void saveTimes() { |
57 |
File file = new File(pluginFolder, FILENAME); |
58 |
try { |
59 |
config.save(file); |
60 |
} catch (IOException e) { |
61 |
System.out.println("[HoerupUtils] Could not save logintime.yml " + e.getMessage() ); |
62 |
} |
63 |
} |
64 |
|
65 |
} |