diff --git a/src/main/java/me/chrommob/minestore/MineStore.java b/src/main/java/me/chrommob/minestore/MineStore.java index 0f15270..b93b39e 100644 --- a/src/main/java/me/chrommob/minestore/MineStore.java +++ b/src/main/java/me/chrommob/minestore/MineStore.java @@ -6,11 +6,15 @@ import io.netty.channel.EventLoopGroup; import io.netty.channel.nio.NioEventLoopGroup; import io.netty.channel.socket.nio.NioServerSocketChannel; +import lombok.Getter; import me.chrommob.minestore.commands.Buy; import me.chrommob.minestore.commands.Reload; import me.chrommob.minestore.commands.Store; import me.chrommob.minestore.data.Config; import me.chrommob.minestore.gui.Event; +import me.chrommob.minestore.mysql.MySQLData; +import me.chrommob.minestore.mysql.connection.ConnectionPool; +import me.chrommob.minestore.mysql.data.UserManager; import me.chrommob.minestore.placeholders.PlaceholderHook; import me.chrommob.minestore.commands.PunishmentManager; import me.chrommob.minestore.commandexecution.JoinQuitListener; @@ -22,7 +26,10 @@ import org.bukkit.plugin.PluginManager; import org.bukkit.plugin.java.JavaPlugin; + public final class MineStore extends JavaPlugin { + @Getter + UserManager userManager; public static MineStore instance; Mode mode = Mode.WEBSOCKET; @@ -33,10 +40,10 @@ public void onLoad() { @Override public void onEnable() { + instance = this; Metrics metrics = new Metrics(this, 14043); dependencyCheck(); PluginManager plManager = Bukkit.getPluginManager(); - instance = this; PaperCommandManager manager = new PaperCommandManager(this); manager.registerCommand(new Reload()); plManager.registerEvents(new JoinQuitListener(), this); @@ -49,6 +56,7 @@ public void onEnable() { } else { mode = Mode.WEBLISTENER; } + MySQLData.setEnabled(getConfig().getBoolean("mysql.enabled")); Config.setStoreEnabled(getConfig().getBoolean("store-enabled")); Config.setPort(getConfig().getInt("port")); Config.setGuiEnabled(getConfig().getBoolean("gui-enabled")); @@ -70,6 +78,11 @@ public void onEnable() { if (Config.isStoreEnabled()) { manager.registerCommand(new Store()); } + if (MySQLData.isEnabled() && Config.isVaultPresent()) { + new ConnectionPool(this); + ConnectionPool.createTable(); + userManager = new UserManager(); + } } @Override @@ -85,6 +98,13 @@ private void dependencyCheck() { Config.setPlaceholderPresent(true); new PlaceholderHook(this).register(); } + if (Bukkit.getPluginManager().getPlugin("Vault") == null) { + Bukkit.getLogger().info("[MineStore] Vault found!"); + Config.setVaultPresent(false); + } + else { + Config.setVaultPresent(true); + } } private void NettyServer(int port) throws Exception @@ -105,6 +125,11 @@ private void NettyServer(int port) throws Exception public void loadConfig() { reloadConfig(); + MySQLData.setIp(getConfig().getString("mysql.ip")); + MySQLData.setPort(getConfig().getInt("mysql.port")); + MySQLData.setUser(getConfig().getString("mysql.username")); + MySQLData.setPassword(getConfig().getString("mysql.password")); + MySQLData.setDatabase(getConfig().getString("mysql.database")); Config.setPassword(getConfig().getString("password")); Config.setApiUrl(getConfig().getString("store-api")); Config.setStoreMessage(getConfig().getString("store-message")); diff --git a/src/main/java/me/chrommob/minestore/commandexecution/JoinQuitListener.java b/src/main/java/me/chrommob/minestore/commandexecution/JoinQuitListener.java index c371373..c29fb01 100644 --- a/src/main/java/me/chrommob/minestore/commandexecution/JoinQuitListener.java +++ b/src/main/java/me/chrommob/minestore/commandexecution/JoinQuitListener.java @@ -1,19 +1,25 @@ package me.chrommob.minestore.commandexecution; +import me.chrommob.minestore.MineStore; import me.chrommob.minestore.commands.PunishmentManager; +import me.chrommob.minestore.data.Config; +import me.chrommob.minestore.mysql.MySQLData; +import me.chrommob.minestore.mysql.data.UserManager; import org.bukkit.Bukkit; import org.bukkit.event.EventHandler; import org.bukkit.event.Listener; import org.bukkit.event.player.PlayerJoinEvent; +import java.util.UUID; + import static me.chrommob.minestore.commandexecution.Command.runLater; public class JoinQuitListener implements Listener { @EventHandler public void onPlayerJoin(final PlayerJoinEvent event) { - String name = event.getPlayer().getName(); try { + String name = event.getPlayer().getName(); if (runLater.get(name).isEmpty()) { runLater.remove(name); } else { @@ -26,6 +32,18 @@ public void onPlayerJoin(final PlayerJoinEvent event) { } } catch (Exception ignored) { } + if (Config.isVaultPresent() && MySQLData.isEnabled()) { + String name = event.getPlayer().getName(); + UUID uuid = event.getPlayer().getUniqueId(); + MineStore.instance.getUserManager().createProfile(uuid, name); + } + } + @EventHandler + public void onPlayerQuit(final PlayerJoinEvent event) { + if (Config.isVaultPresent() && MySQLData.isEnabled()) { + String name = event.getPlayer().getName(); + UUID uuid = event.getPlayer().getUniqueId(); + MineStore.instance.getUserManager().removeProfile(uuid); + } } - } diff --git a/src/main/java/me/chrommob/minestore/commands/Reload.java b/src/main/java/me/chrommob/minestore/commands/Reload.java index 42161fc..189d080 100644 --- a/src/main/java/me/chrommob/minestore/commands/Reload.java +++ b/src/main/java/me/chrommob/minestore/commands/Reload.java @@ -14,7 +14,7 @@ public class Reload extends BaseCommand { private void onReload(CommandSender sender) { try { MineStore.instance.loadConfig(); - sender.sendMessage("§aConfig reloaded! Be aware reloading doesn't reload the mode, websocket port, gui toggle and store toggle."); + sender.sendMessage("§aConfig reloaded! Be aware reloading doesn't reload the mode, websocket port, gui toggle, mysql toggle and store toggle."); } catch (Exception e) { sender.sendMessage("§cAn error occurred while reloading the config."); e.printStackTrace(); diff --git a/src/main/java/me/chrommob/minestore/data/Config.java b/src/main/java/me/chrommob/minestore/data/Config.java index 42ea3fb..8e0216c 100644 --- a/src/main/java/me/chrommob/minestore/data/Config.java +++ b/src/main/java/me/chrommob/minestore/data/Config.java @@ -64,4 +64,9 @@ public class Config { @Setter @Getter private static boolean placeholderPresent; + + @Setter + @Getter + private static boolean vaultPresent; + } \ No newline at end of file diff --git a/src/main/java/me/chrommob/minestore/mysql/MySQLData.java b/src/main/java/me/chrommob/minestore/mysql/MySQLData.java new file mode 100644 index 0000000..c5a66c6 --- /dev/null +++ b/src/main/java/me/chrommob/minestore/mysql/MySQLData.java @@ -0,0 +1,30 @@ +package me.chrommob.minestore.mysql; + +import lombok.Getter; +import lombok.Setter; + +public class MySQLData { + @Getter + @Setter + private static boolean enabled; + + @Getter + @Setter + private static String ip; + + @Getter + @Setter + private static int port; + + @Getter + @Setter + private static String database; + + @Getter + @Setter + private static String user; + + @Getter + @Setter + private static String password; +} diff --git a/src/main/java/me/chrommob/minestore/mysql/connection/ConnectionPool.java b/src/main/java/me/chrommob/minestore/mysql/connection/ConnectionPool.java new file mode 100644 index 0000000..0857386 --- /dev/null +++ b/src/main/java/me/chrommob/minestore/mysql/connection/ConnectionPool.java @@ -0,0 +1,174 @@ +package me.chrommob.minestore.mysql.connection; + +import com.zaxxer.hikari.HikariConfig; +import com.zaxxer.hikari.HikariDataSource; +import me.chrommob.minestore.MineStore; +import me.chrommob.minestore.mysql.MySQLData; +import me.chrommob.minestore.mysql.data.User; +import org.bukkit.Bukkit; +import org.bukkit.configuration.file.FileConfiguration; + +import java.sql.Connection; +import java.sql.PreparedStatement; +import java.sql.ResultSet; +import java.sql.SQLException; +import java.util.Map; +import java.util.UUID; + +public class ConnectionPool { + private static MineStore plugin; + + private static HikariDataSource hikari; + + private String hostname; + private String port; + public static String database; + private String username; + private String password; + + private int minimumConnections; + private int maximumConnections; + private long connectionTimeout; + + public ConnectionPool(MineStore plugin) { + this.plugin = plugin; + init(); + setupPool(); + } + + private void init() { + FileConfiguration fileConfig = plugin.getConfig(); + + this.username = MySQLData.getUser(); + this.password = MySQLData.getPassword(); + this.database = MySQLData.getDatabase(); + this.hostname = MySQLData.getIp(); + this.port = String.valueOf(MySQLData.getPort()); + + this.minimumConnections = 5; + this.maximumConnections = 100; + this.connectionTimeout = 2000; + } + + private void setupPool() { + HikariConfig config = new HikariConfig(); + config.setJdbcUrl( + "jdbc:mysql://" + + hostname + + ":" + + port + + "/" + + database + + "?useUnicode=true&useJDBCCompliantTimezoneShift=true&useLegacyDatetimeCode=false&serverTimezone=UTC" + ); + config.setDriverClassName("com.mysql.jdbc.Driver"); + config.setUsername(username); + config.setPassword(password); + config.setMinimumIdle(minimumConnections); + config.setMaximumPoolSize(maximumConnections); + config.setConnectionTimeout(connectionTimeout); + config.setLeakDetectionThreshold(60000); + config.setIdleTimeout(600000); + config.setMaxLifetime(1800000); + + //config.setConnectionTestQuery(testQuery); + hikari = new HikariDataSource(config); + } + + + public static void close(Connection conn, PreparedStatement ps, ResultSet res) { + if (conn != null) try { conn.close(); } catch (SQLException ignored) {} + if (ps != null) try { ps.close(); } catch (SQLException ignored) {} + if (res != null) try { res.close(); } catch (SQLException ignored) {} + } + + + public static void closePool() { + if (hikari != null && !hikari.isClosed()) { + hikari.close(); + } + } + + public static void createTable(){ + Connection conn = null; + PreparedStatement ps = null; + try { + conn = hikari.getConnection(); + ps = conn.prepareStatement("CREATE TABLE IF NOT EXISTS playerdata" + + " (uuid VARCHAR(255) UNIQUE," + + " username VARCHAR(255) NOT NULL default ''," + + " prefix VARCHAR(255) NOT NULL default ''," + + " suffix VARCHAR(255) NOT NULL default ''," + + " balance DOUBLE NOT NULL default 0.00," + + " player_group VARCHAR(255) NOT NULL default 0," + + " PRIMARY KEY (uuid));"); + ps.executeUpdate(); + } catch (SQLException e) { + e.printStackTrace(); + } + finally { + close(conn, ps, null); + } + } + + public static void updateTable() { + try { + Map userMap = MineStore.instance.getUserManager().getAll(); + userMap.forEach((uuid, user) -> { + update(user); + } + ); + } catch (Exception e) { + e.printStackTrace(); + } + + } + + private static void update(User user) { + Connection conn = null; + PreparedStatement ps = null; + try { + Bukkit.getLogger().info("[MineStore] Updated user: " + user.getName()); + conn = hikari.getConnection(); + String sql_qury = "INSERT INTO playerdata (uuid,username,prefix,suffix,balance,player_group) VALUES (?,?,?,?,?,?) ON DUPLICATE KEY UPDATE username=?,prefix=?,suffix=?,balance=?,player_group=?"; + ps = conn.prepareStatement(sql_qury); + String uuid = user.getUuid().toString(); + String username = user.getName(); + String prefix; + String suffix; + String group; + if (user.getPrefix() == null) { + prefix = ""; + } else { + prefix = user.getPrefix(); + } + if (user.getSuffix() == null) { + suffix = ""; + } else { + suffix = user.getSuffix(); + } + if (user.getGroup_name() == null) { + group = ""; + } else { + group = user.getGroup_name(); + } + ps.setString(1, uuid); + ps.setString(2, username); + ps.setString(3, prefix); + ps.setString(4, suffix); + ps.setDouble(5, user.getBalance()); + ps.setString(6, group); + ps.setString(7, username); + ps.setString(8, prefix); + ps.setString(9, suffix); + ps.setDouble(10, user.getBalance()); + ps.setString(11, group); + ps.executeUpdate(); + } catch (SQLException e) { + e.printStackTrace(); + } + finally { + close(conn, ps, null); + } + } +} diff --git a/src/main/java/me/chrommob/minestore/mysql/data/User.java b/src/main/java/me/chrommob/minestore/mysql/data/User.java new file mode 100644 index 0000000..4c65c08 --- /dev/null +++ b/src/main/java/me/chrommob/minestore/mysql/data/User.java @@ -0,0 +1,56 @@ +package me.chrommob.minestore.mysql.data; + +import java.util.UUID; + +import lombok.Getter; +import lombok.Setter; +import me.chrommob.minestore.MineStore; +import net.milkbowl.vault.chat.Chat; +import net.milkbowl.vault.economy.Economy; +import org.bukkit.Bukkit; +import org.bukkit.plugin.RegisteredServiceProvider; + +@Getter +@Setter +public class User { + private String name; + private UUID uuid; + private String prefix; + private String suffix; + private double balance; + private String group_name; + private static Economy economy; + private static Chat chat; + + public void update() { + RegisteredServiceProvider esp = MineStore.instance.getServer().getServicesManager().getRegistration(Economy.class); + RegisteredServiceProvider csp = MineStore.instance.getServer().getServicesManager().getRegistration(Chat.class); + economy = esp.getProvider(); + chat = csp.getProvider(); + try { + this.prefix = chat.getPlayerPrefix(Bukkit.getPlayer(uuid)); + this.suffix = chat.getPlayerSuffix(Bukkit.getPlayer(uuid)); + this.balance = economy.getBalance(Bukkit.getPlayer(uuid)); + this.group_name = chat.getPrimaryGroup(Bukkit.getPlayer(uuid)); + } catch (Exception e) { + e.printStackTrace(); + } + } + + public User(UUID uuid, String name) { + RegisteredServiceProvider esp = MineStore.instance.getServer().getServicesManager().getRegistration(Economy.class); + RegisteredServiceProvider csp = MineStore.instance.getServer().getServicesManager().getRegistration(Chat.class); + economy = esp.getProvider(); + chat = csp.getProvider(); + this.uuid = uuid; + this.name = name; + try { + this.prefix = chat.getPlayerPrefix(Bukkit.getPlayer(uuid)); + this.suffix = chat.getPlayerSuffix(Bukkit.getPlayer(uuid)); + this.balance = economy.getBalance(Bukkit.getPlayer(uuid)); + this.group_name = chat.getPrimaryGroup(Bukkit.getPlayer(uuid)); + } catch (Exception e) { + e.printStackTrace(); + } + } +} diff --git a/src/main/java/me/chrommob/minestore/mysql/data/UserManager.java b/src/main/java/me/chrommob/minestore/mysql/data/UserManager.java new file mode 100644 index 0000000..1a9984e --- /dev/null +++ b/src/main/java/me/chrommob/minestore/mysql/data/UserManager.java @@ -0,0 +1,46 @@ +package me.chrommob.minestore.mysql.data; + +import lombok.Getter; +import lombok.Setter; + +import java.util.Map; +import java.util.UUID; +import java.util.concurrent.ConcurrentHashMap; + +@Setter +@Getter +public class UserManager { + private final Map profiles = new ConcurrentHashMap<>(); + public User createProfile(UUID uuid, String name) { + User profile = new User(uuid, name); + profiles.put(uuid, profile); + return profile; + } + + public User getProfile(UUID uuid) { + if (profiles.get(uuid) != null) { + return profiles.get(uuid); + } else { + return null; + } + } + + public void removeProfile(UUID uuid) { + profiles.remove(uuid); + } + + public Map getAll(){ + return profiles; + } + + public void updateAll() { + try { + for (User user : profiles.values()) { + user.update(); + } + } catch (Exception e) { + e.printStackTrace(); + } + } + +} diff --git a/src/main/java/me/chrommob/minestore/util/Runnable.java b/src/main/java/me/chrommob/minestore/util/Runnable.java index 3d9a4cf..a828967 100644 --- a/src/main/java/me/chrommob/minestore/util/Runnable.java +++ b/src/main/java/me/chrommob/minestore/util/Runnable.java @@ -1,8 +1,11 @@ package me.chrommob.minestore.util; +import me.chrommob.minestore.MineStore; import me.chrommob.minestore.data.Config; import me.chrommob.minestore.gui.BuyListener; import me.chrommob.minestore.gui.Currency; +import me.chrommob.minestore.mysql.MySQLData; +import me.chrommob.minestore.mysql.connection.ConnectionPool; import me.chrommob.minestore.placeholders.listener.DonationGoalListener; import me.chrommob.minestore.placeholders.listener.LastDonatorListener; import me.chrommob.minestore.placeholders.listener.TopDonoListener; @@ -30,6 +33,10 @@ public static void runListener(String load) { DonationGoalListener.run(); LastDonatorListener.run(); } + if (Config.isVaultPresent() && MySQLData.isEnabled()) { + MineStore.instance.getUserManager().updateAll(); + ConnectionPool.updateTable(); + } } , 0, 30, TimeUnit.SECONDS); } } diff --git a/src/main/resources/config.yml b/src/main/resources/config.yml index b46d1ba..6e59628 100644 --- a/src/main/resources/config.yml +++ b/src/main/resources/config.yml @@ -30,7 +30,7 @@ secret-key: "hard_secret_key_here" #------------------------------------------------# # BUY-GUI # # Do you want to enable buy gui? # -gui-enabled: true +gui-enabled: false format: item-name: "%minestore_item_name%" item-description: "%minestore_item_description%" @@ -44,12 +44,23 @@ settings: #------------------------------------------------# # Link to your store # # /store message # -store-enabled: true +store-enabled: false store-message: "Visit our store: https://store.yourdomain.com/" #------------------------------------------------# #------------------------------------------------# # Link to your store's API # -store-api: "https://yourstore.com/api/top_donators" +store-api: "https://yourstore.com/api/" api-key: "hard_api_key_here" #------------------------------------------------# + +#------------------------------------------------# +# MySQL settings # +mysql: + enabled: false + ip: "localhost" + port: 3306 + username: "root" + password: "" + database: "minestore" +#------------------------------------------------# \ No newline at end of file