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 @Override
public void onDisable() { public void onDisable() {
this.getLogger().info("Stopping CliffbreakVaro!"); this.getLogger().info("Stopping CliffbreakVaro!");
this.playerCache.shutdown();
this.database.shutdown(); this.database.shutdown();
try { try {
this.discordBot.shutdown(); this.discordBot.shutdown();

View file

@ -23,6 +23,8 @@ public class Database {
final Statement s = conn.createStatement(); final Statement s = conn.createStatement();
s.executeUpdate( s.executeUpdate(
"CREATE TABLE IF NOT EXISTS kills (`player` VARCHAR(32) NOT NULL, `victim` VARCHAR(32) NOT NULL);"); "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(); s.close();
} }
@ -56,4 +58,32 @@ public class Database {
} }
return 0; 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; package de.cliffbreak.varo.uitls;
import java.io.File; import org.bukkit.Bukkit;
import java.io.IOException; import org.bukkit.Location;
import org.bukkit.World;
import org.bukkit.configuration.file.FileConfiguration; import org.bukkit.World.Environment;
import org.bukkit.configuration.file.YamlConfiguration;
import org.bukkit.inventory.MainHand; import org.bukkit.inventory.MainHand;
import org.bukkit.scheduler.BukkitRunnable;
import de.cliffbreak.varo.Varo; import de.cliffbreak.varo.Varo;
public class PlayerCache { public class PlayerCache {
private final Varo plugin; private final Varo plugin;
private final File playerCacheFile;
private final FileConfiguration playerCache;
private boolean wasSaved = false;
public PlayerCache(final Varo plugin) { public PlayerCache(final Varo plugin) {
this.playerCacheFile = new File("plugins/CliffbreakVaro", "playercache.yml");
this.playerCache = YamlConfiguration.loadConfiguration(this.playerCacheFile);
this.plugin = plugin; this.plugin = plugin;
} }
public void setPlayerCache(final String uuid, final PlayerCacheType type, final Object value) { public void setPlayerCache(final String uuid, final PlayerCacheType type, final Object value) {
this.playerCache.set("Players." + uuid + "." + type.toString(), value); this.plugin.getDatabase().updatePlayerCache(uuid, type.toString().toLowerCase(), value);
savePlayerCache(); }
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) { 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) { 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) { public MainHand getMainHand(final String uuid) {
return MainHand.valueOf( return MainHand.valueOf((String) this.plugin.getDatabase().getPlayerCache(uuid,
this.playerCache.get("Players." + uuid + "." + PlayerCacheType.MAINHAND.toString()).toString()); PlayerCacheType.MAINHAND.toString().toLowerCase()));
} }
public byte getMainHandAsByte(final String uuid) { public byte getMainHandAsByte(final String uuid) {
@ -48,34 +72,7 @@ public class PlayerCache {
return (byte) 0; 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 { public enum PlayerCacheType {
NAME, SKINPARTS, MAINHAND, NAME, SKINPARTS, MAINHAND, WORLD, X, Y, Z, YAW, PITCH,
} }
} }