Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public static Connection conn;
- public static String host = "jdbc:mysql://localhost:3306/TableName";
- public static String username = "root";
- public static String password = "examplepassword";
- //MAKE SURE THIS IS IN YOUR ONENABLE!!
- public static void setupConnection(){
- try
- {
- Class.forName("com.mysql.jdbc.Driver");
- conn = DriverManager.getConnection(host, username, password);
- Bukkit.getLogger().info("Permissions connected to MySQL");
- LoadGroups();
- }
- catch (Exception ex)
- {
- ex.printStackTrace();
- }
- }
- //MAKE SURE THIS RUNS EVERY TIME A PLAYER LOGS IN
- private static void LoadPlayer(Player p)
- {
- /**
- * Loads player data from MySQL, removes old data
- */
- try
- {
- int group_loaded = 1;
- String permissions_loaded = "";
- String prefix_loaded = "";
- PreparedStatement s = conn.prepareStatement("SELECT * FROM Players WHERE `uuid`=?");
- s.setString(1, p.getUniqueId().toString());
- s.execute();
- ResultSet result = s.getResultSet();
- if (result.next())
- {
- String playerName_loaded = result.getString("name");
- String playerName = p.getName();
- if(!playerName_loaded.equals(playerName))
- {
- s = conn.prepareStatement("UPDATE Players SET `name`=? WHERE `uuid`=?;");
- s.setString(1, p.getName());
- s.setString(2, p.getUniqueId().toString());
- s.execute();
- }
- }
- else
- {
- // The player might exist in database but has no UUID yet.
- s = conn.prepareStatement("SELECT * FROM Players WHERE `name`=?");
- s.setString(1, p.getName());
- s.execute();
- result = s.getResultSet();
- if (result.next())
- {
- // Player exists in database but has no UUID. Lets enter it.
- s = conn.prepareStatement("UPDATE Players SET `uuid`=? WHERE `name`=?;");
- s.setString(1, p.getUniqueId().toString());
- s.setString(2, p.getName());
- s.execute();
- // UUID has been entered into player. Lets continue.
- group_loaded = result.getInt("group");
- permissions_loaded = result.getString("permissions");
- prefix_loaded = result.getString("prefix");
- s.close();
- }
- else
- {
- // Player does not exist in database. Create a new player.
- s.close();
- s = conn.prepareStatement("INSERT INTO Players SET `uuid`=?, `name`=?, 'coins'=0;");
- s.setString(1, p.getUniqueId().toString());
- s.setString(2, p.getName());
- s.execute();
- s.close();
- }
- }
- s.close();
- }
- catch (SQLException ex)
- {
- ex.printStackTrace();
- }
- }
- public static int getCoins(Player p)
- {
- PreparedStatement s;
- try
- {
- s = PermissionManager.conn.prepareStatement("SELECT * FROM Players WHERE `uuid`=?");
- s.setString(1, p.getUniqueId().toString());
- s.execute();
- ResultSet result = s.getResultSet();
- if(result.next())
- {
- return result.getInt("coins");
- }
- else
- Bukkit.getLogger().severe("Could not get coins of an online player! Player does not exist in database!");
- }
- catch (SQLException e)
- {
- e.printStackTrace();
- }
- return 0;
- }
- public static int getChips(Player p)
- {
- PreparedStatement s;
- try
- {
- s = PermissionManager.conn.prepareStatement("SELECT * FROM Players WHERE `uuid`=?");
- s.setString(1, p.getUniqueId().toString());
- s.execute();
- ResultSet result = s.getResultSet();
- if(result.next())
- {
- return result.getInt("chips");
- }
- else
- Bukkit.getLogger().severe("Could not get coins of an online player! Player does not exist in database!");
- }
- catch (SQLException e)
- {
- e.printStackTrace();
- }
- return 0;
- }
- public static void setCoins(Player p, int i)
- {
- PreparedStatement s;
- try
- {
- s = PermissionManager.conn.prepareStatement("UPDATE Players SET `coins`=? WHERE `uuid`=?");
- s.setInt(1, i);
- s.setString(2, p.getUniqueId().toString());
- s.execute();
- }
- catch (SQLException e)
- {
- e.printStackTrace();
- }
- }
- public static void setChips(Player p, int i)
- {
- PreparedStatement s;
- try
- {
- s = PermissionManager.conn.prepareStatement("UPDATE Players SET `chips`=? WHERE `uuid`=?");
- s.setInt(1, i);
- s.setString(2, p.getUniqueId().toString());
- s.execute();
- }
- catch (SQLException e)
- {
- e.printStackTrace();
- }
- }
- public static void removeCoins(Player p, int amount)
- {
- setCoins(p, (getCoins(p) - amount));
- }
- public static void addCoins(Player p, int amount)
- {
- setCoins(p, (getCoins(p) + amount));
- }
- public static void removeChips(Player p, int i)
- {
- int bal = getChips(p);
- bal = bal - i;
- setChips(p, bal);
- }
- public static void addChips(Player p, int i)
- {
- int bal = getChips(p);
- bal = bal + i;
- setChips(p, bal);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement