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 {
private static Map<String, FileConfiguration> files = Maps.newConcurrentMap();
private static final Map<String, FileConfiguration> files = Maps.newConcurrentMap();
public static FileConfiguration get(OfflinePlayer player) {
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"));
}
private static File toFile(String name) {
public static File toFile(String name) {
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();
}
private static boolean isUniqueIdMode() {
public static boolean isUniqueIdMode() {
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);
}
}

View File

@ -43,7 +43,9 @@ public class SecuredFile extends YamlConfiguration {
try {
loadFromString(content);
} catch (InvalidConfigurationException t) {
Files.copy(file, new File(file.getParent(), file.getName() + "_" + new SimpleDateFormat("yyyyMMddHHmmss").format(System.currentTimeMillis()) + ".bak"));
if (!file.getName().endsWith(".bak")) {
Files.copy(file, new File(file.getParent(), file.getName() + "_" + new SimpleDateFormat("yyyyMMddHHmmss").format(System.currentTimeMillis()) + ".bak"));
}
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.util.Arrays;
import java.util.Map;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
/**
* @Author sky
@ -38,26 +40,30 @@ public class I18n20w14a extends I18nBase {
}
};
private File folder = new File(TabooLib.getPlugin().getDataFolder(), "simpleI18n/v2/20w14a");
private Map<String, JsonObject> cache = Maps.newHashMap();
private final File folder = new File(TabooLib.getPlugin().getDataFolder(), "simpleI18n/v2/20w14a");
private final Map<String, JsonObject> cache = Maps.newHashMap();
private final ExecutorService executor = Executors.newSingleThreadExecutor();
@Override
public void init() {
if (folder.exists() && folder.isDirectory()) {
load();
} else {
long time = System.currentTimeMillis();
System.out.println("[TabooLib] Loading Assets...");
try {
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]));
}
executor.submit(() -> {
if (folder.exists() && folder.isDirectory()) {
load();
System.out.println("[TabooLib] Loading Successfully. (" + (System.currentTimeMillis() - time + "ms)"));
} catch (Throwable ignored) {
System.out.println("[TabooLib] Loading Failed. (" + (System.currentTimeMillis() - time + "ms)"));
} else {
System.out.println("[TabooLib] Loading Assets...");
long time = System.currentTimeMillis();
try {
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]));
}
load();
System.out.println("[TabooLib] Loading Successfully. (" + (System.currentTimeMillis() - time + "ms)"));
} catch (Throwable ignored) {
System.out.println("[TabooLib] Loading Failed. (" + (System.currentTimeMillis() - time + "ms)"));
}
}
}
});
}
@Override

View File

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