diff --git a/varo/src/main/java/de/cliffbreak/varo/Varo.java b/varo/src/main/java/de/cliffbreak/varo/Varo.java index 7e6f740..b74eca3 100644 --- a/varo/src/main/java/de/cliffbreak/varo/Varo.java +++ b/varo/src/main/java/de/cliffbreak/varo/Varo.java @@ -93,7 +93,6 @@ public class Varo extends JavaPlugin { @Override public void onDisable() { this.getLogger().info("Stopping CliffbreakVaro!"); - this.playerCache.shutdown(); this.database.shutdown(); try { this.discordBot.shutdown(); diff --git a/varo/src/main/java/de/cliffbreak/varo/database/Database.java b/varo/src/main/java/de/cliffbreak/varo/database/Database.java index 37f75c9..f4d1221 100644 --- a/varo/src/main/java/de/cliffbreak/varo/database/Database.java +++ b/varo/src/main/java/de/cliffbreak/varo/database/Database.java @@ -23,6 +23,8 @@ public class Database { final Statement s = conn.createStatement(); s.executeUpdate( "CREATE TABLE IF NOT EXISTS kills (`player` VARCHAR(32) NOT NULL, `victim` VARCHAR(32) NOT NULL);"); + s.executeUpdate( + "CREATE TABLE IF NOT EXISTS playercache (`player` VARCHAR(32) NOT NULL, `name` VARCHAR(16), `skinparts` INT(3), `mainhand` VARCHAR(5), `world` TEXT, `x` DOUBLE(4,4), `y` DOUBLE(4,4), `z` DOUBLE(4,4), `pitch` FLOAT(24), `yaw` FLOAT(24));"); s.close(); } @@ -56,4 +58,32 @@ public class Database { } return 0; } + + public void updatePlayerCache(final String uuid, final String type, final Object value) { + try { + final Statement s = conn.createStatement(); + if (s.executeUpdate( + "UPDATE playercache SET `" + type + "`='" + value + "' WHERE `player`='" + uuid + "';") == 0) { + s.executeUpdate( + "INSERT INTO playercache (`player`, `" + type + "`) VALUES ('" + uuid + "','" + value + "');"); + } + s.close(); + } catch (final SQLException e) { + e.printStackTrace(); + } + } + + public Object getPlayerCache(final String uuid, final String type) { + try { + final Statement s = conn.createStatement(); + final ResultSet rs = s.executeQuery("SELECT " + type + " FROM playercache WHERE `player` ='" + uuid + "';"); + final Object obj = rs.getObject(type); + s.close(); + return obj; + } catch (final SQLException e) { + e.printStackTrace(); + } + return null; + } + } \ No newline at end of file diff --git a/varo/src/main/java/de/cliffbreak/varo/uitls/PlayerCache.java b/varo/src/main/java/de/cliffbreak/varo/uitls/PlayerCache.java index 87f65e0..f0fe1b9 100644 --- a/varo/src/main/java/de/cliffbreak/varo/uitls/PlayerCache.java +++ b/varo/src/main/java/de/cliffbreak/varo/uitls/PlayerCache.java @@ -1,44 +1,68 @@ package de.cliffbreak.varo.uitls; -import java.io.File; -import java.io.IOException; - -import org.bukkit.configuration.file.FileConfiguration; -import org.bukkit.configuration.file.YamlConfiguration; +import org.bukkit.Bukkit; +import org.bukkit.Location; +import org.bukkit.World; +import org.bukkit.World.Environment; import org.bukkit.inventory.MainHand; -import org.bukkit.scheduler.BukkitRunnable; import de.cliffbreak.varo.Varo; public class PlayerCache { private final Varo plugin; - private final File playerCacheFile; - private final FileConfiguration playerCache; - private boolean wasSaved = false; public PlayerCache(final Varo plugin) { - this.playerCacheFile = new File("plugins/CliffbreakVaro", "playercache.yml"); - this.playerCache = YamlConfiguration.loadConfiguration(this.playerCacheFile); this.plugin = plugin; } public void setPlayerCache(final String uuid, final PlayerCacheType type, final Object value) { - this.playerCache.set("Players." + uuid + "." + type.toString(), value); - savePlayerCache(); + this.plugin.getDatabase().updatePlayerCache(uuid, type.toString().toLowerCase(), value); + } + + public void setPlayerLocationCache(final String uuid, final Location loc) { + this.setPlayerCache(uuid, PlayerCacheType.WORLD, loc.getWorld().getName()); + this.setPlayerCache(uuid, PlayerCacheType.X, loc.getX()); + this.setPlayerCache(uuid, PlayerCacheType.Y, loc.getY()); + this.setPlayerCache(uuid, PlayerCacheType.Z, loc.getZ()); + this.setPlayerCache(uuid, PlayerCacheType.YAW, loc.getYaw()); + this.setPlayerCache(uuid, PlayerCacheType.PITCH, loc.getPitch()); + } + + public Location getPlayerLocationCache(final String uuid) { + if ((String) this.plugin.getDatabase().getPlayerCache(uuid, + PlayerCacheType.WORLD.toString().toLowerCase()) == null) { + for (World world : Bukkit.getWorlds()) { + if (world.getEnvironment().equals(Environment.NORMAL)) + return world.getSpawnLocation(); + } + return null; + } else { + + return new Location( + Bukkit.getWorld((String) this.plugin.getDatabase().getPlayerCache(uuid, + PlayerCacheType.WORLD.toString().toLowerCase())), + (double) this.plugin.getDatabase().getPlayerCache(uuid, PlayerCacheType.X.toString().toLowerCase()), + (double) this.plugin.getDatabase().getPlayerCache(uuid, PlayerCacheType.Y.toString().toLowerCase()), + (double) this.plugin.getDatabase().getPlayerCache(uuid, PlayerCacheType.Z.toString().toLowerCase()), + ((Double) this.plugin.getDatabase().getPlayerCache(uuid, + PlayerCacheType.YAW.toString().toLowerCase())).floatValue(), + ((Double) this.plugin.getDatabase().getPlayerCache(uuid, + PlayerCacheType.PITCH.toString().toLowerCase())).floatValue()); + } } public String getName(final String uuid) { - return (String) this.playerCache.get("Players." + uuid + "." + PlayerCacheType.NAME.toString()); + return (String) this.plugin.getDatabase().getPlayerCache(uuid, PlayerCacheType.NAME.toString().toLowerCase()); } public int getSkinParts(final String uuid) { - return (int) this.playerCache.get("Players." + uuid + "." + PlayerCacheType.SKINPARTS.toString()); + return (int) this.plugin.getDatabase().getPlayerCache(uuid, PlayerCacheType.SKINPARTS.toString().toLowerCase()); } public MainHand getMainHand(final String uuid) { - return MainHand.valueOf( - this.playerCache.get("Players." + uuid + "." + PlayerCacheType.MAINHAND.toString()).toString()); + return MainHand.valueOf((String) this.plugin.getDatabase().getPlayerCache(uuid, + PlayerCacheType.MAINHAND.toString().toLowerCase())); } public byte getMainHandAsByte(final String uuid) { @@ -48,34 +72,7 @@ public class PlayerCache { return (byte) 0; } - public void shutdown() { - try { - this.plugin.getLogger().info("Saving PlayerCache..."); - this.playerCache.save(this.playerCacheFile); - } catch (final IOException e) { - e.printStackTrace(); - } - } - - private void savePlayerCache() { - if (!this.wasSaved) - try { - this.plugin.getLogger().info("Saving PlayerCache..."); - this.playerCache.save(this.playerCacheFile); - this.wasSaved = true; - new BukkitRunnable() { - - @Override - public void run() { - wasSaved = false; - } - }.runTaskLater(this.plugin, 20); // Only save once every second to prevent disklock - } catch (final IOException e) { - e.printStackTrace(); - } - } - public enum PlayerCacheType { - NAME, SKINPARTS, MAINHAND, + NAME, SKINPARTS, MAINHAND, WORLD, X, Y, Z, YAW, PITCH, } } \ No newline at end of file