Merge branch 'dmitriymx/mybatis' into dmitriymx/dev
This commit is contained in:
27
pom.xml
27
pom.xml
@@ -11,6 +11,9 @@
|
|||||||
<properties>
|
<properties>
|
||||||
<java.encoding>UTF-8</java.encoding>
|
<java.encoding>UTF-8</java.encoding>
|
||||||
<java.version>1.8</java.version>
|
<java.version>1.8</java.version>
|
||||||
|
<project.build.sourceEncoding>${java.encoding}</project.build.sourceEncoding>
|
||||||
|
<project.reporting.outputEncoding>${java.encoding}</project.reporting.outputEncoding>
|
||||||
|
|
||||||
<bukkit.version>1.12-R0.1</bukkit.version>
|
<bukkit.version>1.12-R0.1</bukkit.version>
|
||||||
</properties>
|
</properties>
|
||||||
|
|
||||||
@@ -22,15 +25,25 @@
|
|||||||
</repositories>
|
</repositories>
|
||||||
|
|
||||||
<dependencies>
|
<dependencies>
|
||||||
|
<!-- SPIGOT/BUKKIT -->
|
||||||
<dependency>
|
<dependency>
|
||||||
<groupId>org.spigotmc</groupId>
|
<groupId>org.spigotmc</groupId>
|
||||||
<artifactId>spigot-api</artifactId>
|
<artifactId>spigot-api</artifactId>
|
||||||
<version>${bukkit.version}-SNAPSHOT</version>
|
<version>${bukkit.version}-SNAPSHOT</version>
|
||||||
<scope>provided</scope>
|
<scope>provided</scope>
|
||||||
</dependency>
|
</dependency>
|
||||||
|
|
||||||
|
<!-- COMPONENTS -->
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.mybatis</groupId>
|
||||||
|
<artifactId>mybatis</artifactId>
|
||||||
|
<version>3.5.0</version>
|
||||||
|
</dependency>
|
||||||
</dependencies>
|
</dependencies>
|
||||||
|
|
||||||
<build>
|
<build>
|
||||||
|
<finalName>${project.artifactId}-${project.version}</finalName>
|
||||||
|
|
||||||
<resources>
|
<resources>
|
||||||
<resource>
|
<resource>
|
||||||
<directory>src/main/resources</directory>
|
<directory>src/main/resources</directory>
|
||||||
@@ -57,6 +70,20 @@
|
|||||||
<argLine>-Dfile.encoding=${java.encoding}</argLine>
|
<argLine>-Dfile.encoding=${java.encoding}</argLine>
|
||||||
</configuration>
|
</configuration>
|
||||||
</plugin>
|
</plugin>
|
||||||
|
|
||||||
|
<!-- Fat jar -->
|
||||||
|
<!-- mvn assebly:single -->
|
||||||
|
<plugin>
|
||||||
|
<artifactId>maven-assembly-plugin</artifactId>
|
||||||
|
<version>2.2-beta-5</version>
|
||||||
|
<configuration>
|
||||||
|
<finalName>${project.artifactId}-${project.version}-fat</finalName>
|
||||||
|
<appendAssemblyId>false</appendAssemblyId>
|
||||||
|
<descriptorRefs>
|
||||||
|
<descriptorRef>jar-with-dependencies</descriptorRef>
|
||||||
|
</descriptorRefs>
|
||||||
|
</configuration>
|
||||||
|
</plugin>
|
||||||
</plugins>
|
</plugins>
|
||||||
</build>
|
</build>
|
||||||
</project>
|
</project>
|
||||||
@@ -3,24 +3,14 @@ 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 ru.prisonlife.api.store.SQLStore;
|
||||||
|
|
||||||
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 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);
|
||||||
@@ -50,14 +40,6 @@ public class PrisonAPI extends JavaPlugin {
|
|||||||
return sqlStore;
|
return sqlStore;
|
||||||
}
|
}
|
||||||
|
|
||||||
public static PlayerStatsDao getPlayerStatsDao() {
|
|
||||||
return playerStatsDao;
|
|
||||||
}
|
|
||||||
|
|
||||||
public static DailyRespectTaskDao getDailyRespectTaskDao() {
|
|
||||||
return dailyRespectTaskDao;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void onEnable() {
|
public void onEnable() {
|
||||||
saveDefaultConfig();
|
saveDefaultConfig();
|
||||||
@@ -66,38 +48,6 @@ public class PrisonAPI extends JavaPlugin {
|
|||||||
final String dbUser = getConfig().getString("database.user");
|
final String dbUser = getConfig().getString("database.user");
|
||||||
final String dbPasswd = getConfig().getString("database.password");
|
final String dbPasswd = getConfig().getString("database.password");
|
||||||
|
|
||||||
try {
|
|
||||||
sqlStore = new SQLStore(dbUrl, dbUser, dbPasswd);
|
sqlStore = new SQLStore(dbUrl, dbUser, dbPasswd);
|
||||||
playerStatsDao = new PlayerStatsDao(sqlStore);
|
|
||||||
dailyRespectTaskDao = new DailyRespectTaskDao(sqlStore);
|
|
||||||
|
|
||||||
playeryStatsDaoTask = new CachedDaoTask(playerStatsDao)
|
|
||||||
.runTaskLaterAsynchronously(this, 20 * (60 * 1000));
|
|
||||||
dailyRespectTaskDaoTask = new CachedDaoTask(dailyRespectTaskDao)
|
|
||||||
.runTaskLaterAsynchronously(this, 20 * (60 * 1000));
|
|
||||||
} catch (SQLException e) {
|
|
||||||
e.printStackTrace();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void onDisable() {
|
|
||||||
playeryStatsDaoTask.cancel();
|
|
||||||
dailyRespectTaskDaoTask.cancel();
|
|
||||||
|
|
||||||
try {
|
|
||||||
if (!sqlStore.getConnection().isClosed()) {
|
|
||||||
sqlStore.close();
|
|
||||||
}
|
|
||||||
} catch (Exception e) {
|
|
||||||
e.printStackTrace();
|
|
||||||
} finally {
|
|
||||||
sqlStore = null;
|
|
||||||
playerStatsDao = null;
|
|
||||||
dailyRespectTaskDao = null;
|
|
||||||
}
|
|
||||||
|
|
||||||
playeryStatsDaoTask = null;
|
|
||||||
dailyRespectTaskDaoTask = null;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,90 +0,0 @@
|
|||||||
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;
|
|
||||||
}
|
|
||||||
@@ -1,17 +0,0 @@
|
|||||||
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();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,43 +0,0 @@
|
|||||||
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());
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,26 +1,25 @@
|
|||||||
package ru.prisonlife.api.store;
|
package ru.prisonlife.api.store;
|
||||||
|
|
||||||
import java.util.Objects;
|
import java.util.Objects;
|
||||||
import java.util.UUID;
|
|
||||||
|
|
||||||
public class DailyRespectTaskEntity {
|
public class DailyRespectTaskEntity {
|
||||||
|
|
||||||
private UUID uuid;
|
private String uuid;
|
||||||
private int cooldownRespect;
|
private Integer cooldownRespect;
|
||||||
|
|
||||||
public UUID getUuid() {
|
public String getUuid() {
|
||||||
return uuid;
|
return uuid;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void setUuid(UUID uuid) {
|
public void setUuid(String uuid) {
|
||||||
this.uuid = uuid;
|
this.uuid = uuid;
|
||||||
}
|
}
|
||||||
|
|
||||||
public int getCooldownRespect() {
|
public Integer getCooldownRespect() {
|
||||||
return cooldownRespect;
|
return cooldownRespect;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void setCooldownRespect(int cooldownRespect) {
|
public void setCooldownRespect(Integer cooldownRespect) {
|
||||||
this.cooldownRespect = cooldownRespect;
|
this.cooldownRespect = cooldownRespect;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -1,72 +0,0 @@
|
|||||||
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());
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,15 +1,12 @@
|
|||||||
package ru.prisonlife.api.store;
|
package ru.prisonlife.api.store;
|
||||||
|
|
||||||
import ru.prisonlife.api.PrisonAPI.Factions;
|
|
||||||
|
|
||||||
import java.util.Objects;
|
import java.util.Objects;
|
||||||
import java.util.UUID;
|
|
||||||
|
|
||||||
public class PlayerStatsEntity {
|
public class PlayerStatsEntity {
|
||||||
|
|
||||||
private UUID uuid;
|
private String uuid;
|
||||||
private Integer wallet;
|
private Integer wallet;
|
||||||
private Factions faction;
|
private Integer faction;
|
||||||
private Integer rang;
|
private Integer rang;
|
||||||
private Integer respect;
|
private Integer respect;
|
||||||
private Integer cooldownRespect;
|
private Integer cooldownRespect;
|
||||||
@@ -21,11 +18,11 @@ public class PlayerStatsEntity {
|
|||||||
private Integer mobs;
|
private Integer mobs;
|
||||||
private Integer deaths;
|
private Integer deaths;
|
||||||
|
|
||||||
public UUID getUuid() {
|
public String getUuid() {
|
||||||
return uuid;
|
return uuid;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void setUuid(UUID uuid) {
|
public void setUuid(String uuid) {
|
||||||
this.uuid = uuid;
|
this.uuid = uuid;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -37,11 +34,11 @@ public class PlayerStatsEntity {
|
|||||||
this.wallet = wallet;
|
this.wallet = wallet;
|
||||||
}
|
}
|
||||||
|
|
||||||
public Factions getFaction() {
|
public Integer getFaction() {
|
||||||
return faction;
|
return faction;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void setFaction(Factions faction) {
|
public void setFaction(Integer faction) {
|
||||||
this.faction = faction;
|
this.faction = faction;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -1,23 +1,28 @@
|
|||||||
package ru.prisonlife.api.store;
|
package ru.prisonlife.api.store;
|
||||||
|
|
||||||
import java.sql.Connection;
|
import org.apache.ibatis.session.SqlSession;
|
||||||
import java.sql.DriverManager;
|
import org.apache.ibatis.session.SqlSessionFactory;
|
||||||
import java.sql.SQLException;
|
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
|
||||||
|
|
||||||
public class SQLStore implements AutoCloseable {
|
import java.util.Properties;
|
||||||
|
|
||||||
private Connection connection;
|
public class SQLStore {
|
||||||
|
|
||||||
public SQLStore(String url, String user, String password) throws SQLException {
|
private SqlSessionFactory sqlSessionFactory;
|
||||||
this.connection = DriverManager.getConnection(url, user, password);
|
|
||||||
|
public SQLStore(String url, String user, String password) {
|
||||||
|
Properties properties = new Properties();
|
||||||
|
properties.put("url", url);
|
||||||
|
properties.put("user", user);
|
||||||
|
properties.put("password", password);
|
||||||
|
|
||||||
|
sqlSessionFactory = new SqlSessionFactoryBuilder().build(
|
||||||
|
SQLStore.class.getResourceAsStream("config.xml"),
|
||||||
|
properties
|
||||||
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
public Connection getConnection() {
|
public SqlSession openSession() {
|
||||||
return connection;
|
return sqlSessionFactory.openSession();
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void close() throws Exception {
|
|
||||||
connection.close();
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,7 +1,7 @@
|
|||||||
package ru.prisonlife.api.template;
|
package ru.prisonlife.api.template;
|
||||||
|
|
||||||
|
import org.apache.ibatis.session.SqlSession;
|
||||||
import ru.prisonlife.api.PrisonAPI;
|
import ru.prisonlife.api.PrisonAPI;
|
||||||
import ru.prisonlife.api.store.PlayerStatsDao;
|
|
||||||
import ru.prisonlife.api.store.PlayerStatsEntity;
|
import ru.prisonlife.api.store.PlayerStatsEntity;
|
||||||
|
|
||||||
import java.lang.ref.WeakReference;
|
import java.lang.ref.WeakReference;
|
||||||
@@ -20,10 +20,14 @@ public abstract class AbstractApiData {
|
|||||||
if (refPlayerStatsEntity != null && (entity = refPlayerStatsEntity.get()) != null) {
|
if (refPlayerStatsEntity != null && (entity = refPlayerStatsEntity.get()) != null) {
|
||||||
return entity;
|
return entity;
|
||||||
} else {
|
} else {
|
||||||
final PlayerStatsDao playerStatsDao = PrisonAPI.getPlayerStatsDao();
|
final SqlSession session = PrisonAPI.getSqlStore().openSession();
|
||||||
entity = playerStatsDao.findByUuid(me.getPlayer().getUniqueId())
|
entity = session.selectOne("PlayerStats.selectOne", me.getPlayer().getUniqueId().toString());
|
||||||
.orElseThrow(() -> new RuntimeException(
|
session.close();
|
||||||
"no data in db by '" + me.getPlayer().getUniqueId().toString() + "'"));
|
|
||||||
|
if (entity == null) {
|
||||||
|
throw new RuntimeException("no data in db by '" + me.getPlayer().getUniqueId().toString() + "'");
|
||||||
|
}
|
||||||
|
|
||||||
refPlayerStatsEntity = new WeakReference<>(entity);
|
refPlayerStatsEntity = new WeakReference<>(entity);
|
||||||
return entity;
|
return entity;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,9 +1,9 @@
|
|||||||
package ru.prisonlife.api.template;
|
package ru.prisonlife.api.template;
|
||||||
|
|
||||||
|
import org.apache.ibatis.session.SqlSession;
|
||||||
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.PrisonAPI.Factions;
|
||||||
import ru.prisonlife.api.store.AbstractCachedDao;
|
|
||||||
import ru.prisonlife.api.store.DailyRespectTaskEntity;
|
import ru.prisonlife.api.store.DailyRespectTaskEntity;
|
||||||
|
|
||||||
import java.lang.ref.WeakReference;
|
import java.lang.ref.WeakReference;
|
||||||
@@ -28,21 +28,25 @@ public class Faction extends AbstractApiData {
|
|||||||
if (refDailyRespectTaskEntity != null && (entity = refDailyRespectTaskEntity.get()) != null) {
|
if (refDailyRespectTaskEntity != null && (entity = refDailyRespectTaskEntity.get()) != null) {
|
||||||
return entity;
|
return entity;
|
||||||
} else {
|
} else {
|
||||||
final AbstractCachedDao<DailyRespectTaskEntity> cachedDao = PrisonAPI.getDailyRespectTaskDao();
|
final SqlSession session = PrisonAPI.getSqlStore().openSession();
|
||||||
entity = cachedDao.findByUuid(me.getPlayer().getUniqueId())
|
entity = session.selectOne("DailyRespectTask.selectOne", me.getPlayer().getUniqueId().toString());
|
||||||
.orElseThrow(() -> new RuntimeException(
|
session.close();
|
||||||
"no data in db by '" + me.getPlayer().getUniqueId().toString() + "'"));
|
|
||||||
|
if (entity == null) {
|
||||||
|
throw new RuntimeException("no data in db by '" + me.getPlayer().getUniqueId().toString() + "'");
|
||||||
|
}
|
||||||
|
|
||||||
refDailyRespectTaskEntity = new WeakReference<>(entity);
|
refDailyRespectTaskEntity = new WeakReference<>(entity);
|
||||||
return entity;
|
return entity;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public String getType() {
|
public String getType() {
|
||||||
return getPlayerStatsEntity().getFaction().name();
|
return Factions.valueOf(getPlayerStatsEntity().getFaction()).name();
|
||||||
}
|
}
|
||||||
|
|
||||||
public int getId() {
|
public int getId() {
|
||||||
return getPlayerStatsEntity().getFaction().getId();
|
return getPlayerStatsEntity().getFaction();
|
||||||
}
|
}
|
||||||
|
|
||||||
public int getRangId() {
|
public int getRangId() {
|
||||||
@@ -62,7 +66,7 @@ public class Faction extends AbstractApiData {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public void setFaction(int id) {
|
public void setFaction(int id) {
|
||||||
getPlayerStatsEntity().setFaction(Factions.valueOf(id));
|
getPlayerStatsEntity().setFaction(id);
|
||||||
}
|
}
|
||||||
|
|
||||||
public int getRespect() {
|
public int getRespect() {
|
||||||
|
|||||||
@@ -2,6 +2,7 @@ package ru.prisonlife.api.template;
|
|||||||
|
|
||||||
import net.md_5.bungee.api.ChatMessageType;
|
import net.md_5.bungee.api.ChatMessageType;
|
||||||
import net.md_5.bungee.api.chat.TextComponent;
|
import net.md_5.bungee.api.chat.TextComponent;
|
||||||
|
import org.apache.ibatis.session.SqlSession;
|
||||||
import org.bukkit.ChatColor;
|
import org.bukkit.ChatColor;
|
||||||
import org.bukkit.configuration.file.FileConfiguration;
|
import org.bukkit.configuration.file.FileConfiguration;
|
||||||
import org.bukkit.configuration.file.YamlConfiguration;
|
import org.bukkit.configuration.file.YamlConfiguration;
|
||||||
@@ -19,6 +20,8 @@ import java.util.List;
|
|||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
import java.util.Random;
|
import java.util.Random;
|
||||||
|
|
||||||
|
import static ru.prisonlife.api.PrisonAPI.getSqlStore;
|
||||||
|
|
||||||
public class Policeman extends Prisoner {
|
public class Policeman extends Prisoner {
|
||||||
|
|
||||||
private Map<Prisoner, BukkitRunnable> timeInCage = new HashMap<>();
|
private Map<Prisoner, BukkitRunnable> timeInCage = new HashMap<>();
|
||||||
@@ -28,7 +31,8 @@ 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();
|
try (SqlSession session = getSqlStore().openSession()) {
|
||||||
|
final Connection connection = session.getConnection();
|
||||||
|
|
||||||
if (prs.getPlayer().isOnline()) {
|
if (prs.getPlayer().isOnline()) {
|
||||||
Manager mng = new Manager(super.getPrison());
|
Manager mng = new Manager(super.getPrison());
|
||||||
@@ -102,3 +106,4 @@ public class Policeman extends Prisoner {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|||||||
@@ -1,5 +1,6 @@
|
|||||||
package ru.prisonlife.api.template;
|
package ru.prisonlife.api.template;
|
||||||
|
|
||||||
|
import org.apache.ibatis.session.SqlSession;
|
||||||
import org.bukkit.Bukkit;
|
import org.bukkit.Bukkit;
|
||||||
import org.bukkit.Location;
|
import org.bukkit.Location;
|
||||||
import org.bukkit.configuration.file.FileConfiguration;
|
import org.bukkit.configuration.file.FileConfiguration;
|
||||||
@@ -119,7 +120,8 @@ public class Prisoner {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public int getSuspect() {
|
public int getSuspect() {
|
||||||
try(PreparedStatement statement = getSqlStore().getConnection().prepareStatement("SELECT SUSPECT FROM player_stats WHERE UUID = ?")) {
|
try (SqlSession session = getSqlStore().openSession()) {
|
||||||
|
try (PreparedStatement statement = session.getConnection().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()) {
|
||||||
@@ -130,11 +132,13 @@ public class Prisoner {
|
|||||||
} catch (SQLException z) {
|
} catch (SQLException z) {
|
||||||
z.printStackTrace();
|
z.printStackTrace();
|
||||||
}
|
}
|
||||||
|
}
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void setSuspect(int value) {
|
public void setSuspect(int value) {
|
||||||
try(PreparedStatement statement = getSqlStore().getConnection().prepareStatement("UPDATE player_stats SET SUSPECT = ? WHERE UUID = ?")) {
|
try (SqlSession session = getSqlStore().openSession()) {
|
||||||
|
try (PreparedStatement statement = session.getConnection().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());
|
||||||
|
|
||||||
@@ -143,6 +147,7 @@ public class Prisoner {
|
|||||||
z.printStackTrace();
|
z.printStackTrace();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
public void addSuspect(int value) {
|
public void addSuspect(int value) {
|
||||||
int su = getSuspect() + value;
|
int su = getSuspect() + value;
|
||||||
@@ -153,7 +158,8 @@ public class Prisoner {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public int getTimeLeftInCage() {
|
public int getTimeLeftInCage() {
|
||||||
try (PreparedStatement statement = getSqlStore().getConnection().prepareStatement("SELECT TIME_IN_CAGE FROM players_in_cages WHERE UUID = ?")) {
|
try (SqlSession session = getSqlStore().openSession()) {
|
||||||
|
try (PreparedStatement statement = session.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()) {
|
||||||
@@ -164,18 +170,20 @@ public class Prisoner {
|
|||||||
} catch (SQLException e) {
|
} catch (SQLException e) {
|
||||||
e.printStackTrace();
|
e.printStackTrace();
|
||||||
}
|
}
|
||||||
|
}
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void setTimeLeftInCage(int value) {
|
public void setTimeLeftInCage(int value) {
|
||||||
|
try (SqlSession session = getSqlStore().openSession()) {
|
||||||
try {
|
try {
|
||||||
PreparedStatement statement = getSqlStore().getConnection().prepareStatement("SELECT TIME_IN_CAGE FROM players_in_cages WHERE UUID = ?");
|
PreparedStatement statement = session.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.close();
|
statement.close();
|
||||||
|
|
||||||
statement = getSqlStore().getConnection().prepareStatement("UPDATE players_in_cages SET TIME_IN_CAGE = ? WHERE UUID = ?");
|
statement = session.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();
|
||||||
@@ -187,9 +195,11 @@ public class Prisoner {
|
|||||||
z.printStackTrace();
|
z.printStackTrace();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
public int getBlocksLeftToBreak() {
|
public int getBlocksLeftToBreak() {
|
||||||
try (PreparedStatement statement = getSqlStore().getConnection().prepareStatement("SELECT BLOCKS_LEFT FROM players_in_cages WHERE UUID = ?")) {
|
try (SqlSession session = getSqlStore().openSession()) {
|
||||||
|
try (PreparedStatement statement = session.getConnection().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()) {
|
||||||
@@ -200,11 +210,13 @@ public class Prisoner {
|
|||||||
} catch (SQLException e) {
|
} catch (SQLException e) {
|
||||||
e.printStackTrace();
|
e.printStackTrace();
|
||||||
}
|
}
|
||||||
|
}
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
public boolean isLocateInCage() {
|
public boolean isLocateInCage() {
|
||||||
try (PreparedStatement statement = getSqlStore().getConnection().prepareStatement("SELECT UUID FROM players_in_cages WHERE UUID = ?")) {
|
try (SqlSession session = getSqlStore().openSession()) {
|
||||||
|
try (PreparedStatement statement = session.getConnection().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()) {
|
||||||
@@ -215,6 +227,7 @@ public class Prisoner {
|
|||||||
} catch (SQLException e) {
|
} catch (SQLException e) {
|
||||||
e.printStackTrace();
|
e.printStackTrace();
|
||||||
}
|
}
|
||||||
|
}
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -231,7 +244,8 @@ public class Prisoner {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public void register() throws SQLException {
|
public void register() throws SQLException {
|
||||||
final Connection connection = getSqlStore().getConnection();
|
try (SqlSession session = getSqlStore().openSession()) {
|
||||||
|
final Connection connection = session.getConnection();
|
||||||
|
|
||||||
PreparedStatement statement = connection.prepareStatement("SELECT UUID FROM player_stats WHERE UUID = ?");
|
PreparedStatement statement = connection.prepareStatement("SELECT UUID FROM player_stats WHERE UUID = ?");
|
||||||
statement.setString(1, me.getUniqueId().toString());
|
statement.setString(1, me.getUniqueId().toString());
|
||||||
@@ -265,3 +279,4 @@ public class Prisoner {
|
|||||||
statement.close();
|
statement.close();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,20 @@
|
|||||||
|
<?xml version = "1.0" encoding = "UTF-8"?>
|
||||||
|
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
|
||||||
|
<mapper namespace="DailyRespectTask">
|
||||||
|
|
||||||
|
<resultMap id = "result" type = "DailyRespectTask">
|
||||||
|
<result property = "uuid" column = "UUID"/>
|
||||||
|
<result property = "cooldownRespect" column = "COOLDOWN"/>
|
||||||
|
</resultMap>
|
||||||
|
|
||||||
|
<select id="selectOne" parameterType="String" resultMap="result">
|
||||||
|
SELECT COOLDOWN FROM daily_respect_task WHERE UUID = #{id}
|
||||||
|
</select>
|
||||||
|
|
||||||
|
<update id="update" parameterType="DailyRespectTask">
|
||||||
|
UPDATE daily_respect_task
|
||||||
|
SET COOLDOWN = #{cooldownRespect}
|
||||||
|
WHERE UUID = #{uuid};
|
||||||
|
</update>
|
||||||
|
|
||||||
|
</mapper>
|
||||||
@@ -0,0 +1,42 @@
|
|||||||
|
<?xml version = "1.0" encoding = "UTF-8"?>
|
||||||
|
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
|
||||||
|
<mapper namespace="PlayerStats">
|
||||||
|
|
||||||
|
<resultMap id = "result" type = "PlayerStats">
|
||||||
|
<result property = "uuid" column = "UUID"/>
|
||||||
|
<result property = "wallet" column = "WALLET"/>
|
||||||
|
<result property = "faction" column = "FACTION"/>
|
||||||
|
<result property = "rang" column = "F_RANG"/>
|
||||||
|
<result property = "respect" column = "RESPECT"/>
|
||||||
|
<result property = "cooldownRespect" column = "COOLDOWN"/>
|
||||||
|
<result property = "points" column = "POINTS"/>
|
||||||
|
<result property = "level" column = "LEVEL"/>
|
||||||
|
<result property = "serverTime" column = "G_TIME"/>
|
||||||
|
<result property = "blocks" column = "D_BLOCKS"/>
|
||||||
|
<result property = "players" column = "K_PLAYERS"/>
|
||||||
|
<result property = "mobs" column = "K_MOBS"/>
|
||||||
|
<result property = "deaths" column = "DEATHS"/>
|
||||||
|
</resultMap>
|
||||||
|
|
||||||
|
<select id="selectOne" parameterType="String" resultMap="result">
|
||||||
|
SELECT * FROM player_stats WHERE UUID = #{id}
|
||||||
|
</select>
|
||||||
|
|
||||||
|
<update id="update" parameterType="PlayerStats">
|
||||||
|
UPDATE player_stats
|
||||||
|
SET WALLET = #{wallet},
|
||||||
|
FACTION = #{faction},
|
||||||
|
F_RANG = #{rang},
|
||||||
|
RESPECT = #{respect},
|
||||||
|
COOLDOWN = #{cooldownRespect},
|
||||||
|
POINTS = #{points},
|
||||||
|
LEVEL = #{level},
|
||||||
|
G_TIME = #{serverTime},
|
||||||
|
D_BLOCKS = #{blocks},
|
||||||
|
K_PLAYERS = #{players},
|
||||||
|
K_MOBS = #{mobs},
|
||||||
|
DEATHS = #{deaths}
|
||||||
|
WHERE UUID = #{uuid};
|
||||||
|
</update>
|
||||||
|
|
||||||
|
</mapper>
|
||||||
22
src/main/resources/ru/prisonlife/api/store/config.xml
Normal file
22
src/main/resources/ru/prisonlife/api/store/config.xml
Normal file
@@ -0,0 +1,22 @@
|
|||||||
|
<?xml version = "1.0" encoding = "UTF-8"?>
|
||||||
|
<!DOCTYPE configuration PUBLIC "-//mybatis.org//DTD Config 3.0//EN" "http://mybatis.org/dtd/mybatis-3-config.dtd">
|
||||||
|
<configuration>
|
||||||
|
|
||||||
|
<environments default="main">
|
||||||
|
<environment id="main">
|
||||||
|
<transactionManager type="JDBC"/>
|
||||||
|
<dataSource type="POOLED">
|
||||||
|
<property name = "driver" value = "com.mysql.jdbc.Driver"/>
|
||||||
|
<property name = "url" value = "${url}"/>
|
||||||
|
<property name = "username" value = "${user}"/>
|
||||||
|
<property name = "password" value = "${password}"/>
|
||||||
|
</dataSource>
|
||||||
|
</environment>
|
||||||
|
</environments>
|
||||||
|
|
||||||
|
<mappers>
|
||||||
|
<mapper resource = "PlayerStatsMapper.xml"/>
|
||||||
|
<mapper resource = "DailyRespectTaskMapper.xml"/>
|
||||||
|
</mappers>
|
||||||
|
|
||||||
|
</configuration>
|
||||||
Reference in New Issue
Block a user