259 lines
9.7 KiB
Java
259 lines
9.7 KiB
Java
package ru.prisonlife.api.template;
|
|
|
|
import org.bukkit.Bukkit;
|
|
import org.bukkit.Location;
|
|
import org.bukkit.configuration.file.FileConfiguration;
|
|
import org.bukkit.configuration.file.YamlConfiguration;
|
|
import org.bukkit.entity.Player;
|
|
import ru.prisonlife.api.PrisonAPI;
|
|
|
|
import java.io.File;
|
|
import java.sql.PreparedStatement;
|
|
import java.sql.ResultSet;
|
|
import java.sql.SQLException;
|
|
import java.util.ArrayList;
|
|
import java.util.List;
|
|
import java.util.Objects;
|
|
|
|
public class Prisoner {
|
|
|
|
protected Player me;
|
|
protected Level level;
|
|
protected Faction faction;
|
|
protected Wallet wallet;
|
|
protected Stats stats;
|
|
|
|
public Prisoner(Player player) {
|
|
me = player;
|
|
level = new Level(this);
|
|
wallet = new Wallet(this);
|
|
faction = new Faction(this);
|
|
stats = new Stats(this);
|
|
}
|
|
|
|
public Player getPlayer() {
|
|
return me;
|
|
}
|
|
|
|
public Level getPrisonLevel() throws SQLException {
|
|
return this.level;
|
|
}
|
|
|
|
public String getPrison() {
|
|
Location loc = me.getLocation();
|
|
for (File file : Objects.requireNonNull(new File(PrisonAPI.PLUGIN_PATH).listFiles())) {
|
|
FileConfiguration fileConfiguration = YamlConfiguration.loadConfiguration(file);
|
|
List<List<Integer>> data = new ArrayList<>();
|
|
List<Integer> fromConfig = fileConfiguration.getIntegerList("DIAGONAL_POINTS"),
|
|
min = new ArrayList<>(), max = new ArrayList<>();
|
|
int[] loc_n = {loc.getBlockX(), loc.getBlockY() - 1, loc.getBlockZ()};
|
|
data.add(fromConfig.subList(0, 3));
|
|
data.add(fromConfig.subList(3, 6));
|
|
for (int i = 0; i < 3; i++) {
|
|
min.add(Math.min(data.get(0).get(i), data.get(1).get(i)));
|
|
max.add(Math.max(data.get(0).get(i), data.get(1).get(i)));
|
|
}
|
|
int c = 0;
|
|
for (int k : loc_n) {
|
|
for (int n = 0; n < 3; n++) {
|
|
if (k >= min.get(n) && k <= max.get(n)) {
|
|
++c;
|
|
}
|
|
}
|
|
}
|
|
if (c == 3) {
|
|
return file.getName().replace("PRISON_", "").replace(".yml", "");
|
|
}
|
|
}
|
|
return "Not Prison";
|
|
}
|
|
|
|
public String getTerritory() {
|
|
String prison = getPrison();
|
|
if(!prison.equals("Not Prison")) {
|
|
Location loc = me.getLocation();
|
|
FileConfiguration fileConfiguration = YamlConfiguration.loadConfiguration(new File(PrisonAPI.PLUGIN_PATH, "PRISON_" + prison + ".yml"));
|
|
try {
|
|
if(fileConfiguration.getConfigurationSection("TERRITORIES").getKeys(false).size() != 0) {
|
|
for(String terr : fileConfiguration.getConfigurationSection("TERRITORIES").getKeys(false)) {
|
|
List<List<Integer>> data = new ArrayList<>();
|
|
List<Integer> fromConfig = fileConfiguration.getIntegerList("TERRITORIES." + terr + ".DIAGONAL_POINTS"),
|
|
min = new ArrayList<>(), max = new ArrayList<>();
|
|
int[] loc_n = {loc.getBlockX(), loc.getBlockY() - 1, loc.getBlockZ()};
|
|
data.add(fromConfig.subList(0, 3));
|
|
data.add(fromConfig.subList(3, 6));
|
|
for(int i = 0; i < 3; i++) {
|
|
min.add(Math.min(data.get(0).get(i), data.get(1).get(i)));
|
|
max.add(Math.max(data.get(0).get(i), data.get(1).get(i)));
|
|
}
|
|
int c = 0;
|
|
for(int k : loc_n) {
|
|
for(int n = 0; n < 3; n++) {
|
|
if(k >= min.get(n) && k <= max.get(n)) {
|
|
++c;
|
|
}
|
|
}
|
|
}
|
|
if(c == 3)
|
|
return fileConfiguration.getString("TERRITORIES." + terr + ".NAME");
|
|
}
|
|
}
|
|
} catch (NullPointerException z) {
|
|
}
|
|
}
|
|
return "Not Territory";
|
|
}
|
|
|
|
public void teleportToFactionSpawn() {
|
|
try {
|
|
Prisoner ps = new Prisoner(me);
|
|
Manager mng = new Manager(getPrison());
|
|
String f = ps.getFaction().getType();
|
|
List<Integer> c = mng.getSpawnCords(ps.getFaction().getId());
|
|
if(!f.equals("NONE") && c.size() != 0)
|
|
me.teleport(new Location(Bukkit.getWorld(mng.getWorld()), c.get(0) + 0.5, c.get(1), c.get(2) + 0.5));
|
|
} catch(SQLException z) {
|
|
z.printStackTrace();
|
|
}
|
|
}
|
|
|
|
public int getSuspect() {
|
|
try {
|
|
try(PreparedStatement statement = PrisonAPI.CONN.prepareStatement("SELECT SUSPECT FROM player_stats WHERE UUID = ?")) {
|
|
statement.setString(1, me.getPlayer().getUniqueId().toString());
|
|
|
|
try(ResultSet resultSet = statement.executeQuery()) {
|
|
if(resultSet.next())
|
|
return resultSet.getInt(1);
|
|
}
|
|
}
|
|
} catch(SQLException z) {
|
|
z.printStackTrace();
|
|
}
|
|
return 0;
|
|
}
|
|
|
|
public void setSuspect(int value) {
|
|
try {
|
|
try(PreparedStatement statement = PrisonAPI.CONN.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();
|
|
}
|
|
}
|
|
|
|
public void addSuspect(int value) {
|
|
int su = getSuspect() + value;
|
|
setSuspect(su);
|
|
if(su > 6)
|
|
setSuspect(su - (su - 6));
|
|
}
|
|
|
|
public int getTimeLeftInCage() {
|
|
try {
|
|
PreparedStatement statement = PrisonAPI.CONN.prepareStatement("SELECT TIME_IN_CAGE FROM players_in_cages WHERE UUID = ?");
|
|
statement.setString(1, me.getUniqueId().toString());
|
|
try(ResultSet resultSet = statement.executeQuery()) {
|
|
if(resultSet.next())
|
|
return resultSet.getInt(1);
|
|
}
|
|
} catch (SQLException e) {
|
|
e.printStackTrace();
|
|
}
|
|
return 0;
|
|
}
|
|
|
|
public void setTimeLeftInCage(int value) {
|
|
try {
|
|
PreparedStatement statement = PrisonAPI.CONN.prepareStatement("SELECT TIME_IN_CAGE FROM players_in_cages WHERE UUID = ?");
|
|
statement.setString(1, me.getUniqueId().toString());
|
|
try(ResultSet resultSet = statement.executeQuery()) {
|
|
if(resultSet.next()) {
|
|
statement = PrisonAPI.CONN.prepareStatement("UPDATE players_in_cages SET TIME_IN_CAGE = ? WHERE UUID = ?");
|
|
statement.setInt(1, value);
|
|
statement.setString(2, me.getUniqueId().toString());
|
|
statement.execute();
|
|
}
|
|
}
|
|
} catch(SQLException z) {
|
|
z.printStackTrace();
|
|
}
|
|
}
|
|
|
|
public int getBlocksLeftToBreak() {
|
|
try {
|
|
PreparedStatement statement = PrisonAPI.CONN.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);
|
|
}
|
|
} catch (SQLException e) {
|
|
e.printStackTrace();
|
|
}
|
|
return 0;
|
|
}
|
|
|
|
public boolean isLocateInCage() {
|
|
try {
|
|
PreparedStatement statement = PrisonAPI.CONN.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;
|
|
}
|
|
} catch (SQLException e) {
|
|
e.printStackTrace();
|
|
}
|
|
return false;
|
|
}
|
|
|
|
public Faction getFaction() {
|
|
return faction;
|
|
}
|
|
|
|
public Wallet getWallet() {
|
|
return wallet;
|
|
}
|
|
|
|
public Stats getStats() {
|
|
return stats;
|
|
}
|
|
|
|
public void register() throws SQLException {
|
|
PreparedStatement statement = PrisonAPI.CONN.prepareStatement("SELECT UUID FROM player_stats WHERE UUID = ?");
|
|
statement.setString(1, me.getUniqueId().toString());
|
|
|
|
try(ResultSet resultSet = statement.executeQuery()) {
|
|
if(!resultSet.next()) {
|
|
statement.close();
|
|
|
|
statement = PrisonAPI.CONN.prepareStatement(
|
|
"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 = PrisonAPI.CONN.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);
|
|
}
|
|
}
|
|
statement.close();
|
|
}
|
|
} |