This commit is contained in:
sky 2020-05-06 02:07:05 +08:00
parent c0b983ca86
commit 12e8d3593d
3 changed files with 34 additions and 21 deletions

View File

@ -4,6 +4,7 @@ import com.google.common.collect.Lists;
import io.izzel.taboolib.TabooLib; import io.izzel.taboolib.TabooLib;
import io.izzel.taboolib.TabooLibAPI; import io.izzel.taboolib.TabooLibAPI;
import io.izzel.taboolib.common.loader.Startup; import io.izzel.taboolib.common.loader.Startup;
import io.izzel.taboolib.common.loader.StartupLoader;
import io.izzel.taboolib.module.ai.SimpleAiSelector; import io.izzel.taboolib.module.ai.SimpleAiSelector;
import io.izzel.taboolib.module.command.lite.CommandBuilder; import io.izzel.taboolib.module.command.lite.CommandBuilder;
import io.izzel.taboolib.module.db.local.Local; import io.izzel.taboolib.module.db.local.Local;
@ -22,6 +23,7 @@ import io.izzel.taboolib.util.item.Items;
import io.izzel.taboolib.util.lite.Signs; import io.izzel.taboolib.util.lite.Signs;
import org.bukkit.Bukkit; import org.bukkit.Bukkit;
import org.bukkit.Location; import org.bukkit.Location;
import org.bukkit.configuration.file.FileConfiguration;
import org.bukkit.entity.Player; import org.bukkit.entity.Player;
import org.bukkit.entity.Skeleton; import org.bukkit.entity.Skeleton;
import org.bukkit.event.EventHandler; import org.bukkit.event.EventHandler;
@ -41,6 +43,10 @@ import java.util.stream.Collectors;
@TListener @TListener
public class ListenerCommand implements Listener { public class ListenerCommand implements Listener {
static {
StartupLoader.register(ListenerCommand.class);
}
abstract static class Module { abstract static class Module {
abstract public String[] name(); abstract public String[] name();
@ -175,6 +181,12 @@ public class ListenerCommand implements Listener {
@Override @Override
public void run(Player player) { public void run(Player player) {
TellrawJson.create().append("§8[§fTabooLib§8] §7LocalPlayer: ").append("§c[...]").hoverText(LocalPlayer.get(player).saveToString()).send(player); TellrawJson.create().append("§8[§fTabooLib§8] §7LocalPlayer: ").append("§c[...]").hoverText(LocalPlayer.get(player).saveToString()).send(player);
long time = System.currentTimeMillis();
FileConfiguration conf = LocalPlayer.get0(player);
player.sendMessage("§8[§fTabooLib§8] §7get: " + (System.currentTimeMillis() - time) + "ms");
time = System.currentTimeMillis();
LocalPlayer.set0(player, conf);
player.sendMessage("§8[§fTabooLib§8] §7set: " + (System.currentTimeMillis() - time) + "ms");
} }
}); });

View File

@ -2,7 +2,7 @@ package io.izzel.taboolib.common.loader;
import com.google.common.collect.Lists; import com.google.common.collect.Lists;
import io.izzel.taboolib.TabooLib; import io.izzel.taboolib.TabooLib;
import io.izzel.taboolib.common.listener.ListenerCommand; import io.izzel.taboolib.module.db.local.LocalPlayer;
import io.izzel.taboolib.module.inject.TInjectHelper; import io.izzel.taboolib.module.inject.TInjectHelper;
import java.lang.annotation.Annotation; import java.lang.annotation.Annotation;
@ -19,7 +19,7 @@ public class StartupLoader {
static List<Class<?>> classList = Lists.newArrayList(); static List<Class<?>> classList = Lists.newArrayList();
static { static {
StartupLoader.register(ListenerCommand.class); StartupLoader.register(LocalPlayer.class);
} }
public static void register(Class<?> clazz) { public static void register(Class<?> clazz) {

View File

@ -3,7 +3,6 @@ package io.izzel.taboolib.module.db.local;
import com.google.common.collect.Maps; import com.google.common.collect.Maps;
import io.izzel.taboolib.TabooLib; import io.izzel.taboolib.TabooLib;
import io.izzel.taboolib.common.loader.Startup; import io.izzel.taboolib.common.loader.Startup;
import io.izzel.taboolib.common.loader.StartupLoader;
import io.izzel.taboolib.module.db.IHost; import io.izzel.taboolib.module.db.IHost;
import io.izzel.taboolib.module.db.source.DBSource; import io.izzel.taboolib.module.db.source.DBSource;
import io.izzel.taboolib.module.db.sql.SQLTable; import io.izzel.taboolib.module.db.sql.SQLTable;
@ -21,6 +20,8 @@ import java.nio.charset.StandardCharsets;
import java.util.Base64; import java.util.Base64;
import java.util.Map; import java.util.Map;
import java.util.UUID; import java.util.UUID;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
/** /**
* @Author 坏黑 * @Author 坏黑
@ -28,32 +29,25 @@ import java.util.UUID;
*/ */
public class LocalPlayer { public class LocalPlayer {
static {
StartupLoader.register(LocalPlayer.class);
}
private static final Map<String, FileConfiguration> files = Maps.newConcurrentMap(); private static final Map<String, FileConfiguration> files = Maps.newConcurrentMap();
private static final ExecutorService executor = Executors.newSingleThreadExecutor();
private static IHost host; private static IHost host;
private static SQLTable table; private static SQLTable table;
private static DataSource dataSource; private static DataSource dataSource;
@Startup.Starting @Startup.Starting
public static void init() { public static void init() {
host = new SQLiteHost(new File(LocalPlayer.getFolder(), "v2/data.db"), TabooLib.getPlugin()); host = new SQLiteHost(Files.file(LocalPlayer.getFolder(), "v2/data.db"), TabooLib.getPlugin());
table = new SQLTable("player_data"); table = new SQLTable("player_data");
try { try {
dataSource = DBSource.create(host); dataSource = DBSource.create(host);
table.executeUpdate("create table if not exists player_data (id integer not null primary key autoincrement, name text not null primary key, data text)").dataSource(dataSource).run(); table.executeUpdate("create table if not exists player_data (name text primary key, data text)").dataSource(dataSource).run();
} catch (Throwable t) { } catch (Throwable t) {
t.printStackTrace(); t.printStackTrace();
} }
} }
public static synchronized boolean find0(OfflinePlayer player) { public static FileConfiguration get0(OfflinePlayer player) {
return table.select(Where.equals("name", LocalPlayer.toName(player))).find(dataSource);
}
public static synchronized FileConfiguration get0(OfflinePlayer player) {
File file = toFile(toName(player)); File file = toFile(toName(player));
if (file.exists()) { if (file.exists()) {
try { try {
@ -63,15 +57,22 @@ public class LocalPlayer {
Files.deepDelete(file); Files.deepDelete(file);
} }
} }
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); try {
return executor.submit(() -> 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)).get();
} catch (Throwable t) {
t.printStackTrace();
}
return new SecuredFile();
} }
public static synchronized void set0(OfflinePlayer player, FileConfiguration data) { public static void set0(OfflinePlayer player, FileConfiguration data) {
if (find0(player)) { executor.submit(() -> {
table.update(Where.equals("name", LocalPlayer.toName(player))).set("data", Base64.getEncoder().encodeToString(data.saveToString().getBytes(StandardCharsets.UTF_8))).run(dataSource); if (table.select(Where.equals("name", LocalPlayer.toName(player))).find(dataSource)) {
} else { table.update(Where.equals("name", LocalPlayer.toName(player))).set("data", Base64.getEncoder().encodeToString(data.saveToString().getBytes(StandardCharsets.UTF_8))).run(dataSource);
table.insert(null, LocalPlayer.toName(player), 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);
}
});
} }
public static FileConfiguration get(OfflinePlayer player) { public static FileConfiguration get(OfflinePlayer player) {