Merge branch 'dmitriymx/dev' into dev
This commit is contained in:
1
.gitattributes
vendored
Normal file
1
.gitattributes
vendored
Normal file
@@ -0,0 +1 @@
|
||||
* text=auto
|
||||
@@ -3,17 +3,25 @@ package ru.prisonlife.api;
|
||||
import org.bukkit.Bukkit;
|
||||
import org.bukkit.plugin.Plugin;
|
||||
import org.bukkit.plugin.java.JavaPlugin;
|
||||
import org.bukkit.scheduler.BukkitTask;
|
||||
import ru.prisonlife.api.store.CachedDaoTask;
|
||||
import ru.prisonlife.api.store.DailyRespectTaskDao;
|
||||
import ru.prisonlife.api.store.PlayerStatsDao;
|
||||
import ru.prisonlife.api.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";
|
||||
|
||||
public static final Plugin plugin = Bukkit.getPluginManager().getPlugin("PrisonAPI");
|
||||
|
||||
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);
|
||||
|
||||
@@ -38,25 +46,58 @@ 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();
|
||||
|
||||
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");
|
||||
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;
|
||||
}
|
||||
}
|
||||
|
||||
90
src/main/java/ru/prisonlife/api/store/AbstractCachedDao.java
Normal file
90
src/main/java/ru/prisonlife/api/store/AbstractCachedDao.java
Normal file
@@ -0,0 +1,90 @@
|
||||
package ru.prisonlife.api.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<E> implements AutoCloseable {
|
||||
|
||||
private SQLStore store;
|
||||
private Map<UUID, E> cache = new ConcurrentSkipListMap<>();
|
||||
|
||||
public AbstractCachedDao(SQLStore store) {
|
||||
this.store = store;
|
||||
}
|
||||
|
||||
public Optional<E> 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<E> 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;
|
||||
}
|
||||
17
src/main/java/ru/prisonlife/api/store/CachedDaoTask.java
Normal file
17
src/main/java/ru/prisonlife/api/store/CachedDaoTask.java
Normal file
@@ -0,0 +1,17 @@
|
||||
package ru.prisonlife.api.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();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,43 @@
|
||||
package ru.prisonlife.api.store;
|
||||
|
||||
import java.sql.PreparedStatement;
|
||||
import java.sql.ResultSet;
|
||||
import java.sql.SQLException;
|
||||
|
||||
public class DailyRespectTaskDao extends AbstractCachedDao<DailyRespectTaskEntity> {
|
||||
|
||||
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());
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,39 @@
|
||||
package ru.prisonlife.api.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());
|
||||
}
|
||||
}
|
||||
72
src/main/java/ru/prisonlife/api/store/PlayerStatsDao.java
Normal file
72
src/main/java/ru/prisonlife/api/store/PlayerStatsDao.java
Normal file
@@ -0,0 +1,72 @@
|
||||
package ru.prisonlife.api.store;
|
||||
|
||||
|
||||
import ru.prisonlife.api.PrisonAPI.Factions;
|
||||
|
||||
import java.sql.PreparedStatement;
|
||||
import java.sql.ResultSet;
|
||||
import java.sql.SQLException;
|
||||
import java.util.UUID;
|
||||
|
||||
public class PlayerStatsDao extends AbstractCachedDao<PlayerStatsEntity> {
|
||||
|
||||
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());
|
||||
}
|
||||
}
|
||||
140
src/main/java/ru/prisonlife/api/store/PlayerStatsEntity.java
Normal file
140
src/main/java/ru/prisonlife/api/store/PlayerStatsEntity.java
Normal file
@@ -0,0 +1,140 @@
|
||||
package ru.prisonlife.api.store;
|
||||
|
||||
import ru.prisonlife.api.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;
|
||||
}
|
||||
}
|
||||
23
src/main/java/ru/prisonlife/api/store/SQLStore.java
Normal file
23
src/main/java/ru/prisonlife/api/store/SQLStore.java
Normal file
@@ -0,0 +1,23 @@
|
||||
package ru.prisonlife.api.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();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,31 @@
|
||||
package ru.prisonlife.api.template;
|
||||
|
||||
import ru.prisonlife.api.PrisonAPI;
|
||||
import ru.prisonlife.api.store.PlayerStatsDao;
|
||||
import ru.prisonlife.api.store.PlayerStatsEntity;
|
||||
|
||||
import java.lang.ref.WeakReference;
|
||||
|
||||
public abstract class AbstractApiData {
|
||||
|
||||
protected Prisoner me;
|
||||
private WeakReference<PlayerStatsEntity> 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;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -2,159 +2,97 @@ package ru.prisonlife.api.template;
|
||||
|
||||
import org.bukkit.Bukkit;
|
||||
import ru.prisonlife.api.PrisonAPI;
|
||||
import ru.prisonlife.api.PrisonAPI.Factions;
|
||||
import ru.prisonlife.api.store.AbstractCachedDao;
|
||||
import ru.prisonlife.api.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;
|
||||
|
||||
public class Faction {
|
||||
public class Faction extends AbstractApiData {
|
||||
|
||||
private Prisoner me;
|
||||
private WeakReference<DailyRespectTaskEntity> refDailyRespectTaskEntity;
|
||||
|
||||
Faction(Prisoner prisoner) {
|
||||
me = prisoner;
|
||||
}
|
||||
|
||||
public String getType() throws SQLException {
|
||||
try (PreparedStatement statement = PrisonAPI.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";
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public int getId() throws SQLException {
|
||||
try (PreparedStatement statement = PrisonAPI.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 PrisonAPI.Factions.valueOf(resultSet.getString(1)).getId();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
public int getRangId() throws SQLException {
|
||||
try (PreparedStatement statement = PrisonAPI.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 String getRangType() throws SQLException {
|
||||
String[][] post = {
|
||||
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"}};
|
||||
|
||||
public Faction(Prisoner me) {
|
||||
super(me);
|
||||
}
|
||||
|
||||
private DailyRespectTaskEntity getDailyRespectTaskEntity() {
|
||||
DailyRespectTaskEntity entity;
|
||||
if (refDailyRespectTaskEntity != null && (entity = refDailyRespectTaskEntity.get()) != null) {
|
||||
return entity;
|
||||
} else {
|
||||
final AbstractCachedDao<DailyRespectTaskEntity> 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 String getType() {
|
||||
return getPlayerStatsEntity().getFaction().name();
|
||||
}
|
||||
|
||||
public int getId() {
|
||||
return getPlayerStatsEntity().getFaction().getId();
|
||||
}
|
||||
|
||||
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 = PrisonAPI.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 = PrisonAPI.CONN.prepareStatement("UPDATE player_stats SET FACTION = ? WHERE UUID = ?")) {
|
||||
statement.setString(1, PrisonAPI.Factions.valueOf(id).name());
|
||||
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 = PrisonAPI.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 = PrisonAPI.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 = PrisonAPI.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 = PrisonAPI.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<Prisoner> 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());
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,49 +1,27 @@
|
||||
package ru.prisonlife.api.template;
|
||||
|
||||
import ru.prisonlife.api.PrisonAPI;
|
||||
public class Level extends AbstractApiData {
|
||||
|
||||
import java.sql.PreparedStatement;
|
||||
import java.sql.ResultSet;
|
||||
import java.sql.SQLException;
|
||||
|
||||
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 {
|
||||
public Score getScore() {
|
||||
return score;
|
||||
}
|
||||
|
||||
public double getLevel() throws SQLException, NullPointerException {
|
||||
try (PreparedStatement statement = PrisonAPI.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 = PrisonAPI.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);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -10,6 +10,7 @@ import org.bukkit.scheduler.BukkitRunnable;
|
||||
import ru.prisonlife.api.PrisonAPI;
|
||||
|
||||
import java.io.File;
|
||||
import java.sql.Connection;
|
||||
import java.sql.PreparedStatement;
|
||||
import java.sql.ResultSet;
|
||||
import java.sql.SQLException;
|
||||
@@ -27,16 +28,18 @@ public class Policeman extends Prisoner {
|
||||
}
|
||||
|
||||
public void arrestPrisoner(Prisoner prs) throws SQLException {
|
||||
final Connection connection = PrisonAPI.getSqlStore().getConnection();
|
||||
|
||||
if(prs.getPlayer().isOnline()) {
|
||||
Manager mng = new Manager(super.getPrison());
|
||||
List<String> all_cages = mng.getCages();
|
||||
PreparedStatement st = PrisonAPI.CONN.prepareStatement("SELECT UUID FROM player_stats WHERE UUID = ?");
|
||||
PreparedStatement st = connection.prepareStatement("SELECT UUID FROM player_stats WHERE UUID = ?");
|
||||
st.setString(1, me.getUniqueId().toString());
|
||||
try(ResultSet resultSet = st.executeQuery()) {
|
||||
if(!resultSet.next()) {
|
||||
int su = prs.getSuspect();
|
||||
st.close();
|
||||
st = PrisonAPI.CONN.prepareStatement(
|
||||
st = connection.prepareStatement(
|
||||
"INSERT INTO players_in_cages(NAME, UUID, TIME_IN_CAGE, BLOCKS_LEFT) " +
|
||||
"VALUES (?, ?, ?, ?)");
|
||||
st.setString(1, getPlayer().getName());
|
||||
@@ -64,7 +67,7 @@ public class Policeman extends Prisoner {
|
||||
try {
|
||||
int time = 0;
|
||||
String uuid = prs.getPlayer().getUniqueId().toString();
|
||||
PreparedStatement st = PrisonAPI.CONN.prepareStatement(
|
||||
PreparedStatement st = connection.prepareStatement(
|
||||
"SELECT TIME_IN_CAGE FROM players_in_cages WHERE UUID = ?");
|
||||
st.setString(1, uuid);
|
||||
try(ResultSet resultSet = st.executeQuery()) {
|
||||
@@ -77,12 +80,12 @@ public class Policeman extends Prisoner {
|
||||
actionBar.cancel();
|
||||
cancel();
|
||||
timeInCage.remove(prs);
|
||||
st = PrisonAPI.CONN.prepareStatement("DELETE FROM players_in_cages WHERE UUID = ?");
|
||||
st = connection.prepareStatement("DELETE FROM players_in_cages WHERE UUID = ?");
|
||||
st.setString(1, uuid);
|
||||
st.execute();
|
||||
} else {
|
||||
if(prs.getPlayer().isOnline()) {
|
||||
st = PrisonAPI.CONN.prepareStatement("UPDATE players_in_cages SET TIME_IN_CAGE = " + (time - 1) + " WHERE UUID = ?");
|
||||
st = connection.prepareStatement("UPDATE players_in_cages SET TIME_IN_CAGE = " + (time - 1) + " WHERE UUID = ?");
|
||||
st.setString(1, uuid);
|
||||
st.execute();
|
||||
}
|
||||
|
||||
@@ -8,6 +8,7 @@ import org.bukkit.entity.Player;
|
||||
import ru.prisonlife.api.PrisonAPI;
|
||||
|
||||
import java.io.File;
|
||||
import java.sql.Connection;
|
||||
import java.sql.PreparedStatement;
|
||||
import java.sql.ResultSet;
|
||||
import java.sql.SQLException;
|
||||
@@ -15,6 +16,8 @@ import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.Objects;
|
||||
|
||||
import static ru.prisonlife.api.PrisonAPI.getSqlStore;
|
||||
|
||||
public class Prisoner {
|
||||
|
||||
protected Player me;
|
||||
@@ -35,7 +38,7 @@ public class Prisoner {
|
||||
return me;
|
||||
}
|
||||
|
||||
public Level getPrisonLevel() throws SQLException {
|
||||
public Level getPrisonLevel() {
|
||||
return this.level;
|
||||
}
|
||||
|
||||
@@ -94,10 +97,11 @@ public class Prisoner {
|
||||
}
|
||||
}
|
||||
}
|
||||
if(c == 3)
|
||||
if(c == 3) {
|
||||
return fileConfiguration.getString("TERRITORIES." + terr + ".NAME");
|
||||
}
|
||||
}
|
||||
}
|
||||
} catch (NullPointerException z) {
|
||||
}
|
||||
}
|
||||
@@ -105,25 +109,21 @@ public class Prisoner {
|
||||
}
|
||||
|
||||
public void teleportToFactionSpawn() {
|
||||
try {
|
||||
Prisoner ps = new Prisoner(me);
|
||||
Manager mng = new Manager(getPrison());
|
||||
String f = ps.getFaction().getType();
|
||||
List<Integer> c = mng.getSpawnCords(ps.getFaction().getId());
|
||||
if(!f.equals("NONE") && c.size() != 0)
|
||||
if (!f.equals("NONE") && c.size() != 0) {
|
||||
me.teleport(new Location(Bukkit.getWorld(mng.getWorld()), c.get(0) + 0.5, c.get(1), c.get(2) + 0.5));
|
||||
} catch(SQLException z) {
|
||||
z.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
public int getSuspect() {
|
||||
try {
|
||||
try(PreparedStatement statement = PrisonAPI.CONN.prepareStatement("SELECT SUSPECT FROM player_stats WHERE UUID = ?")) {
|
||||
try(PreparedStatement statement = getSqlStore().getConnection().prepareStatement("SELECT SUSPECT FROM player_stats WHERE UUID = ?")) {
|
||||
statement.setString(1, me.getPlayer().getUniqueId().toString());
|
||||
|
||||
try(ResultSet resultSet = statement.executeQuery()) {
|
||||
if(resultSet.next())
|
||||
if(resultSet.next()) {
|
||||
return resultSet.getInt(1);
|
||||
}
|
||||
}
|
||||
@@ -134,13 +134,11 @@ public class Prisoner {
|
||||
}
|
||||
|
||||
public void setSuspect(int value) {
|
||||
try {
|
||||
try(PreparedStatement statement = PrisonAPI.CONN.prepareStatement("UPDATE player_stats SET SUSPECT = ? WHERE UUID = ?")) {
|
||||
try(PreparedStatement statement = getSqlStore().getConnection().prepareStatement("UPDATE player_stats SET SUSPECT = ? WHERE UUID = ?")) {
|
||||
statement.setInt(1, value);
|
||||
statement.setString(2, me.getPlayer().getUniqueId().toString());
|
||||
|
||||
statement.executeUpdate();
|
||||
}
|
||||
} catch(SQLException z) {
|
||||
z.printStackTrace();
|
||||
}
|
||||
@@ -149,18 +147,20 @@ public class Prisoner {
|
||||
public void addSuspect(int value) {
|
||||
int su = getSuspect() + value;
|
||||
setSuspect(su);
|
||||
if(su > 6)
|
||||
if(su > 6) {
|
||||
setSuspect(su - (su - 6));
|
||||
}
|
||||
}
|
||||
|
||||
public int getTimeLeftInCage() {
|
||||
try {
|
||||
PreparedStatement statement = PrisonAPI.CONN.prepareStatement("SELECT TIME_IN_CAGE FROM players_in_cages WHERE UUID = ?");
|
||||
try (PreparedStatement statement = getSqlStore().getConnection().prepareStatement("SELECT TIME_IN_CAGE FROM players_in_cages WHERE UUID = ?")) {
|
||||
statement.setString(1, me.getUniqueId().toString());
|
||||
|
||||
try(ResultSet resultSet = statement.executeQuery()) {
|
||||
if(resultSet.next())
|
||||
if(resultSet.next()) {
|
||||
return resultSet.getInt(1);
|
||||
}
|
||||
}
|
||||
} catch (SQLException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
@@ -169,14 +169,18 @@ public class Prisoner {
|
||||
|
||||
public void setTimeLeftInCage(int value) {
|
||||
try {
|
||||
PreparedStatement statement = PrisonAPI.CONN.prepareStatement("SELECT TIME_IN_CAGE FROM players_in_cages WHERE UUID = ?");
|
||||
PreparedStatement statement = getSqlStore().getConnection().prepareStatement("SELECT TIME_IN_CAGE FROM players_in_cages WHERE UUID = ?");
|
||||
statement.setString(1, me.getUniqueId().toString());
|
||||
try(ResultSet resultSet = statement.executeQuery()) {
|
||||
if(resultSet.next()) {
|
||||
statement = PrisonAPI.CONN.prepareStatement("UPDATE players_in_cages SET TIME_IN_CAGE = ? WHERE UUID = ?");
|
||||
statement.close();
|
||||
|
||||
statement = getSqlStore().getConnection().prepareStatement("UPDATE players_in_cages SET TIME_IN_CAGE = ? WHERE UUID = ?");
|
||||
statement.setInt(1, value);
|
||||
statement.setString(2, me.getUniqueId().toString());
|
||||
statement.execute();
|
||||
|
||||
statement.close();
|
||||
}
|
||||
}
|
||||
} catch(SQLException z) {
|
||||
@@ -185,13 +189,14 @@ public class Prisoner {
|
||||
}
|
||||
|
||||
public int getBlocksLeftToBreak() {
|
||||
try {
|
||||
PreparedStatement statement = PrisonAPI.CONN.prepareStatement("SELECT BLOCKS_LEFT FROM players_in_cages WHERE UUID = ?");
|
||||
try (PreparedStatement statement = getSqlStore().getConnection().prepareStatement("SELECT BLOCKS_LEFT FROM players_in_cages WHERE UUID = ?")) {
|
||||
statement.setString(1, me.getUniqueId().toString());
|
||||
|
||||
try(ResultSet resultSet = statement.executeQuery()) {
|
||||
if(resultSet.next())
|
||||
if(resultSet.next()) {
|
||||
return resultSet.getInt(1);
|
||||
}
|
||||
}
|
||||
} catch (SQLException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
@@ -199,13 +204,14 @@ public class Prisoner {
|
||||
}
|
||||
|
||||
public boolean isLocateInCage() {
|
||||
try {
|
||||
PreparedStatement statement = PrisonAPI.CONN.prepareStatement("SELECT UUID FROM players_in_cages WHERE UUID = ?");
|
||||
try (PreparedStatement statement = getSqlStore().getConnection().prepareStatement("SELECT UUID FROM players_in_cages WHERE UUID = ?")) {
|
||||
statement.setString(1, me.getUniqueId().toString());
|
||||
|
||||
try(ResultSet resultSet = statement.executeQuery()) {
|
||||
if(resultSet.next())
|
||||
if(resultSet.next()) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
} catch (SQLException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
@@ -225,14 +231,16 @@ public class Prisoner {
|
||||
}
|
||||
|
||||
public void register() throws SQLException {
|
||||
PreparedStatement statement = PrisonAPI.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 = PrisonAPI.CONN.prepareStatement(
|
||||
statement = connection.prepareStatement(
|
||||
"INSERT INTO player_stats(NAME, UUID, LEVEL, POINTS, FACTION, RESPECT, F_RANG, SUSPECT, WALLET, " +
|
||||
"G_TIME, D_BLOCKS, K_PLAYERS, K_MOBS, DEATHS) "
|
||||
+ "VALUES (?, ?, 1, 0, 'NONE', 0, 0, 0, 0, 0, 0, 0, 0, 0)");
|
||||
@@ -241,7 +249,7 @@ public class Prisoner {
|
||||
statement.executeUpdate();
|
||||
statement.close();
|
||||
|
||||
statement = PrisonAPI.CONN.prepareStatement(
|
||||
statement = connection.prepareStatement(
|
||||
"INSERT INTO daily_respect_task(NAME, UUID, COOLDOWN) " +
|
||||
"VALUES (?, ?, ?)");
|
||||
statement.setString(1, getPlayer().getName());
|
||||
|
||||
@@ -1,43 +1,20 @@
|
||||
package ru.prisonlife.api.template;
|
||||
|
||||
import ru.prisonlife.api.PrisonAPI;
|
||||
public class Score extends AbstractApiData {
|
||||
|
||||
import java.sql.PreparedStatement;
|
||||
import java.sql.ResultSet;
|
||||
import java.sql.SQLException;
|
||||
|
||||
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 = PrisonAPI.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 = PrisonAPI.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);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,151 +1,68 @@
|
||||
package ru.prisonlife.api.template;
|
||||
|
||||
import ru.prisonlife.api.PrisonAPI;
|
||||
public class Stats extends AbstractApiData {
|
||||
|
||||
import java.sql.PreparedStatement;
|
||||
import java.sql.ResultSet;
|
||||
import java.sql.SQLException;
|
||||
|
||||
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 = PrisonAPI.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 = PrisonAPI.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 = PrisonAPI.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 = PrisonAPI.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 = PrisonAPI.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 = PrisonAPI.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 = PrisonAPI.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 = PrisonAPI.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 = PrisonAPI.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 = PrisonAPI.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);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,47 +1,24 @@
|
||||
package ru.prisonlife.api.template;
|
||||
|
||||
import ru.prisonlife.api.PrisonAPI;
|
||||
public class Wallet extends AbstractApiData {
|
||||
|
||||
import java.sql.PreparedStatement;
|
||||
import java.sql.ResultSet;
|
||||
import java.sql.SQLException;
|
||||
|
||||
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 = PrisonAPI.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 = PrisonAPI.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);
|
||||
}
|
||||
}
|
||||
|
||||
4
src/main/resources/config.yml
Normal file
4
src/main/resources/config.yml
Normal file
@@ -0,0 +1,4 @@
|
||||
database:
|
||||
url: jdbc:mysql://localhost:3306/prison4life
|
||||
user: root
|
||||
password: root
|
||||
Reference in New Issue
Block a user