Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class Core extends JavaPlugin implements Listener {
- private static Connection connection;
- public void onEnable(){
- getServer().getPluginManager().registerEvents(this, this);
- }
- 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 'RuseCoins' 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 coins = 0;
- if(playerDataContainsPlayer(event.getPlayer())){
- PreparedStatement sql = connection.prepareStatement("SELECT coins FROM `RuseCoins` WHERE player=?;");
- sql.setString(1, event.getPlayer().getName());
- ResultSet result = sql.executeQuery();
- result.next();
- coins = result.getInt("coins");
- PreparedStatement coinsUpdate = connection.prepareStatement("UPDATE `RuseCoins` SET coins=?;");
- coinsUpdate.setInt(1, coins + 1);
- //1 is what ur adding to those coins
- coinsUpdate.setString(2, event.getPlayer().getName());
- coinsUpdate.executeUpdate();
- coinsUpdate.close();
- sql.close();
- result.close();
- }else{
- PreparedStatement newPlayer = connection.prepareStatement("INSERT INTO `RuseCoins` values(?, 0);");
- //0 is the starting amount
- newPlayer.setString(1, event.getPlayer().getName());
- newPlayer.execute();
- newPlayer.close();
- }
- }catch (Exception e){
- e.printStackTrace();
- }finally {
- closeConnection();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement