Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package de.Jeyprox.mysql;
- import java.io.File;
- import java.io.IOException;
- import java.sql.Connection;
- import java.sql.DriverManager;
- import java.sql.PreparedStatement;
- import java.sql.ResultSet;
- import java.sql.SQLException;
- import java.util.UUID;
- import org.bukkit.Bukkit;
- import org.bukkit.configuration.file.YamlConfiguration;
- public class Coins {
- public static Connection con;
- // Global Stats
- private static void createmysqlcon() {
- File file = new File("plugins/Coins/mysql.yml");
- YamlConfiguration cfg = YamlConfiguration.loadConfiguration(file);
- if (!file.exists()) {
- try {
- file.getParentFile().mkdirs();
- file.getParentFile().createNewFile();
- file.createNewFile();
- } catch (IOException e) {
- e.printStackTrace();
- }
- cfg.options().header("Config fuer MySQL");
- cfg.set("Coins.Host", "localhost");
- cfg.set("Coins.Port", "3306");
- cfg.set("Coins.DB", "Coins");
- cfg.set("Coins.User", "root");
- cfg.set("Coins.Password", "password");
- try {
- cfg.save(file);
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
- CHost = cfg.getString("Coins.Host");
- CPort = cfg.getString("Coins.Port");
- CDB = cfg.getString("Coins.DB");
- CUser = cfg.getString("Coins.User");
- CPassword = cfg.getString("Coins.Password");
- }
- static String CHost;
- static String CPort;
- static String CDB;
- static String CUser;
- static String CPassword;
- public static void connect() {
- createmysqlcon();
- if (!isconnected()) {
- try {
- con = DriverManager.getConnection("jdbc:mysql://" + CHost + ":" + CPort + "/" + CDB + "?autoReconnect=true",
- CUser, CPassword);
- } catch (SQLException e) {
- e.printStackTrace();
- }
- }
- createTable();
- }
- public static void disconnect() {
- if (isconnected()) {
- try {
- con.close();
- } catch (SQLException e) {
- e.printStackTrace();
- }
- }
- }
- public static void update(String qry) {
- try {
- PreparedStatement ps = con.prepareStatement(qry);
- ps.executeUpdate();
- } catch (SQLException e) {
- e.printStackTrace();
- }
- }
- public static ResultSet getResult(String qry) {
- try {
- PreparedStatement ps = con.prepareStatement(qry);
- return ps.executeQuery();
- } catch (SQLException e) {
- e.printStackTrace();
- }
- return null;
- }
- private static void createTable() {
- if (isconnected()) {
- try {
- con.createStatement().executeUpdate(
- "CREATE TABLE IF NOT EXISTS CoinsT (Spielername VARCHAR(100), UUID VARCHAR(100), Coins INT(255))");
- } catch (SQLException e) {
- e.printStackTrace();
- }
- }
- }
- // Coins
- public static int getCoins(String uuid) {
- ResultSet rs = Coins.getResult("SELECT * FROM CoinsT WHERE UUID='" + uuid + "'");
- try {
- while (rs.next()) {
- return rs.getInt("Coins");
- }
- } catch (SQLException e) {
- e.printStackTrace();
- return 0;
- }
- return 0;
- }
- public static void addCoins(String uuid, int Coinsi) {
- int oldCoins = getCoins(uuid);
- int newCoins = oldCoins + Coinsi;
- Coins.update("UPDATE CoinsT SET Coins='" + newCoins + "' WHERE UUID='" + uuid + "'");
- Bukkit.getPluginManager().callEvent(new CoinsEvent(Bukkit.getPlayer(UUID.fromString(uuid))));
- }
- public static void removeCoins(String uuid, int Coinsi) {
- if (!(getCoins(uuid) < 5)) {
- int oldcoins = getCoins(uuid);
- int newCoins = oldcoins - Coinsi;
- Coins.update("UPDATE CoinsT SET Coins='" + newCoins + "' WHERE UUID='" + uuid + "'");
- Bukkit.getPluginManager().callEvent(new CoinsEvent(Bukkit.getPlayer(UUID.fromString(uuid))));
- }
- }
- //
- public static boolean isconnected() {
- if (con == null) {
- return false;
- } else {
- return true;
- }
- }
- public static boolean playerExists(String uuid) {
- try {
- ResultSet rs = Coins.getResult("SELECT * FROM CoinsT WHERE UUID='" + uuid + "'");
- if (rs.next()) {
- return rs.getString("UUID") != null;
- }
- return false;
- } catch (SQLException e) {
- e.printStackTrace();
- }
- return false;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement