отделяем работу с базой от данных
This commit is contained in:
@@ -1,14 +1,22 @@
|
|||||||
package ru.prisonlife.prisonapi;
|
package ru.prisonlife.prisonapi;
|
||||||
|
|
||||||
import org.bukkit.plugin.java.JavaPlugin;
|
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;
|
import java.sql.SQLException;
|
||||||
|
|
||||||
public class PrisonAPI extends JavaPlugin {
|
public class PrisonAPI extends JavaPlugin {
|
||||||
|
|
||||||
public static final String PLUGIN_PATH = "plugins/PrisonManager";
|
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 {
|
public enum Factions {
|
||||||
NONE(0), ASIANS(1), LATINOS(2), NIGGAZ(3), POLICE(4);
|
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() {
|
public void onEnable() {
|
||||||
saveDefaultConfig();
|
saveDefaultConfig();
|
||||||
|
|
||||||
@@ -44,19 +63,37 @@ public class PrisonAPI extends JavaPlugin {
|
|||||||
final String dbPasswd = getConfig().getString("database.password");
|
final String dbPasswd = getConfig().getString("database.password");
|
||||||
|
|
||||||
try {
|
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) {
|
} catch (SQLException e) {
|
||||||
e.printStackTrace();
|
e.printStackTrace();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
public void onDisable() {
|
public void onDisable() {
|
||||||
|
playeryStatsDaoTask.cancel();
|
||||||
|
dailyRespectTaskDaoTask.cancel();
|
||||||
|
|
||||||
try {
|
try {
|
||||||
if (!CONN.isClosed()) {
|
if (!sqlStore.getConnection().isClosed()) {
|
||||||
CONN.close();
|
sqlStore.close();
|
||||||
}
|
}
|
||||||
} catch (SQLException e) {
|
} catch (Exception e) {
|
||||||
e.printStackTrace();
|
e.printStackTrace();
|
||||||
|
} finally {
|
||||||
|
sqlStore = null;
|
||||||
|
playerStatsDao = null;
|
||||||
|
dailyRespectTaskDao = null;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
playeryStatsDaoTask = null;
|
||||||
|
dailyRespectTaskDaoTask = null;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -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<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;
|
||||||
|
}
|
||||||
@@ -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();
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -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<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.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());
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -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<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());
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -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;
|
||||||
|
}
|
||||||
|
}
|
||||||
23
src/main/java/ru/prisonlife/PrisonAPI/store/SQLStore.java
Normal file
23
src/main/java/ru/prisonlife/PrisonAPI/store/SQLStore.java
Normal file
@@ -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();
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -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<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;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -1,162 +1,98 @@
|
|||||||
package ru.prisonlife.prisonapi.template;
|
package ru.prisonlife.prisonapi.template;
|
||||||
|
|
||||||
import org.bukkit.Bukkit;
|
import org.bukkit.Bukkit;
|
||||||
|
import ru.prisonlife.prisonapi.PrisonAPI;
|
||||||
import ru.prisonlife.prisonapi.PrisonAPI.Factions;
|
import ru.prisonlife.prisonapi.PrisonAPI.Factions;
|
||||||
|
import ru.prisonlife.prisonapi.store.AbstractCachedDao;
|
||||||
|
import ru.prisonlife.prisonapi.store.DailyRespectTaskEntity;
|
||||||
|
|
||||||
import java.sql.PreparedStatement;
|
import java.lang.ref.WeakReference;
|
||||||
import java.sql.ResultSet;
|
|
||||||
import java.sql.SQLException;
|
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.stream.Collectors;
|
import java.util.stream.Collectors;
|
||||||
|
|
||||||
import static ru.prisonlife.prisonapi.PrisonAPI.CONN;
|
public class Faction extends AbstractApiData {
|
||||||
|
|
||||||
public class Faction {
|
private WeakReference<DailyRespectTaskEntity> refDailyRespectTaskEntity;
|
||||||
|
|
||||||
private Prisoner me;
|
private static final String[][] post = {
|
||||||
|
|
||||||
Faction(Prisoner prisoner) {
|
|
||||||
me = prisoner;
|
|
||||||
}
|
|
||||||
|
|
||||||
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";
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
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 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 String getRangType() throws SQLException {
|
|
||||||
String[][] post = {
|
|
||||||
{"Vakasu", "Tomadati", "So Honbute", "Vakagasira", "Kambu", "Kumite"},
|
{"Vakasu", "Tomadati", "So Honbute", "Vakagasira", "Kambu", "Kumite"},
|
||||||
{"Novaro", "Amigo", "Latino", "Veterano", "Elite", "Padre"},
|
{"Novaro", "Amigo", "Latino", "Veterano", "Elite", "Padre"},
|
||||||
{"Baby", "Cracker", "Little Nigga", "Big Nigga", "Star", "Daddy"}};
|
{"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];
|
return post[getId() - 1][getRangId() - 1];
|
||||||
}
|
}
|
||||||
|
|
||||||
public void addRang(int value) throws SQLException {
|
public void addRang(int value) {
|
||||||
setRang(getRangId() + value);
|
setRang(getRangId() + value);
|
||||||
}
|
}
|
||||||
|
|
||||||
public void setRang(int value) throws SQLException {
|
public void setRang(int value) {
|
||||||
try (PreparedStatement statement = CONN.prepareStatement("UPDATE player_stats SET F_RANG = ? WHERE UUID = ?")) {
|
getPlayerStatsEntity().setRang(value);
|
||||||
statement.setInt(1, value);
|
|
||||||
statement.setString(2, me.getPlayer().getUniqueId().toString());
|
|
||||||
|
|
||||||
statement.executeUpdate();
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public void setFaction(int id) throws SQLException {
|
public void setFaction(int id) {
|
||||||
try (PreparedStatement statement = CONN.prepareStatement("UPDATE player_stats SET FACTION = ? WHERE UUID = ?")) {
|
getPlayerStatsEntity().setFaction(Factions.valueOf(id));
|
||||||
statement.setInt(1, Factions.valueOf(id).getId());
|
|
||||||
statement.setString(2, me.getPlayer().getUniqueId().toString());
|
|
||||||
|
|
||||||
statement.executeUpdate();
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public int getRespect() throws SQLException {
|
public int getRespect() {
|
||||||
try (PreparedStatement statement = CONN.prepareStatement("SELECT RESPECT FROM prison4life.player_stats WHERE UUID = ?")) {
|
return getPlayerStatsEntity().getRespect();
|
||||||
statement.setString(1, me.getPlayer().getUniqueId().toString());
|
|
||||||
|
|
||||||
try (ResultSet resultSet = statement.executeQuery()) {
|
|
||||||
if (resultSet.next()) {
|
|
||||||
return resultSet.getInt(1);
|
|
||||||
} else {
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public void addRespect(int value) throws SQLException {
|
public void addRespect(int value) {
|
||||||
setRespect(getRespect() + value);
|
setRespect(getRespect() + value);
|
||||||
}
|
}
|
||||||
|
|
||||||
public void setRespect(int value) throws SQLException {
|
public void setRespect(int value) {
|
||||||
try (PreparedStatement statement = CONN.prepareStatement("UPDATE player_stats SET RESPECT = ? WHERE UUID = ?")) {
|
getPlayerStatsEntity().setRespect(value);
|
||||||
statement.setInt(1, value);
|
|
||||||
statement.setString(2, me.getPlayer().getUniqueId().toString());
|
|
||||||
|
|
||||||
statement.executeUpdate();
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public int getRespectDailyCooldown() throws SQLException {
|
public int getRespectDailyCooldown() {
|
||||||
try (PreparedStatement statement = CONN.prepareStatement("SELECT COOLDOWN FROM daily_respect_task WHERE UUID = ?")) {
|
return getDailyRespectTaskEntity().getCooldownRespect();
|
||||||
statement.setString(1, me.getPlayer().getUniqueId().toString());
|
|
||||||
|
|
||||||
try (ResultSet resultSet = statement.executeQuery()) {
|
|
||||||
if (resultSet.next()) {
|
|
||||||
return resultSet.getInt(1);
|
|
||||||
} else {
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public void addRespectDailyCooldown(int value) throws SQLException {
|
public void addRespectDailyCooldown(int value) {
|
||||||
setRespectDailyCooldown(getRespectDailyCooldown() + value);
|
setRespectDailyCooldown(getRespectDailyCooldown() + value);
|
||||||
}
|
}
|
||||||
|
|
||||||
public void setRespectDailyCooldown(int value) throws SQLException {
|
public void setRespectDailyCooldown(int value) {
|
||||||
try (PreparedStatement statement = CONN.prepareStatement("UPDATE daily_respect_task SET COOLDOWN = ? WHERE UUID = ?")) {
|
getDailyRespectTaskEntity().setCooldownRespect(value);
|
||||||
statement.setInt(1, value);
|
|
||||||
statement.setString(2, me.getPlayer().getUniqueId().toString());
|
|
||||||
|
|
||||||
statement.executeUpdate();
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public List<Prisoner> getOnlineList() {
|
public List<Prisoner> getOnlineList() {
|
||||||
return Bukkit.getOnlinePlayers().stream()
|
return Bukkit.getOnlinePlayers().stream()
|
||||||
.map(Prisoner::new)
|
.map(Prisoner::new)
|
||||||
.filter(prisoner -> {
|
.filter(prisoner -> prisoner.getFaction().getType().equals(me.getFaction().getType()))
|
||||||
try {
|
|
||||||
return prisoner.getFaction().getType().equals(me.getFaction().getType());
|
|
||||||
} catch (SQLException e) {
|
|
||||||
e.printStackTrace();
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
})
|
|
||||||
.collect(Collectors.toList());
|
.collect(Collectors.toList());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,57 +1,27 @@
|
|||||||
package ru.prisonlife.prisonapi.template;
|
package ru.prisonlife.prisonapi.template;
|
||||||
|
|
||||||
import java.sql.PreparedStatement;
|
public class Level extends AbstractApiData {
|
||||||
import java.sql.ResultSet;
|
|
||||||
import java.sql.SQLException;
|
|
||||||
|
|
||||||
import static ru.prisonlife.prisonapi.PrisonAPI.CONN;
|
|
||||||
|
|
||||||
public class Level {
|
|
||||||
|
|
||||||
private Prisoner me;
|
|
||||||
private Score score;
|
private Score score;
|
||||||
|
|
||||||
Level(Prisoner prisoner) {
|
public Level(Prisoner me) {
|
||||||
me = prisoner;
|
super(me);
|
||||||
score = new Score(me);
|
this.score = new Score(me);
|
||||||
}
|
}
|
||||||
|
|
||||||
public Score getScore() throws SQLException {
|
public Score getScore() {
|
||||||
//FIXME приведи в порядок метод
|
return score;
|
||||||
/*
|
|
||||||
connect();
|
|
||||||
if (resultSet.first()) {
|
|
||||||
return this.score;
|
|
||||||
}
|
|
||||||
disconnect();
|
|
||||||
*/
|
|
||||||
return null;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public double getLevel() throws SQLException, NullPointerException {
|
public double getLevel() {
|
||||||
try (PreparedStatement statement = CONN.prepareStatement("SELECT LEVEL FROM player_stats WHERE UUID = ?")) {
|
return getPlayerStatsEntity().getLevel();
|
||||||
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 {
|
public void addLevel(double value) {
|
||||||
setLevel(getLevel() + value);
|
setLevel(getLevel() + value);
|
||||||
}
|
}
|
||||||
|
|
||||||
public void setLevel(double value) throws SQLException, NullPointerException {
|
public void setLevel(double value) {
|
||||||
try (PreparedStatement statement = CONN.prepareStatement("UPDATE player_stats SET LEVEL = ? WHERE UUID = ?")) {
|
getPlayerStatsEntity().setLevel(value);
|
||||||
statement.setDouble(1, getLevel() + value);
|
|
||||||
statement.setString(2, me.getPlayer().getUniqueId().toString());
|
|
||||||
|
|
||||||
statement.executeUpdate();
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -6,6 +6,7 @@ import org.bukkit.configuration.file.YamlConfiguration;
|
|||||||
import org.bukkit.entity.Player;
|
import org.bukkit.entity.Player;
|
||||||
|
|
||||||
import java.io.File;
|
import java.io.File;
|
||||||
|
import java.sql.Connection;
|
||||||
import java.sql.PreparedStatement;
|
import java.sql.PreparedStatement;
|
||||||
import java.sql.ResultSet;
|
import java.sql.ResultSet;
|
||||||
import java.sql.SQLException;
|
import java.sql.SQLException;
|
||||||
@@ -13,8 +14,8 @@ import java.util.ArrayList;
|
|||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.Objects;
|
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.PLUGIN_PATH;
|
||||||
|
import static ru.prisonlife.prisonapi.PrisonAPI.getSqlStore;
|
||||||
|
|
||||||
public class Prisoner {
|
public class Prisoner {
|
||||||
|
|
||||||
@@ -120,14 +121,16 @@ public class Prisoner {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public void register() throws SQLException {
|
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());
|
statement.setString(1, me.getUniqueId().toString());
|
||||||
|
|
||||||
try (ResultSet resultSet = statement.executeQuery()) {
|
try (ResultSet resultSet = statement.executeQuery()) {
|
||||||
if (!resultSet.next()) {
|
if (!resultSet.next()) {
|
||||||
statement.close();
|
statement.close();
|
||||||
|
|
||||||
statement = CONN.prepareStatement(
|
statement = connection.prepareStatement(
|
||||||
"INSERT INTO player_stats(NAME, UUID, LEVEL, POINTS, FACTION, RESPECT, F_RANG, WALLET, " +
|
"INSERT INTO player_stats(NAME, UUID, LEVEL, POINTS, FACTION, RESPECT, F_RANG, WALLET, " +
|
||||||
"G_TIME, D_BLOCKS, K_PLAYERS, K_MOBS, DEATHS) "
|
"G_TIME, D_BLOCKS, K_PLAYERS, K_MOBS, DEATHS) "
|
||||||
+ "VALUES (?, ?, 1, 0, 'NONE', 0, 0, 0, 0, 0, 0, 0, 0)");
|
+ "VALUES (?, ?, 1, 0, 'NONE', 0, 0, 0, 0, 0, 0, 0, 0)");
|
||||||
@@ -136,7 +139,7 @@ public class Prisoner {
|
|||||||
statement.executeUpdate();
|
statement.executeUpdate();
|
||||||
statement.close();
|
statement.close();
|
||||||
|
|
||||||
statement = CONN.prepareStatement(
|
statement = connection.prepareStatement(
|
||||||
"INSERT INTO daily_respect_task(NAME, UUID, COOLDOWN) " +
|
"INSERT INTO daily_respect_task(NAME, UUID, COOLDOWN) " +
|
||||||
"VALUES (?, ?, ?)");
|
"VALUES (?, ?, ?)");
|
||||||
statement.setString(1, getPlayer().getName());
|
statement.setString(1, getPlayer().getName());
|
||||||
|
|||||||
@@ -1,43 +1,20 @@
|
|||||||
package ru.prisonlife.prisonapi.template;
|
package ru.prisonlife.prisonapi.template;
|
||||||
|
|
||||||
import java.sql.PreparedStatement;
|
public class Score extends AbstractApiData {
|
||||||
import java.sql.ResultSet;
|
|
||||||
import java.sql.SQLException;
|
|
||||||
|
|
||||||
import static ru.prisonlife.prisonapi.PrisonAPI.CONN;
|
public Score(Prisoner me) {
|
||||||
|
super(me);
|
||||||
public class Score {
|
|
||||||
|
|
||||||
private Prisoner me;
|
|
||||||
|
|
||||||
Score(Prisoner prisoner) {
|
|
||||||
me = prisoner;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public double getPoints() throws SQLException {
|
public double getPoints() {
|
||||||
try (PreparedStatement statement = CONN.prepareStatement("SELECT POINTS FROM player_stats WHERE UUID = ?")) {
|
return getPlayerStatsEntity().getPoints();
|
||||||
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 {
|
public void setPoints(double value) {
|
||||||
try (PreparedStatement statement = CONN.prepareStatement("UPDATE player_stats SET POINTS = ? WHERE UUID = ?")) {
|
getPlayerStatsEntity().setPoints(value);
|
||||||
statement.setDouble(1, value);
|
|
||||||
statement.setString(2, me.getPlayer().getUniqueId().toString());
|
|
||||||
|
|
||||||
statement.executeUpdate();
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public void addPoints(double value) throws SQLException {
|
public void addPoints(double value) {
|
||||||
setPoints(getPoints() + value);
|
setPoints(getPoints() + value);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,151 +1,68 @@
|
|||||||
package ru.prisonlife.prisonapi.template;
|
package ru.prisonlife.prisonapi.template;
|
||||||
|
|
||||||
import java.sql.PreparedStatement;
|
public class Stats extends AbstractApiData {
|
||||||
import java.sql.ResultSet;
|
|
||||||
import java.sql.SQLException;
|
|
||||||
|
|
||||||
import static ru.prisonlife.prisonapi.PrisonAPI.CONN;
|
public Stats(Prisoner me) {
|
||||||
|
super(me);
|
||||||
public class Stats {
|
|
||||||
|
|
||||||
private Prisoner me;
|
|
||||||
|
|
||||||
Stats(Prisoner prisoner) {
|
|
||||||
me = prisoner;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public void setServerTime(int value) throws SQLException {
|
public void setServerTime(int value) {
|
||||||
try (PreparedStatement statement = CONN.prepareStatement("UPDATE player_stats SET G_TIME = ? WHERE UUID = ?")) {
|
getPlayerStatsEntity().setServerTime(value);
|
||||||
statement.setDouble(1, value);
|
|
||||||
statement.setString(2, me.getPlayer().getUniqueId().toString());
|
|
||||||
|
|
||||||
statement.executeUpdate();
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public int getServerTime() throws SQLException {
|
public int getServerTime() {
|
||||||
try (PreparedStatement statement = CONN.prepareStatement("SELECT G_TIME FROM player_stats WHERE UUID = ?")) {
|
return getPlayerStatsEntity().getServerTime();
|
||||||
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 addServerTime(int value) throws SQLException {
|
public void addServerTime(int value) {
|
||||||
setServerTime(getServerTime() + value);
|
setServerTime(getServerTime() + value);
|
||||||
}
|
}
|
||||||
|
|
||||||
public void setBlocks(int value) throws SQLException {
|
public void setBlocks(int value) {
|
||||||
try (PreparedStatement statement = CONN.prepareStatement("UPDATE player_stats SET B = ? WHERE UUID = ?")) {
|
getPlayerStatsEntity().setBlocks(value);
|
||||||
statement.setDouble(1, value);
|
|
||||||
statement.setString(2, me.getPlayer().getUniqueId().toString());
|
|
||||||
|
|
||||||
statement.executeUpdate();
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public int getBlocks() throws SQLException {
|
public int getBlocks() {
|
||||||
try (PreparedStatement statement = CONN.prepareStatement("SELECT D_BLOCKS FROM player_stats WHERE UUID = ?")) {
|
return getPlayerStatsEntity().getBlocks();
|
||||||
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 addBlocks(int value) throws SQLException {
|
public void addBlocks(int value) {
|
||||||
setBlocks(getBlocks() + value);
|
setBlocks(getBlocks() + value);
|
||||||
}
|
}
|
||||||
|
|
||||||
public void setPlayers(int value) throws SQLException {
|
public void setPlayers(int value) {
|
||||||
try (PreparedStatement statement = CONN.prepareStatement("UPDATE player_stats SET K_PLAYERS = ? WHERE UUID = ?")) {
|
getPlayerStatsEntity().setPlayers(value);
|
||||||
statement.setDouble(1, value);
|
|
||||||
statement.setString(2, me.getPlayer().getUniqueId().toString());
|
|
||||||
|
|
||||||
statement.executeUpdate();
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public int getPlayers() throws SQLException {
|
public int getPlayers() {
|
||||||
try (PreparedStatement statement = CONN.prepareStatement("SELECT K_PLAYERS FROM player_stats WHERE UUID = ?")) {
|
return getPlayerStatsEntity().getPlayers();
|
||||||
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 addPlayers(int value) throws SQLException {
|
public void addPlayers(int value) {
|
||||||
setPlayers(getPlayers() + value);
|
setPlayers(getPlayers() + value);
|
||||||
}
|
}
|
||||||
|
|
||||||
public void setMobs(int value) throws SQLException {
|
public void setMobs(int value) {
|
||||||
try (PreparedStatement statement = CONN.prepareStatement("UPDATE player_stats SET K_MOBS = ? WHERE UUID = ?")) {
|
getPlayerStatsEntity().setMobs(value);
|
||||||
statement.setDouble(1, value);
|
|
||||||
statement.setString(2, me.getPlayer().getUniqueId().toString());
|
|
||||||
|
|
||||||
statement.executeUpdate();
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public int getMobs() throws SQLException {
|
public int getMobs() {
|
||||||
try (PreparedStatement statement = CONN.prepareStatement("SELECT K_MOBS FROM player_stats WHERE UUID = ?")) {
|
return getPlayerStatsEntity().getMobs();
|
||||||
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 addMobs(int value) throws SQLException {
|
public void addMobs(int value) {
|
||||||
setMobs(getMobs() + value);
|
setMobs(getMobs() + value);
|
||||||
}
|
}
|
||||||
|
|
||||||
public void setDeaths(int value) throws SQLException {
|
public void setDeaths(int value) {
|
||||||
try (PreparedStatement statement = CONN.prepareStatement("UPDATE player_stats SET DEATHS = ? WHERE UUID = ?")) {
|
getPlayerStatsEntity().setDeaths(value);
|
||||||
statement.setDouble(1, value);
|
|
||||||
statement.setString(2, me.getPlayer().getUniqueId().toString());
|
|
||||||
|
|
||||||
statement.executeUpdate();
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public int getDeaths() throws SQLException {
|
public int getDeaths() {
|
||||||
try (PreparedStatement statement = CONN.prepareStatement("SELECT DEATHS FROM player_stats WHERE UUID = ?")) {
|
return getPlayerStatsEntity().getDeaths();
|
||||||
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 addDeaths(int value) throws SQLException {
|
public void addDeaths(int value) {
|
||||||
setDeaths(getDeaths() + value);
|
setDeaths(getDeaths() + value);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,47 +1,24 @@
|
|||||||
package ru.prisonlife.prisonapi.template;
|
package ru.prisonlife.prisonapi.template;
|
||||||
|
|
||||||
import java.sql.PreparedStatement;
|
public class Wallet extends AbstractApiData {
|
||||||
import java.sql.ResultSet;
|
|
||||||
import java.sql.SQLException;
|
|
||||||
|
|
||||||
import static ru.prisonlife.prisonapi.PrisonAPI.CONN;
|
public Wallet(Prisoner me) {
|
||||||
|
super(me);
|
||||||
public class Wallet {
|
|
||||||
|
|
||||||
private Prisoner me;
|
|
||||||
|
|
||||||
Wallet(Prisoner prisoner) {
|
|
||||||
me = prisoner;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public int getBalance() throws SQLException {
|
public int getBalance() {
|
||||||
try (PreparedStatement statement = CONN.prepareStatement("SELECT WALLET FROM player_stats WHERE UUID = ?")) {
|
return getPlayerStatsEntity().getWallet();
|
||||||
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 {
|
public void addBalance(int value) {
|
||||||
setBalance(getBalance() + value);
|
setBalance(getBalance() + value);
|
||||||
}
|
}
|
||||||
|
|
||||||
public void deposit(int value) throws SQLException {
|
public void deposit(int value) {
|
||||||
setBalance(getBalance() - value);
|
setBalance(getBalance() - value);
|
||||||
}
|
}
|
||||||
|
|
||||||
public void setBalance(int value) throws SQLException {
|
public void setBalance(int value) {
|
||||||
try (PreparedStatement statement = CONN.prepareStatement("UPDATE player_stats SET WALLET = ? WHERE UUID = ?")) {
|
getPlayerStatsEntity().setWallet(value);
|
||||||
statement.setDouble(1, value);
|
|
||||||
statement.setString(2, me.getPlayer().getUniqueId().toString());
|
|
||||||
|
|
||||||
statement.executeUpdate();
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user