From 32bdc620acc180032bbd5b3cb62eed34112507de Mon Sep 17 00:00:00 2001 From: DmitriyMX Date: Sun, 24 Feb 2019 16:03:18 +0300 Subject: [PATCH 1/4] add .gitattributes --- .gitattributes | 1 + 1 file changed, 1 insertion(+) create mode 100644 .gitattributes diff --git a/.gitattributes b/.gitattributes new file mode 100644 index 0000000..2125666 --- /dev/null +++ b/.gitattributes @@ -0,0 +1 @@ +* text=auto \ No newline at end of file From 8a695ae9090064d5261b0d9d37d0aeae1793f30f Mon Sep 17 00:00:00 2001 From: DmitriyMX Date: Sun, 24 Feb 2019 16:03:56 +0300 Subject: [PATCH 2/4] crlf -> lf --- .../ru/prisonlife/PrisonAPI/PrisonAPI.java | 116 +++---- .../prisonlife/PrisonAPI/template/Level.java | 114 +++---- .../PrisonAPI/template/Prisoner.java | 306 +++++++++--------- .../prisonlife/PrisonAPI/template/Score.java | 86 ++--- .../prisonlife/PrisonAPI/template/Wallet.java | 94 +++--- 5 files changed, 358 insertions(+), 358 deletions(-) diff --git a/src/main/java/ru/prisonlife/PrisonAPI/PrisonAPI.java b/src/main/java/ru/prisonlife/PrisonAPI/PrisonAPI.java index fa629cf..6dd553f 100644 --- a/src/main/java/ru/prisonlife/PrisonAPI/PrisonAPI.java +++ b/src/main/java/ru/prisonlife/PrisonAPI/PrisonAPI.java @@ -1,58 +1,58 @@ -package ru.prisonlife.prisonapi; - -import org.bukkit.plugin.java.JavaPlugin; - -import java.sql.Connection; -import java.sql.DriverManager; -import java.sql.SQLException; - -public class PrisonAPI extends JavaPlugin { - - public static final String PLUGIN_PATH = "plugins/PrisonManager"; - - public enum Factions { - NONE(0), ASIANS(1), LATINOS(2), NIGGAZ(3), POLICE(4); - - public static Factions valueOf(int id) { - for (Factions factions : Factions.values()) { - if (factions.id == id) { - return factions; - } - } - - throw new IllegalArgumentException("Factions id " + id + " not defined!"); - } - - private int id; - - Factions(int id) { - this.id = id; - } - - public int getId() { - return id; - } - } - - public static Connection CONN; - - public void onEnable() { - try { - CONN = DriverManager.getConnection( - "jdbc:mysql://localhost:3306/prison4life", - "root", "root"); - } catch (SQLException e) { - e.printStackTrace(); - } - } - - public void onDisable() { - try { - if (!CONN.isClosed()) { - CONN.close(); - } - } catch (SQLException e) { - e.printStackTrace(); - } - } -} +package ru.prisonlife.prisonapi; + +import org.bukkit.plugin.java.JavaPlugin; + +import java.sql.Connection; +import java.sql.DriverManager; +import java.sql.SQLException; + +public class PrisonAPI extends JavaPlugin { + + public static final String PLUGIN_PATH = "plugins/PrisonManager"; + + public enum Factions { + NONE(0), ASIANS(1), LATINOS(2), NIGGAZ(3), POLICE(4); + + public static Factions valueOf(int id) { + for (Factions factions : Factions.values()) { + if (factions.id == id) { + return factions; + } + } + + throw new IllegalArgumentException("Factions id " + id + " not defined!"); + } + + private int id; + + Factions(int id) { + this.id = id; + } + + public int getId() { + return id; + } + } + + public static Connection CONN; + + public void onEnable() { + try { + CONN = DriverManager.getConnection( + "jdbc:mysql://localhost:3306/prison4life", + "root", "root"); + } catch (SQLException e) { + e.printStackTrace(); + } + } + + public void onDisable() { + try { + if (!CONN.isClosed()) { + CONN.close(); + } + } catch (SQLException e) { + e.printStackTrace(); + } + } +} diff --git a/src/main/java/ru/prisonlife/PrisonAPI/template/Level.java b/src/main/java/ru/prisonlife/PrisonAPI/template/Level.java index 87a3f1b..950cc39 100644 --- a/src/main/java/ru/prisonlife/PrisonAPI/template/Level.java +++ b/src/main/java/ru/prisonlife/PrisonAPI/template/Level.java @@ -1,57 +1,57 @@ -package ru.prisonlife.prisonapi.template; - -import java.sql.PreparedStatement; -import java.sql.ResultSet; -import java.sql.SQLException; - -import static ru.prisonlife.prisonapi.PrisonAPI.CONN; - -public class Level { - - private Prisoner me; - private Score score; - - Level(Prisoner prisoner) { - me = prisoner; - score = new Score(me); - } - - public Score getScore() throws SQLException { - //FIXME приведи в порядок метод - /* - connect(); - if (resultSet.first()) { - return this.score; - } - disconnect(); - */ - return null; - } - - public double getLevel() throws SQLException, NullPointerException { - try (PreparedStatement statement = CONN.prepareStatement("SELECT LEVEL FROM player_stats WHERE UUID = ?")) { - statement.setString(1, me.getPlayer().getUniqueId().toString()); - - try (ResultSet resultSet = statement.executeQuery()) { - if (resultSet.next()) { - return resultSet.getDouble(1); - } else { - return 1d; //TODO нужно или default значение, или exception - } - } - } - } - - public void addLevel(double value) throws SQLException, NullPointerException { - setLevel(getLevel() + value); - } - - public void setLevel(double value) throws SQLException, NullPointerException { - try (PreparedStatement statement = CONN.prepareStatement("UPDATE player_stats SET LEVEL = ? WHERE UUID = ?")) { - statement.setDouble(1, getLevel() + value); - statement.setString(2, me.getPlayer().getUniqueId().toString()); - - statement.executeUpdate(); - } - } -} +package ru.prisonlife.prisonapi.template; + +import java.sql.PreparedStatement; +import java.sql.ResultSet; +import java.sql.SQLException; + +import static ru.prisonlife.prisonapi.PrisonAPI.CONN; + +public class Level { + + private Prisoner me; + private Score score; + + Level(Prisoner prisoner) { + me = prisoner; + score = new Score(me); + } + + public Score getScore() throws SQLException { + //FIXME приведи в порядок метод + /* + connect(); + if (resultSet.first()) { + return this.score; + } + disconnect(); + */ + return null; + } + + public double getLevel() throws SQLException, NullPointerException { + try (PreparedStatement statement = CONN.prepareStatement("SELECT LEVEL FROM player_stats WHERE UUID = ?")) { + statement.setString(1, me.getPlayer().getUniqueId().toString()); + + try (ResultSet resultSet = statement.executeQuery()) { + if (resultSet.next()) { + return resultSet.getDouble(1); + } else { + return 1d; //TODO нужно или default значение, или exception + } + } + } + } + + public void addLevel(double value) throws SQLException, NullPointerException { + setLevel(getLevel() + value); + } + + public void setLevel(double value) throws SQLException, NullPointerException { + try (PreparedStatement statement = CONN.prepareStatement("UPDATE player_stats SET LEVEL = ? WHERE UUID = ?")) { + statement.setDouble(1, getLevel() + value); + statement.setString(2, me.getPlayer().getUniqueId().toString()); + + statement.executeUpdate(); + } + } +} diff --git a/src/main/java/ru/prisonlife/PrisonAPI/template/Prisoner.java b/src/main/java/ru/prisonlife/PrisonAPI/template/Prisoner.java index 5b2bd9c..39aec0f 100644 --- a/src/main/java/ru/prisonlife/PrisonAPI/template/Prisoner.java +++ b/src/main/java/ru/prisonlife/PrisonAPI/template/Prisoner.java @@ -1,154 +1,154 @@ -package ru.prisonlife.prisonapi.template; - -import org.bukkit.Location; -import org.bukkit.configuration.file.FileConfiguration; -import org.bukkit.configuration.file.YamlConfiguration; -import org.bukkit.entity.Player; - -import java.io.File; -import java.sql.PreparedStatement; -import java.sql.ResultSet; -import java.sql.SQLException; -import java.util.ArrayList; -import java.util.List; -import java.util.Objects; - -import static ru.prisonlife.prisonapi.PrisonAPI.CONN; -import static ru.prisonlife.prisonapi.PrisonAPI.PLUGIN_PATH; - -public class Prisoner { - - private Player me; - private Level level; - private Faction faction; - private Wallet wallet; - private Stats stats; - - public Prisoner(Player player) { - me = player; - level = new Level(this); - wallet = new Wallet(this); - faction = new Faction(this); - stats = new Stats(this); - } - - public Player getPlayer() { - return me; - } - - public Level getPrisonLevel() throws SQLException { - return this.level; - } - - public String getPrison() { - Location loc = me.getLocation(); - for (File file : Objects.requireNonNull(new File(PLUGIN_PATH).listFiles())) { - FileConfiguration fileConfiguration = YamlConfiguration.loadConfiguration(file); - List> data = new ArrayList<>(); - List fromConfig = fileConfiguration.getIntegerList("DIAGONAL_POINTS"), - min = new ArrayList<>(), max = new ArrayList<>(); - int[] loc_n = {loc.getBlockX(), loc.getBlockY() - 1, loc.getBlockZ()}; - data.add(fromConfig.subList(0, 3)); - data.add(fromConfig.subList(3, 6)); - for (int i = 0; i < 3; i++) { - min.add(Math.min(data.get(0).get(i), data.get(1).get(i))); - max.add(Math.max(data.get(0).get(i), data.get(1).get(i))); - } - int c = 0; - for (int k : loc_n) { - for (int n = 0; n < 3; n++) { - if (k >= min.get(n) && k <= max.get(n)) { - ++c; - } - } - } - if (c == 3) { - return file.getName().replace("PRISON_", "").replace(".yml", ""); - } - } - return "Not Prison"; - } - - public String getTerritory(String type) { - String prison = getPrison(); - if (!prison.equals("Not Prison") && (type.equals("name") || type.equals("owner"))) { - Location loc = me.getLocation(); - FileConfiguration fileConfiguration = YamlConfiguration.loadConfiguration(new File(PLUGIN_PATH, "PRISON_" + prison + ".yml")); - try { - if (fileConfiguration.getConfigurationSection("TERRITORIES").getKeys(false).size() != 0) { - for (String terr : fileConfiguration.getConfigurationSection("TERRITORIES").getKeys(false)) { - List> data = new ArrayList<>(); - List fromConfig = fileConfiguration.getIntegerList("TERRITORIES." + terr + ".DIAGONAL_POINTS"), - min = new ArrayList<>(), max = new ArrayList<>(); - int[] loc_n = {loc.getBlockX(), loc.getBlockY() - 1, loc.getBlockZ()}; - data.add(fromConfig.subList(0, 3)); - data.add(fromConfig.subList(3, 6)); - for (int i = 0; i < 3; i++) { - min.add(Math.min(data.get(0).get(i), data.get(1).get(i))); - max.add(Math.max(data.get(0).get(i), data.get(1).get(i))); - } - int c = 0; - for (int k : loc_n) { - for (int n = 0; n < 3; n++) { - if (k >= min.get(n) && k <= max.get(n)) { - ++c; - } - } - } - if (c == 3) { - return type.equals("name") ? fileConfiguration.getString("TERRITORIES." + terr + ".NAME") : - (type.equals("owner") ? fileConfiguration.getString("TERRITORIES." + terr + ".OWNER") : null); - } - } - } - } catch (NullPointerException z) { - } - } - return "Not Territory"; - } - - public Faction getFaction() { - return faction; - } - - public Wallet getWallet() { - return wallet; - } - - public Stats getStats() { - return stats; - } - - public void register() throws SQLException { - PreparedStatement statement = CONN.prepareStatement("SELECT UUID FROM player_stats WHERE UUID = ?"); - statement.setString(1, me.getUniqueId().toString()); - - try (ResultSet resultSet = statement.executeQuery()) { - if (!resultSet.next()) { - statement.close(); - - statement = CONN.prepareStatement( - "INSERT INTO player_stats(NAME, UUID, LEVEL, POINTS, FACTION, RESPECT, F_RANG, WALLET, " + - "G_TIME, D_BLOCKS, K_PLAYERS, K_MOBS, DEATHS) " - + "VALUES (?, ?, 1, 0, 'NONE', 0, 0, 0, 0, 0, 0, 0, 0)"); - statement.setString(1, getPlayer().getName()); - statement.setString(2, me.getUniqueId().toString()); - statement.executeUpdate(); - statement.close(); - - statement = CONN.prepareStatement( - "INSERT INTO daily_respect_task(NAME, UUID, COOLDOWN) " + - "VALUES (?, ?, ?)"); - statement.setString(1, getPlayer().getName()); - statement.setString(2, me.getUniqueId().toString()); - statement.setInt(3, (3600 * 24)); - statement.executeUpdate(); - - level = new Level(this); - stats = new Stats(this); - wallet = new Wallet(this); - } - } - statement.close(); - } +package ru.prisonlife.prisonapi.template; + +import org.bukkit.Location; +import org.bukkit.configuration.file.FileConfiguration; +import org.bukkit.configuration.file.YamlConfiguration; +import org.bukkit.entity.Player; + +import java.io.File; +import java.sql.PreparedStatement; +import java.sql.ResultSet; +import java.sql.SQLException; +import java.util.ArrayList; +import java.util.List; +import java.util.Objects; + +import static ru.prisonlife.prisonapi.PrisonAPI.CONN; +import static ru.prisonlife.prisonapi.PrisonAPI.PLUGIN_PATH; + +public class Prisoner { + + private Player me; + private Level level; + private Faction faction; + private Wallet wallet; + private Stats stats; + + public Prisoner(Player player) { + me = player; + level = new Level(this); + wallet = new Wallet(this); + faction = new Faction(this); + stats = new Stats(this); + } + + public Player getPlayer() { + return me; + } + + public Level getPrisonLevel() throws SQLException { + return this.level; + } + + public String getPrison() { + Location loc = me.getLocation(); + for (File file : Objects.requireNonNull(new File(PLUGIN_PATH).listFiles())) { + FileConfiguration fileConfiguration = YamlConfiguration.loadConfiguration(file); + List> data = new ArrayList<>(); + List fromConfig = fileConfiguration.getIntegerList("DIAGONAL_POINTS"), + min = new ArrayList<>(), max = new ArrayList<>(); + int[] loc_n = {loc.getBlockX(), loc.getBlockY() - 1, loc.getBlockZ()}; + data.add(fromConfig.subList(0, 3)); + data.add(fromConfig.subList(3, 6)); + for (int i = 0; i < 3; i++) { + min.add(Math.min(data.get(0).get(i), data.get(1).get(i))); + max.add(Math.max(data.get(0).get(i), data.get(1).get(i))); + } + int c = 0; + for (int k : loc_n) { + for (int n = 0; n < 3; n++) { + if (k >= min.get(n) && k <= max.get(n)) { + ++c; + } + } + } + if (c == 3) { + return file.getName().replace("PRISON_", "").replace(".yml", ""); + } + } + return "Not Prison"; + } + + public String getTerritory(String type) { + String prison = getPrison(); + if (!prison.equals("Not Prison") && (type.equals("name") || type.equals("owner"))) { + Location loc = me.getLocation(); + FileConfiguration fileConfiguration = YamlConfiguration.loadConfiguration(new File(PLUGIN_PATH, "PRISON_" + prison + ".yml")); + try { + if (fileConfiguration.getConfigurationSection("TERRITORIES").getKeys(false).size() != 0) { + for (String terr : fileConfiguration.getConfigurationSection("TERRITORIES").getKeys(false)) { + List> data = new ArrayList<>(); + List fromConfig = fileConfiguration.getIntegerList("TERRITORIES." + terr + ".DIAGONAL_POINTS"), + min = new ArrayList<>(), max = new ArrayList<>(); + int[] loc_n = {loc.getBlockX(), loc.getBlockY() - 1, loc.getBlockZ()}; + data.add(fromConfig.subList(0, 3)); + data.add(fromConfig.subList(3, 6)); + for (int i = 0; i < 3; i++) { + min.add(Math.min(data.get(0).get(i), data.get(1).get(i))); + max.add(Math.max(data.get(0).get(i), data.get(1).get(i))); + } + int c = 0; + for (int k : loc_n) { + for (int n = 0; n < 3; n++) { + if (k >= min.get(n) && k <= max.get(n)) { + ++c; + } + } + } + if (c == 3) { + return type.equals("name") ? fileConfiguration.getString("TERRITORIES." + terr + ".NAME") : + (type.equals("owner") ? fileConfiguration.getString("TERRITORIES." + terr + ".OWNER") : null); + } + } + } + } catch (NullPointerException z) { + } + } + return "Not Territory"; + } + + public Faction getFaction() { + return faction; + } + + public Wallet getWallet() { + return wallet; + } + + public Stats getStats() { + return stats; + } + + public void register() throws SQLException { + PreparedStatement statement = CONN.prepareStatement("SELECT UUID FROM player_stats WHERE UUID = ?"); + statement.setString(1, me.getUniqueId().toString()); + + try (ResultSet resultSet = statement.executeQuery()) { + if (!resultSet.next()) { + statement.close(); + + statement = CONN.prepareStatement( + "INSERT INTO player_stats(NAME, UUID, LEVEL, POINTS, FACTION, RESPECT, F_RANG, WALLET, " + + "G_TIME, D_BLOCKS, K_PLAYERS, K_MOBS, DEATHS) " + + "VALUES (?, ?, 1, 0, 'NONE', 0, 0, 0, 0, 0, 0, 0, 0)"); + statement.setString(1, getPlayer().getName()); + statement.setString(2, me.getUniqueId().toString()); + statement.executeUpdate(); + statement.close(); + + statement = CONN.prepareStatement( + "INSERT INTO daily_respect_task(NAME, UUID, COOLDOWN) " + + "VALUES (?, ?, ?)"); + statement.setString(1, getPlayer().getName()); + statement.setString(2, me.getUniqueId().toString()); + statement.setInt(3, (3600 * 24)); + statement.executeUpdate(); + + level = new Level(this); + stats = new Stats(this); + wallet = new Wallet(this); + } + } + statement.close(); + } } \ No newline at end of file diff --git a/src/main/java/ru/prisonlife/PrisonAPI/template/Score.java b/src/main/java/ru/prisonlife/PrisonAPI/template/Score.java index 59ebf47..2f4e525 100644 --- a/src/main/java/ru/prisonlife/PrisonAPI/template/Score.java +++ b/src/main/java/ru/prisonlife/PrisonAPI/template/Score.java @@ -1,43 +1,43 @@ -package ru.prisonlife.prisonapi.template; - -import java.sql.PreparedStatement; -import java.sql.ResultSet; -import java.sql.SQLException; - -import static ru.prisonlife.prisonapi.PrisonAPI.CONN; - -public class Score { - - private Prisoner me; - - Score(Prisoner prisoner) { - me = prisoner; - } - - public double getPoints() throws SQLException { - try (PreparedStatement statement = CONN.prepareStatement("SELECT POINTS FROM player_stats WHERE UUID = ?")) { - statement.setString(1, me.getPlayer().getUniqueId().toString()); - - try (ResultSet resultSet = statement.executeQuery()) { - if (resultSet.next()) { - return resultSet.getDouble(1); - } else { - return 0d; //TODO нужно или default значение, или exception - } - } - } - } - - public void setPoints(double value) throws SQLException { - try (PreparedStatement statement = CONN.prepareStatement("UPDATE player_stats SET POINTS = ? WHERE UUID = ?")) { - statement.setDouble(1, value); - statement.setString(2, me.getPlayer().getUniqueId().toString()); - - statement.executeUpdate(); - } - } - - public void addPoints(double value) throws SQLException { - setPoints(getPoints() + value); - } -} +package ru.prisonlife.prisonapi.template; + +import java.sql.PreparedStatement; +import java.sql.ResultSet; +import java.sql.SQLException; + +import static ru.prisonlife.prisonapi.PrisonAPI.CONN; + +public class Score { + + private Prisoner me; + + Score(Prisoner prisoner) { + me = prisoner; + } + + public double getPoints() throws SQLException { + try (PreparedStatement statement = CONN.prepareStatement("SELECT POINTS FROM player_stats WHERE UUID = ?")) { + statement.setString(1, me.getPlayer().getUniqueId().toString()); + + try (ResultSet resultSet = statement.executeQuery()) { + if (resultSet.next()) { + return resultSet.getDouble(1); + } else { + return 0d; //TODO нужно или default значение, или exception + } + } + } + } + + public void setPoints(double value) throws SQLException { + try (PreparedStatement statement = CONN.prepareStatement("UPDATE player_stats SET POINTS = ? WHERE UUID = ?")) { + statement.setDouble(1, value); + statement.setString(2, me.getPlayer().getUniqueId().toString()); + + statement.executeUpdate(); + } + } + + public void addPoints(double value) throws SQLException { + setPoints(getPoints() + value); + } +} diff --git a/src/main/java/ru/prisonlife/PrisonAPI/template/Wallet.java b/src/main/java/ru/prisonlife/PrisonAPI/template/Wallet.java index cc9fd9d..2ee483c 100644 --- a/src/main/java/ru/prisonlife/PrisonAPI/template/Wallet.java +++ b/src/main/java/ru/prisonlife/PrisonAPI/template/Wallet.java @@ -1,47 +1,47 @@ -package ru.prisonlife.prisonapi.template; - -import java.sql.PreparedStatement; -import java.sql.ResultSet; -import java.sql.SQLException; - -import static ru.prisonlife.prisonapi.PrisonAPI.CONN; - -public class Wallet { - - private Prisoner me; - - Wallet(Prisoner prisoner) { - me = prisoner; - } - - public int getBalance() throws SQLException { - try (PreparedStatement statement = CONN.prepareStatement("SELECT WALLET FROM player_stats WHERE UUID = ?")) { - statement.setString(1, me.getPlayer().getUniqueId().toString()); - - try (ResultSet resultSet = statement.executeQuery()) { - if (resultSet.next()) { - return resultSet.getInt(1); - } else { - return 0; //TODO нужно или default значение, или exception - } - } - } - } - - public void addBalance(int value) throws SQLException { - setBalance(getBalance() + value); - } - - public void deposit(int value) throws SQLException { - setBalance(getBalance() - value); - } - - public void setBalance(int value) throws SQLException { - try (PreparedStatement statement = CONN.prepareStatement("UPDATE player_stats SET WALLET = ? WHERE UUID = ?")) { - statement.setDouble(1, value); - statement.setString(2, me.getPlayer().getUniqueId().toString()); - - statement.executeUpdate(); - } - } -} +package ru.prisonlife.prisonapi.template; + +import java.sql.PreparedStatement; +import java.sql.ResultSet; +import java.sql.SQLException; + +import static ru.prisonlife.prisonapi.PrisonAPI.CONN; + +public class Wallet { + + private Prisoner me; + + Wallet(Prisoner prisoner) { + me = prisoner; + } + + public int getBalance() throws SQLException { + try (PreparedStatement statement = CONN.prepareStatement("SELECT WALLET FROM player_stats WHERE UUID = ?")) { + statement.setString(1, me.getPlayer().getUniqueId().toString()); + + try (ResultSet resultSet = statement.executeQuery()) { + if (resultSet.next()) { + return resultSet.getInt(1); + } else { + return 0; //TODO нужно или default значение, или exception + } + } + } + } + + public void addBalance(int value) throws SQLException { + setBalance(getBalance() + value); + } + + public void deposit(int value) throws SQLException { + setBalance(getBalance() - value); + } + + public void setBalance(int value) throws SQLException { + try (PreparedStatement statement = CONN.prepareStatement("UPDATE player_stats SET WALLET = ? WHERE UUID = ?")) { + statement.setDouble(1, value); + statement.setString(2, me.getPlayer().getUniqueId().toString()); + + statement.executeUpdate(); + } + } +} From d045a84d4c700671d52c3ad4eceea910aa0d332f Mon Sep 17 00:00:00 2001 From: DmitriyMX Date: Sun, 24 Feb 2019 16:09:49 +0300 Subject: [PATCH 3/4] =?UTF-8?q?=D0=BF=D0=B5=D1=80=D0=B5=D0=BD=D0=BE=D1=81?= =?UTF-8?q?=20=D0=BD=D0=B0=D1=81=D1=82=D1=80=D0=BE=D0=B5=D0=BA=20=D0=B1?= =?UTF-8?q?=D0=B0=D0=B7=D1=8B=20=D0=B4=D0=B0=D0=BD=D1=8B=D0=B9=20=D0=B2=20?= =?UTF-8?q?=D0=BA=D0=BE=D0=BD=D1=84=D0=B8=D0=B3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/ru/prisonlife/PrisonAPI/PrisonAPI.java | 10 +++++++--- src/main/resources/config.yml | 4 ++++ 2 files changed, 11 insertions(+), 3 deletions(-) create mode 100644 src/main/resources/config.yml diff --git a/src/main/java/ru/prisonlife/PrisonAPI/PrisonAPI.java b/src/main/java/ru/prisonlife/PrisonAPI/PrisonAPI.java index 6dd553f..bbda40f 100644 --- a/src/main/java/ru/prisonlife/PrisonAPI/PrisonAPI.java +++ b/src/main/java/ru/prisonlife/PrisonAPI/PrisonAPI.java @@ -37,10 +37,14 @@ public class PrisonAPI extends JavaPlugin { public static Connection CONN; public void onEnable() { + saveDefaultConfig(); + + final String dbUrl = getConfig().getString("database.url"); + final String dbUser = getConfig().getString("database.user"); + final String dbPasswd = getConfig().getString("database.password"); + try { - CONN = DriverManager.getConnection( - "jdbc:mysql://localhost:3306/prison4life", - "root", "root"); + CONN = DriverManager.getConnection(dbUrl, dbUser, dbPasswd); } catch (SQLException e) { e.printStackTrace(); } diff --git a/src/main/resources/config.yml b/src/main/resources/config.yml new file mode 100644 index 0000000..b8b244a --- /dev/null +++ b/src/main/resources/config.yml @@ -0,0 +1,4 @@ +database: + url: jdbc:mysql://localhost:3306/prison4life + user: root + password: root \ No newline at end of file From b9364a25b5e810f548927a8bf00b5ef7be31dda8 Mon Sep 17 00:00:00 2001 From: DmitriyMX Date: Sun, 24 Feb 2019 21:26:23 +0300 Subject: [PATCH 4/4] =?UTF-8?q?=D0=BE=D1=82=D0=B4=D0=B5=D0=BB=D1=8F=D0=B5?= =?UTF-8?q?=D0=BC=20=D1=80=D0=B0=D0=B1=D0=BE=D1=82=D1=83=20=D1=81=20=D0=B1?= =?UTF-8?q?=D0=B0=D0=B7=D0=BE=D0=B9=20=D0=BE=D1=82=20=D0=B4=D0=B0=D0=BD?= =?UTF-8?q?=D0=BD=D1=8B=D1=85?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../ru/prisonlife/PrisonAPI/PrisonAPI.java | 51 +++++- .../PrisonAPI/store/AbstractCachedDao.java | 90 ++++++++++ .../PrisonAPI/store/CachedDaoTask.java | 17 ++ .../PrisonAPI/store/DailyRespectTaskDao.java | 43 +++++ .../store/DailyRespectTaskEntity.java | 39 +++++ .../PrisonAPI/store/PlayerStatsDao.java | 71 ++++++++ .../PrisonAPI/store/PlayerStatsEntity.java | 140 ++++++++++++++++ .../prisonlife/PrisonAPI/store/SQLStore.java | 23 +++ .../PrisonAPI/template/AbstractApiData.java | 31 ++++ .../PrisonAPI/template/Faction.java | 158 ++++++------------ .../prisonlife/PrisonAPI/template/Level.java | 52 ++---- .../PrisonAPI/template/Prisoner.java | 11 +- .../prisonlife/PrisonAPI/template/Score.java | 39 +---- .../prisonlife/PrisonAPI/template/Stats.java | 139 ++++----------- .../prisonlife/PrisonAPI/template/Wallet.java | 41 +---- 15 files changed, 608 insertions(+), 337 deletions(-) create mode 100644 src/main/java/ru/prisonlife/PrisonAPI/store/AbstractCachedDao.java create mode 100644 src/main/java/ru/prisonlife/PrisonAPI/store/CachedDaoTask.java create mode 100644 src/main/java/ru/prisonlife/PrisonAPI/store/DailyRespectTaskDao.java create mode 100644 src/main/java/ru/prisonlife/PrisonAPI/store/DailyRespectTaskEntity.java create mode 100644 src/main/java/ru/prisonlife/PrisonAPI/store/PlayerStatsDao.java create mode 100644 src/main/java/ru/prisonlife/PrisonAPI/store/PlayerStatsEntity.java create mode 100644 src/main/java/ru/prisonlife/PrisonAPI/store/SQLStore.java create mode 100644 src/main/java/ru/prisonlife/PrisonAPI/template/AbstractApiData.java diff --git a/src/main/java/ru/prisonlife/PrisonAPI/PrisonAPI.java b/src/main/java/ru/prisonlife/PrisonAPI/PrisonAPI.java index bbda40f..56449d6 100644 --- a/src/main/java/ru/prisonlife/PrisonAPI/PrisonAPI.java +++ b/src/main/java/ru/prisonlife/PrisonAPI/PrisonAPI.java @@ -1,14 +1,22 @@ package ru.prisonlife.prisonapi; import org.bukkit.plugin.java.JavaPlugin; +import org.bukkit.scheduler.BukkitTask; +import ru.prisonlife.prisonapi.store.CachedDaoTask; +import ru.prisonlife.prisonapi.store.DailyRespectTaskDao; +import ru.prisonlife.prisonapi.store.PlayerStatsDao; +import ru.prisonlife.prisonapi.store.SQLStore; -import java.sql.Connection; -import java.sql.DriverManager; import java.sql.SQLException; public class PrisonAPI extends JavaPlugin { public static final String PLUGIN_PATH = "plugins/PrisonManager"; + private static SQLStore sqlStore; + private static PlayerStatsDao playerStatsDao; + private static DailyRespectTaskDao dailyRespectTaskDao; + private BukkitTask playeryStatsDaoTask; + private BukkitTask dailyRespectTaskDaoTask; public enum Factions { NONE(0), ASIANS(1), LATINOS(2), NIGGAZ(3), POLICE(4); @@ -34,8 +42,19 @@ public class PrisonAPI extends JavaPlugin { } } - public static Connection CONN; + public static SQLStore getSqlStore() { + return sqlStore; + } + public static PlayerStatsDao getPlayerStatsDao() { + return playerStatsDao; + } + + public static DailyRespectTaskDao getDailyRespectTaskDao() { + return dailyRespectTaskDao; + } + + @Override public void onEnable() { saveDefaultConfig(); @@ -44,19 +63,37 @@ public class PrisonAPI extends JavaPlugin { final String dbPasswd = getConfig().getString("database.password"); try { - CONN = DriverManager.getConnection(dbUrl, dbUser, dbPasswd); + sqlStore = new SQLStore(dbUrl, dbUser, dbPasswd); + playerStatsDao = new PlayerStatsDao(sqlStore); + dailyRespectTaskDao = new DailyRespectTaskDao(sqlStore); + + playeryStatsDaoTask = new CachedDaoTask(playerStatsDao) + .runTaskLaterAsynchronously(this, 20 * (60 * 1000)); + dailyRespectTaskDaoTask = new CachedDaoTask(dailyRespectTaskDao) + .runTaskLaterAsynchronously(this, 20 * (60 * 1000)); } catch (SQLException e) { e.printStackTrace(); } } + @Override public void onDisable() { + playeryStatsDaoTask.cancel(); + dailyRespectTaskDaoTask.cancel(); + try { - if (!CONN.isClosed()) { - CONN.close(); + if (!sqlStore.getConnection().isClosed()) { + sqlStore.close(); } - } catch (SQLException e) { + } catch (Exception e) { e.printStackTrace(); + } finally { + sqlStore = null; + playerStatsDao = null; + dailyRespectTaskDao = null; } + + playeryStatsDaoTask = null; + dailyRespectTaskDaoTask = null; } } diff --git a/src/main/java/ru/prisonlife/PrisonAPI/store/AbstractCachedDao.java b/src/main/java/ru/prisonlife/PrisonAPI/store/AbstractCachedDao.java new file mode 100644 index 0000000..20cc9d3 --- /dev/null +++ b/src/main/java/ru/prisonlife/PrisonAPI/store/AbstractCachedDao.java @@ -0,0 +1,90 @@ +package ru.prisonlife.prisonapi.store; + +import java.sql.Connection; +import java.sql.PreparedStatement; +import java.sql.ResultSet; +import java.sql.SQLException; +import java.util.*; +import java.util.concurrent.ConcurrentSkipListMap; + +public abstract class AbstractCachedDao implements AutoCloseable { + + private SQLStore store; + private Map cache = new ConcurrentSkipListMap<>(); + + public AbstractCachedDao(SQLStore store) { + this.store = store; + } + + public Optional findByUuid(UUID uuid) { + E entity; + if ((entity = cache.get(uuid)) != null) { + return Optional.of(entity); + } + + final Connection connection = store.getConnection(); + + try (final PreparedStatement statement = connection.prepareStatement(getSelectSQL())) { + statement.setString(1, uuid.toString()); + + try (ResultSet resultSet = statement.executeQuery()) { + if (resultSet.next()) { + entity = mappingResultEntity(resultSet); + + cache.put(uuid, entity); + return Optional.of(entity); + } else { + //FIXME need log + return Optional.empty(); + } + } + } catch (SQLException e) { + e.printStackTrace(); //FIXME need log + return Optional.empty(); + } + } + + /** + * Запись кэша в БД и последующий его сброс + */ + public void flush() { + if (cache.isEmpty()) { + return; + } + + //TODO думаю лучше будет разделить на порции по N обновлений за один раз + final String updateSql = generateUpdateSQL(); + final StringJoiner sj = new StringJoiner(";"); + for (int i = 0; i < cache.size(); i++) { + sj.add(updateSql); + } + + final Connection connection = store.getConnection(); + + try (final PreparedStatement statement = connection.prepareStatement(sj.toString())) { + Iterator iterator = cache.values().iterator(); + for (int i = 1; i < cache.size(); i += countFields()) { + final E entity = iterator.next(); + mappingUpdateEntity(statement, entity, i); + } + + statement.executeLargeUpdate(); + } catch (SQLException e) { + e.printStackTrace(); //FIXME need log + } + + cache.clear(); + } + + @Override + public void close() throws Exception { + store.close(); + } + + abstract String getSelectSQL(); + abstract E mappingResultEntity(ResultSet resultSet)throws SQLException; + + abstract String generateUpdateSQL(); + abstract int countFields(); + abstract void mappingUpdateEntity(final PreparedStatement statement, E entity, int index) throws SQLException; +} diff --git a/src/main/java/ru/prisonlife/PrisonAPI/store/CachedDaoTask.java b/src/main/java/ru/prisonlife/PrisonAPI/store/CachedDaoTask.java new file mode 100644 index 0000000..4eca539 --- /dev/null +++ b/src/main/java/ru/prisonlife/PrisonAPI/store/CachedDaoTask.java @@ -0,0 +1,17 @@ +package ru.prisonlife.prisonapi.store; + +import org.bukkit.scheduler.BukkitRunnable; + +public class CachedDaoTask extends BukkitRunnable { + + private AbstractCachedDao abstractCachedDao; + + public CachedDaoTask(AbstractCachedDao abstractCachedDao) { + this.abstractCachedDao = abstractCachedDao; + } + + @Override + public void run() { + abstractCachedDao.flush(); + } +} diff --git a/src/main/java/ru/prisonlife/PrisonAPI/store/DailyRespectTaskDao.java b/src/main/java/ru/prisonlife/PrisonAPI/store/DailyRespectTaskDao.java new file mode 100644 index 0000000..58f7d58 --- /dev/null +++ b/src/main/java/ru/prisonlife/PrisonAPI/store/DailyRespectTaskDao.java @@ -0,0 +1,43 @@ +package ru.prisonlife.prisonapi.store; + +import java.sql.PreparedStatement; +import java.sql.ResultSet; +import java.sql.SQLException; + +public class DailyRespectTaskDao extends AbstractCachedDao { + + public DailyRespectTaskDao(SQLStore store) { + super(store); + } + + @Override + String getSelectSQL() { + return "SELECT COOLDOWN FROM daily_respect_task WHERE UUID = ?"; + } + + @Override + DailyRespectTaskEntity mappingResultEntity(ResultSet resultSet) throws SQLException { + DailyRespectTaskEntity entity = new DailyRespectTaskEntity(); + entity.setCooldownRespect(resultSet.getInt("COOLDOWN")); + + return entity; + } + + @Override + String generateUpdateSQL() { + return "UPDATE daily_respect_task " + + "SET COOLDOWN = ? " + + "WHERE UUID = ?"; + } + + @Override + int countFields() { + return 1; + } + + @Override + void mappingUpdateEntity(PreparedStatement statement, DailyRespectTaskEntity entity, int index) throws SQLException { + statement.setInt(index, entity.getCooldownRespect()); + statement.setString(index + 1, entity.getUuid().toString()); + } +} diff --git a/src/main/java/ru/prisonlife/PrisonAPI/store/DailyRespectTaskEntity.java b/src/main/java/ru/prisonlife/PrisonAPI/store/DailyRespectTaskEntity.java new file mode 100644 index 0000000..57cbad2 --- /dev/null +++ b/src/main/java/ru/prisonlife/PrisonAPI/store/DailyRespectTaskEntity.java @@ -0,0 +1,39 @@ +package ru.prisonlife.prisonapi.store; + +import java.util.Objects; +import java.util.UUID; + +public class DailyRespectTaskEntity { + + private UUID uuid; + private int cooldownRespect; + + public UUID getUuid() { + return uuid; + } + + public void setUuid(UUID uuid) { + this.uuid = uuid; + } + + public int getCooldownRespect() { + return cooldownRespect; + } + + public void setCooldownRespect(int cooldownRespect) { + this.cooldownRespect = cooldownRespect; + } + + @Override + public boolean equals(Object o) { + if (this == o) return true; + if (o == null || getClass() != o.getClass()) return false; + DailyRespectTaskEntity that = (DailyRespectTaskEntity) o; + return Objects.equals(getUuid(), that.getUuid()); + } + + @Override + public int hashCode() { + return Objects.hash(getUuid()); + } +} diff --git a/src/main/java/ru/prisonlife/PrisonAPI/store/PlayerStatsDao.java b/src/main/java/ru/prisonlife/PrisonAPI/store/PlayerStatsDao.java new file mode 100644 index 0000000..323b603 --- /dev/null +++ b/src/main/java/ru/prisonlife/PrisonAPI/store/PlayerStatsDao.java @@ -0,0 +1,71 @@ +package ru.prisonlife.prisonapi.store; + +import ru.prisonlife.prisonapi.PrisonAPI.Factions; + +import java.sql.PreparedStatement; +import java.sql.ResultSet; +import java.sql.SQLException; +import java.util.UUID; + +public class PlayerStatsDao extends AbstractCachedDao { + + public PlayerStatsDao(SQLStore store) { + super(store); + } + + @Override + String getSelectSQL() { + return "SELECT * FROM player_stats WHERE UUID = ?"; + } + + @Override + PlayerStatsEntity mappingResultEntity(ResultSet resultSet) throws SQLException { + PlayerStatsEntity entity = new PlayerStatsEntity(); + entity.setUuid(UUID.fromString(resultSet.getString("UUID"))); + entity.setWallet(resultSet.getInt("WALLET")); + entity.setFaction(Factions.valueOf(resultSet.getString("FACTION"))); + entity.setRang(resultSet.getInt("F_RANG")); + entity.setRespect(resultSet.getInt("RESPECT")); + entity.setCooldownRespect(resultSet.getInt("COOLDOWN")); + entity.setPoints(resultSet.getDouble("POINTS")); + entity.setLevel(resultSet.getDouble("LEVEL")); + entity.setServerTime(resultSet.getInt("G_TIME")); + entity.setBlocks(resultSet.getInt("D_BLOCKS")); + entity.setPlayers(resultSet.getInt("K_PLAYERS")); + entity.setMobs(resultSet.getInt("K_MOBS")); + entity.setDeaths(resultSet.getInt("DEATHS")); + + return entity; + } + + @Override + String generateUpdateSQL() { + return "UPDATE player_stats " + + "SET WALLET = ?, FACTION = ?, F_RANG = ?, RESPECT = ?, " + + "COOLDOWN = ?, POINTS = ?, LEVEL = ?, G_TIME = ?, D_BLOCKS = ?, " + + "K_PLAYERS = ?, K_MOBS = ?, DEATHS = ? " + + "WHERE UUID = ?"; + } + + @Override + int countFields() { + return 13; + } + + @Override + void mappingUpdateEntity(PreparedStatement statement, PlayerStatsEntity entity, int index) throws SQLException { + statement.setInt(index, entity.getWallet()); + statement.setString(index + 1, entity.getFaction().name()); + statement.setInt(index + 2, entity.getRang()); + statement.setInt(index + 3, entity.getRespect()); + statement.setInt(index + 4, entity.getCooldownRespect()); + statement.setDouble(index + 5, entity.getPoints()); + statement.setDouble(index + 6, entity.getLevel()); + statement.setInt(index + 7, entity.getServerTime()); + statement.setInt(index + 8, entity.getBlocks()); + statement.setInt(index + 9, entity.getPlayers()); + statement.setInt(index + 10, entity.getMobs()); + statement.setInt(index + 11, entity.getDeaths()); + statement.setString(index + 12, entity.getUuid().toString()); + } +} diff --git a/src/main/java/ru/prisonlife/PrisonAPI/store/PlayerStatsEntity.java b/src/main/java/ru/prisonlife/PrisonAPI/store/PlayerStatsEntity.java new file mode 100644 index 0000000..33b92b7 --- /dev/null +++ b/src/main/java/ru/prisonlife/PrisonAPI/store/PlayerStatsEntity.java @@ -0,0 +1,140 @@ +package ru.prisonlife.prisonapi.store; + +import ru.prisonlife.prisonapi.PrisonAPI.Factions; + +import java.util.Objects; +import java.util.UUID; + +public class PlayerStatsEntity { + + private UUID uuid; + private Integer wallet; + private Factions faction; + private Integer rang; + private Integer respect; + private Integer cooldownRespect; + private Double points; + private Double level; + private Integer serverTime; + private Integer blocks; + private Integer players; + private Integer mobs; + private Integer deaths; + + public UUID getUuid() { + return uuid; + } + + public void setUuid(UUID uuid) { + this.uuid = uuid; + } + + public Integer getWallet() { + return wallet; + } + + public void setWallet(Integer wallet) { + this.wallet = wallet; + } + + public Factions getFaction() { + return faction; + } + + public void setFaction(Factions faction) { + this.faction = faction; + } + + public Integer getRang() { + return rang; + } + + public void setRang(Integer rang) { + this.rang = rang; + } + + public Integer getRespect() { + return respect; + } + + public void setRespect(Integer respect) { + this.respect = respect; + } + + public Integer getCooldownRespect() { + return cooldownRespect; + } + + public void setCooldownRespect(Integer cooldownRespect) { + this.cooldownRespect = cooldownRespect; + } + + public Double getPoints() { + return points; + } + + public void setPoints(Double points) { + this.points = points; + } + + public Double getLevel() { + return level; + } + + public void setLevel(Double level) { + this.level = level; + } + + @Override + public boolean equals(Object o) { + if (this == o) return true; + if (o == null || getClass() != o.getClass()) return false; + PlayerStatsEntity that = (PlayerStatsEntity) o; + return Objects.equals(getUuid(), that.getUuid()); + } + + @Override + public int hashCode() { + return Objects.hash(getUuid()); + } + + public int getServerTime() { + return serverTime; + } + + public void setServerTime(int serverTime) { + this.serverTime = serverTime; + } + + public int getBlocks() { + return blocks; + } + + public void setBlocks(int blocks) { + this.blocks = blocks; + } + + public int getPlayers() { + return players; + } + + public void setPlayers(int players) { + this.players = players; + } + + public int getMobs() { + return mobs; + } + + public void setMobs(int mobs) { + this.mobs = mobs; + } + + public Integer getDeaths() { + return deaths; + } + + public void setDeaths(Integer deaths) { + this.deaths = deaths; + } +} diff --git a/src/main/java/ru/prisonlife/PrisonAPI/store/SQLStore.java b/src/main/java/ru/prisonlife/PrisonAPI/store/SQLStore.java new file mode 100644 index 0000000..1c555b3 --- /dev/null +++ b/src/main/java/ru/prisonlife/PrisonAPI/store/SQLStore.java @@ -0,0 +1,23 @@ +package ru.prisonlife.prisonapi.store; + +import java.sql.Connection; +import java.sql.DriverManager; +import java.sql.SQLException; + +public class SQLStore implements AutoCloseable { + + private Connection connection; + + public SQLStore(String url, String user, String password) throws SQLException { + this.connection = DriverManager.getConnection(url, user, password); + } + + public Connection getConnection() { + return connection; + } + + @Override + public void close() throws Exception { + connection.close(); + } +} diff --git a/src/main/java/ru/prisonlife/PrisonAPI/template/AbstractApiData.java b/src/main/java/ru/prisonlife/PrisonAPI/template/AbstractApiData.java new file mode 100644 index 0000000..01571de --- /dev/null +++ b/src/main/java/ru/prisonlife/PrisonAPI/template/AbstractApiData.java @@ -0,0 +1,31 @@ +package ru.prisonlife.prisonapi.template; + +import ru.prisonlife.prisonapi.PrisonAPI; +import ru.prisonlife.prisonapi.store.PlayerStatsDao; +import ru.prisonlife.prisonapi.store.PlayerStatsEntity; + +import java.lang.ref.WeakReference; + +public abstract class AbstractApiData { + + protected Prisoner me; + private WeakReference refPlayerStatsEntity; + + public AbstractApiData(Prisoner me) { + this.me = me; + } + + protected PlayerStatsEntity getPlayerStatsEntity() { + PlayerStatsEntity entity; + if (refPlayerStatsEntity != null && (entity = refPlayerStatsEntity.get()) != null) { + return entity; + } else { + final PlayerStatsDao playerStatsDao = PrisonAPI.getPlayerStatsDao(); + entity = playerStatsDao.findByUuid(me.getPlayer().getUniqueId()) + .orElseThrow(() -> new RuntimeException( + "no data in db by '" + me.getPlayer().getUniqueId().toString() + "'")); + refPlayerStatsEntity = new WeakReference<>(entity); + return entity; + } + } +} diff --git a/src/main/java/ru/prisonlife/PrisonAPI/template/Faction.java b/src/main/java/ru/prisonlife/PrisonAPI/template/Faction.java index 78d4dbb..a7555b7 100644 --- a/src/main/java/ru/prisonlife/PrisonAPI/template/Faction.java +++ b/src/main/java/ru/prisonlife/PrisonAPI/template/Faction.java @@ -1,162 +1,98 @@ package ru.prisonlife.prisonapi.template; import org.bukkit.Bukkit; +import ru.prisonlife.prisonapi.PrisonAPI; import ru.prisonlife.prisonapi.PrisonAPI.Factions; +import ru.prisonlife.prisonapi.store.AbstractCachedDao; +import ru.prisonlife.prisonapi.store.DailyRespectTaskEntity; -import java.sql.PreparedStatement; -import java.sql.ResultSet; -import java.sql.SQLException; +import java.lang.ref.WeakReference; import java.util.List; import java.util.stream.Collectors; -import static ru.prisonlife.prisonapi.PrisonAPI.CONN; +public class Faction extends AbstractApiData { -public class Faction { + private WeakReference refDailyRespectTaskEntity; - private Prisoner me; + private static final String[][] post = { + {"Vakasu", "Tomadati", "So Honbute", "Vakagasira", "Kambu", "Kumite"}, + {"Novaro", "Amigo", "Latino", "Veterano", "Elite", "Padre"}, + {"Baby", "Cracker", "Little Nigga", "Big Nigga", "Star", "Daddy"}}; - Faction(Prisoner prisoner) { - me = prisoner; + public Faction(Prisoner me) { + super(me); } - public String getType() throws SQLException { - try (PreparedStatement statement = CONN.prepareStatement("SELECT FACTION FROM player_stats WHERE UUID = ?")) { - statement.setString(1, me.getPlayer().getUniqueId().toString()); - - try (ResultSet resultSet = statement.executeQuery()) { - if (resultSet.next()) { - return resultSet.getString(1); - } else { - return "null"; - } - } + private DailyRespectTaskEntity getDailyRespectTaskEntity() { + DailyRespectTaskEntity entity; + if (refDailyRespectTaskEntity != null && (entity = refDailyRespectTaskEntity.get()) != null) { + return entity; + } else { + final AbstractCachedDao cachedDao = PrisonAPI.getDailyRespectTaskDao(); + entity = cachedDao.findByUuid(me.getPlayer().getUniqueId()) + .orElseThrow(() -> new RuntimeException( + "no data in db by '" + me.getPlayer().getUniqueId().toString() + "'")); + refDailyRespectTaskEntity = new WeakReference<>(entity); + return entity; } } - public int getId() throws SQLException { - try (PreparedStatement statement = CONN.prepareStatement("SELECT FACTION FROM player_stats WHERE UUID = ?")) { - statement.setString(1, me.getPlayer().getUniqueId().toString()); - - try (ResultSet resultSet = statement.executeQuery()) { - if (resultSet.next()) { - return Factions.valueOf(resultSet.getString(1)).getId(); - } - } - } - - return 0; + public String getType() { + return getPlayerStatsEntity().getFaction().name(); } - public int getRangId() throws SQLException { - try (PreparedStatement statement = CONN.prepareStatement("SELECT F_RANG FROM player_stats WHERE UUID = ?")) { - statement.setString(1, me.getPlayer().getUniqueId().toString()); - - try (ResultSet resultSet = statement.executeQuery()) { - if (resultSet.next()) { - return resultSet.getInt(1); - } else { - return 0; - } - } - } + public int getId() { + return getPlayerStatsEntity().getFaction().getId(); } - public String getRangType() throws SQLException { - String[][] post = { - {"Vakasu", "Tomadati", "So Honbute", "Vakagasira", "Kambu", "Kumite"}, - {"Novaro", "Amigo", "Latino", "Veterano", "Elite", "Padre"}, - {"Baby", "Cracker", "Little Nigga", "Big Nigga", "Star", "Daddy"}}; + public int getRangId() { + return getPlayerStatsEntity().getRang(); + } + public String getRangType() { return post[getId() - 1][getRangId() - 1]; } - public void addRang(int value) throws SQLException { + public void addRang(int value) { setRang(getRangId() + value); } - public void setRang(int value) throws SQLException { - try (PreparedStatement statement = CONN.prepareStatement("UPDATE player_stats SET F_RANG = ? WHERE UUID = ?")) { - statement.setInt(1, value); - statement.setString(2, me.getPlayer().getUniqueId().toString()); - - statement.executeUpdate(); - } + public void setRang(int value) { + getPlayerStatsEntity().setRang(value); } - public void setFaction(int id) throws SQLException { - try (PreparedStatement statement = CONN.prepareStatement("UPDATE player_stats SET FACTION = ? WHERE UUID = ?")) { - statement.setInt(1, Factions.valueOf(id).getId()); - statement.setString(2, me.getPlayer().getUniqueId().toString()); - - statement.executeUpdate(); - } + public void setFaction(int id) { + getPlayerStatsEntity().setFaction(Factions.valueOf(id)); } - public int getRespect() throws SQLException { - try (PreparedStatement statement = CONN.prepareStatement("SELECT RESPECT FROM prison4life.player_stats WHERE UUID = ?")) { - statement.setString(1, me.getPlayer().getUniqueId().toString()); - - try (ResultSet resultSet = statement.executeQuery()) { - if (resultSet.next()) { - return resultSet.getInt(1); - } else { - return 0; - } - } - } + public int getRespect() { + return getPlayerStatsEntity().getRespect(); } - public void addRespect(int value) throws SQLException { + public void addRespect(int value) { setRespect(getRespect() + value); } - public void setRespect(int value) throws SQLException { - try (PreparedStatement statement = CONN.prepareStatement("UPDATE player_stats SET RESPECT = ? WHERE UUID = ?")) { - statement.setInt(1, value); - statement.setString(2, me.getPlayer().getUniqueId().toString()); - - statement.executeUpdate(); - } + public void setRespect(int value) { + getPlayerStatsEntity().setRespect(value); } - public int getRespectDailyCooldown() throws SQLException { - try (PreparedStatement statement = CONN.prepareStatement("SELECT COOLDOWN FROM daily_respect_task WHERE UUID = ?")) { - statement.setString(1, me.getPlayer().getUniqueId().toString()); - - try (ResultSet resultSet = statement.executeQuery()) { - if (resultSet.next()) { - return resultSet.getInt(1); - } else { - return 0; - } - } - } + public int getRespectDailyCooldown() { + return getDailyRespectTaskEntity().getCooldownRespect(); } - public void addRespectDailyCooldown(int value) throws SQLException { + public void addRespectDailyCooldown(int value) { setRespectDailyCooldown(getRespectDailyCooldown() + value); } - public void setRespectDailyCooldown(int value) throws SQLException { - try (PreparedStatement statement = CONN.prepareStatement("UPDATE daily_respect_task SET COOLDOWN = ? WHERE UUID = ?")) { - statement.setInt(1, value); - statement.setString(2, me.getPlayer().getUniqueId().toString()); - - statement.executeUpdate(); - } + public void setRespectDailyCooldown(int value) { + getDailyRespectTaskEntity().setCooldownRespect(value); } public List getOnlineList() { return Bukkit.getOnlinePlayers().stream() .map(Prisoner::new) - .filter(prisoner -> { - try { - return prisoner.getFaction().getType().equals(me.getFaction().getType()); - } catch (SQLException e) { - e.printStackTrace(); - return false; - } - }) + .filter(prisoner -> prisoner.getFaction().getType().equals(me.getFaction().getType())) .collect(Collectors.toList()); } } diff --git a/src/main/java/ru/prisonlife/PrisonAPI/template/Level.java b/src/main/java/ru/prisonlife/PrisonAPI/template/Level.java index 950cc39..329f4d0 100644 --- a/src/main/java/ru/prisonlife/PrisonAPI/template/Level.java +++ b/src/main/java/ru/prisonlife/PrisonAPI/template/Level.java @@ -1,57 +1,27 @@ package ru.prisonlife.prisonapi.template; -import java.sql.PreparedStatement; -import java.sql.ResultSet; -import java.sql.SQLException; +public class Level extends AbstractApiData { -import static ru.prisonlife.prisonapi.PrisonAPI.CONN; - -public class Level { - - private Prisoner me; private Score score; - Level(Prisoner prisoner) { - me = prisoner; - score = new Score(me); + public Level(Prisoner me) { + super(me); + this.score = new Score(me); } - public Score getScore() throws SQLException { - //FIXME приведи в порядок метод - /* - connect(); - if (resultSet.first()) { - return this.score; - } - disconnect(); - */ - return null; + public Score getScore() { + return score; } - public double getLevel() throws SQLException, NullPointerException { - try (PreparedStatement statement = CONN.prepareStatement("SELECT LEVEL FROM player_stats WHERE UUID = ?")) { - statement.setString(1, me.getPlayer().getUniqueId().toString()); - - try (ResultSet resultSet = statement.executeQuery()) { - if (resultSet.next()) { - return resultSet.getDouble(1); - } else { - return 1d; //TODO нужно или default значение, или exception - } - } - } + public double getLevel() { + return getPlayerStatsEntity().getLevel(); } - public void addLevel(double value) throws SQLException, NullPointerException { + public void addLevel(double value) { setLevel(getLevel() + value); } - public void setLevel(double value) throws SQLException, NullPointerException { - try (PreparedStatement statement = CONN.prepareStatement("UPDATE player_stats SET LEVEL = ? WHERE UUID = ?")) { - statement.setDouble(1, getLevel() + value); - statement.setString(2, me.getPlayer().getUniqueId().toString()); - - statement.executeUpdate(); - } + public void setLevel(double value) { + getPlayerStatsEntity().setLevel(value); } } diff --git a/src/main/java/ru/prisonlife/PrisonAPI/template/Prisoner.java b/src/main/java/ru/prisonlife/PrisonAPI/template/Prisoner.java index 39aec0f..84e9ab5 100644 --- a/src/main/java/ru/prisonlife/PrisonAPI/template/Prisoner.java +++ b/src/main/java/ru/prisonlife/PrisonAPI/template/Prisoner.java @@ -6,6 +6,7 @@ import org.bukkit.configuration.file.YamlConfiguration; import org.bukkit.entity.Player; import java.io.File; +import java.sql.Connection; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; @@ -13,8 +14,8 @@ import java.util.ArrayList; import java.util.List; import java.util.Objects; -import static ru.prisonlife.prisonapi.PrisonAPI.CONN; import static ru.prisonlife.prisonapi.PrisonAPI.PLUGIN_PATH; +import static ru.prisonlife.prisonapi.PrisonAPI.getSqlStore; public class Prisoner { @@ -120,14 +121,16 @@ public class Prisoner { } public void register() throws SQLException { - PreparedStatement statement = CONN.prepareStatement("SELECT UUID FROM player_stats WHERE UUID = ?"); + final Connection connection = getSqlStore().getConnection(); + + PreparedStatement statement = connection.prepareStatement("SELECT UUID FROM player_stats WHERE UUID = ?"); statement.setString(1, me.getUniqueId().toString()); try (ResultSet resultSet = statement.executeQuery()) { if (!resultSet.next()) { statement.close(); - statement = CONN.prepareStatement( + statement = connection.prepareStatement( "INSERT INTO player_stats(NAME, UUID, LEVEL, POINTS, FACTION, RESPECT, F_RANG, WALLET, " + "G_TIME, D_BLOCKS, K_PLAYERS, K_MOBS, DEATHS) " + "VALUES (?, ?, 1, 0, 'NONE', 0, 0, 0, 0, 0, 0, 0, 0)"); @@ -136,7 +139,7 @@ public class Prisoner { statement.executeUpdate(); statement.close(); - statement = CONN.prepareStatement( + statement = connection.prepareStatement( "INSERT INTO daily_respect_task(NAME, UUID, COOLDOWN) " + "VALUES (?, ?, ?)"); statement.setString(1, getPlayer().getName()); diff --git a/src/main/java/ru/prisonlife/PrisonAPI/template/Score.java b/src/main/java/ru/prisonlife/PrisonAPI/template/Score.java index 2f4e525..b753aec 100644 --- a/src/main/java/ru/prisonlife/PrisonAPI/template/Score.java +++ b/src/main/java/ru/prisonlife/PrisonAPI/template/Score.java @@ -1,43 +1,20 @@ package ru.prisonlife.prisonapi.template; -import java.sql.PreparedStatement; -import java.sql.ResultSet; -import java.sql.SQLException; +public class Score extends AbstractApiData { -import static ru.prisonlife.prisonapi.PrisonAPI.CONN; - -public class Score { - - private Prisoner me; - - Score(Prisoner prisoner) { - me = prisoner; + public Score(Prisoner me) { + super(me); } - public double getPoints() throws SQLException { - try (PreparedStatement statement = CONN.prepareStatement("SELECT POINTS FROM player_stats WHERE UUID = ?")) { - statement.setString(1, me.getPlayer().getUniqueId().toString()); - - try (ResultSet resultSet = statement.executeQuery()) { - if (resultSet.next()) { - return resultSet.getDouble(1); - } else { - return 0d; //TODO нужно или default значение, или exception - } - } - } + public double getPoints() { + return getPlayerStatsEntity().getPoints(); } - public void setPoints(double value) throws SQLException { - try (PreparedStatement statement = CONN.prepareStatement("UPDATE player_stats SET POINTS = ? WHERE UUID = ?")) { - statement.setDouble(1, value); - statement.setString(2, me.getPlayer().getUniqueId().toString()); - - statement.executeUpdate(); - } + public void setPoints(double value) { + getPlayerStatsEntity().setPoints(value); } - public void addPoints(double value) throws SQLException { + public void addPoints(double value) { setPoints(getPoints() + value); } } diff --git a/src/main/java/ru/prisonlife/PrisonAPI/template/Stats.java b/src/main/java/ru/prisonlife/PrisonAPI/template/Stats.java index cad3e4b..3726c22 100644 --- a/src/main/java/ru/prisonlife/PrisonAPI/template/Stats.java +++ b/src/main/java/ru/prisonlife/PrisonAPI/template/Stats.java @@ -1,151 +1,68 @@ package ru.prisonlife.prisonapi.template; -import java.sql.PreparedStatement; -import java.sql.ResultSet; -import java.sql.SQLException; +public class Stats extends AbstractApiData { -import static ru.prisonlife.prisonapi.PrisonAPI.CONN; - -public class Stats { - - private Prisoner me; - - Stats(Prisoner prisoner) { - me = prisoner; + public Stats(Prisoner me) { + super(me); } - public void setServerTime(int value) throws SQLException { - try (PreparedStatement statement = CONN.prepareStatement("UPDATE player_stats SET G_TIME = ? WHERE UUID = ?")) { - statement.setDouble(1, value); - statement.setString(2, me.getPlayer().getUniqueId().toString()); - - statement.executeUpdate(); - } + public void setServerTime(int value) { + getPlayerStatsEntity().setServerTime(value); } - public int getServerTime() throws SQLException { - try (PreparedStatement statement = CONN.prepareStatement("SELECT G_TIME FROM player_stats WHERE UUID = ?")) { - statement.setString(1, me.getPlayer().getUniqueId().toString()); - - try (ResultSet resultSet = statement.executeQuery()) { - if (resultSet.next()) { - return resultSet.getInt(1); - } else { - return 0; //TODO нужно или default значение, или exception - } - } - } + public int getServerTime() { + return getPlayerStatsEntity().getServerTime(); } - public void addServerTime(int value) throws SQLException { + public void addServerTime(int value) { setServerTime(getServerTime() + value); } - public void setBlocks(int value) throws SQLException { - try (PreparedStatement statement = CONN.prepareStatement("UPDATE player_stats SET B = ? WHERE UUID = ?")) { - statement.setDouble(1, value); - statement.setString(2, me.getPlayer().getUniqueId().toString()); - - statement.executeUpdate(); - } + public void setBlocks(int value) { + getPlayerStatsEntity().setBlocks(value); } - public int getBlocks() throws SQLException { - try (PreparedStatement statement = CONN.prepareStatement("SELECT D_BLOCKS FROM player_stats WHERE UUID = ?")) { - statement.setString(1, me.getPlayer().getUniqueId().toString()); - - try (ResultSet resultSet = statement.executeQuery()) { - if (resultSet.next()) { - return resultSet.getInt(1); - } else { - return 0; //TODO нужно или default значение, или exception - } - } - } + public int getBlocks() { + return getPlayerStatsEntity().getBlocks(); } - public void addBlocks(int value) throws SQLException { + public void addBlocks(int value) { setBlocks(getBlocks() + value); } - public void setPlayers(int value) throws SQLException { - try (PreparedStatement statement = CONN.prepareStatement("UPDATE player_stats SET K_PLAYERS = ? WHERE UUID = ?")) { - statement.setDouble(1, value); - statement.setString(2, me.getPlayer().getUniqueId().toString()); - - statement.executeUpdate(); - } + public void setPlayers(int value) { + getPlayerStatsEntity().setPlayers(value); } - public int getPlayers() throws SQLException { - try (PreparedStatement statement = CONN.prepareStatement("SELECT K_PLAYERS FROM player_stats WHERE UUID = ?")) { - statement.setString(1, me.getPlayer().getUniqueId().toString()); - - try (ResultSet resultSet = statement.executeQuery()) { - if (resultSet.next()) { - return resultSet.getInt(1); - } else { - return 0; //TODO нужно или default значение, или exception - } - } - } + public int getPlayers() { + return getPlayerStatsEntity().getPlayers(); } - public void addPlayers(int value) throws SQLException { + public void addPlayers(int value) { setPlayers(getPlayers() + value); } - public void setMobs(int value) throws SQLException { - try (PreparedStatement statement = CONN.prepareStatement("UPDATE player_stats SET K_MOBS = ? WHERE UUID = ?")) { - statement.setDouble(1, value); - statement.setString(2, me.getPlayer().getUniqueId().toString()); - - statement.executeUpdate(); - } + public void setMobs(int value) { + getPlayerStatsEntity().setMobs(value); } - public int getMobs() throws SQLException { - try (PreparedStatement statement = CONN.prepareStatement("SELECT K_MOBS FROM player_stats WHERE UUID = ?")) { - statement.setString(1, me.getPlayer().getUniqueId().toString()); - - try (ResultSet resultSet = statement.executeQuery()) { - if (resultSet.next()) { - return resultSet.getInt(1); - } else { - return 0; //TODO нужно или default значение, или exception - } - } - } + public int getMobs() { + return getPlayerStatsEntity().getMobs(); } - public void addMobs(int value) throws SQLException { + public void addMobs(int value) { setMobs(getMobs() + value); } - public void setDeaths(int value) throws SQLException { - try (PreparedStatement statement = CONN.prepareStatement("UPDATE player_stats SET DEATHS = ? WHERE UUID = ?")) { - statement.setDouble(1, value); - statement.setString(2, me.getPlayer().getUniqueId().toString()); - - statement.executeUpdate(); - } + public void setDeaths(int value) { + getPlayerStatsEntity().setDeaths(value); } - public int getDeaths() throws SQLException { - try (PreparedStatement statement = CONN.prepareStatement("SELECT DEATHS FROM player_stats WHERE UUID = ?")) { - statement.setString(1, me.getPlayer().getUniqueId().toString()); - - try (ResultSet resultSet = statement.executeQuery()) { - if (resultSet.next()) { - return resultSet.getInt(1); - } else { - return 0; //TODO нужно или default значение, или exception - } - } - } + public int getDeaths() { + return getPlayerStatsEntity().getDeaths(); } - public void addDeaths(int value) throws SQLException { + public void addDeaths(int value) { setDeaths(getDeaths() + value); } } diff --git a/src/main/java/ru/prisonlife/PrisonAPI/template/Wallet.java b/src/main/java/ru/prisonlife/PrisonAPI/template/Wallet.java index 2ee483c..cad55e3 100644 --- a/src/main/java/ru/prisonlife/PrisonAPI/template/Wallet.java +++ b/src/main/java/ru/prisonlife/PrisonAPI/template/Wallet.java @@ -1,47 +1,24 @@ package ru.prisonlife.prisonapi.template; -import java.sql.PreparedStatement; -import java.sql.ResultSet; -import java.sql.SQLException; +public class Wallet extends AbstractApiData { -import static ru.prisonlife.prisonapi.PrisonAPI.CONN; - -public class Wallet { - - private Prisoner me; - - Wallet(Prisoner prisoner) { - me = prisoner; + public Wallet(Prisoner me) { + super(me); } - public int getBalance() throws SQLException { - try (PreparedStatement statement = CONN.prepareStatement("SELECT WALLET FROM player_stats WHERE UUID = ?")) { - statement.setString(1, me.getPlayer().getUniqueId().toString()); - - try (ResultSet resultSet = statement.executeQuery()) { - if (resultSet.next()) { - return resultSet.getInt(1); - } else { - return 0; //TODO нужно или default значение, или exception - } - } - } + public int getBalance() { + return getPlayerStatsEntity().getWallet(); } - public void addBalance(int value) throws SQLException { + public void addBalance(int value) { setBalance(getBalance() + value); } - public void deposit(int value) throws SQLException { + public void deposit(int value) { setBalance(getBalance() - value); } - public void setBalance(int value) throws SQLException { - try (PreparedStatement statement = CONN.prepareStatement("UPDATE player_stats SET WALLET = ? WHERE UUID = ?")) { - statement.setDouble(1, value); - statement.setString(2, me.getPlayer().getUniqueId().toString()); - - statement.executeUpdate(); - } + public void setBalance(int value) { + getPlayerStatsEntity().setWallet(value); } }