Update from Git Manager GUI
This commit is contained in:
@@ -1,59 +0,0 @@
|
||||
package net.craftersland.eco.bridge;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
import org.bukkit.Bukkit;
|
||||
import org.bukkit.entity.Player;
|
||||
|
||||
public class BackgroundTask {
|
||||
|
||||
private Eco m;
|
||||
private long lastSave = System.currentTimeMillis();
|
||||
|
||||
public BackgroundTask(Eco m) {
|
||||
this.m = m;
|
||||
runTask();
|
||||
}
|
||||
|
||||
private void runTask() {
|
||||
if (m.getConfigHandler().getBoolean("General.saveDataTask.enabled") == true) {
|
||||
Eco.log.info("Data save task is enabled.");
|
||||
} else {
|
||||
Eco.log.info("Data save task is disabled.");
|
||||
}
|
||||
Bukkit.getScheduler().runTaskTimerAsynchronously(m, new Runnable() {
|
||||
|
||||
@Override
|
||||
public void run() {
|
||||
m.getEcoDataHandler().updateBalanceMap();
|
||||
runSaveData();
|
||||
}
|
||||
|
||||
}, 20L, 20L);
|
||||
}
|
||||
|
||||
private void runSaveData() {
|
||||
if (m.getConfigHandler().getBoolean("General.saveDataTask.enabled") == true) {
|
||||
if (Bukkit.getOnlinePlayers().isEmpty() == false) {
|
||||
if (System.currentTimeMillis() - lastSave >= m.getConfigHandler().getInteger("General.saveDataTask.interval") * 60 * 1000) {
|
||||
List<Player> onlinePlayers = new ArrayList<Player>(Bukkit.getOnlinePlayers());
|
||||
lastSave = System.currentTimeMillis();
|
||||
if (m.getConfigHandler().getBoolean("General.saveDataTask.hideLogMessages") == false) {
|
||||
Eco.log.info("Saving online players data...");
|
||||
}
|
||||
for (Player p : onlinePlayers) {
|
||||
if (p.isOnline() == true) {
|
||||
m.getEcoDataHandler().onDataSaveFunction(p, false, "false", false);
|
||||
}
|
||||
}
|
||||
if (m.getConfigHandler().getBoolean("General.saveDataTask.hideLogMessages") == false) {
|
||||
Eco.log.info("Data save complete for " + onlinePlayers.size() + " players.");
|
||||
}
|
||||
onlinePlayers.clear();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,97 +0,0 @@
|
||||
package net.craftersland.eco.bridge;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
import java.util.logging.Logger;
|
||||
|
||||
import net.craftersland.eco.bridge.database.EcoMysqlHandler;
|
||||
import net.craftersland.eco.bridge.database.MysqlSetup;
|
||||
import net.craftersland.eco.bridge.events.PlayerDisconnect;
|
||||
import net.craftersland.eco.bridge.events.PlayerJoin;
|
||||
import net.craftersland.eco.bridge.events.handlers.EcoDataHandler;
|
||||
import net.milkbowl.vault.economy.Economy;
|
||||
|
||||
import org.bukkit.Bukkit;
|
||||
import org.bukkit.entity.Player;
|
||||
import org.bukkit.event.HandlerList;
|
||||
import org.bukkit.plugin.PluginManager;
|
||||
import org.bukkit.plugin.RegisteredServiceProvider;
|
||||
import org.bukkit.plugin.java.JavaPlugin;
|
||||
|
||||
public class Eco extends JavaPlugin {
|
||||
|
||||
public static Logger log;
|
||||
public static Economy vault = null;
|
||||
public static String pluginName = "MysqlEcoBridge";
|
||||
public Map<Player, Integer> syncCompleteTasks = new HashMap<Player, Integer>();
|
||||
|
||||
private static ConfigHandler configHandler;
|
||||
private static MysqlSetup mysqlSetup;
|
||||
private static EcoMysqlHandler ecoMysqlHandler;
|
||||
private static EcoDataHandler edH;
|
||||
private static BackgroundTask bt;
|
||||
|
||||
@Override
|
||||
public void onEnable() {
|
||||
log = getLogger();
|
||||
//Setup Vault for economy
|
||||
if (setupEconomy() == false) {
|
||||
log.severe("Warning! - Vault installed? If yes Economy system installed?");
|
||||
getServer().getPluginManager().disablePlugin(this);
|
||||
return;
|
||||
}
|
||||
//Load Configuration
|
||||
configHandler = new ConfigHandler(this);
|
||||
//Setup MySQL
|
||||
mysqlSetup = new MysqlSetup(this);
|
||||
ecoMysqlHandler = new EcoMysqlHandler(this);
|
||||
edH = new EcoDataHandler(this);
|
||||
bt = new BackgroundTask(this);
|
||||
//Register Listeners
|
||||
PluginManager pm = getServer().getPluginManager();
|
||||
pm.registerEvents(new PlayerJoin(this), this);
|
||||
pm.registerEvents(new PlayerDisconnect(this), this);
|
||||
log.info(pluginName + " loaded successfully!");
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onDisable() {
|
||||
Bukkit.getScheduler().cancelTasks(this);
|
||||
HandlerList.unregisterAll(this);
|
||||
if (mysqlSetup.getConnection() != null) {
|
||||
edH.onShutDownDataSave();
|
||||
mysqlSetup.closeConnection();
|
||||
}
|
||||
log.info(pluginName + " is disabled!");
|
||||
}
|
||||
|
||||
private boolean setupEconomy() {
|
||||
if (getServer().getPluginManager().getPlugin("Vault") == null) {
|
||||
return false;
|
||||
}
|
||||
RegisteredServiceProvider<Economy> rsp = getServer().getServicesManager().getRegistration(Economy.class);
|
||||
if (rsp == null) {
|
||||
return false;
|
||||
}
|
||||
vault = rsp.getProvider();
|
||||
log.info("Using economy system: " + rsp.getProvider().getName());
|
||||
return vault != null;
|
||||
}
|
||||
|
||||
public ConfigHandler getConfigHandler() {
|
||||
return configHandler;
|
||||
}
|
||||
public MysqlSetup getMysqlSetup() {
|
||||
return mysqlSetup;
|
||||
}
|
||||
public EcoMysqlHandler getEcoMysqlHandler() {
|
||||
return ecoMysqlHandler;
|
||||
}
|
||||
public EcoDataHandler getEcoDataHandler() {
|
||||
return edH;
|
||||
}
|
||||
public BackgroundTask getBackgroundTask() {
|
||||
return bt;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,179 +0,0 @@
|
||||
package net.craftersland.eco.bridge.database;
|
||||
|
||||
import java.sql.Connection;
|
||||
import java.sql.PreparedStatement;
|
||||
import java.sql.ResultSet;
|
||||
import java.sql.SQLException;
|
||||
|
||||
import org.bukkit.entity.Player;
|
||||
|
||||
import net.craftersland.eco.bridge.Eco;
|
||||
|
||||
public class EcoMysqlHandler {
|
||||
|
||||
private Eco eco;
|
||||
|
||||
public EcoMysqlHandler(Eco eco) {
|
||||
this.eco = eco;
|
||||
}
|
||||
|
||||
public String[] getData(Player player) {
|
||||
if (!hasAccount(player)) {
|
||||
createAccount(player);
|
||||
}
|
||||
PreparedStatement preparedUpdateStatement = null;
|
||||
ResultSet result = null;
|
||||
Connection conn = eco.getMysqlSetup().getConnection();
|
||||
if (conn != null) {
|
||||
try {
|
||||
String sql = "SELECT * FROM `" + eco.getConfigHandler().getString("database.mysql.dataTableName") + "` WHERE `player_uuid` = ? LIMIT 1";
|
||||
preparedUpdateStatement = conn.prepareStatement(sql);
|
||||
preparedUpdateStatement.setString(1, player.getUniqueId().toString());
|
||||
|
||||
result = preparedUpdateStatement.executeQuery();
|
||||
while (result.next()) {
|
||||
String[] data = {"" + result.getDouble("money"), result.getString("sync_complete"), result.getString("last_seen")};
|
||||
return data;
|
||||
}
|
||||
} catch (SQLException e) {
|
||||
Eco.log.warning("Error: " + e.getMessage());
|
||||
e.printStackTrace();
|
||||
} finally {
|
||||
try {
|
||||
if (result != null) {
|
||||
result.close();
|
||||
}
|
||||
if (preparedUpdateStatement != null) {
|
||||
preparedUpdateStatement.close();
|
||||
}
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
public void setSyncStatus(Player player, String syncStatus) {
|
||||
PreparedStatement preparedUpdateStatement = null;
|
||||
Connection conn = eco.getMysqlSetup().getConnection();
|
||||
if (conn != null) {
|
||||
try {
|
||||
String data = "UPDATE `" + eco.getConfigHandler().getString("database.mysql.dataTableName") + "` " + "SET `sync_complete` = ?" + ", `last_seen` = ?" + " WHERE `player_uuid` = ?";
|
||||
preparedUpdateStatement = conn.prepareStatement(data);
|
||||
preparedUpdateStatement.setString(1, syncStatus);
|
||||
preparedUpdateStatement.setString(2, String.valueOf(System.currentTimeMillis()));
|
||||
preparedUpdateStatement.setString(3, player.getUniqueId().toString());
|
||||
|
||||
preparedUpdateStatement.executeUpdate();
|
||||
} catch (SQLException e) {
|
||||
Eco.log.warning("Error: " + e.getMessage());
|
||||
e.printStackTrace();
|
||||
} finally {
|
||||
try {
|
||||
if (preparedUpdateStatement != null) {
|
||||
preparedUpdateStatement.close();
|
||||
}
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public void setData(Player player, Double money, String syncComplete) {
|
||||
if (!hasAccount(player)) {
|
||||
createAccount(player);
|
||||
}
|
||||
PreparedStatement preparedUpdateStatement = null;
|
||||
Connection conn = eco.getMysqlSetup().getConnection();
|
||||
if (conn != null) {
|
||||
try {
|
||||
String data = "UPDATE `" + eco.getConfigHandler().getString("database.mysql.dataTableName") + "` " + "SET `player_name` = ?" + ", `money` = ?" + ", `sync_complete` = ?" + ", `last_seen` = ?" + " WHERE `player_uuid` = ?";
|
||||
preparedUpdateStatement = conn.prepareStatement(data);
|
||||
preparedUpdateStatement.setString(1, player.getName());
|
||||
preparedUpdateStatement.setDouble(2, money);
|
||||
preparedUpdateStatement.setString(3, syncComplete);
|
||||
preparedUpdateStatement.setString(4, String.valueOf(System.currentTimeMillis()));
|
||||
preparedUpdateStatement.setString(5, player.getUniqueId().toString());
|
||||
|
||||
preparedUpdateStatement.executeUpdate();
|
||||
} catch (SQLException e) {
|
||||
Eco.log.warning("Error: " + e.getMessage());
|
||||
e.printStackTrace();
|
||||
} finally {
|
||||
try {
|
||||
if (preparedUpdateStatement != null) {
|
||||
preparedUpdateStatement.close();
|
||||
}
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public void createAccount(Player player) {
|
||||
PreparedStatement preparedStatement = null;
|
||||
Connection conn = eco.getMysqlSetup().getConnection();
|
||||
if (conn != null) {
|
||||
try {
|
||||
String sql = "INSERT INTO `" + eco.getConfigHandler().getString("database.mysql.dataTableName") + "`(`player_uuid`, `player_name`, `money`, `last_seen`, `sync_complete`) " + "VALUES(?, ?, ?, ?, ?)";
|
||||
preparedStatement = conn.prepareStatement(sql);
|
||||
|
||||
preparedStatement.setString(1, player.getUniqueId().toString());
|
||||
preparedStatement.setString(2, player.getName());
|
||||
preparedStatement.setDouble(3, 0.0);
|
||||
preparedStatement.setString(4, String.valueOf(System.currentTimeMillis()));
|
||||
preparedStatement.setString(5, "true");
|
||||
|
||||
preparedStatement.executeUpdate();
|
||||
} catch (SQLException e) {
|
||||
Eco.log.warning("Error: " + e.getMessage());
|
||||
e.printStackTrace();
|
||||
} finally {
|
||||
try {
|
||||
if (preparedStatement != null) {
|
||||
preparedStatement.close();
|
||||
}
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public boolean hasAccount(Player player) {
|
||||
PreparedStatement preparedUpdateStatement = null;
|
||||
ResultSet result = null;
|
||||
Connection conn = eco.getMysqlSetup().getConnection();
|
||||
if (conn != null) {
|
||||
try {
|
||||
String sql = "SELECT `player_uuid` FROM `" + eco.getConfigHandler().getString("database.mysql.dataTableName") + "` WHERE `player_uuid` = ? LIMIT 1";
|
||||
preparedUpdateStatement = conn.prepareStatement(sql);
|
||||
preparedUpdateStatement.setString(1, player.getUniqueId().toString());
|
||||
|
||||
result = preparedUpdateStatement.executeQuery();
|
||||
while (result.next()) {
|
||||
return true;
|
||||
}
|
||||
} catch (SQLException e) {
|
||||
Eco.log.warning("Error: " + e.getMessage());
|
||||
e.printStackTrace();
|
||||
} finally {
|
||||
try {
|
||||
if (result != null) {
|
||||
result.close();
|
||||
}
|
||||
if (preparedUpdateStatement != null) {
|
||||
preparedUpdateStatement.close();
|
||||
}
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,208 +0,0 @@
|
||||
package net.craftersland.eco.bridge.database;
|
||||
|
||||
import java.sql.Connection;
|
||||
import java.sql.DatabaseMetaData;
|
||||
import java.sql.DriverManager;
|
||||
import java.sql.PreparedStatement;
|
||||
import java.sql.ResultSet;
|
||||
import java.sql.SQLException;
|
||||
import java.util.Properties;
|
||||
|
||||
import org.bukkit.Bukkit;
|
||||
|
||||
import net.craftersland.eco.bridge.Eco;
|
||||
|
||||
public class MysqlSetup {
|
||||
|
||||
private Connection conn = null;
|
||||
private Eco eco;
|
||||
|
||||
public MysqlSetup(Eco eco) {
|
||||
this.eco = eco;
|
||||
connectToDatabase();
|
||||
setupDatabase();
|
||||
updateTables();
|
||||
databaseMaintenanceTask();
|
||||
}
|
||||
|
||||
public void connectToDatabase() {
|
||||
Eco.log.info("Connecting to the database...");
|
||||
try {
|
||||
//Load Drivers
|
||||
Class.forName("com.mysql.jdbc.Driver");
|
||||
Properties properties = new Properties();
|
||||
properties.setProperty("user", eco.getConfigHandler().getString("database.mysql.user"));
|
||||
properties.setProperty("password", eco.getConfigHandler().getString("database.mysql.password"));
|
||||
properties.setProperty("autoReconnect", "true");
|
||||
properties.setProperty("verifyServerCertificate", "false");
|
||||
properties.setProperty("useSSL", eco.getConfigHandler().getString("database.mysql.sslEnabled"));
|
||||
properties.setProperty("requireSSL", eco.getConfigHandler().getString("database.mysql.sslEnabled"));
|
||||
|
||||
//Connect to database
|
||||
conn = DriverManager.getConnection("jdbc:mysql://" + eco.getConfigHandler().getString("database.mysql.host") + ":" + eco.getConfigHandler().getString("database.mysql.port") + "/" + eco.getConfigHandler().getString("database.mysql.databaseName"), properties);
|
||||
|
||||
} catch (ClassNotFoundException e) {
|
||||
Eco.log.severe("Could not locate drivers for mysql! Error: " + e.getMessage());
|
||||
return;
|
||||
} catch (SQLException e) {
|
||||
Eco.log.severe("Could not connect to mysql database! Error: " + e.getMessage());
|
||||
return;
|
||||
}
|
||||
Eco.log.info("Database connection successful!");
|
||||
}
|
||||
|
||||
public void setupDatabase() {
|
||||
if (conn == null) return;
|
||||
//Create tables if needed
|
||||
PreparedStatement query = null;
|
||||
try {
|
||||
String data = "CREATE TABLE IF NOT EXISTS `" + eco.getConfigHandler().getString("database.mysql.dataTableName") + "` (id int(10) AUTO_INCREMENT, player_uuid varchar(50) NOT NULL UNIQUE, player_name varchar(50) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci NOT NULL, money double(30,2) NOT NULL, sync_complete varchar(5) NOT NULL, last_seen varchar(30) NOT NULL, PRIMARY KEY(id));";
|
||||
query = conn.prepareStatement(data);
|
||||
query.execute();
|
||||
} catch (SQLException e) {
|
||||
e.printStackTrace();
|
||||
Eco.log.severe("Error creating tables! Error: " + e.getMessage());
|
||||
} finally {
|
||||
try {
|
||||
if (query != null) {
|
||||
query.close();
|
||||
}
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public Connection getConnection() {
|
||||
checkConnection();
|
||||
return conn;
|
||||
}
|
||||
|
||||
public void checkConnection() {
|
||||
try {
|
||||
if (conn == null) {
|
||||
Eco.log.warning("Connection failed. Reconnecting...");
|
||||
reConnect();
|
||||
}
|
||||
if (!conn.isValid(3)) {
|
||||
Eco.log.warning("Connection is idle or terminated. Reconnecting...");
|
||||
reConnect();
|
||||
}
|
||||
if (conn.isClosed() == true) {
|
||||
Eco.log.warning("Connection is closed. Reconnecting...");
|
||||
reConnect();
|
||||
}
|
||||
} catch (Exception e) {
|
||||
Eco.log.severe("Could not reconnect to Database! Error: " + e.getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
public boolean reConnect() {
|
||||
try {
|
||||
long start = 0;
|
||||
long end = 0;
|
||||
|
||||
start = System.currentTimeMillis();
|
||||
Eco.log.info("Attempting to establish a connection to the MySQL server!");
|
||||
Class.forName("com.mysql.jdbc.Driver");
|
||||
Properties properties = new Properties();
|
||||
properties.setProperty("user", eco.getConfigHandler().getString("database.mysql.user"));
|
||||
properties.setProperty("password", eco.getConfigHandler().getString("database.mysql.password"));
|
||||
properties.setProperty("autoReconnect", "true");
|
||||
properties.setProperty("verifyServerCertificate", "false");
|
||||
properties.setProperty("useSSL", eco.getConfigHandler().getString("database.mysql.sslEnabled"));
|
||||
properties.setProperty("requireSSL", eco.getConfigHandler().getString("database.mysql.sslEnabled"));
|
||||
//properties.setProperty("useUnicode", "true");
|
||||
//properties.setProperty("characterEncoding", "utf8");
|
||||
//properties.setProperty("characterSetResults", "utf8");
|
||||
//properties.setProperty("connectionCollation", "utf8mb4_unicode_ci");
|
||||
conn = DriverManager.getConnection("jdbc:mysql://" + eco.getConfigHandler().getString("database.mysql.host") + ":" + eco.getConfigHandler().getString("database.mysql.port") + "/" + eco.getConfigHandler().getString("database.mysql.databaseName"), properties);
|
||||
end = System.currentTimeMillis();
|
||||
Eco.log.info("Connection to MySQL server established in " + ((end - start)) + " ms!");
|
||||
return true;
|
||||
} catch (Exception e) {
|
||||
Eco.log.severe("Error re-connecting to the database! Error: " + e.getMessage());
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
public void closeConnection() {
|
||||
try {
|
||||
Eco.log.info("Closing database connection...");
|
||||
conn.close();
|
||||
conn = null;
|
||||
} catch (SQLException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
private void updateTables() {
|
||||
if (conn != null) {
|
||||
DatabaseMetaData md = null;
|
||||
ResultSet rs1 = null;
|
||||
PreparedStatement query1 = null;
|
||||
try {
|
||||
md = conn.getMetaData();
|
||||
rs1 = md.getColumns(null, null, eco.getConfigHandler().getString("database.mysql.dataTableName"), "sync_complete");
|
||||
if (rs1.next()) {
|
||||
|
||||
} else {
|
||||
String data = "ALTER TABLE `" + eco.getConfigHandler().getString("database.mysql.dataTableName") + "` ADD sync_complete varchar(5) NOT NULL DEFAULT 'true';";
|
||||
query1 = conn.prepareStatement(data);
|
||||
query1.execute();
|
||||
}
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
} finally {
|
||||
try {
|
||||
if (query1 != null) {
|
||||
query1.close();
|
||||
}
|
||||
if (rs1 != null) {
|
||||
rs1.close();
|
||||
}
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private void databaseMaintenanceTask() {
|
||||
if (eco.getConfigHandler().getBoolean("database.removeOldAccounts.enabled") == true) {
|
||||
Bukkit.getScheduler().runTaskLaterAsynchronously(eco, new Runnable() {
|
||||
|
||||
@Override
|
||||
public void run() {
|
||||
if (conn != null) {
|
||||
long inactivityDays = Long.parseLong(eco.getConfigHandler().getString("database.removeOldAccounts.inactivity"));
|
||||
long inactivityMils = inactivityDays * 24 * 60 * 60 * 1000;
|
||||
long curentTime = System.currentTimeMillis();
|
||||
long inactiveTime = curentTime - inactivityMils;
|
||||
Eco.log.info("Database maintenance task started...");
|
||||
PreparedStatement preparedStatement = null;
|
||||
try {
|
||||
String sql = "DELETE FROM `" + eco.getConfigHandler().getString("database.mysql.dataTableName") + "` WHERE `last_seen` < ?";
|
||||
preparedStatement = conn.prepareStatement(sql);
|
||||
preparedStatement.setString(1, String.valueOf(inactiveTime));
|
||||
preparedStatement.execute();
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
} finally {
|
||||
try {
|
||||
if (preparedStatement != null) {
|
||||
preparedStatement.close();
|
||||
}
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
Eco.log.info("Database maintenance complete!");
|
||||
}
|
||||
}
|
||||
|
||||
}, 100 * 20L);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,71 +0,0 @@
|
||||
package net.craftersland.eco.bridge.events;
|
||||
|
||||
import org.bukkit.Bukkit;
|
||||
import org.bukkit.entity.Player;
|
||||
import org.bukkit.event.EventHandler;
|
||||
import org.bukkit.event.Listener;
|
||||
import org.bukkit.event.player.PlayerJoinEvent;
|
||||
import org.bukkit.scheduler.BukkitTask;
|
||||
|
||||
import net.craftersland.eco.bridge.Eco;
|
||||
|
||||
public class PlayerJoin implements Listener {
|
||||
|
||||
private Eco eco;
|
||||
|
||||
public PlayerJoin(Eco eco) {
|
||||
this.eco = eco;
|
||||
}
|
||||
|
||||
@EventHandler
|
||||
public void onJoin(final PlayerJoinEvent event) {
|
||||
Bukkit.getScheduler().runTaskLaterAsynchronously(eco, new Runnable() {
|
||||
|
||||
@Override
|
||||
public void run() {
|
||||
if (event.getPlayer() != null) {
|
||||
if (event.getPlayer().isOnline() == true) {
|
||||
Player p = event.getPlayer();
|
||||
eco.getEcoDataHandler().onJoinFunction(p);
|
||||
syncCompleteTask(p);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}, 5L);
|
||||
}
|
||||
|
||||
private void syncCompleteTask(final Player p) {
|
||||
if (p != null) {
|
||||
if (p.isOnline() == true) {
|
||||
final long startTime = System.currentTimeMillis();
|
||||
BukkitTask task = Bukkit.getScheduler().runTaskTimerAsynchronously(eco, new Runnable() {
|
||||
|
||||
@Override
|
||||
public void run() {
|
||||
if (p.isOnline() == true) {
|
||||
if (eco.getEcoDataHandler().isSyncComplete(p)) {
|
||||
if (eco.syncCompleteTasks.containsKey(p) == true) {
|
||||
int taskID = eco.syncCompleteTasks.get(p);
|
||||
eco.syncCompleteTasks.remove(p);
|
||||
Bukkit.getScheduler().cancelTask(taskID);
|
||||
}
|
||||
} else {
|
||||
if (System.currentTimeMillis() - startTime >= 10 * 1000) {
|
||||
if (eco.syncCompleteTasks.containsKey(p) == true) {
|
||||
int taskID = eco.syncCompleteTasks.get(p);
|
||||
eco.syncCompleteTasks.remove(p);
|
||||
Bukkit.getScheduler().cancelTask(taskID);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}, 5L, 20L);
|
||||
eco.syncCompleteTasks.put(p, task.getTaskId());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,191 +0,0 @@
|
||||
package net.craftersland.eco.bridge.events.handlers;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashMap;
|
||||
import java.util.HashSet;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Set;
|
||||
|
||||
import org.bukkit.Bukkit;
|
||||
import org.bukkit.entity.Player;
|
||||
import org.bukkit.scheduler.BukkitTask;
|
||||
|
||||
import net.craftersland.eco.bridge.Eco;
|
||||
|
||||
public class EcoDataHandler {
|
||||
|
||||
private Eco eco;
|
||||
private Map<Player, Double> backupMoney = new HashMap<Player, Double>();
|
||||
private Map<Player, Double> balanceMap = new HashMap<Player, Double>();
|
||||
private Map<Player, Integer> runningTasks = new HashMap<Player, Integer>();
|
||||
private Set<Player> playersInSync = new HashSet<Player>();
|
||||
|
||||
public EcoDataHandler(Eco eco) {
|
||||
this.eco = eco;
|
||||
}
|
||||
|
||||
public void onShutDownDataSave() {
|
||||
Eco.log.info("Saving online players data...");
|
||||
List<Player> onlinePlayers = new ArrayList<Player>(Bukkit.getOnlinePlayers());
|
||||
|
||||
for (Player p : onlinePlayers) {
|
||||
if (p.isOnline() == true) {
|
||||
onDataSaveFunction(p, true, "true", true);
|
||||
}
|
||||
}
|
||||
Eco.log.info("Data save complete for " + onlinePlayers.size() + " players.");
|
||||
}
|
||||
|
||||
public void updateBalanceMap() {
|
||||
List<Player> onlinePlayers = new ArrayList<Player>(Bukkit.getOnlinePlayers());
|
||||
if (onlinePlayers.isEmpty() == false) {
|
||||
for (Player p : onlinePlayers) {
|
||||
if (playersInSync.contains(p) == true) {
|
||||
balanceMap.put(p, Eco.vault.getBalance(p));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public boolean isSyncComplete(Player p) {
|
||||
if (playersInSync.contains(p) == true) {
|
||||
return true;
|
||||
} else {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
private void dataCleanup(Player p, Boolean isDisabling) {
|
||||
if (isDisabling == false) {
|
||||
playersInSync.remove(p);
|
||||
backupMoney.remove(p);
|
||||
balanceMap.remove(p);
|
||||
if (runningTasks.containsKey(p) == true) {
|
||||
Bukkit.getScheduler().cancelTask(runningTasks.get(p));
|
||||
runningTasks.remove(p);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private void setPlayerData(final Player p, String[] data, boolean cancelTask) {
|
||||
try {
|
||||
Double bal = Eco.vault.getBalance(p);
|
||||
|
||||
if (bal != null) {
|
||||
if (bal != 0.0) {
|
||||
Eco.vault.withdrawPlayer(p, bal);
|
||||
}
|
||||
Double mysqlBal = Double.parseDouble(data[0]);
|
||||
Double localBal = Eco.vault.getBalance(p);
|
||||
if (mysqlBal >= localBal) {
|
||||
Double finalBalance = mysqlBal - localBal;
|
||||
Eco.vault.depositPlayer(p, finalBalance);
|
||||
} else if (mysqlBal < localBal) {
|
||||
Double finalBalance = localBal - mysqlBal;
|
||||
Eco.vault.withdrawPlayer(p, finalBalance);
|
||||
}
|
||||
}
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
Double backupBalance = backupMoney.get(p);
|
||||
if (backupBalance != 0.0) {
|
||||
Double bal = Eco.vault.getBalance(p);
|
||||
if (bal != null) {
|
||||
if (bal != 0.0) {
|
||||
Eco.vault.withdrawPlayer(p, bal);
|
||||
}
|
||||
Double localBal = Eco.vault.getBalance(p);
|
||||
if (backupBalance >= localBal) {
|
||||
Double finalBalance = backupBalance - localBal;
|
||||
Eco.vault.depositPlayer(p, finalBalance);
|
||||
} else if (backupBalance < localBal) {
|
||||
Double finalBalance = localBal - backupBalance;
|
||||
Eco.vault.depositPlayer(p, finalBalance);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
eco.getEcoMysqlHandler().setSyncStatus(p, "false");
|
||||
playersInSync.add(p);
|
||||
backupMoney.remove(p);
|
||||
if (cancelTask == true) {
|
||||
int taskID = runningTasks.get(p);
|
||||
runningTasks.remove(p);
|
||||
Bukkit.getScheduler().cancelTask(taskID);
|
||||
}
|
||||
}
|
||||
|
||||
public void onDataSaveFunction(Player p, Boolean datacleanup, String syncStatus, Boolean isDisabling) {
|
||||
boolean isPlayerInSync = playersInSync.contains(p);
|
||||
if (isDisabling == false) {
|
||||
if (datacleanup == true) {
|
||||
dataCleanup(p, isDisabling);
|
||||
}
|
||||
if (isPlayerInSync == true) {
|
||||
eco.getEcoMysqlHandler().setData(p, Eco.vault.getBalance(p), syncStatus);
|
||||
}
|
||||
} else {
|
||||
if (isPlayerInSync == true) {
|
||||
if (balanceMap.containsKey(p) == true) {
|
||||
eco.getEcoMysqlHandler().setData(p, balanceMap.get(p), syncStatus);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public void onJoinFunction(final Player p) {
|
||||
if (eco.getEcoMysqlHandler().hasAccount(p) == true) {
|
||||
final double balance = Eco.vault.getBalance(p);
|
||||
backupMoney.put(p, balance);
|
||||
Bukkit.getScheduler().runTask(eco, new Runnable() {
|
||||
|
||||
@Override
|
||||
public void run() {
|
||||
if (balance != 0.0) {
|
||||
Eco.vault.withdrawPlayer(p, balance);
|
||||
}
|
||||
String[] data = eco.getEcoMysqlHandler().getData(p);
|
||||
if (data[1].matches("true")) {
|
||||
setPlayerData(p, data, false);
|
||||
} else {
|
||||
final long taskStart = System.currentTimeMillis();
|
||||
BukkitTask task = Bukkit.getScheduler().runTaskTimerAsynchronously(eco, new Runnable() {
|
||||
|
||||
@Override
|
||||
public void run() {
|
||||
if (p.isOnline() == true) {
|
||||
final String[] data = eco.getEcoMysqlHandler().getData(p);
|
||||
Bukkit.getScheduler().runTask(eco, new Runnable() {
|
||||
|
||||
@Override
|
||||
public void run() {
|
||||
if (data[1].matches("true")) {
|
||||
setPlayerData(p, data, true);
|
||||
} else if (System.currentTimeMillis() - Long.parseLong(data[2]) >= 15 * 1000) {
|
||||
setPlayerData(p, data, true);
|
||||
}
|
||||
}
|
||||
|
||||
});
|
||||
}
|
||||
if (System.currentTimeMillis() - taskStart >= 10 * 1000) {
|
||||
int taskID = runningTasks.get(p);
|
||||
runningTasks.remove(p);
|
||||
Bukkit.getScheduler().cancelTask(taskID);
|
||||
}
|
||||
}
|
||||
|
||||
}, 20L, 20L);
|
||||
runningTasks.put(p, task.getTaskId());
|
||||
}
|
||||
}
|
||||
|
||||
});
|
||||
} else {
|
||||
playersInSync.add(p);
|
||||
onDataSaveFunction(p, false, "false", false);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
43
src/main/java/mviper/eco/bridge/BackgroundTask.java
Normal file
43
src/main/java/mviper/eco/bridge/BackgroundTask.java
Normal file
@@ -0,0 +1,43 @@
|
||||
package net.mviper.eco.bridge;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import org.bukkit.Bukkit;
|
||||
import org.bukkit.entity.Player;
|
||||
|
||||
public class BackgroundTask {
|
||||
|
||||
private Eco m;
|
||||
private long lastSave = System.currentTimeMillis();
|
||||
|
||||
public BackgroundTask(Eco m) {
|
||||
this.m = m;
|
||||
runTask();
|
||||
}
|
||||
|
||||
private void runTask() {
|
||||
Bukkit.getScheduler().runTaskTimerAsynchronously(m, () -> {
|
||||
m.getEcoDataHandler().updateBalanceMap();
|
||||
runSaveData();
|
||||
}, 20L, 20L);
|
||||
}
|
||||
|
||||
private void runSaveData() {
|
||||
if (m.getConfigHandler().getBoolean("General.saveDataTask.enabled")) {
|
||||
if (!Bukkit.getOnlinePlayers().isEmpty()) {
|
||||
if (System.currentTimeMillis() - lastSave >= m.getConfigHandler().getInteger("General.saveDataTask.interval") * 60 * 1000) {
|
||||
List<Player> onlinePlayers = new ArrayList<>(Bukkit.getOnlinePlayers());
|
||||
lastSave = System.currentTimeMillis();
|
||||
if (!m.getConfigHandler().getBoolean("General.saveDataTask.hideLogMessages")) {
|
||||
Eco.log.info("Synchronisiere Online-Spieler mit MySQL...");
|
||||
}
|
||||
for (Player p : onlinePlayers) {
|
||||
if (p.isOnline()) {
|
||||
m.getEcoDataHandler().onDataSaveFunction(p, false, "false", false);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,60 +1,60 @@
|
||||
package net.craftersland.eco.bridge;
|
||||
|
||||
import java.io.File;
|
||||
|
||||
public class ConfigHandler {
|
||||
|
||||
private Eco eco;
|
||||
|
||||
public ConfigHandler(Eco eco) {
|
||||
this.eco = eco;
|
||||
loadConfig();
|
||||
}
|
||||
|
||||
public void loadConfig() {
|
||||
File pluginFolder = new File("plugins" + System.getProperty("file.separator") + Eco.pluginName);
|
||||
if (pluginFolder.exists() == false) {
|
||||
pluginFolder.mkdir();
|
||||
}
|
||||
File configFile = new File("plugins" + System.getProperty("file.separator") + Eco.pluginName + System.getProperty("file.separator") + "config.yml");
|
||||
if (configFile.exists() == false) {
|
||||
Eco.log.info("No config file found! Creating new one...");
|
||||
eco.saveDefaultConfig();
|
||||
}
|
||||
try {
|
||||
Eco.log.info("Loading the config file...");
|
||||
eco.getConfig().load(configFile);
|
||||
} catch (Exception e) {
|
||||
Eco.log.severe("Could not load the config file! You need to regenerate the config! Error: " + e.getMessage());
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
public String getString(String key) {
|
||||
if (!eco.getConfig().contains(key)) {
|
||||
eco.getLogger().severe("Could not locate " + key + " in the config.yml inside of the " + Eco.pluginName + " folder! (Try generating a new one by deleting the current)");
|
||||
return "errorCouldNotLocateInConfigYml:" + key;
|
||||
} else {
|
||||
return eco.getConfig().getString(key);
|
||||
}
|
||||
}
|
||||
|
||||
public Integer getInteger(String key) {
|
||||
if (!eco.getConfig().contains(key)) {
|
||||
eco.getLogger().severe("Could not locate " + key + " in the config.yml inside of the " + Eco.pluginName + " folder! (Try generating a new one by deleting the current)");
|
||||
return null;
|
||||
} else {
|
||||
return eco.getConfig().getInt(key);
|
||||
}
|
||||
}
|
||||
|
||||
public Boolean getBoolean(String key) {
|
||||
if (!eco.getConfig().contains(key)) {
|
||||
eco.getLogger().severe("Could not locate " + key + " in the config.yml inside of the " + Eco.pluginName + " folder! (Try generating a new one by deleting the current)");
|
||||
return null;
|
||||
} else {
|
||||
return eco.getConfig().getBoolean(key);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
package net.mviper.eco.bridge;
|
||||
|
||||
import java.io.File;
|
||||
|
||||
public class ConfigHandler {
|
||||
|
||||
private Eco eco;
|
||||
|
||||
public ConfigHandler(Eco eco) {
|
||||
this.eco = eco;
|
||||
loadConfig();
|
||||
}
|
||||
|
||||
public void loadConfig() {
|
||||
File pluginFolder = new File("plugins" + System.getProperty("file.separator") + Eco.pluginName);
|
||||
if (pluginFolder.exists() == false) {
|
||||
pluginFolder.mkdir();
|
||||
}
|
||||
File configFile = new File("plugins" + System.getProperty("file.separator") + Eco.pluginName + System.getProperty("file.separator") + "config.yml");
|
||||
if (configFile.exists() == false) {
|
||||
Eco.log.info("No config file found! Creating new one...");
|
||||
eco.saveDefaultConfig();
|
||||
}
|
||||
try {
|
||||
Eco.log.info("Loading the config file...");
|
||||
eco.getConfig().load(configFile);
|
||||
} catch (Exception e) {
|
||||
Eco.log.severe("Could not load the config file! You need to regenerate the config! Error: " + e.getMessage());
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
public String getString(String key) {
|
||||
if (!eco.getConfig().contains(key)) {
|
||||
eco.getLogger().severe("Could not locate " + key + " in the config.yml inside of the " + Eco.pluginName + " folder! (Try generating a new one by deleting the current)");
|
||||
return "errorCouldNotLocateInConfigYml:" + key;
|
||||
} else {
|
||||
return eco.getConfig().getString(key);
|
||||
}
|
||||
}
|
||||
|
||||
public Integer getInteger(String key) {
|
||||
if (!eco.getConfig().contains(key)) {
|
||||
eco.getLogger().severe("Could not locate " + key + " in the config.yml inside of the " + Eco.pluginName + " folder! (Try generating a new one by deleting the current)");
|
||||
return null;
|
||||
} else {
|
||||
return eco.getConfig().getInt(key);
|
||||
}
|
||||
}
|
||||
|
||||
public Boolean getBoolean(String key) {
|
||||
if (!eco.getConfig().contains(key)) {
|
||||
eco.getLogger().severe("Could not locate " + key + " in the config.yml inside of the " + Eco.pluginName + " folder! (Try generating a new one by deleting the current)");
|
||||
return null;
|
||||
} else {
|
||||
return eco.getConfig().getBoolean(key);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
77
src/main/java/mviper/eco/bridge/Eco.java
Normal file
77
src/main/java/mviper/eco/bridge/Eco.java
Normal file
@@ -0,0 +1,77 @@
|
||||
package net.mviper.eco.bridge;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
import java.util.logging.Logger;
|
||||
|
||||
import net.mviper.eco.bridge.database.EcoMysqlHandler;
|
||||
import net.mviper.eco.bridge.database.MysqlSetup;
|
||||
import net.mviper.eco.bridge.events.PlayerDisconnect;
|
||||
import net.mviper.eco.bridge.events.PlayerJoin;
|
||||
import net.mviper.eco.bridge.events.handlers.EcoDataHandler;
|
||||
import net.milkbowl.vault.economy.Economy;
|
||||
|
||||
import org.bukkit.Bukkit;
|
||||
import org.bukkit.entity.Player;
|
||||
import org.bukkit.event.HandlerList;
|
||||
import org.bukkit.plugin.PluginManager;
|
||||
import org.bukkit.plugin.RegisteredServiceProvider;
|
||||
import org.bukkit.plugin.java.JavaPlugin;
|
||||
|
||||
public class Eco extends JavaPlugin {
|
||||
|
||||
public static Logger log;
|
||||
public static Economy vault = null;
|
||||
public static String pluginName = "MysqlEcoBridge";
|
||||
public Map<Player, Integer> syncCompleteTasks = new HashMap<Player, Integer>();
|
||||
|
||||
private static ConfigHandler configHandler;
|
||||
private static MysqlSetup mysqlSetup;
|
||||
private static EcoMysqlHandler ecoMysqlHandler;
|
||||
private static EcoDataHandler edH;
|
||||
private static BackgroundTask bt;
|
||||
|
||||
@Override
|
||||
public void onEnable() {
|
||||
log = getLogger();
|
||||
if (!setupEconomy()) {
|
||||
log.severe(String.format("[%s] - Deaktiviert aufgrund fehlender Vault-Abhängigkeit!", getDescription().getName()));
|
||||
getServer().getPluginManager().disablePlugin(this);
|
||||
return;
|
||||
}
|
||||
configHandler = new ConfigHandler(this);
|
||||
mysqlSetup = new MysqlSetup(this);
|
||||
ecoMysqlHandler = new EcoMysqlHandler(this);
|
||||
edH = new EcoDataHandler(this);
|
||||
bt = new BackgroundTask(this);
|
||||
|
||||
PluginManager pm = getServer().getPluginManager();
|
||||
pm.registerEvents(new PlayerJoin(this), this);
|
||||
pm.registerEvents(new PlayerDisconnect(this), this);
|
||||
log.info(pluginName + " erfolgreich geladen!");
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onDisable() {
|
||||
Bukkit.getScheduler().cancelTasks(this);
|
||||
HandlerList.unregisterAll(this);
|
||||
if (mysqlSetup != null && mysqlSetup.getConnection() != null) {
|
||||
edH.onShutDownDataSave();
|
||||
mysqlSetup.closeConnection();
|
||||
}
|
||||
log.info(pluginName + " wurde deaktiviert!");
|
||||
}
|
||||
|
||||
private boolean setupEconomy() {
|
||||
if (getServer().getPluginManager().getPlugin("Vault") == null) return false;
|
||||
RegisteredServiceProvider<Economy> rsp = getServer().getServicesManager().getRegistration(Economy.class);
|
||||
if (rsp == null) return false;
|
||||
vault = rsp.getProvider();
|
||||
return vault != null;
|
||||
}
|
||||
|
||||
public ConfigHandler getConfigHandler() { return configHandler; }
|
||||
public MysqlSetup getMysqlSetup() { return mysqlSetup; }
|
||||
public EcoMysqlHandler getEcoMysqlHandler() { return ecoMysqlHandler; }
|
||||
public EcoDataHandler getEcoDataHandler() { return edH; }
|
||||
}
|
||||
@@ -0,0 +1,86 @@
|
||||
package net.mviper.eco.bridge.database;
|
||||
|
||||
import java.sql.Connection;
|
||||
import java.sql.PreparedStatement;
|
||||
import java.sql.ResultSet;
|
||||
import java.sql.SQLException;
|
||||
|
||||
import org.bukkit.entity.Player;
|
||||
|
||||
import net.mviper.eco.bridge.Eco;
|
||||
|
||||
public class EcoMysqlHandler {
|
||||
|
||||
private Eco eco;
|
||||
|
||||
public EcoMysqlHandler(Eco eco) {
|
||||
this.eco = eco;
|
||||
}
|
||||
|
||||
public String[] getData(Player player) {
|
||||
if (!hasAccount(player)) {
|
||||
createAccount(player);
|
||||
}
|
||||
Connection conn = eco.getMysqlSetup().getConnection();
|
||||
if (conn != null) {
|
||||
try (PreparedStatement pst = conn.prepareStatement("SELECT * FROM `" + eco.getConfigHandler().getString("database.mysql.dataTableName") + "` WHERE `player_uuid` = ? LIMIT 1")) {
|
||||
pst.setString(1, player.getUniqueId().toString());
|
||||
try (ResultSet rs = pst.executeQuery()) {
|
||||
if (rs.next()) {
|
||||
return new String[]{String.valueOf(rs.getDouble("money")), rs.getString("sync_complete"), rs.getString("last_seen")};
|
||||
}
|
||||
}
|
||||
} catch (SQLException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
return new String[]{"0.0", "true", String.valueOf(System.currentTimeMillis())};
|
||||
}
|
||||
|
||||
public void setData(Player player, double money, String syncComplete) {
|
||||
Connection conn = eco.getMysqlSetup().getConnection();
|
||||
if (conn != null) {
|
||||
String sql = "UPDATE `" + eco.getConfigHandler().getString("database.mysql.dataTableName") + "` SET `money` = ?, `sync_complete` = ?, `last_seen` = ? WHERE `player_uuid` = ?";
|
||||
try (PreparedStatement pst = conn.prepareStatement(sql)) {
|
||||
pst.setDouble(1, money);
|
||||
pst.setString(2, syncComplete);
|
||||
pst.setString(3, String.valueOf(System.currentTimeMillis()));
|
||||
pst.setString(4, player.getUniqueId().toString());
|
||||
pst.executeUpdate();
|
||||
} catch (SQLException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public boolean hasAccount(Player player) {
|
||||
Connection conn = eco.getMysqlSetup().getConnection();
|
||||
if (conn != null) {
|
||||
try (PreparedStatement pst = conn.prepareStatement("SELECT `player_uuid` FROM `" + eco.getConfigHandler().getString("database.mysql.dataTableName") + "` WHERE `player_uuid` = ? LIMIT 1")) {
|
||||
pst.setString(1, player.getUniqueId().toString());
|
||||
try (ResultSet rs = pst.executeQuery()) {
|
||||
return rs.next();
|
||||
}
|
||||
} catch (SQLException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
private void createAccount(Player player) {
|
||||
Connection conn = eco.getMysqlSetup().getConnection();
|
||||
if (conn != null) {
|
||||
try (PreparedStatement pst = conn.prepareStatement("INSERT INTO `" + eco.getConfigHandler().getString("database.mysql.dataTableName") + "` (player_uuid, player_name, money, sync_complete, last_seen) VALUES (?, ?, ?, ?, ?)")) {
|
||||
pst.setString(1, player.getUniqueId().toString());
|
||||
pst.setString(2, player.getName());
|
||||
pst.setDouble(3, 0.0);
|
||||
pst.setString(4, "true");
|
||||
pst.setString(5, String.valueOf(System.currentTimeMillis()));
|
||||
pst.executeUpdate();
|
||||
} catch (SQLException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
62
src/main/java/mviper/eco/bridge/database/MysqlSetup.java
Normal file
62
src/main/java/mviper/eco/bridge/database/MysqlSetup.java
Normal file
@@ -0,0 +1,62 @@
|
||||
package net.mviper.eco.bridge.database;
|
||||
|
||||
import java.sql.Connection;
|
||||
import java.sql.DriverManager;
|
||||
import java.sql.PreparedStatement;
|
||||
import java.sql.SQLException;
|
||||
import java.util.Properties;
|
||||
import net.mviper.eco.bridge.Eco;
|
||||
import org.bukkit.Bukkit;
|
||||
|
||||
public class MysqlSetup {
|
||||
|
||||
private Connection conn = null;
|
||||
private Eco eco;
|
||||
|
||||
public MysqlSetup(Eco eco) {
|
||||
this.eco = eco;
|
||||
connectToDatabase();
|
||||
setupDatabase();
|
||||
}
|
||||
|
||||
public void connectToDatabase() {
|
||||
try {
|
||||
Class.forName("com.mysql.jdbc.Driver");
|
||||
Properties props = new Properties();
|
||||
props.setProperty("user", eco.getConfigHandler().getString("database.mysql.user"));
|
||||
props.setProperty("password", eco.getConfigHandler().getString("database.mysql.password"));
|
||||
props.setProperty("autoReconnect", "true");
|
||||
props.setProperty("useSSL", eco.getConfigHandler().getString("database.mysql.sslEnabled"));
|
||||
|
||||
String url = "jdbc:mysql://" + eco.getConfigHandler().getString("database.mysql.host") + ":" +
|
||||
eco.getConfigHandler().getString("database.mysql.port") + "/" +
|
||||
eco.getConfigHandler().getString("database.mysql.databaseName");
|
||||
conn = DriverManager.getConnection(url, props);
|
||||
} catch (Exception e) {
|
||||
Eco.log.severe("MySQL Verbindung fehlgeschlagen: " + e.getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
private void setupDatabase() {
|
||||
if (conn != null) {
|
||||
String query = "CREATE TABLE IF NOT EXISTS `" + eco.getConfigHandler().getString("database.mysql.dataTableName") + "` (" +
|
||||
"`id` int(11) NOT NULL AUTO_INCREMENT," +
|
||||
"`player_uuid` varchar(45) NOT NULL," +
|
||||
"`player_name` varchar(16) NOT NULL," +
|
||||
"`money` double NOT NULL DEFAULT '0'," +
|
||||
"`sync_complete` varchar(5) NOT NULL DEFAULT 'true'," +
|
||||
"`last_seen` varchar(30) NOT NULL," +
|
||||
"PRIMARY KEY (`id`), UNIQUE KEY `player_uuid_UNIQUE` (`player_uuid`)) ENGINE=InnoDB DEFAULT CHARSET=utf8;";
|
||||
try (PreparedStatement pst = conn.prepareStatement(query)) {
|
||||
pst.executeUpdate();
|
||||
} catch (SQLException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public Connection getConnection() { return conn; }
|
||||
public void closeConnection() {
|
||||
try { if (conn != null) conn.close(); } catch (SQLException e) { e.printStackTrace(); }
|
||||
}
|
||||
}
|
||||
@@ -1,42 +1,42 @@
|
||||
package net.craftersland.eco.bridge.events;
|
||||
|
||||
import org.bukkit.Bukkit;
|
||||
import org.bukkit.entity.Player;
|
||||
import org.bukkit.event.EventHandler;
|
||||
import org.bukkit.event.Listener;
|
||||
import org.bukkit.event.player.PlayerQuitEvent;
|
||||
|
||||
import net.craftersland.eco.bridge.Eco;
|
||||
|
||||
public class PlayerDisconnect implements Listener {
|
||||
|
||||
private Eco eco;
|
||||
|
||||
public PlayerDisconnect(Eco eco) {
|
||||
this.eco = eco;
|
||||
}
|
||||
|
||||
@EventHandler
|
||||
public void onDisconnect(final PlayerQuitEvent event) {
|
||||
Bukkit.getScheduler().runTaskLaterAsynchronously(eco, new Runnable() {
|
||||
|
||||
@Override
|
||||
public void run() {
|
||||
if (event.getPlayer() != null) {
|
||||
Player p = event.getPlayer();
|
||||
cleanup(p);
|
||||
eco.getEcoDataHandler().onDataSaveFunction(p, true, "true", false);
|
||||
}
|
||||
}
|
||||
|
||||
}, 1L);
|
||||
}
|
||||
|
||||
private void cleanup(Player p) {
|
||||
if (eco.syncCompleteTasks.containsKey(p) == true) {
|
||||
Bukkit.getScheduler().cancelTask(eco.syncCompleteTasks.get(p));
|
||||
eco.syncCompleteTasks.remove(p);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
package net.mviper.eco.bridge.events;
|
||||
|
||||
import org.bukkit.Bukkit;
|
||||
import org.bukkit.entity.Player;
|
||||
import org.bukkit.event.EventHandler;
|
||||
import org.bukkit.event.Listener;
|
||||
import org.bukkit.event.player.PlayerQuitEvent;
|
||||
|
||||
import net.mviper.eco.bridge.Eco;
|
||||
|
||||
public class PlayerDisconnect implements Listener {
|
||||
|
||||
private Eco eco;
|
||||
|
||||
public PlayerDisconnect(Eco eco) {
|
||||
this.eco = eco;
|
||||
}
|
||||
|
||||
@EventHandler
|
||||
public void onDisconnect(final PlayerQuitEvent event) {
|
||||
Bukkit.getScheduler().runTaskLaterAsynchronously(eco, new Runnable() {
|
||||
|
||||
@Override
|
||||
public void run() {
|
||||
if (event.getPlayer() != null) {
|
||||
Player p = event.getPlayer();
|
||||
cleanup(p);
|
||||
eco.getEcoDataHandler().onDataSaveFunction(p, true, "true", false);
|
||||
}
|
||||
}
|
||||
|
||||
}, 1L);
|
||||
}
|
||||
|
||||
private void cleanup(Player p) {
|
||||
if (eco.syncCompleteTasks.containsKey(p) == true) {
|
||||
Bukkit.getScheduler().cancelTask(eco.syncCompleteTasks.get(p));
|
||||
eco.syncCompleteTasks.remove(p);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
21
src/main/java/mviper/eco/bridge/events/PlayerJoin.java
Normal file
21
src/main/java/mviper/eco/bridge/events/PlayerJoin.java
Normal file
@@ -0,0 +1,21 @@
|
||||
package net.mviper.eco.bridge.events;
|
||||
|
||||
import net.mviper.eco.bridge.Eco;
|
||||
import org.bukkit.Bukkit;
|
||||
import org.bukkit.event.EventHandler;
|
||||
import org.bukkit.event.Listener;
|
||||
import org.bukkit.event.player.PlayerJoinEvent;
|
||||
|
||||
public class PlayerJoin implements Listener {
|
||||
private Eco eco;
|
||||
public PlayerJoin(Eco eco) { this.eco = eco; }
|
||||
|
||||
@EventHandler
|
||||
public void onJoin(PlayerJoinEvent event) {
|
||||
Bukkit.getScheduler().runTaskLaterAsynchronously(eco, () -> {
|
||||
if (event.getPlayer().isOnline()) {
|
||||
eco.getEcoDataHandler().onJoinFunction(event.getPlayer());
|
||||
}
|
||||
}, 5L);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,107 @@
|
||||
package net.mviper.eco.bridge.events.handlers;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashMap;
|
||||
import java.util.HashSet;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Set;
|
||||
|
||||
import org.bukkit.Bukkit;
|
||||
import org.bukkit.entity.Player;
|
||||
import org.bukkit.scheduler.BukkitTask;
|
||||
|
||||
import net.mviper.eco.bridge.Eco;
|
||||
|
||||
public class EcoDataHandler {
|
||||
|
||||
private Eco eco;
|
||||
private Map<Player, Double> backupMoney = new HashMap<Player, Double>();
|
||||
private Map<Player, Double> balanceMap = new HashMap<Player, Double>();
|
||||
private Map<Player, Integer> runningTasks = new HashMap<Player, Integer>();
|
||||
private Set<Player> playersInSync = new HashSet<Player>();
|
||||
|
||||
public EcoDataHandler(Eco eco) {
|
||||
this.eco = eco;
|
||||
}
|
||||
|
||||
public void onShutDownDataSave() {
|
||||
Eco.log.info("Speichere Daten aller Online-Spieler...");
|
||||
List<Player> onlinePlayers = new ArrayList<Player>(Bukkit.getOnlinePlayers());
|
||||
for (Player p : onlinePlayers) {
|
||||
if (p.isOnline()) {
|
||||
onDataSaveFunction(p, true, "true", true);
|
||||
}
|
||||
}
|
||||
Eco.log.info("Speicherung abgeschlossen für " + onlinePlayers.size() + " Spieler.");
|
||||
}
|
||||
|
||||
public void onJoinFunction(final Player p) {
|
||||
playersInSync.remove(p);
|
||||
final double balance = Eco.vault.getBalance(p);
|
||||
backupMoney.put(p, balance);
|
||||
|
||||
Bukkit.getScheduler().runTask(eco, () -> {
|
||||
if (balance != 0.0) {
|
||||
Eco.vault.withdrawPlayer(p, balance);
|
||||
}
|
||||
String[] data = eco.getEcoMysqlHandler().getData(p);
|
||||
if (data[1].equalsIgnoreCase("true")) {
|
||||
setPlayerData(p, data, false);
|
||||
} else {
|
||||
startSyncTask(p);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
private void startSyncTask(final Player p) {
|
||||
final long taskStart = System.currentTimeMillis();
|
||||
BukkitTask task = Bukkit.getScheduler().runTaskTimerAsynchronously(eco, () -> {
|
||||
if (p.isOnline()) {
|
||||
final String[] data = eco.getEcoMysqlHandler().getData(p);
|
||||
Bukkit.getScheduler().runTask(eco, () -> {
|
||||
if (data[1].equalsIgnoreCase("true") || (System.currentTimeMillis() - Long.parseLong(data[2]) >= 15 * 1000)) {
|
||||
setPlayerData(p, data, true);
|
||||
}
|
||||
});
|
||||
}
|
||||
}, 10L, 20L);
|
||||
runningTasks.put(p, task.getTaskId());
|
||||
}
|
||||
|
||||
public void setPlayerData(Player p, String[] data, boolean cancelTask) {
|
||||
if (cancelTask && runningTasks.containsKey(p)) {
|
||||
Bukkit.getScheduler().cancelTask(runningTasks.get(p));
|
||||
runningTasks.remove(p);
|
||||
}
|
||||
double money = Double.parseDouble(data[0]);
|
||||
Eco.vault.depositPlayer(p, money);
|
||||
playersInSync.add(p);
|
||||
balanceMap.put(p, money);
|
||||
backupMoney.remove(p);
|
||||
}
|
||||
|
||||
public void onDataSaveFunction(Player p, boolean removeData, String syncStatus, boolean isShutdown) {
|
||||
if (playersInSync.contains(p)) {
|
||||
double currentBalance = Eco.vault.getBalance(p);
|
||||
eco.getEcoMysqlHandler().setData(p, currentBalance, syncStatus);
|
||||
if (removeData) {
|
||||
playersInSync.remove(p);
|
||||
balanceMap.remove(p);
|
||||
}
|
||||
} else if (backupMoney.containsKey(p)) {
|
||||
Eco.vault.depositPlayer(p, backupMoney.get(p));
|
||||
backupMoney.remove(p);
|
||||
}
|
||||
}
|
||||
|
||||
public boolean isSyncComplete(Player p) {
|
||||
return playersInSync.contains(p);
|
||||
}
|
||||
|
||||
public void updateBalanceMap() {
|
||||
for (Player p : playersInSync) {
|
||||
balanceMap.put(p, Eco.vault.getBalance(p));
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,39 +1,59 @@
|
||||
# default config.yml for MysqlEcoBridge
|
||||
# config version 3.4.1
|
||||
# +-----------------------------------------------------------------------+
|
||||
# | MySQL Eco Bridge 3 |
|
||||
# | Version: 3.6 | by M-Viper |
|
||||
# +-----------------------------------------------------------------------+
|
||||
|
||||
#MySQL Database details
|
||||
# -------------------------------------------------------------------------
|
||||
# DATENBANK-EINSTELLUNGEN
|
||||
# -------------------------------------------------------------------------
|
||||
# Hier konfigurierst du die Verbindung zu deinem MySQL/MariaDB Server.
|
||||
# HINWEIS: Die Datenbank muss bereits existieren, Tabellen werden erstellt.
|
||||
database:
|
||||
mysql:
|
||||
#MySQL server address
|
||||
host: 127.0.0.1
|
||||
#MySQL server port (default 3306)
|
||||
port: 3306
|
||||
#Database name (NOTE! You need to create the database, then the plugin will create the tables.)
|
||||
databaseName: 'mydatabase'
|
||||
#Tables name (the plugin will auto create it)
|
||||
dataTableName: 'eco_accounts'
|
||||
#User name
|
||||
user: 'admin'
|
||||
#User password
|
||||
password: 'cheesecake'
|
||||
#SSL connection.
|
||||
sslEnabled: false
|
||||
#This maintenance task runs async with a 2 min delay after the server starts.
|
||||
removeOldAccounts:
|
||||
#Enable or disable database clean up of old accounts. | (true or false)
|
||||
enabled: false
|
||||
#Inactivity in days. Default 60 days.
|
||||
inactivity: 60
|
||||
|
||||
|
||||
#Other configurable options
|
||||
mysql:
|
||||
host: 127.0.0.1
|
||||
port: 3306
|
||||
databaseName: 'mydatabase'
|
||||
dataTableName: 'eco_accounts'
|
||||
user: 'admin'
|
||||
password: 'password123'
|
||||
# SSL verschlüsselt die Verbindung zwischen Server und Datenbank.
|
||||
sslEnabled: false
|
||||
|
||||
# -----------------------------------------------------------------------
|
||||
# DATENBANK-WARTUNG (CLEANUP)
|
||||
# -----------------------------------------------------------------------
|
||||
# Diese Aufgabe läuft automatisch im Hintergrund (2 Min. nach Start).
|
||||
# Sie hilft dabei, die Datenbank schlank zu halten.
|
||||
removeOldAccounts:
|
||||
# Sollen inaktive Accounts automatisch gelöscht werden?
|
||||
enabled: false
|
||||
# Ab wie vielen Tagen Inaktivität gilt ein Account als veraltet?
|
||||
inactivity: 60
|
||||
|
||||
# -------------------------------------------------------------------------
|
||||
# ALLGEMEINE PLUGIN-OPTIONEN
|
||||
# -------------------------------------------------------------------------
|
||||
General:
|
||||
#Save online players accounts to database task. Disable this task and data will only be saved when the player disconnects.
|
||||
#To limit data loss in case the server crashes enable this saving task. It runs async so there will be no lag involved.
|
||||
saveDataTask:
|
||||
#Enable or disable the data save task. | (true or false)
|
||||
enabled: true
|
||||
#Time between data saves in minutes. | Default 3 min.
|
||||
interval: 3
|
||||
#Hide the data save task log messages.
|
||||
hideLogMessages: false
|
||||
# BACKUP-SYSTEM (AUTOSAVE)
|
||||
# Verhindert Datenverlust bei Serverabstürzen. Wenn deaktiviert, wird
|
||||
# das Geld NUR beim Verlassen des Servers (Disconnect) gespeichert.
|
||||
saveDataTask:
|
||||
# Automatisches Speichern im Hintergrund aktivieren?
|
||||
enabled: true
|
||||
# Intervall in Minuten (Standard: 3).
|
||||
interval: 3
|
||||
# Wenn true, werden die "Saving online players..." Nachrichten in der
|
||||
# Konsole unterdrückt, um den Log sauber zu halten.
|
||||
hideLogMessages: false
|
||||
|
||||
# -------------------------------------------------------------------------
|
||||
# NACHRICHTEN & FEEDBACK (Optional/Erweiterbar)
|
||||
# -------------------------------------------------------------------------
|
||||
ChatMessages:
|
||||
# Nachricht, die gesendet wird, wenn die Synchronisation abgeschlossen ist.
|
||||
# Nutze "" um die Nachricht zu deaktivieren. Farbcodes mit & möglich.
|
||||
syncComplete: "&a[Eco] &7Deine Kontodaten wurden erfolgreich synchronisiert!"
|
||||
|
||||
# +-----------------------------------------------------------------------+
|
||||
# | HINWEIS: Änderungen werden erst nach einem Server-Neustart aktiv. |
|
||||
# +-----------------------------------------------------------------------+
|
||||
@@ -1,7 +1,9 @@
|
||||
name: MysqlEcoBridge
|
||||
main: net.craftersland.eco.bridge.Eco
|
||||
version: 3.5
|
||||
main: net.mviper.eco.bridge.Eco
|
||||
version: 3.6
|
||||
api-version: 1.15
|
||||
depend: [Vault]
|
||||
author: M_Viper
|
||||
website: www.m-viper.de
|
||||
load: STARTUP
|
||||
description: Sync your economy accounts over many servers using MySQL.
|
||||
description: Synchronisiert Vault-Economy-Accounts über mehrere Server hinweg via MySQL.
|
||||
Reference in New Issue
Block a user