feat: add PlayerCache to Database

This commit is contained in:
Simon Giesel 2020-05-19 22:06:02 +02:00
parent 994fccf845
commit 59c5b5cec9
3 changed files with 72 additions and 46 deletions

View file

@ -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();

View file

@ -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;
}
}

View file

@ -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,
}
}