Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class Core extends JavaPlugin implements Listener {
- public static Connection connection;
- public static Core instance;
- public void onEnable(){
- instance = this;
- getServer().getPluginManager().registerEvents(this, this);
- getCommand("addcoins").setExecutor(new CoinControl());
- getCommand("checkcoins").setExecutor(new CoinControl());
- getCommand("setcoins").setExecutor(new CoinControl());
- getCommand("removecoins").setExecutor(new CoinControl());
- }
- public void onDisable(){
- try {
- if (connection != null && connection.isClosed())
- connection.close();
- }catch (SQLException e){
- e.printStackTrace();
- }
- }
- public synchronized static void openConnection(){
- try {
- connection = DriverManager.getConnection("jdbc:mysql://sql6.freesqldatabase.com:3306/sql6142980", "sql6142980", "YXH5m6ikp8");
- }catch (Exception e){
- e.printStackTrace();
- }
- }
- public synchronized static void closeConnection(){
- try {
- connection.close();
- }catch (Exception e){
- e.printStackTrace();
- }
- }
- public synchronized static boolean playerDataContainsPlayer(Player player){
- try {
- PreparedStatement sql = connection.prepareStatement("SELECT * FROM `player_data` WHERE player=?;");
- sql.setString(1, player.getName());
- ResultSet resultSet = sql.executeQuery();
- boolean containsPlayer = resultSet.next();
- sql.close();
- resultSet.close();
- return containsPlayer;
- }catch (Exception e){
- e.printStackTrace();
- return false;
- }
- }
- @EventHandler
- public void onPlayerLogin(PlayerLoginEvent event){
- openConnection();
- try {
- int previousLogins = 0;
- if(playerDataContainsPlayer(event.getPlayer())){
- PreparedStatement sql = connection.prepareStatement("SELECT logins FROM `player_data` WHERE player=?;");
- sql.setString(1, event.getPlayer().getName());
- ResultSet result = sql.executeQuery();
- result.next();
- previousLogins = result.getInt("logins");
- PreparedStatement loginsUpdate = connection.prepareStatement("UPDATE `player_data` SET logins=? WHERE player=?;");
- loginsUpdate.setInt(1, previousLogins + 1);
- loginsUpdate.setString(2, event.getPlayer().getName());
- loginsUpdate.executeUpdate();
- loginsUpdate.close();
- sql.close();
- result.close();
- }else{
- PreparedStatement newPlayer = connection.prepareStatement("INSERT INTO `player_data` values(?,0,0,0,1);");
- newPlayer.setString(1, event.getPlayer().getName());
- newPlayer.execute();
- newPlayer.close();
- PreparedStatement newCoins = connection.prepareStatement("INSERT INTO `player_data` values(?,0,0,0,0);");
- newCoins.setString(1, event.getPlayer().getName());
- newCoins.setInt(2, 0);
- newCoins.execute();
- newCoins.close();
- }
- }catch (Exception e){
- e.printStackTrace();
- }finally {
- closeConnection();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement