This commit is contained in:
sky 2020-05-01 11:56:00 +08:00
parent a99ca8c957
commit 133eb4e31e
5 changed files with 96 additions and 27 deletions

View File

@ -20,7 +20,7 @@ import java.util.UUID;
*/ */
public class LocalPlayer { public class LocalPlayer {
private static Map<String, FileConfiguration> files = Maps.newConcurrentMap(); private static final Map<String, FileConfiguration> files = Maps.newConcurrentMap();
public static FileConfiguration get(OfflinePlayer player) { public static FileConfiguration get(OfflinePlayer player) {
return TabooLibAPI.isOriginLoaded() ? TabooLibAPI.getPluginBridge().taboolibGetPlayerData(toName(player)) : files.computeIfAbsent(toName(player), n -> Files.load(toFile(n))); return TabooLibAPI.isOriginLoaded() ? TabooLibAPI.getPluginBridge().taboolibGetPlayerData(toName(player)) : files.computeIfAbsent(toName(player), n -> Files.load(toFile(n)));
@ -56,19 +56,19 @@ public class LocalPlayer {
return Files.folder(TabooLib.getConfig().getString("LOCAL-PLAYER")); return Files.folder(TabooLib.getConfig().getString("LOCAL-PLAYER"));
} }
private static File toFile(String name) { public static File toFile(String name) {
return Files.file(getFolder(), name + ".yml"); return Files.file(getFolder(), name + ".yml");
} }
private static String toName(OfflinePlayer player) { public static String toName(OfflinePlayer player) {
return isUniqueIdMode() ? player.getUniqueId().toString() : player.getName(); return isUniqueIdMode() ? player.getUniqueId().toString() : player.getName();
} }
private static boolean isUniqueIdMode() { public static boolean isUniqueIdMode() {
return TabooLib.getConfig().getBoolean("LOCAL-PLAYER-UUID"); return TabooLib.getConfig().getBoolean("LOCAL-PLAYER-UUID");
} }
private static Player toPlayer(String name) { public static Player toPlayer(String name) {
return isUniqueIdMode() ? Bukkit.getPlayer(UUID.fromString(name)) : Bukkit.getPlayerExact(name); return isUniqueIdMode() ? Bukkit.getPlayer(UUID.fromString(name)) : Bukkit.getPlayerExact(name);
} }
} }

View File

@ -43,7 +43,9 @@ public class SecuredFile extends YamlConfiguration {
try { try {
loadFromString(content); loadFromString(content);
} catch (InvalidConfigurationException t) { } catch (InvalidConfigurationException t) {
if (!file.getName().endsWith(".bak")) {
Files.copy(file, new File(file.getParent(), file.getName() + "_" + new SimpleDateFormat("yyyyMMddHHmmss").format(System.currentTimeMillis()) + ".bak")); Files.copy(file, new File(file.getParent(), file.getName() + "_" + new SimpleDateFormat("yyyyMMddHHmmss").format(System.currentTimeMillis()) + ".bak"));
}
throw t; throw t;
} }
} }

View File

@ -0,0 +1,64 @@
package io.izzel.taboolib.module.db.local.player;
import com.google.common.collect.Maps;
import io.izzel.taboolib.TabooLib;
import io.izzel.taboolib.module.db.IHost;
import io.izzel.taboolib.module.db.local.LocalPlayer;
import io.izzel.taboolib.module.db.local.SecuredFile;
import io.izzel.taboolib.module.db.source.DBSource;
import io.izzel.taboolib.module.db.sql.SQLTable;
import io.izzel.taboolib.module.db.sql.query.Where;
import io.izzel.taboolib.module.db.sqlite.SQLiteHost;
import io.izzel.taboolib.module.inject.TFunction;
import org.bukkit.Bukkit;
import org.bukkit.OfflinePlayer;
import org.bukkit.configuration.file.FileConfiguration;
import javax.sql.DataSource;
import java.io.File;
import java.nio.charset.StandardCharsets;
import java.util.Base64;
import java.util.Map;
/**
* @Author sky
* @Since 2020-04-29 21:46
*/
public class StoredPlayer {
private static IHost host;
private static SQLTable table;
private static DataSource dataSource;
private static final Map<String, FileConfiguration> caches = Maps.newConcurrentMap();
@TFunction.Init
public static void init() {
if (Bukkit.getPluginManager().getPlugin("TabooLib") != null) {
host = new SQLiteHost(new File(LocalPlayer.getFolder(), "data.db"), TabooLib.getPlugin());
table = new SQLTable("player_data");
try {
dataSource = DBSource.create(host);
table.executeUpdate("create table player_data (id integer not null primary key autoincrement, name text, data text)").dataSource(dataSource).run();
} catch (Throwable t) {
t.printStackTrace();
}
}
}
public static synchronized boolean find(OfflinePlayer player) {
return table.select(Where.equals("name", LocalPlayer.toName(player))).find(dataSource);
}
public static synchronized FileConfiguration get(OfflinePlayer player) {
return table.select(Where.equals("name", LocalPlayer.toName(player))).to(dataSource).resultNext(r -> SecuredFile.loadConfiguration(new String(Base64.getDecoder().decode(r.getString("data")), StandardCharsets.UTF_8))).run(new SecuredFile(), SecuredFile.class);
}
public static synchronized void set(OfflinePlayer player, FileConfiguration data) {
if (find(player)) {
table.update(Where.equals("name", LocalPlayer.toName(player))).set("data", Base64.getEncoder().encodeToString(data.saveToString().getBytes(StandardCharsets.UTF_8))).run(dataSource);
} else {
table.insert(LocalPlayer.toName(player), Base64.getEncoder().encodeToString(data.saveToString().getBytes(StandardCharsets.UTF_8))).run(dataSource);
}
}
}

View File

@ -16,6 +16,8 @@ import java.io.File;
import java.nio.charset.StandardCharsets; import java.nio.charset.StandardCharsets;
import java.util.Arrays; import java.util.Arrays;
import java.util.Map; import java.util.Map;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
/** /**
* @Author sky * @Author sky
@ -38,16 +40,19 @@ public class I18n20w14a extends I18nBase {
} }
}; };
private File folder = new File(TabooLib.getPlugin().getDataFolder(), "simpleI18n/v2/20w14a"); private final File folder = new File(TabooLib.getPlugin().getDataFolder(), "simpleI18n/v2/20w14a");
private Map<String, JsonObject> cache = Maps.newHashMap(); private final Map<String, JsonObject> cache = Maps.newHashMap();
private final ExecutorService executor = Executors.newSingleThreadExecutor();
@Override @Override
public void init() { public void init() {
executor.submit(() -> {
if (folder.exists() && folder.isDirectory()) { if (folder.exists() && folder.isDirectory()) {
load(); load();
} else { } else {
long time = System.currentTimeMillis();
System.out.println("[TabooLib] Loading Assets..."); System.out.println("[TabooLib] Loading Assets...");
long time = System.currentTimeMillis();
try { try {
for (String[] locale : LOCALE) { for (String[] locale : LOCALE) {
Files.toFile(Files.readFromURL("https://resources.download.minecraft.net/" + locale[1].substring(0, 2) + "/" + locale[1], StandardCharsets.UTF_8, "{}"), Files.file(folder, locale[0])); Files.toFile(Files.readFromURL("https://resources.download.minecraft.net/" + locale[1].substring(0, 2) + "/" + locale[1], StandardCharsets.UTF_8, "{}"), Files.file(folder, locale[0]));
@ -58,6 +63,7 @@ public class I18n20w14a extends I18nBase {
System.out.println("[TabooLib] Loading Failed. (" + (System.currentTimeMillis() - time + "ms)")); System.out.println("[TabooLib] Loading Failed. (" + (System.currentTimeMillis() - time + "ms)"));
} }
} }
});
} }
@Override @Override

View File

@ -14,6 +14,7 @@ import java.io.*;
import java.math.BigInteger; import java.math.BigInteger;
import java.net.URISyntaxException; import java.net.URISyntaxException;
import java.net.URL; import java.net.URL;
import java.net.UnknownHostException;
import java.nio.channels.FileChannel; import java.nio.channels.FileChannel;
import java.nio.charset.Charset; import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets; import java.nio.charset.StandardCharsets;
@ -244,17 +245,13 @@ public class Files {
} }
public static String readFromURL(String url) { public static String readFromURL(String url) {
try (InputStream inputStream = new URL(url).openStream(); BufferedInputStream bufferedInputStream = new BufferedInputStream(inputStream)) { return readFromURL(url, StandardCharsets.UTF_8);
return new String(IO.readFully(bufferedInputStream));
} catch (Throwable t) {
t.printStackTrace();
}
return null;
} }
public static String readFromURL(String url, Charset charset) { public static String readFromURL(String url, Charset charset) {
try (InputStream inputStream = new URL(url).openStream(); BufferedInputStream bufferedInputStream = new BufferedInputStream(inputStream)) { try (InputStream inputStream = new URL(url).openStream(); BufferedInputStream bufferedInputStream = new BufferedInputStream(inputStream)) {
return new String(IO.readFully(bufferedInputStream), charset); return new String(IO.readFully(bufferedInputStream), charset);
} catch (UnknownHostException ignored) {
} catch (Throwable t) { } catch (Throwable t) {
t.printStackTrace(); t.printStackTrace();
} }