Hello, MyBatis!
This commit is contained in:
@@ -3,24 +3,14 @@ package ru.prisonlife.api;
|
||||
import org.bukkit.Bukkit;
|
||||
import org.bukkit.plugin.Plugin;
|
||||
import org.bukkit.plugin.java.JavaPlugin;
|
||||
import org.bukkit.scheduler.BukkitTask;
|
||||
import ru.prisonlife.api.store.CachedDaoTask;
|
||||
import ru.prisonlife.api.store.DailyRespectTaskDao;
|
||||
import ru.prisonlife.api.store.PlayerStatsDao;
|
||||
import ru.prisonlife.api.store.SQLStore;
|
||||
|
||||
import java.sql.SQLException;
|
||||
|
||||
public class PrisonAPI extends JavaPlugin {
|
||||
|
||||
public static final String PLUGIN_PATH = "plugins/PrisonManager";
|
||||
public static final Plugin plugin = Bukkit.getPluginManager().getPlugin("PrisonAPI");
|
||||
|
||||
private static SQLStore sqlStore;
|
||||
private static PlayerStatsDao playerStatsDao;
|
||||
private static DailyRespectTaskDao dailyRespectTaskDao;
|
||||
private BukkitTask playeryStatsDaoTask;
|
||||
private BukkitTask dailyRespectTaskDaoTask;
|
||||
|
||||
public enum Factions {
|
||||
NONE(0), ASIANS(1), LATINOS(2), NIGGAZ(3), POLICE(4);
|
||||
@@ -50,14 +40,6 @@ public class PrisonAPI extends JavaPlugin {
|
||||
return sqlStore;
|
||||
}
|
||||
|
||||
public static PlayerStatsDao getPlayerStatsDao() {
|
||||
return playerStatsDao;
|
||||
}
|
||||
|
||||
public static DailyRespectTaskDao getDailyRespectTaskDao() {
|
||||
return dailyRespectTaskDao;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onEnable() {
|
||||
saveDefaultConfig();
|
||||
@@ -66,38 +48,6 @@ public class PrisonAPI extends JavaPlugin {
|
||||
final String dbUser = getConfig().getString("database.user");
|
||||
final String dbPasswd = getConfig().getString("database.password");
|
||||
|
||||
try {
|
||||
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;
|
||||
sqlStore = new SQLStore(dbUrl, dbUser, dbPasswd);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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;
|
||||
|
||||
import java.util.Objects;
|
||||
import java.util.UUID;
|
||||
|
||||
public class DailyRespectTaskEntity {
|
||||
|
||||
private UUID uuid;
|
||||
private int cooldownRespect;
|
||||
private String uuid;
|
||||
private Integer cooldownRespect;
|
||||
|
||||
public UUID getUuid() {
|
||||
public String getUuid() {
|
||||
return uuid;
|
||||
}
|
||||
|
||||
public void setUuid(UUID uuid) {
|
||||
public void setUuid(String uuid) {
|
||||
this.uuid = uuid;
|
||||
}
|
||||
|
||||
public int getCooldownRespect() {
|
||||
public Integer getCooldownRespect() {
|
||||
return cooldownRespect;
|
||||
}
|
||||
|
||||
public void setCooldownRespect(int cooldownRespect) {
|
||||
public void setCooldownRespect(Integer 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;
|
||||
|
||||
import ru.prisonlife.api.PrisonAPI.Factions;
|
||||
|
||||
import java.util.Objects;
|
||||
import java.util.UUID;
|
||||
|
||||
public class PlayerStatsEntity {
|
||||
|
||||
private UUID uuid;
|
||||
private String uuid;
|
||||
private Integer wallet;
|
||||
private Factions faction;
|
||||
private Integer faction;
|
||||
private Integer rang;
|
||||
private Integer respect;
|
||||
private Integer cooldownRespect;
|
||||
@@ -21,11 +18,11 @@ public class PlayerStatsEntity {
|
||||
private Integer mobs;
|
||||
private Integer deaths;
|
||||
|
||||
public UUID getUuid() {
|
||||
public String getUuid() {
|
||||
return uuid;
|
||||
}
|
||||
|
||||
public void setUuid(UUID uuid) {
|
||||
public void setUuid(String uuid) {
|
||||
this.uuid = uuid;
|
||||
}
|
||||
|
||||
@@ -37,11 +34,11 @@ public class PlayerStatsEntity {
|
||||
this.wallet = wallet;
|
||||
}
|
||||
|
||||
public Factions getFaction() {
|
||||
public Integer getFaction() {
|
||||
return faction;
|
||||
}
|
||||
|
||||
public void setFaction(Factions faction) {
|
||||
public void setFaction(Integer faction) {
|
||||
this.faction = faction;
|
||||
}
|
||||
|
||||
|
||||
@@ -1,23 +1,28 @@
|
||||
package ru.prisonlife.api.store;
|
||||
|
||||
import java.sql.Connection;
|
||||
import java.sql.DriverManager;
|
||||
import java.sql.SQLException;
|
||||
import org.apache.ibatis.session.SqlSession;
|
||||
import org.apache.ibatis.session.SqlSessionFactory;
|
||||
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 {
|
||||
this.connection = DriverManager.getConnection(url, user, password);
|
||||
private SqlSessionFactory sqlSessionFactory;
|
||||
|
||||
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() {
|
||||
return connection;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void close() throws Exception {
|
||||
connection.close();
|
||||
public SqlSession openSession() {
|
||||
return sqlSessionFactory.openSession();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
package ru.prisonlife.api.template;
|
||||
|
||||
import org.apache.ibatis.session.SqlSession;
|
||||
import ru.prisonlife.api.PrisonAPI;
|
||||
import ru.prisonlife.api.store.PlayerStatsDao;
|
||||
import ru.prisonlife.api.store.PlayerStatsEntity;
|
||||
|
||||
import java.lang.ref.WeakReference;
|
||||
@@ -20,10 +20,14 @@ public abstract class AbstractApiData {
|
||||
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() + "'"));
|
||||
final SqlSession session = PrisonAPI.getSqlStore().openSession();
|
||||
entity = session.selectOne("PlayerStats.selectOne", me.getPlayer().getUniqueId().toString());
|
||||
session.close();
|
||||
|
||||
if (entity == null) {
|
||||
throw new RuntimeException("no data in db by '" + me.getPlayer().getUniqueId().toString() + "'");
|
||||
}
|
||||
|
||||
refPlayerStatsEntity = new WeakReference<>(entity);
|
||||
return entity;
|
||||
}
|
||||
|
||||
@@ -1,9 +1,9 @@
|
||||
package ru.prisonlife.api.template;
|
||||
|
||||
import org.apache.ibatis.session.SqlSession;
|
||||
import org.bukkit.Bukkit;
|
||||
import ru.prisonlife.api.PrisonAPI;
|
||||
import ru.prisonlife.api.PrisonAPI.Factions;
|
||||
import ru.prisonlife.api.store.AbstractCachedDao;
|
||||
import ru.prisonlife.api.store.DailyRespectTaskEntity;
|
||||
|
||||
import java.lang.ref.WeakReference;
|
||||
@@ -28,21 +28,25 @@ public class Faction extends AbstractApiData {
|
||||
if (refDailyRespectTaskEntity != null && (entity = refDailyRespectTaskEntity.get()) != null) {
|
||||
return entity;
|
||||
} else {
|
||||
final AbstractCachedDao<DailyRespectTaskEntity> cachedDao = PrisonAPI.getDailyRespectTaskDao();
|
||||
entity = cachedDao.findByUuid(me.getPlayer().getUniqueId())
|
||||
.orElseThrow(() -> new RuntimeException(
|
||||
"no data in db by '" + me.getPlayer().getUniqueId().toString() + "'"));
|
||||
final SqlSession session = PrisonAPI.getSqlStore().openSession();
|
||||
entity = session.selectOne("DailyRespectTask.selectOne", me.getPlayer().getUniqueId().toString());
|
||||
session.close();
|
||||
|
||||
if (entity == null) {
|
||||
throw new RuntimeException("no data in db by '" + me.getPlayer().getUniqueId().toString() + "'");
|
||||
}
|
||||
|
||||
refDailyRespectTaskEntity = new WeakReference<>(entity);
|
||||
return entity;
|
||||
}
|
||||
}
|
||||
|
||||
public String getType() {
|
||||
return getPlayerStatsEntity().getFaction().name();
|
||||
return Factions.valueOf(getPlayerStatsEntity().getFaction()).name();
|
||||
}
|
||||
|
||||
public int getId() {
|
||||
return getPlayerStatsEntity().getFaction().getId();
|
||||
return getPlayerStatsEntity().getFaction();
|
||||
}
|
||||
|
||||
public int getRangId() {
|
||||
@@ -62,7 +66,7 @@ public class Faction extends AbstractApiData {
|
||||
}
|
||||
|
||||
public void setFaction(int id) {
|
||||
getPlayerStatsEntity().setFaction(Factions.valueOf(id));
|
||||
getPlayerStatsEntity().setFaction(id);
|
||||
}
|
||||
|
||||
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.chat.TextComponent;
|
||||
import org.apache.ibatis.session.SqlSession;
|
||||
import org.bukkit.ChatColor;
|
||||
import org.bukkit.configuration.file.FileConfiguration;
|
||||
import org.bukkit.configuration.file.YamlConfiguration;
|
||||
@@ -19,6 +20,8 @@ import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Random;
|
||||
|
||||
import static ru.prisonlife.api.PrisonAPI.getSqlStore;
|
||||
|
||||
public class Policeman extends Prisoner {
|
||||
|
||||
private Map<Prisoner, BukkitRunnable> timeInCage = new HashMap<>();
|
||||
@@ -28,77 +31,79 @@ public class Policeman extends Prisoner {
|
||||
}
|
||||
|
||||
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()) {
|
||||
Manager mng = new Manager(super.getPrison());
|
||||
List<String> all_cages = mng.getCages();
|
||||
PreparedStatement st = connection.prepareStatement("SELECT UUID FROM player_stats WHERE UUID = ?");
|
||||
st.setString(1, me.getUniqueId().toString());
|
||||
try(ResultSet resultSet = st.executeQuery()) {
|
||||
if(!resultSet.next()) {
|
||||
int su = prs.getSuspect();
|
||||
st.close();
|
||||
st = connection.prepareStatement(
|
||||
"INSERT INTO players_in_cages(NAME, UUID, TIME_IN_CAGE, BLOCKS_LEFT) " +
|
||||
"VALUES (?, ?, ?, ?)");
|
||||
st.setString(1, getPlayer().getName());
|
||||
st.setString(2, me.getUniqueId().toString());
|
||||
st.setInt(3, (su * 6));
|
||||
st.setInt(4, (su * 100));
|
||||
st.executeUpdate();
|
||||
}
|
||||
}
|
||||
st.close();
|
||||
prs.setSuspect(0);
|
||||
mng.teleportInToCage(prs, all_cages.get(new Random().nextInt(all_cages.size())));
|
||||
FileConfiguration configurationSuspect = YamlConfiguration.loadConfiguration(new File("plugins/PrisonSuspect/", "parameters.yml"));
|
||||
String actionMessage = ChatColor.translateAlternateColorCodes('&',
|
||||
configurationSuspect.getString("MESSAGES.ACTION_BAR_PROGRESS"));
|
||||
BukkitRunnable actionBar = new BukkitRunnable() {//"MESSAGES.WHEN_PRISONER_CAN_EXIT_FROM_CAGE"
|
||||
@Override
|
||||
public void run() {
|
||||
prs.getPlayer().spigot().sendMessage(ChatMessageType.ACTION_BAR, TextComponent.fromLegacyText(actionMessage));
|
||||
}
|
||||
};
|
||||
timeInCage.put(prs, new BukkitRunnable() {
|
||||
@Override
|
||||
public void run() { // Future = Пометить, что когда in Cage и вышел, то удалить поток, а если зашёл и in Cage - запустить ~ Optimize
|
||||
try {
|
||||
int time = 0;
|
||||
String uuid = prs.getPlayer().getUniqueId().toString();
|
||||
PreparedStatement st = connection.prepareStatement(
|
||||
"SELECT TIME_IN_CAGE FROM players_in_cages WHERE UUID = ?");
|
||||
st.setString(1, uuid);
|
||||
try(ResultSet resultSet = st.executeQuery()) {
|
||||
if(resultSet.next())
|
||||
time = resultSet.getInt(1);
|
||||
}
|
||||
if(time == 0) {
|
||||
prs.getPlayer().sendMessage(ChatColor.translateAlternateColorCodes('&',
|
||||
configurationSuspect.getString("MESSAGES.WHEN_PRISONER_CAN_EXIT_FROM_CAGE")));
|
||||
actionBar.cancel();
|
||||
cancel();
|
||||
timeInCage.remove(prs);
|
||||
st = connection.prepareStatement("DELETE FROM players_in_cages WHERE UUID = ?");
|
||||
st.setString(1, uuid);
|
||||
st.execute();
|
||||
} else {
|
||||
if(prs.getPlayer().isOnline()) {
|
||||
st = connection.prepareStatement("UPDATE players_in_cages SET TIME_IN_CAGE = " + (time - 1) + " WHERE UUID = ?");
|
||||
st.setString(1, uuid);
|
||||
st.execute();
|
||||
}
|
||||
}
|
||||
if (prs.getPlayer().isOnline()) {
|
||||
Manager mng = new Manager(super.getPrison());
|
||||
List<String> all_cages = mng.getCages();
|
||||
PreparedStatement st = connection.prepareStatement("SELECT UUID FROM player_stats WHERE UUID = ?");
|
||||
st.setString(1, me.getUniqueId().toString());
|
||||
try (ResultSet resultSet = st.executeQuery()) {
|
||||
if (!resultSet.next()) {
|
||||
int su = prs.getSuspect();
|
||||
st.close();
|
||||
} catch(SQLException e) {
|
||||
e.printStackTrace();
|
||||
st = connection.prepareStatement(
|
||||
"INSERT INTO players_in_cages(NAME, UUID, TIME_IN_CAGE, BLOCKS_LEFT) " +
|
||||
"VALUES (?, ?, ?, ?)");
|
||||
st.setString(1, getPlayer().getName());
|
||||
st.setString(2, me.getUniqueId().toString());
|
||||
st.setInt(3, (su * 6));
|
||||
st.setInt(4, (su * 100));
|
||||
st.executeUpdate();
|
||||
}
|
||||
}
|
||||
});
|
||||
int m = 20 * 60;
|
||||
actionBar.runTaskTimer(PrisonAPI.plugin, 5, 5);
|
||||
timeInCage.get(prs).runTaskTimer(PrisonAPI.plugin, m, m);
|
||||
st.close();
|
||||
prs.setSuspect(0);
|
||||
mng.teleportInToCage(prs, all_cages.get(new Random().nextInt(all_cages.size())));
|
||||
FileConfiguration configurationSuspect = YamlConfiguration.loadConfiguration(new File("plugins/PrisonSuspect/", "parameters.yml"));
|
||||
String actionMessage = ChatColor.translateAlternateColorCodes('&',
|
||||
configurationSuspect.getString("MESSAGES.ACTION_BAR_PROGRESS"));
|
||||
BukkitRunnable actionBar = new BukkitRunnable() {//"MESSAGES.WHEN_PRISONER_CAN_EXIT_FROM_CAGE"
|
||||
@Override
|
||||
public void run() {
|
||||
prs.getPlayer().spigot().sendMessage(ChatMessageType.ACTION_BAR, TextComponent.fromLegacyText(actionMessage));
|
||||
}
|
||||
};
|
||||
timeInCage.put(prs, new BukkitRunnable() {
|
||||
@Override
|
||||
public void run() { // Future = Пометить, что когда in Cage и вышел, то удалить поток, а если зашёл и in Cage - запустить ~ Optimize
|
||||
try {
|
||||
int time = 0;
|
||||
String uuid = prs.getPlayer().getUniqueId().toString();
|
||||
PreparedStatement st = connection.prepareStatement(
|
||||
"SELECT TIME_IN_CAGE FROM players_in_cages WHERE UUID = ?");
|
||||
st.setString(1, uuid);
|
||||
try (ResultSet resultSet = st.executeQuery()) {
|
||||
if (resultSet.next())
|
||||
time = resultSet.getInt(1);
|
||||
}
|
||||
if (time == 0) {
|
||||
prs.getPlayer().sendMessage(ChatColor.translateAlternateColorCodes('&',
|
||||
configurationSuspect.getString("MESSAGES.WHEN_PRISONER_CAN_EXIT_FROM_CAGE")));
|
||||
actionBar.cancel();
|
||||
cancel();
|
||||
timeInCage.remove(prs);
|
||||
st = connection.prepareStatement("DELETE FROM players_in_cages WHERE UUID = ?");
|
||||
st.setString(1, uuid);
|
||||
st.execute();
|
||||
} else {
|
||||
if (prs.getPlayer().isOnline()) {
|
||||
st = connection.prepareStatement("UPDATE players_in_cages SET TIME_IN_CAGE = " + (time - 1) + " WHERE UUID = ?");
|
||||
st.setString(1, uuid);
|
||||
st.execute();
|
||||
}
|
||||
}
|
||||
st.close();
|
||||
} catch (SQLException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
});
|
||||
int m = 20 * 60;
|
||||
actionBar.runTaskTimer(PrisonAPI.plugin, 5, 5);
|
||||
timeInCage.get(prs).runTaskTimer(PrisonAPI.plugin, m, m);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
package ru.prisonlife.api.template;
|
||||
|
||||
import org.apache.ibatis.session.SqlSession;
|
||||
import org.bukkit.Bukkit;
|
||||
import org.bukkit.Location;
|
||||
import org.bukkit.configuration.file.FileConfiguration;
|
||||
@@ -119,28 +120,32 @@ public class Prisoner {
|
||||
}
|
||||
|
||||
public int getSuspect() {
|
||||
try(PreparedStatement statement = getSqlStore().getConnection().prepareStatement("SELECT SUSPECT FROM player_stats WHERE UUID = ?")) {
|
||||
statement.setString(1, me.getPlayer().getUniqueId().toString());
|
||||
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());
|
||||
|
||||
try(ResultSet resultSet = statement.executeQuery()) {
|
||||
if(resultSet.next()) {
|
||||
return resultSet.getInt(1);
|
||||
try (ResultSet resultSet = statement.executeQuery()) {
|
||||
if (resultSet.next()) {
|
||||
return resultSet.getInt(1);
|
||||
}
|
||||
}
|
||||
} catch (SQLException z) {
|
||||
z.printStackTrace();
|
||||
}
|
||||
} catch(SQLException z) {
|
||||
z.printStackTrace();
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
public void setSuspect(int value) {
|
||||
try(PreparedStatement statement = getSqlStore().getConnection().prepareStatement("UPDATE player_stats SET SUSPECT = ? WHERE UUID = ?")) {
|
||||
statement.setInt(1, value);
|
||||
statement.setString(2, me.getPlayer().getUniqueId().toString());
|
||||
try (SqlSession session = getSqlStore().openSession()) {
|
||||
try (PreparedStatement statement = session.getConnection().prepareStatement("UPDATE player_stats SET SUSPECT = ? WHERE UUID = ?")) {
|
||||
statement.setInt(1, value);
|
||||
statement.setString(2, me.getPlayer().getUniqueId().toString());
|
||||
|
||||
statement.executeUpdate();
|
||||
} catch(SQLException z) {
|
||||
z.printStackTrace();
|
||||
statement.executeUpdate();
|
||||
} catch (SQLException z) {
|
||||
z.printStackTrace();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -153,67 +158,75 @@ public class Prisoner {
|
||||
}
|
||||
|
||||
public int getTimeLeftInCage() {
|
||||
try (PreparedStatement statement = getSqlStore().getConnection().prepareStatement("SELECT TIME_IN_CAGE FROM players_in_cages WHERE UUID = ?")) {
|
||||
statement.setString(1, me.getUniqueId().toString());
|
||||
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());
|
||||
|
||||
try(ResultSet resultSet = statement.executeQuery()) {
|
||||
if(resultSet.next()) {
|
||||
return resultSet.getInt(1);
|
||||
try (ResultSet resultSet = statement.executeQuery()) {
|
||||
if (resultSet.next()) {
|
||||
return resultSet.getInt(1);
|
||||
}
|
||||
}
|
||||
} catch (SQLException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
} catch (SQLException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
public void setTimeLeftInCage(int value) {
|
||||
try {
|
||||
PreparedStatement statement = getSqlStore().getConnection().prepareStatement("SELECT TIME_IN_CAGE FROM players_in_cages WHERE UUID = ?");
|
||||
statement.setString(1, me.getUniqueId().toString());
|
||||
try(ResultSet resultSet = statement.executeQuery()) {
|
||||
if(resultSet.next()) {
|
||||
statement.close();
|
||||
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());
|
||||
try (ResultSet resultSet = statement.executeQuery()) {
|
||||
if (resultSet.next()) {
|
||||
statement.close();
|
||||
|
||||
statement = getSqlStore().getConnection().prepareStatement("UPDATE players_in_cages SET TIME_IN_CAGE = ? WHERE UUID = ?");
|
||||
statement.setInt(1, value);
|
||||
statement.setString(2, me.getUniqueId().toString());
|
||||
statement.execute();
|
||||
statement = session.getConnection().prepareStatement("UPDATE players_in_cages SET TIME_IN_CAGE = ? WHERE UUID = ?");
|
||||
statement.setInt(1, value);
|
||||
statement.setString(2, me.getUniqueId().toString());
|
||||
statement.execute();
|
||||
|
||||
statement.close();
|
||||
statement.close();
|
||||
}
|
||||
}
|
||||
} catch (SQLException z) {
|
||||
z.printStackTrace();
|
||||
}
|
||||
} catch(SQLException z) {
|
||||
z.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
public int getBlocksLeftToBreak() {
|
||||
try (PreparedStatement statement = getSqlStore().getConnection().prepareStatement("SELECT BLOCKS_LEFT FROM players_in_cages WHERE UUID = ?")) {
|
||||
statement.setString(1, me.getUniqueId().toString());
|
||||
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());
|
||||
|
||||
try(ResultSet resultSet = statement.executeQuery()) {
|
||||
if(resultSet.next()) {
|
||||
return resultSet.getInt(1);
|
||||
try (ResultSet resultSet = statement.executeQuery()) {
|
||||
if (resultSet.next()) {
|
||||
return resultSet.getInt(1);
|
||||
}
|
||||
}
|
||||
} catch (SQLException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
} catch (SQLException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
public boolean isLocateInCage() {
|
||||
try (PreparedStatement statement = getSqlStore().getConnection().prepareStatement("SELECT UUID FROM players_in_cages WHERE UUID = ?")) {
|
||||
statement.setString(1, me.getUniqueId().toString());
|
||||
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());
|
||||
|
||||
try(ResultSet resultSet = statement.executeQuery()) {
|
||||
if(resultSet.next()) {
|
||||
return true;
|
||||
try (ResultSet resultSet = statement.executeQuery()) {
|
||||
if (resultSet.next()) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
} catch (SQLException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
} catch (SQLException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
return false;
|
||||
}
|
||||
@@ -231,37 +244,39 @@ public class Prisoner {
|
||||
}
|
||||
|
||||
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 = ?");
|
||||
statement.setString(1, me.getUniqueId().toString());
|
||||
PreparedStatement statement = connection.prepareStatement("SELECT UUID FROM player_stats WHERE UUID = ?");
|
||||
statement.setString(1, me.getUniqueId().toString());
|
||||
|
||||
try(ResultSet resultSet = statement.executeQuery()) {
|
||||
if(!resultSet.next()) {
|
||||
statement.close();
|
||||
try (ResultSet resultSet = statement.executeQuery()) {
|
||||
if (!resultSet.next()) {
|
||||
statement.close();
|
||||
|
||||
statement = connection.prepareStatement(
|
||||
"INSERT INTO player_stats(NAME, UUID, LEVEL, POINTS, FACTION, RESPECT, F_RANG, SUSPECT, WALLET, " +
|
||||
"G_TIME, D_BLOCKS, K_PLAYERS, K_MOBS, DEATHS) "
|
||||
+ "VALUES (?, ?, 1, 0, 'NONE', 0, 0, 0, 0, 0, 0, 0, 0, 0)");
|
||||
statement.setString(1, getPlayer().getName());
|
||||
statement.setString(2, me.getUniqueId().toString());
|
||||
statement.executeUpdate();
|
||||
statement.close();
|
||||
statement = connection.prepareStatement(
|
||||
"INSERT INTO player_stats(NAME, UUID, LEVEL, POINTS, FACTION, RESPECT, F_RANG, SUSPECT, WALLET, " +
|
||||
"G_TIME, D_BLOCKS, K_PLAYERS, K_MOBS, DEATHS) "
|
||||
+ "VALUES (?, ?, 1, 0, 'NONE', 0, 0, 0, 0, 0, 0, 0, 0, 0)");
|
||||
statement.setString(1, getPlayer().getName());
|
||||
statement.setString(2, me.getUniqueId().toString());
|
||||
statement.executeUpdate();
|
||||
statement.close();
|
||||
|
||||
statement = connection.prepareStatement(
|
||||
"INSERT INTO daily_respect_task(NAME, UUID, COOLDOWN) " +
|
||||
"VALUES (?, ?, ?)");
|
||||
statement.setString(1, getPlayer().getName());
|
||||
statement.setString(2, me.getUniqueId().toString());
|
||||
statement.setInt(3, (3600 * 24));
|
||||
statement.executeUpdate();
|
||||
statement = connection.prepareStatement(
|
||||
"INSERT INTO daily_respect_task(NAME, UUID, COOLDOWN) " +
|
||||
"VALUES (?, ?, ?)");
|
||||
statement.setString(1, getPlayer().getName());
|
||||
statement.setString(2, me.getUniqueId().toString());
|
||||
statement.setInt(3, (3600 * 24));
|
||||
statement.executeUpdate();
|
||||
|
||||
level = new Level(this);
|
||||
stats = new Stats(this);
|
||||
wallet = new Wallet(this);
|
||||
level = new Level(this);
|
||||
stats = new Stats(this);
|
||||
wallet = new Wallet(this);
|
||||
}
|
||||
}
|
||||
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