mirror of
https://github.com/exituser/Brewery.git
synced 2026-09-17 06:38:56 +00:00
Implemented Playerdata DB Syncing
This commit is contained in:
@@ -29,7 +29,7 @@ import java.lang.reflect.Method;
|
||||
import java.util.*;
|
||||
|
||||
public class BPlayer {
|
||||
private static Map<String, BPlayer> players = new HashMap<>();// Players name/uuid and BPlayer
|
||||
private static Map<String, BPlayer> players = new HashMap<>();// Players uuid and BPlayer
|
||||
private static Map<Player, MutableInt> pTasks = new HashMap<>();// Player and count
|
||||
private static int taskId;
|
||||
private static boolean modAge = true;
|
||||
@@ -37,21 +37,24 @@ public class BPlayer {
|
||||
private static Method itemHandle;
|
||||
private static Field age;
|
||||
|
||||
private final String uuid;
|
||||
private int quality = 0;// = quality of drunkeness * drunkeness
|
||||
private int drunkeness = 0;// = amount of drunkeness
|
||||
private int offlineDrunk = 0;// drunkeness when gone offline
|
||||
private Vector push = new Vector(0, 0, 0);
|
||||
private int time = 20;
|
||||
|
||||
public BPlayer() {
|
||||
public BPlayer(String uuid) {
|
||||
this.uuid = uuid;
|
||||
}
|
||||
|
||||
// reading from file
|
||||
public BPlayer(String name, int quality, int drunkeness, int offlineDrunk) {
|
||||
public BPlayer(String uuid, int quality, int drunkeness, int offlineDrunk) {
|
||||
this.quality = quality;
|
||||
this.drunkeness = drunkeness;
|
||||
this.offlineDrunk = offlineDrunk;
|
||||
players.put(name, this);
|
||||
this.uuid = uuid;
|
||||
players.put(uuid, this);
|
||||
}
|
||||
|
||||
@Nullable
|
||||
@@ -111,13 +114,16 @@ public class BPlayer {
|
||||
|
||||
// Create a new BPlayer and add it to the list
|
||||
public static BPlayer addPlayer(Player player) {
|
||||
BPlayer bPlayer = new BPlayer();
|
||||
BPlayer bPlayer = new BPlayer(BUtil.playerString(player));
|
||||
players.put(BUtil.playerString(player), bPlayer);
|
||||
return bPlayer;
|
||||
}
|
||||
|
||||
public static void remove(Player player) {
|
||||
players.remove(BUtil.playerString(player));
|
||||
if (BConfig.sqlDrunkSync && BConfig.sqlSync != null) {
|
||||
BConfig.sqlSync.removePlayer(player.getUniqueId());
|
||||
}
|
||||
}
|
||||
|
||||
public static int numDrunkPlayers() {
|
||||
@@ -128,6 +134,9 @@ public class BPlayer {
|
||||
for (Iterator<Map.Entry<String, BPlayer>> iterator = players.entrySet().iterator(); iterator.hasNext(); ) {
|
||||
Map.Entry<String, BPlayer> entry = iterator.next();
|
||||
if (entry.getValue() == this) {
|
||||
if (BConfig.sqlDrunkSync && BConfig.sqlSync != null) {
|
||||
BConfig.sqlSync.removePlayer(UUID.fromString(entry.getKey()));
|
||||
}
|
||||
iterator.remove();
|
||||
return;
|
||||
}
|
||||
@@ -183,6 +192,7 @@ public class BPlayer {
|
||||
if (bPlayer.drunkeness > 100) {
|
||||
bPlayer.drinkCap(player);
|
||||
}
|
||||
bPlayer.syncToSQL();
|
||||
//player.sendMessage("Betrunkenheit: §8[§7⭑⭑⭑⭒§0⭑§8] §8[§6|||||||||||||||||§0|||||||||§8]");
|
||||
return true;
|
||||
}
|
||||
@@ -191,6 +201,7 @@ public class BPlayer {
|
||||
public void drinkCap(Player player) {
|
||||
quality = getQuality() * 100;
|
||||
drunkeness = 100;
|
||||
syncToSQL();
|
||||
if (BConfig.overdrinkKick && !player.hasPermission("brewery.bypass.overdrink")) {
|
||||
P.p.getServer().getScheduler().scheduleSyncDelayedTask(P.p, () -> passOut(player), 1);
|
||||
} else {
|
||||
@@ -233,9 +244,11 @@ public class BPlayer {
|
||||
}
|
||||
quality = getQuality();
|
||||
if (drunkeness <= -offlineDrunk) {
|
||||
syncToSQL();
|
||||
return drunkeness <= -BConfig.hangoverTime;
|
||||
}
|
||||
}
|
||||
syncToSQL();
|
||||
return false;
|
||||
}
|
||||
|
||||
@@ -291,6 +304,7 @@ public class BPlayer {
|
||||
public void passOut(Player player) {
|
||||
player.kickPlayer(P.p.languageReader.get("Player_DrunkPassOut"));
|
||||
offlineDrunk = drunkeness;
|
||||
syncToSQL();
|
||||
}
|
||||
|
||||
|
||||
@@ -344,7 +358,7 @@ public class BPlayer {
|
||||
}
|
||||
hangoverEffects(player);
|
||||
// wird der spieler noch gebraucht?
|
||||
players.remove(BUtil.playerString(player));
|
||||
remove(player);
|
||||
|
||||
} else if (offlineDrunk - drunkeness >= 30) {
|
||||
Location randomLoc = Wakeup.getRandom(player.getLocation());
|
||||
@@ -357,10 +371,13 @@ public class BPlayer {
|
||||
}
|
||||
|
||||
offlineDrunk = 0;
|
||||
syncToSQL();
|
||||
|
||||
}
|
||||
|
||||
public void disconnecting() {
|
||||
offlineDrunk = drunkeness;
|
||||
syncToSQL();
|
||||
}
|
||||
|
||||
public void goHome(final Player player) {
|
||||
@@ -658,19 +675,29 @@ public class BPlayer {
|
||||
Iterator<Map.Entry<String, BPlayer>> iter = players.entrySet().iterator();
|
||||
while (iter.hasNext()) {
|
||||
Map.Entry<String, BPlayer> entry = iter.next();
|
||||
String name = entry.getKey();
|
||||
String uuid = entry.getKey();
|
||||
BPlayer bplayer = entry.getValue();
|
||||
if (bplayer.drunkeness == soberPerMin) {
|
||||
// Prevent 0 drunkeness
|
||||
soberPerMin++;
|
||||
}
|
||||
if (bplayer.drain(BUtil.getPlayerfromString(name), soberPerMin)) {
|
||||
if (bplayer.drain(BUtil.getPlayerfromString(uuid), soberPerMin)) {
|
||||
iter.remove();
|
||||
if (BConfig.sqlDrunkSync && BConfig.sqlSync != null) {
|
||||
BConfig.sqlSync.removePlayer(UUID.fromString(uuid));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Sync Drunkeness Data to SQL if enabled
|
||||
public void syncToSQL() {
|
||||
if (BConfig.sqlDrunkSync && BConfig.sqlSync != null) {
|
||||
BConfig.sqlSync.updatePlayer(UUID.fromString(uuid), this);
|
||||
}
|
||||
}
|
||||
|
||||
// save all data
|
||||
public static void save(ConfigurationSection config) {
|
||||
for (Map.Entry<String, BPlayer> entry : players.entrySet()) {
|
||||
@@ -687,6 +714,11 @@ public class BPlayer {
|
||||
|
||||
// #### getter/setter ####
|
||||
|
||||
|
||||
public String getUuid() {
|
||||
return uuid;
|
||||
}
|
||||
|
||||
public int getDrunkeness() {
|
||||
return drunkeness;
|
||||
}
|
||||
@@ -702,6 +734,7 @@ public class BPlayer {
|
||||
}
|
||||
}
|
||||
this.drunkeness = drunkeness;
|
||||
syncToSQL();
|
||||
}
|
||||
|
||||
public int getQuality() {
|
||||
|
||||
@@ -27,6 +27,14 @@ import java.util.List;
|
||||
*/
|
||||
public class BreweryApi {
|
||||
|
||||
/**
|
||||
* Get the Current Version of the Brewery API.
|
||||
* <p>Higher numbers mean newer API, but it doesn't necessarily mean that something has changed, may be additions only
|
||||
*/
|
||||
public static int getApiVersion() {
|
||||
return 2;
|
||||
}
|
||||
|
||||
/**
|
||||
* Remove any data that this Plugin may associate with the given Block.
|
||||
* <p>Currently Cauldrons and Barrels (Cauldron, Wood, Woodstairs, Fence, Sign)
|
||||
|
||||
@@ -80,7 +80,6 @@ public class BConfig {
|
||||
|
||||
//MySQL
|
||||
public static String sqlHost, sqlPort, sqlDB;
|
||||
private static String sqlUser, sqlPW;
|
||||
public static SQLSync sqlSync;
|
||||
public static boolean sqlDrunkSync;
|
||||
|
||||
@@ -345,9 +344,9 @@ public class BConfig {
|
||||
sqlDrunkSync = sqlCfg.getBoolean("syncDrunkeness");
|
||||
sqlHost = sqlCfg.getString("host", null);
|
||||
sqlPort = sqlCfg.getString("port", null);
|
||||
sqlUser = sqlCfg.getString("user", null);
|
||||
sqlDB = sqlCfg.getString("database", null);
|
||||
sqlPW = sqlCfg.getString("password", null);
|
||||
String sqlUser = sqlCfg.getString("user", null);
|
||||
String sqlPW = sqlCfg.getString("password", null);
|
||||
|
||||
sqlSync = new SQLSync();
|
||||
if (!sqlSync.init(sqlUser, sqlPW)) {
|
||||
|
||||
@@ -164,11 +164,11 @@ public class BData {
|
||||
// loading BPlayer
|
||||
section = data.getConfigurationSection("Player");
|
||||
if (section != null) {
|
||||
// keys have players name
|
||||
for (String name : section.getKeys(false)) {
|
||||
// keys have players uuid
|
||||
for (String uuid : section.getKeys(false)) {
|
||||
try {
|
||||
//noinspection ResultOfMethodCallIgnored
|
||||
UUID.fromString(name);
|
||||
UUID.fromString(uuid);
|
||||
if (!P.useUUID) {
|
||||
continue;
|
||||
}
|
||||
@@ -178,11 +178,11 @@ public class BData {
|
||||
}
|
||||
}
|
||||
|
||||
int quality = section.getInt(name + ".quality");
|
||||
int drunk = section.getInt(name + ".drunk");
|
||||
int offDrunk = section.getInt(name + ".offDrunk", 0);
|
||||
int quality = section.getInt(uuid + ".quality");
|
||||
int drunk = section.getInt(uuid + ".drunk");
|
||||
int offDrunk = section.getInt(uuid + ".offDrunk", 0);
|
||||
|
||||
new BPlayer(name, quality, drunk, offDrunk);
|
||||
new BPlayer(uuid, quality, drunk, offDrunk);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -186,8 +186,17 @@ public class PlayerListener implements Listener {
|
||||
DistortChat.playerCommand(event);
|
||||
}
|
||||
|
||||
@EventHandler(priority = EventPriority.MONITOR, ignoreCancelled = true)
|
||||
public void onPlayerLoginAsync(AsyncPlayerPreLoginEvent event) {
|
||||
if (event.getLoginResult() == AsyncPlayerPreLoginEvent.Result.ALLOWED) {
|
||||
if (BConfig.sqlDrunkSync && BConfig.sqlSync != null) {
|
||||
BConfig.sqlSync.fetchPlayerLoginData(event.getUniqueId());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// player joins while passed out
|
||||
@EventHandler()
|
||||
@EventHandler
|
||||
public void onPlayerLogin(PlayerLoginEvent event) {
|
||||
if (event.getResult() == PlayerLoginEvent.Result.ALLOWED) {
|
||||
final Player player = event.getPlayer();
|
||||
|
||||
@@ -3,13 +3,8 @@ package com.dre.brewery.utility;
|
||||
import com.dre.brewery.BPlayer;
|
||||
import com.dre.brewery.P;
|
||||
import com.dre.brewery.filedata.BConfig;
|
||||
import org.bukkit.entity.Player;
|
||||
|
||||
import java.sql.Connection;
|
||||
import java.sql.DriverManager;
|
||||
import java.sql.PreparedStatement;
|
||||
import java.sql.SQLException;
|
||||
import java.sql.Statement;
|
||||
import java.sql.*;
|
||||
import java.util.UUID;
|
||||
import java.util.concurrent.ArrayBlockingQueue;
|
||||
import java.util.concurrent.BlockingQueue;
|
||||
@@ -24,10 +19,10 @@ public class SQLSync {
|
||||
private String user, password;
|
||||
|
||||
|
||||
public void updatePlayer(Player player, BPlayer bPlayer) {
|
||||
removePlayer(player.getUniqueId());
|
||||
public void updatePlayer(UUID uuid, BPlayer bPlayer) {
|
||||
removePlayer(uuid);
|
||||
SQLData_BP bP = new SQLData_BP();
|
||||
bP.uuid = player.getUniqueId();
|
||||
bP.uuid = uuid;
|
||||
bP.drunkeness = bPlayer.getDrunkeness();
|
||||
bP.offlineDrunk = bPlayer.getOfflineDrunkeness();
|
||||
bP.quality = bPlayer.getQuality();
|
||||
@@ -72,6 +67,32 @@ public class SQLSync {
|
||||
}
|
||||
}
|
||||
|
||||
// Run Async
|
||||
public void fetchPlayerLoginData(final UUID uuid) {
|
||||
try {
|
||||
if (!checkConnection()) {
|
||||
if (!openConnection()) {
|
||||
P.p.errorLog("Opening SQL Connection failed");
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
Statement statement = connection.createStatement();
|
||||
if (statement.execute("SELECT * FROM BreweryZ_BPlayers WHERE uuid = " + uuid.toString() + ";")) {
|
||||
final ResultSet result = statement.getResultSet();
|
||||
P.p.getServer().getScheduler().runTask(P.p, () -> {
|
||||
try {
|
||||
new BPlayer(uuid.toString(), result.getInt("quality"), result.getInt("drunkeness"), result.getInt("offlineDrunk"));
|
||||
} catch (SQLException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
});
|
||||
}
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
private void initAsyncTask() {
|
||||
if (sqlTaskRunning) return;
|
||||
sqlTaskRunning = true;
|
||||
|
||||
Reference in New Issue
Block a user