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.Bukkit;
|
||||||
import org.bukkit.plugin.Plugin;
|
import org.bukkit.plugin.Plugin;
|
||||||
import org.bukkit.plugin.java.JavaPlugin;
|
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;
|
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";
|
||||||
|
|
||||||
public static final Plugin plugin = Bukkit.getPluginManager().getPlugin("PrisonAPI");
|
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 {
|
public enum Factions {
|
||||||
NONE(0), ASIANS(1), LATINOS(2), NIGGAZ(3), POLICE(4);
|
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() {
|
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 {
|
try {
|
||||||
CONN = DriverManager.getConnection(
|
sqlStore = new SQLStore(dbUrl, dbUser, dbPasswd);
|
||||||
"jdbc:mysql://localhost:3306/prison4life",
|
playerStatsDao = new PlayerStatsDao(sqlStore);
|
||||||
"root", "root");
|
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;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
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 org.bukkit.Bukkit;
|
||||||
import ru.prisonlife.api.PrisonAPI;
|
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.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;
|
||||||
|
|
||||||
public class Faction {
|
public class Faction extends AbstractApiData {
|
||||||
|
|
||||||
private Prisoner me;
|
private WeakReference<DailyRespectTaskEntity> refDailyRespectTaskEntity;
|
||||||
|
|
||||||
Faction(Prisoner prisoner) {
|
private static final String[][] post = {
|
||||||
me = prisoner;
|
{"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);
|
||||||
}
|
}
|
||||||
|
|
||||||
public String getType() throws SQLException {
|
private DailyRespectTaskEntity getDailyRespectTaskEntity() {
|
||||||
try (PreparedStatement statement = PrisonAPI.CONN.prepareStatement("SELECT FACTION FROM player_stats WHERE UUID = ?")) {
|
DailyRespectTaskEntity entity;
|
||||||
statement.setString(1, me.getPlayer().getUniqueId().toString());
|
if (refDailyRespectTaskEntity != null && (entity = refDailyRespectTaskEntity.get()) != null) {
|
||||||
|
return entity;
|
||||||
try (ResultSet resultSet = statement.executeQuery()) {
|
} else {
|
||||||
if (resultSet.next()) {
|
final AbstractCachedDao<DailyRespectTaskEntity> cachedDao = PrisonAPI.getDailyRespectTaskDao();
|
||||||
return resultSet.getString(1);
|
entity = cachedDao.findByUuid(me.getPlayer().getUniqueId())
|
||||||
} else {
|
.orElseThrow(() -> new RuntimeException(
|
||||||
return "null";
|
"no data in db by '" + me.getPlayer().getUniqueId().toString() + "'"));
|
||||||
}
|
refDailyRespectTaskEntity = new WeakReference<>(entity);
|
||||||
}
|
return entity;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public int getId() throws SQLException {
|
public String getType() {
|
||||||
try (PreparedStatement statement = PrisonAPI.CONN.prepareStatement("SELECT FACTION FROM player_stats WHERE UUID = ?")) {
|
return getPlayerStatsEntity().getFaction().name();
|
||||||
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 {
|
public int getId() {
|
||||||
try (PreparedStatement statement = PrisonAPI.CONN.prepareStatement("SELECT F_RANG FROM player_stats WHERE UUID = ?")) {
|
return getPlayerStatsEntity().getFaction().getId();
|
||||||
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 {
|
public int getRangId() {
|
||||||
String[][] post = {
|
return getPlayerStatsEntity().getRang();
|
||||||
{"Vakasu", "Tomadati", "So Honbute", "Vakagasira", "Kambu", "Kumite"},
|
}
|
||||||
{"Novaro", "Amigo", "Latino", "Veterano", "Elite", "Padre"},
|
|
||||||
{"Baby", "Cracker", "Little Nigga", "Big Nigga", "Star", "Daddy"}};
|
|
||||||
|
|
||||||
|
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 = PrisonAPI.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 = PrisonAPI.CONN.prepareStatement("UPDATE player_stats SET FACTION = ? WHERE UUID = ?")) {
|
getPlayerStatsEntity().setFaction(Factions.valueOf(id));
|
||||||
statement.setString(1, PrisonAPI.Factions.valueOf(id).name());
|
|
||||||
statement.setString(2, me.getPlayer().getUniqueId().toString());
|
|
||||||
|
|
||||||
statement.executeUpdate();
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public int getRespect() throws SQLException {
|
public int getRespect() {
|
||||||
try (PreparedStatement statement = PrisonAPI.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 = PrisonAPI.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 = PrisonAPI.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 = PrisonAPI.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,49 +1,27 @@
|
|||||||
package ru.prisonlife.api.template;
|
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;
|
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() {
|
||||||
return score;
|
return score;
|
||||||
}
|
}
|
||||||
|
|
||||||
public double getLevel() throws SQLException, NullPointerException {
|
public double getLevel() {
|
||||||
try (PreparedStatement statement = PrisonAPI.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 = PrisonAPI.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();
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -10,6 +10,7 @@ import org.bukkit.scheduler.BukkitRunnable;
|
|||||||
import ru.prisonlife.api.PrisonAPI;
|
import ru.prisonlife.api.PrisonAPI;
|
||||||
|
|
||||||
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;
|
||||||
@@ -27,16 +28,18 @@ public class Policeman extends Prisoner {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public void arrestPrisoner(Prisoner prs) throws SQLException {
|
public void arrestPrisoner(Prisoner prs) throws SQLException {
|
||||||
|
final Connection connection = PrisonAPI.getSqlStore().getConnection();
|
||||||
|
|
||||||
if(prs.getPlayer().isOnline()) {
|
if(prs.getPlayer().isOnline()) {
|
||||||
Manager mng = new Manager(super.getPrison());
|
Manager mng = new Manager(super.getPrison());
|
||||||
List<String> all_cages = mng.getCages();
|
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());
|
st.setString(1, me.getUniqueId().toString());
|
||||||
try(ResultSet resultSet = st.executeQuery()) {
|
try(ResultSet resultSet = st.executeQuery()) {
|
||||||
if(!resultSet.next()) {
|
if(!resultSet.next()) {
|
||||||
int su = prs.getSuspect();
|
int su = prs.getSuspect();
|
||||||
st.close();
|
st.close();
|
||||||
st = PrisonAPI.CONN.prepareStatement(
|
st = connection.prepareStatement(
|
||||||
"INSERT INTO players_in_cages(NAME, UUID, TIME_IN_CAGE, BLOCKS_LEFT) " +
|
"INSERT INTO players_in_cages(NAME, UUID, TIME_IN_CAGE, BLOCKS_LEFT) " +
|
||||||
"VALUES (?, ?, ?, ?)");
|
"VALUES (?, ?, ?, ?)");
|
||||||
st.setString(1, getPlayer().getName());
|
st.setString(1, getPlayer().getName());
|
||||||
@@ -64,7 +67,7 @@ public class Policeman extends Prisoner {
|
|||||||
try {
|
try {
|
||||||
int time = 0;
|
int time = 0;
|
||||||
String uuid = prs.getPlayer().getUniqueId().toString();
|
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 = ?");
|
"SELECT TIME_IN_CAGE FROM players_in_cages WHERE UUID = ?");
|
||||||
st.setString(1, uuid);
|
st.setString(1, uuid);
|
||||||
try(ResultSet resultSet = st.executeQuery()) {
|
try(ResultSet resultSet = st.executeQuery()) {
|
||||||
@@ -77,12 +80,12 @@ public class Policeman extends Prisoner {
|
|||||||
actionBar.cancel();
|
actionBar.cancel();
|
||||||
cancel();
|
cancel();
|
||||||
timeInCage.remove(prs);
|
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.setString(1, uuid);
|
||||||
st.execute();
|
st.execute();
|
||||||
} else {
|
} else {
|
||||||
if(prs.getPlayer().isOnline()) {
|
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.setString(1, uuid);
|
||||||
st.execute();
|
st.execute();
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -8,6 +8,7 @@ import org.bukkit.entity.Player;
|
|||||||
import ru.prisonlife.api.PrisonAPI;
|
import ru.prisonlife.api.PrisonAPI;
|
||||||
|
|
||||||
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;
|
||||||
@@ -15,6 +16,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.api.PrisonAPI.getSqlStore;
|
||||||
|
|
||||||
public class Prisoner {
|
public class Prisoner {
|
||||||
|
|
||||||
protected Player me;
|
protected Player me;
|
||||||
@@ -35,7 +38,7 @@ public class Prisoner {
|
|||||||
return me;
|
return me;
|
||||||
}
|
}
|
||||||
|
|
||||||
public Level getPrisonLevel() throws SQLException {
|
public Level getPrisonLevel() {
|
||||||
return this.level;
|
return this.level;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -94,8 +97,9 @@ public class Prisoner {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if(c == 3)
|
if(c == 3) {
|
||||||
return fileConfiguration.getString("TERRITORIES." + terr + ".NAME");
|
return fileConfiguration.getString("TERRITORIES." + terr + ".NAME");
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
} catch (NullPointerException z) {
|
} catch (NullPointerException z) {
|
||||||
@@ -105,26 +109,22 @@ public class Prisoner {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public void teleportToFactionSpawn() {
|
public void teleportToFactionSpawn() {
|
||||||
try {
|
Prisoner ps = new Prisoner(me);
|
||||||
Prisoner ps = new Prisoner(me);
|
Manager mng = new Manager(getPrison());
|
||||||
Manager mng = new Manager(getPrison());
|
String f = ps.getFaction().getType();
|
||||||
String f = ps.getFaction().getType();
|
List<Integer> c = mng.getSpawnCords(ps.getFaction().getId());
|
||||||
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));
|
||||||
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() {
|
public int getSuspect() {
|
||||||
try {
|
try(PreparedStatement statement = getSqlStore().getConnection().prepareStatement("SELECT SUSPECT FROM player_stats WHERE UUID = ?")) {
|
||||||
try(PreparedStatement statement = PrisonAPI.CONN.prepareStatement("SELECT SUSPECT FROM player_stats WHERE UUID = ?")) {
|
statement.setString(1, me.getPlayer().getUniqueId().toString());
|
||||||
statement.setString(1, me.getPlayer().getUniqueId().toString());
|
|
||||||
|
|
||||||
try(ResultSet resultSet = statement.executeQuery()) {
|
try(ResultSet resultSet = statement.executeQuery()) {
|
||||||
if(resultSet.next())
|
if(resultSet.next()) {
|
||||||
return resultSet.getInt(1);
|
return resultSet.getInt(1);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
} catch(SQLException z) {
|
} catch(SQLException z) {
|
||||||
@@ -134,13 +134,11 @@ public class Prisoner {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public void setSuspect(int value) {
|
public void setSuspect(int value) {
|
||||||
try {
|
try(PreparedStatement statement = getSqlStore().getConnection().prepareStatement("UPDATE player_stats SET SUSPECT = ? WHERE UUID = ?")) {
|
||||||
try(PreparedStatement statement = PrisonAPI.CONN.prepareStatement("UPDATE player_stats SET SUSPECT = ? WHERE UUID = ?")) {
|
statement.setInt(1, value);
|
||||||
statement.setInt(1, value);
|
statement.setString(2, me.getPlayer().getUniqueId().toString());
|
||||||
statement.setString(2, me.getPlayer().getUniqueId().toString());
|
|
||||||
|
|
||||||
statement.executeUpdate();
|
statement.executeUpdate();
|
||||||
}
|
|
||||||
} catch(SQLException z) {
|
} catch(SQLException z) {
|
||||||
z.printStackTrace();
|
z.printStackTrace();
|
||||||
}
|
}
|
||||||
@@ -149,17 +147,19 @@ public class Prisoner {
|
|||||||
public void addSuspect(int value) {
|
public void addSuspect(int value) {
|
||||||
int su = getSuspect() + value;
|
int su = getSuspect() + value;
|
||||||
setSuspect(su);
|
setSuspect(su);
|
||||||
if(su > 6)
|
if(su > 6) {
|
||||||
setSuspect(su - (su - 6));
|
setSuspect(su - (su - 6));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public int getTimeLeftInCage() {
|
public int getTimeLeftInCage() {
|
||||||
try {
|
try (PreparedStatement statement = getSqlStore().getConnection().prepareStatement("SELECT TIME_IN_CAGE FROM players_in_cages WHERE UUID = ?")) {
|
||||||
PreparedStatement statement = PrisonAPI.CONN.prepareStatement("SELECT TIME_IN_CAGE FROM players_in_cages 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()) {
|
||||||
return resultSet.getInt(1);
|
return resultSet.getInt(1);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
} catch (SQLException e) {
|
} catch (SQLException e) {
|
||||||
e.printStackTrace();
|
e.printStackTrace();
|
||||||
@@ -169,14 +169,18 @@ public class Prisoner {
|
|||||||
|
|
||||||
public void setTimeLeftInCage(int value) {
|
public void setTimeLeftInCage(int value) {
|
||||||
try {
|
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());
|
statement.setString(1, me.getUniqueId().toString());
|
||||||
try(ResultSet resultSet = statement.executeQuery()) {
|
try(ResultSet resultSet = statement.executeQuery()) {
|
||||||
if(resultSet.next()) {
|
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.setInt(1, value);
|
||||||
statement.setString(2, me.getUniqueId().toString());
|
statement.setString(2, me.getUniqueId().toString());
|
||||||
statement.execute();
|
statement.execute();
|
||||||
|
|
||||||
|
statement.close();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
} catch(SQLException z) {
|
} catch(SQLException z) {
|
||||||
@@ -185,12 +189,13 @@ public class Prisoner {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public int getBlocksLeftToBreak() {
|
public int getBlocksLeftToBreak() {
|
||||||
try {
|
try (PreparedStatement statement = getSqlStore().getConnection().prepareStatement("SELECT BLOCKS_LEFT FROM players_in_cages WHERE UUID = ?")) {
|
||||||
PreparedStatement statement = PrisonAPI.CONN.prepareStatement("SELECT BLOCKS_LEFT FROM players_in_cages 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()) {
|
||||||
return resultSet.getInt(1);
|
return resultSet.getInt(1);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
} catch (SQLException e) {
|
} catch (SQLException e) {
|
||||||
e.printStackTrace();
|
e.printStackTrace();
|
||||||
@@ -199,12 +204,13 @@ public class Prisoner {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public boolean isLocateInCage() {
|
public boolean isLocateInCage() {
|
||||||
try {
|
try (PreparedStatement statement = getSqlStore().getConnection().prepareStatement("SELECT UUID FROM players_in_cages WHERE UUID = ?")) {
|
||||||
PreparedStatement statement = PrisonAPI.CONN.prepareStatement("SELECT UUID FROM players_in_cages 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()) {
|
||||||
return true;
|
return true;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
} catch (SQLException e) {
|
} catch (SQLException e) {
|
||||||
e.printStackTrace();
|
e.printStackTrace();
|
||||||
@@ -225,14 +231,16 @@ public class Prisoner {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public void register() throws SQLException {
|
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());
|
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 = PrisonAPI.CONN.prepareStatement(
|
statement = connection.prepareStatement(
|
||||||
"INSERT INTO player_stats(NAME, UUID, LEVEL, POINTS, FACTION, RESPECT, F_RANG, SUSPECT, WALLET, " +
|
"INSERT INTO player_stats(NAME, UUID, LEVEL, POINTS, FACTION, RESPECT, F_RANG, SUSPECT, 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, 0)");
|
+ "VALUES (?, ?, 1, 0, 'NONE', 0, 0, 0, 0, 0, 0, 0, 0, 0)");
|
||||||
@@ -241,7 +249,7 @@ public class Prisoner {
|
|||||||
statement.executeUpdate();
|
statement.executeUpdate();
|
||||||
statement.close();
|
statement.close();
|
||||||
|
|
||||||
statement = PrisonAPI.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.api.template;
|
package ru.prisonlife.api.template;
|
||||||
|
|
||||||
import ru.prisonlife.api.PrisonAPI;
|
public class Score extends AbstractApiData {
|
||||||
|
|
||||||
import java.sql.PreparedStatement;
|
public Score(Prisoner me) {
|
||||||
import java.sql.ResultSet;
|
super(me);
|
||||||
import java.sql.SQLException;
|
|
||||||
|
|
||||||
public class Score {
|
|
||||||
|
|
||||||
private Prisoner me;
|
|
||||||
|
|
||||||
Score(Prisoner prisoner) {
|
|
||||||
me = prisoner;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public double getPoints() throws SQLException {
|
public double getPoints() {
|
||||||
try (PreparedStatement statement = PrisonAPI.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 = PrisonAPI.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.api.template;
|
package ru.prisonlife.api.template;
|
||||||
|
|
||||||
import ru.prisonlife.api.PrisonAPI;
|
public class Stats extends AbstractApiData {
|
||||||
|
|
||||||
import java.sql.PreparedStatement;
|
public Stats(Prisoner me) {
|
||||||
import java.sql.ResultSet;
|
super(me);
|
||||||
import java.sql.SQLException;
|
|
||||||
|
|
||||||
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 = PrisonAPI.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 = PrisonAPI.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 = PrisonAPI.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 = PrisonAPI.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 = PrisonAPI.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 = PrisonAPI.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 = PrisonAPI.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 = PrisonAPI.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 = PrisonAPI.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 = PrisonAPI.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.api.template;
|
package ru.prisonlife.api.template;
|
||||||
|
|
||||||
import ru.prisonlife.api.PrisonAPI;
|
public class Wallet extends AbstractApiData {
|
||||||
|
|
||||||
import java.sql.PreparedStatement;
|
public Wallet(Prisoner me) {
|
||||||
import java.sql.ResultSet;
|
super(me);
|
||||||
import java.sql.SQLException;
|
|
||||||
|
|
||||||
public class Wallet {
|
|
||||||
|
|
||||||
private Prisoner me;
|
|
||||||
|
|
||||||
Wallet(Prisoner prisoner) {
|
|
||||||
me = prisoner;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public int getBalance() throws SQLException {
|
public int getBalance() {
|
||||||
try (PreparedStatement statement = PrisonAPI.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 = PrisonAPI.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();
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
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