2014-04-17 03:53:02 +00:00
|
|
|
package com.earth2me.essentials;
|
|
|
|
|
|
|
|
import com.google.common.io.Files;
|
2015-04-15 04:06:16 +00:00
|
|
|
import org.bukkit.Bukkit;
|
|
|
|
|
|
|
|
import java.io.*;
|
2014-04-18 03:48:34 +00:00
|
|
|
import java.util.ArrayList;
|
2015-11-30 19:51:22 +00:00
|
|
|
import java.util.HashMap;
|
2014-04-17 03:53:02 +00:00
|
|
|
import java.util.Map;
|
|
|
|
import java.util.UUID;
|
2015-04-15 04:06:16 +00:00
|
|
|
import java.util.concurrent.*;
|
2014-04-17 03:53:02 +00:00
|
|
|
import java.util.logging.Level;
|
|
|
|
import java.util.regex.Pattern;
|
|
|
|
|
|
|
|
|
2015-04-15 04:06:16 +00:00
|
|
|
public class UUIDMap {
|
|
|
|
private final transient net.ess3.api.IEssentials ess;
|
|
|
|
private File userList;
|
|
|
|
private final transient Pattern splitPattern = Pattern.compile(",");
|
2016-03-02 00:45:48 +00:00
|
|
|
|
|
|
|
private static boolean pendingWrite;
|
|
|
|
private static final ScheduledExecutorService writeScheduler = Executors.newScheduledThreadPool(1);
|
|
|
|
private final Runnable writeTaskRunnable;
|
2015-04-15 04:06:16 +00:00
|
|
|
|
2016-03-02 15:46:29 +00:00
|
|
|
private static boolean loading = false;
|
|
|
|
|
2015-04-15 04:06:16 +00:00
|
|
|
public UUIDMap(final net.ess3.api.IEssentials ess) {
|
|
|
|
this.ess = ess;
|
|
|
|
userList = new File(ess.getDataFolder(), "usermap.csv");
|
2016-03-02 00:45:48 +00:00
|
|
|
pendingWrite = false;
|
|
|
|
writeTaskRunnable = new Runnable() {
|
|
|
|
@Override
|
|
|
|
public void run() {
|
|
|
|
if (pendingWrite) {
|
|
|
|
try {
|
|
|
|
new WriteRunner(ess.getDataFolder(), userList, ess.getUserMap().getNames()).run();
|
|
|
|
} catch (Throwable t) { // bad code to prevent task from being suppressed
|
|
|
|
t.printStackTrace();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
writeScheduler.scheduleWithFixedDelay(writeTaskRunnable, 5, 5, TimeUnit.SECONDS);
|
2015-04-15 04:06:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public void loadAllUsers(final ConcurrentSkipListMap<String, UUID> names, final ConcurrentSkipListMap<UUID, ArrayList<String>> history) {
|
|
|
|
try {
|
|
|
|
if (!userList.exists()) {
|
|
|
|
userList.createNewFile();
|
|
|
|
}
|
|
|
|
|
2016-03-02 00:45:48 +00:00
|
|
|
if (ess.getSettings().isDebug()) {
|
|
|
|
ess.getLogger().log(Level.INFO, "Reading usermap from disk");
|
|
|
|
}
|
2015-04-15 04:06:16 +00:00
|
|
|
|
2016-03-02 00:45:48 +00:00
|
|
|
names.clear();
|
|
|
|
history.clear();
|
2016-03-02 15:46:29 +00:00
|
|
|
loading = true;
|
2016-03-02 00:45:48 +00:00
|
|
|
|
|
|
|
try (BufferedReader reader = new BufferedReader(new FileReader(userList))) {
|
|
|
|
while (true) {
|
|
|
|
final String line = reader.readLine();
|
|
|
|
if (line == null) {
|
|
|
|
break;
|
|
|
|
} else {
|
|
|
|
final String[] values = splitPattern.split(line);
|
|
|
|
if (values.length == 2) {
|
|
|
|
final String name = values[0];
|
|
|
|
final UUID uuid = UUID.fromString(values[1]);
|
|
|
|
names.put(name, uuid);
|
|
|
|
if (!history.containsKey(uuid)) {
|
|
|
|
final ArrayList<String> list = new ArrayList<>();
|
|
|
|
list.add(name);
|
|
|
|
history.put(uuid, list);
|
|
|
|
} else {
|
|
|
|
final ArrayList<String> list = history.get(uuid);
|
|
|
|
if (!list.contains(name)) {
|
2015-04-15 04:06:16 +00:00
|
|
|
list.add(name);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2016-03-02 15:46:29 +00:00
|
|
|
loading = false;
|
2015-04-15 04:06:16 +00:00
|
|
|
} catch (IOException ex) {
|
|
|
|
Bukkit.getLogger().log(Level.SEVERE, ex.getMessage(), ex);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public void writeUUIDMap() {
|
2016-03-02 00:45:48 +00:00
|
|
|
pendingWrite = true;
|
2015-04-15 04:06:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public void forceWriteUUIDMap() {
|
|
|
|
if (ess.getSettings().isDebug()) {
|
|
|
|
ess.getLogger().log(Level.INFO, "Forcing usermap write to disk");
|
|
|
|
}
|
2016-03-02 00:45:48 +00:00
|
|
|
pendingWrite = true;
|
|
|
|
writeTaskRunnable.run();
|
2015-04-15 04:06:16 +00:00
|
|
|
}
|
|
|
|
|
2016-03-02 00:45:48 +00:00
|
|
|
public void shutdown() {
|
|
|
|
writeScheduler.submit(writeTaskRunnable);
|
|
|
|
writeScheduler.shutdown();
|
2015-04-15 04:06:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
private static class WriteRunner implements Runnable {
|
|
|
|
private final File location;
|
|
|
|
private final File endFile;
|
2015-11-30 19:51:22 +00:00
|
|
|
private final Map<String, UUID> names;
|
2015-04-15 04:06:16 +00:00
|
|
|
|
2016-03-02 00:45:48 +00:00
|
|
|
private WriteRunner(final File location, final File endFile, final Map<String, UUID> names) {
|
2015-04-15 04:06:16 +00:00
|
|
|
this.location = location;
|
|
|
|
this.endFile = endFile;
|
2016-03-02 00:45:48 +00:00
|
|
|
this.names = new HashMap<>(names);
|
2015-04-15 04:06:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void run() {
|
2016-03-02 00:45:48 +00:00
|
|
|
pendingWrite = false;
|
2016-03-02 15:46:29 +00:00
|
|
|
if (loading || names.isEmpty()) {
|
2016-03-02 15:33:11 +00:00
|
|
|
return;
|
|
|
|
}
|
2016-03-02 00:45:48 +00:00
|
|
|
File configFile = null;
|
2015-04-15 04:06:16 +00:00
|
|
|
|
2016-03-02 00:45:48 +00:00
|
|
|
try {
|
|
|
|
configFile = File.createTempFile("usermap", ".tmp.csv", location);
|
2015-04-15 04:06:16 +00:00
|
|
|
|
2016-03-02 00:45:48 +00:00
|
|
|
final BufferedWriter bWriter = new BufferedWriter(new FileWriter(configFile));
|
|
|
|
for (Map.Entry<String, UUID> entry : names.entrySet()) {
|
|
|
|
bWriter.write(entry.getKey() + "," + entry.getValue().toString());
|
|
|
|
bWriter.newLine();
|
|
|
|
}
|
2015-04-15 04:06:16 +00:00
|
|
|
|
2016-03-02 00:45:48 +00:00
|
|
|
bWriter.close();
|
|
|
|
Files.move(configFile, endFile);
|
|
|
|
} catch (IOException ex) {
|
|
|
|
try {
|
|
|
|
if (configFile != null && configFile.exists()) {
|
|
|
|
Files.move(configFile, new File(endFile.getParentFile(), "usermap.bak.csv"));
|
2015-04-15 04:06:16 +00:00
|
|
|
}
|
2016-03-02 00:45:48 +00:00
|
|
|
} catch (Exception ex2) {
|
|
|
|
Bukkit.getLogger().log(Level.SEVERE, ex2.getMessage(), ex2);
|
2015-04-15 04:06:16 +00:00
|
|
|
}
|
2016-03-02 00:45:48 +00:00
|
|
|
Bukkit.getLogger().log(Level.WARNING, ex.getMessage(), ex);
|
2015-04-15 04:06:16 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2015-11-30 19:51:22 +00:00
|
|
|
}
|