diff --git a/src/main/java/ru/prisonlife/PrisonAPI/template/Level.java b/src/main/java/ru/prisonlife/PrisonAPI/template/Level.java new file mode 100644 index 0000000..e81b274 --- /dev/null +++ b/src/main/java/ru/prisonlife/PrisonAPI/template/Level.java @@ -0,0 +1,58 @@ +package ru.prisonlife.PrisonAPI.template; + +import ru.prisonlife.PrisonAPI.connect.DataBasePrison4Life; + +import java.sql.PreparedStatement; +import java.sql.ResultSet; +import java.sql.SQLException; + +public class Level extends DataBasePrison4Life { + private PreparedStatement state; + private ResultSet r_set; + private Prisoner me; + private Score score; + Level(Prisoner prisoner) { me = prisoner; } + + public Score getScore() throws SQLException { + setInResUUID(); + if(r_set.first()) + return this.score; + disconnect(); + return null; + } + + public double getLevel() throws SQLException, NullPointerException { + setInResUUID(); + if(r_set.first()) { + r_set = state.executeQuery("SELECT LEVEL FROM PLAYERS_LEVEL WHERE UUID = '" + me.getPlayer().getUniqueId() + "'"); + if(r_set.first()) + return r_set.getDouble(1); + } + disconnect(); + return NullPointerException.class.hashCode(); + } + + public void addLevel(double value) throws SQLException, NullPointerException { + setInResUUID(); + if(r_set.first()) + state.execute("UPDATE PLAYERS_LEVEL SET LEVEL = " + (getLevel() + value) + " WHERE UUID = '" + me.getPlayer().getUniqueId() + "'"); + disconnect(); + } + + public void setLevel(double value) throws SQLException, NullPointerException { + setInResUUID(); + if(r_set.first()) + state.execute("UPDATE PLAYERS_LEVEL SET LEVEL = " + value + " WHERE UUID = '" + me.getPlayer().getUniqueId() + "'"); + } + + private void setInResUUID() throws SQLException { + state = connection.prepareStatement("SELECT * UUID FROM ECONOMY WHERE UUID = ?;"); + state.setString(1, me.getPlayer().getUniqueId().toString()); + r_set = state.executeQuery("SELECT UUID FROM ECONOMY WHERE UUID = '" + me.getPlayer().getUniqueId() + "'"); + } + + private void disconnect() throws SQLException { + r_set.close(); + state.close(); + } +} diff --git a/src/main/java/ru/prisonlife/PrisonAPI/template/Prisoner.java b/src/main/java/ru/prisonlife/PrisonAPI/template/Prisoner.java index 8b13789..792653e 100644 --- a/src/main/java/ru/prisonlife/PrisonAPI/template/Prisoner.java +++ b/src/main/java/ru/prisonlife/PrisonAPI/template/Prisoner.java @@ -1 +1,80 @@ - +package ru.prisonlife.PrisonAPI.template; + +import org.bukkit.entity.Player; +import ru.prisonlife.PrisonAPI.connect.DataBasePrison4Life; + +import java.sql.*; + +public class Prisoner extends DataBasePrison4Life { + private Player me; + private Wallet wallet; + private Level level; + private PreparedStatement state; + private ResultSet r_set; + + public Prisoner(Player player) throws SQLException { + me = player; + setInResUUID(); + try { + if(r_set.next()) + wallet = new Wallet(this); + } catch(NullPointerException z) {} + } + + public Player getPlayer() { return me; } + + public Wallet getWallet() { return this.wallet; } + + public void setWallet() throws SQLException { + setInResUUID(); + if(!r_set.next()) { + state.execute("INSERT INTO ECONOMY(NAME, UUID, WALLET) VALUES (" + + "'" + getPlayer().getName() + "', '" + me.getUniqueId() + "', 0);"); + wallet = new Wallet(this); + } + disconnect(); + } + + public void delWallet() throws SQLException { + setInResUUID(); + if(r_set.first()) + state.execute("DELETE FROM ECONOMY WHERE UUID = '" + me.getUniqueId() + "'"); + disconnect(); + } + + public Level getPrisonLevel() throws SQLException { + setInResUUID(); + if(r_set.first()) + return this.level; + disconnect(); + return null; + } + + public void setPrisonLevel() throws SQLException { + setInResUUID(); + if(!r_set.next()) { + state.execute("INSERT INTO PLAYERS_LEVEL(NAME, UUID, POINTS, LEVEL) VALUES (" + + "'" + me.getName() + "', '" + me.getUniqueId() + "', 0, 1);"); + level = new Level(this); + } + disconnect(); + } + + public void delPrisonLevel() throws SQLException { + setInResUUID(); + if(r_set.first()) { + state.execute("DELETE FROM PLAYERS_LEVEL WHERE UUID = '" + me.getUniqueId() + "'"); + } + } + + private void setInResUUID() throws SQLException { + state = connection.prepareStatement("SELECT * UUID FROM ECONOMY WHERE UUID = ?;"); + state.setString(1, me.getUniqueId().toString()); + r_set = state.executeQuery("SELECT UUID FROM ECONOMY WHERE UUID = '" + me.getUniqueId() + "'"); + } + + private void disconnect() throws SQLException { + r_set.close(); + state.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 new file mode 100644 index 0000000..5fe81d5 --- /dev/null +++ b/src/main/java/ru/prisonlife/PrisonAPI/template/Score.java @@ -0,0 +1,51 @@ +package ru.prisonlife.PrisonAPI.template; + +import ru.prisonlife.PrisonAPI.connect.DataBasePrison4Life; + +import java.sql.PreparedStatement; +import java.sql.ResultSet; +import java.sql.SQLException; + +public class Score extends DataBasePrison4Life { + private PreparedStatement state; + private ResultSet r_set; + private Prisoner me; + Score(Prisoner prisoner) { me = prisoner; } + + public double getPoints() throws SQLException { + setInResUUID(); + if(r_set.first()) { + r_set = state.executeQuery("SELECT POINTS FROM PLAYERS_LEVEL WHERE UUID = '" + me.getPlayer().getUniqueId() + "'"); + if(r_set.first()) { + return r_set.getDouble(1); + } + } + disconnect(); + return NullPointerException.class.hashCode(); + } + + public void setPoints(double value) throws SQLException { + setInResUUID(); + if(r_set.first()) + state.execute("UPDATE PLAYERS_LEVEL SET POINTS = " + value + " WHERE UUID = '" + me.getPlayer().getUniqueId() + "'"); + disconnect(); + } + + public void addPoints(double value) throws SQLException { + setInResUUID(); + if(r_set.first()) + state.execute("UPDATE PLAYERS_LEVEL SET POINTS = " + (getPoints() + value) + " WHERE UUID = '" + me.getPlayer().getUniqueId() + "'"); + disconnect(); + } + + private void setInResUUID() throws SQLException { + state = connection.prepareStatement("SELECT * UUID FROM ECONOMY WHERE UUID = ?;"); + state.setString(1, me.getPlayer().getUniqueId().toString()); + r_set = state.executeQuery("SELECT UUID FROM ECONOMY WHERE UUID = '" + me.getPlayer().getUniqueId() + "'"); + } + + private void disconnect() throws SQLException { + r_set.close(); + state.close(); + } +} diff --git a/src/main/java/ru/prisonlife/PrisonAPI/template/Wallet.java b/src/main/java/ru/prisonlife/PrisonAPI/template/Wallet.java new file mode 100644 index 0000000..aed2f2c --- /dev/null +++ b/src/main/java/ru/prisonlife/PrisonAPI/template/Wallet.java @@ -0,0 +1,60 @@ +package ru.prisonlife.PrisonAPI.template; + +import ru.prisonlife.PrisonAPI.connect.DataBasePrison4Life; + +import java.sql.PreparedStatement; +import java.sql.ResultSet; +import java.sql.SQLException; + +public class Wallet extends DataBasePrison4Life { + private PreparedStatement state; + private ResultSet r_set; + private Prisoner me; + Wallet(Prisoner prisoner) { me = prisoner; } + + public int getBalance() throws SQLException { + setInResUUID(); + if(r_set.first()) { + r_set = state.executeQuery("SELECT WALLET FROM ECONOMY WHERE UUID = '" + me.getPlayer().getUniqueId() + "'"); + if(r_set.first()) { + return r_set.getInt(1); + } + disconnect(); + } + return NullPointerException.class.hashCode(); + } + + public void addBalance(int value) throws SQLException { + setInResUUID(); + if(r_set.first()) { + state.execute("UPDATE ECONOMY SET WALLET = " + (getBalance() + value) + " WHERE UUID = '" + me.getPlayer().getUniqueId() + "'"); + disconnect(); + } + } + + public void deposit(int value) throws SQLException { + setInResUUID(); + if(r_set.first()) { + state.execute("UPDATE ECONOMY SET WALLET = " + (getBalance() - value) + " WHERE UUID = '" + me.getPlayer().getUniqueId() + "'"); + } + } + + public void setBalance(int value) throws SQLException { + setInResUUID(); + if(r_set.first()) { + state.execute("UPDATE ECONOMY SET WALLET = " + value + " WHERE UUID = '" + me.getPlayer().getUniqueId() + "'"); + disconnect(); + } + } + + private void setInResUUID() throws SQLException { + state = connection.prepareStatement("SELECT * UUID FROM ECONOMY WHERE UUID = ?;"); + state.setString(1, me.getPlayer().getUniqueId().toString()); + r_set = state.executeQuery("SELECT UUID FROM ECONOMY WHERE UUID = '" + me.getPlayer().getUniqueId() + "'"); + } + + private void disconnect() throws SQLException { + r_set.close(); + state.close(); + } +}