Archived
0

crlf -> lf

This commit is contained in:
2019-02-24 16:03:56 +03:00
parent 32bdc620ac
commit 8a695ae909
5 changed files with 358 additions and 358 deletions

View File

@@ -1,58 +1,58 @@
package ru.prisonlife.prisonapi; package ru.prisonlife.prisonapi;
import org.bukkit.plugin.java.JavaPlugin; import org.bukkit.plugin.java.JavaPlugin;
import java.sql.Connection; import java.sql.Connection;
import java.sql.DriverManager; 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 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);
public static Factions valueOf(int id) { public static Factions valueOf(int id) {
for (Factions factions : Factions.values()) { for (Factions factions : Factions.values()) {
if (factions.id == id) { if (factions.id == id) {
return factions; return factions;
} }
} }
throw new IllegalArgumentException("Factions id " + id + " not defined!"); throw new IllegalArgumentException("Factions id " + id + " not defined!");
} }
private int id; private int id;
Factions(int id) { Factions(int id) {
this.id = id; this.id = id;
} }
public int getId() { public int getId() {
return id; return id;
} }
} }
public static Connection CONN; public static Connection CONN;
public void onEnable() { public void onEnable() {
try { try {
CONN = DriverManager.getConnection( CONN = DriverManager.getConnection(
"jdbc:mysql://localhost:3306/prison4life", "jdbc:mysql://localhost:3306/prison4life",
"root", "root"); "root", "root");
} catch (SQLException e) { } catch (SQLException e) {
e.printStackTrace(); e.printStackTrace();
} }
} }
public void onDisable() { public void onDisable() {
try { try {
if (!CONN.isClosed()) { if (!CONN.isClosed()) {
CONN.close(); CONN.close();
} }
} catch (SQLException e) { } catch (SQLException e) {
e.printStackTrace(); e.printStackTrace();
} }
} }
} }

View File

@@ -1,57 +1,57 @@
package ru.prisonlife.prisonapi.template; package ru.prisonlife.prisonapi.template;
import java.sql.PreparedStatement; import java.sql.PreparedStatement;
import java.sql.ResultSet; import java.sql.ResultSet;
import java.sql.SQLException; import java.sql.SQLException;
import static ru.prisonlife.prisonapi.PrisonAPI.CONN; import static ru.prisonlife.prisonapi.PrisonAPI.CONN;
public class Level { public class Level {
private Prisoner me; private Prisoner me;
private Score score; private Score score;
Level(Prisoner prisoner) { Level(Prisoner prisoner) {
me = prisoner; me = prisoner;
score = new Score(me); score = new Score(me);
} }
public Score getScore() throws SQLException { public Score getScore() throws SQLException {
//FIXME приведи в порядок метод //FIXME приведи в порядок метод
/* /*
connect(); connect();
if (resultSet.first()) { if (resultSet.first()) {
return this.score; return this.score;
} }
disconnect(); disconnect();
*/ */
return null; return null;
} }
public double getLevel() throws SQLException, NullPointerException { public double getLevel() throws SQLException, NullPointerException {
try (PreparedStatement statement = CONN.prepareStatement("SELECT LEVEL FROM player_stats WHERE UUID = ?")) { try (PreparedStatement statement = CONN.prepareStatement("SELECT LEVEL 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.getDouble(1); return resultSet.getDouble(1);
} else { } else {
return 1d; //TODO нужно или default значение, или exception return 1d; //TODO нужно или default значение, или exception
} }
} }
} }
} }
public void addLevel(double value) throws SQLException, NullPointerException { public void addLevel(double value) throws SQLException, NullPointerException {
setLevel(getLevel() + value); setLevel(getLevel() + value);
} }
public void setLevel(double value) throws SQLException, NullPointerException { public void setLevel(double value) throws SQLException, NullPointerException {
try (PreparedStatement statement = CONN.prepareStatement("UPDATE player_stats SET LEVEL = ? WHERE UUID = ?")) { try (PreparedStatement statement = CONN.prepareStatement("UPDATE player_stats SET LEVEL = ? WHERE UUID = ?")) {
statement.setDouble(1, getLevel() + value); statement.setDouble(1, getLevel() + value);
statement.setString(2, me.getPlayer().getUniqueId().toString()); statement.setString(2, me.getPlayer().getUniqueId().toString());
statement.executeUpdate(); statement.executeUpdate();
} }
} }
} }

View File

@@ -1,154 +1,154 @@
package ru.prisonlife.prisonapi.template; package ru.prisonlife.prisonapi.template;
import org.bukkit.Location; import org.bukkit.Location;
import org.bukkit.configuration.file.FileConfiguration; import org.bukkit.configuration.file.FileConfiguration;
import org.bukkit.configuration.file.YamlConfiguration; import org.bukkit.configuration.file.YamlConfiguration;
import org.bukkit.entity.Player; import org.bukkit.entity.Player;
import java.io.File; import java.io.File;
import java.sql.PreparedStatement; import java.sql.PreparedStatement;
import java.sql.ResultSet; import java.sql.ResultSet;
import java.sql.SQLException; import java.sql.SQLException;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
import java.util.Objects; import java.util.Objects;
import static ru.prisonlife.prisonapi.PrisonAPI.CONN; import static ru.prisonlife.prisonapi.PrisonAPI.CONN;
import static ru.prisonlife.prisonapi.PrisonAPI.PLUGIN_PATH; import static ru.prisonlife.prisonapi.PrisonAPI.PLUGIN_PATH;
public class Prisoner { public class Prisoner {
private Player me; private Player me;
private Level level; private Level level;
private Faction faction; private Faction faction;
private Wallet wallet; private Wallet wallet;
private Stats stats; private Stats stats;
public Prisoner(Player player) { public Prisoner(Player player) {
me = player; me = player;
level = new Level(this); level = new Level(this);
wallet = new Wallet(this); wallet = new Wallet(this);
faction = new Faction(this); faction = new Faction(this);
stats = new Stats(this); stats = new Stats(this);
} }
public Player getPlayer() { public Player getPlayer() {
return me; return me;
} }
public Level getPrisonLevel() throws SQLException { public Level getPrisonLevel() throws SQLException {
return this.level; return this.level;
} }
public String getPrison() { public String getPrison() {
Location loc = me.getLocation(); Location loc = me.getLocation();
for (File file : Objects.requireNonNull(new File(PLUGIN_PATH).listFiles())) { for (File file : Objects.requireNonNull(new File(PLUGIN_PATH).listFiles())) {
FileConfiguration fileConfiguration = YamlConfiguration.loadConfiguration(file); FileConfiguration fileConfiguration = YamlConfiguration.loadConfiguration(file);
List<List<Integer>> data = new ArrayList<>(); List<List<Integer>> data = new ArrayList<>();
List<Integer> fromConfig = fileConfiguration.getIntegerList("DIAGONAL_POINTS"), List<Integer> fromConfig = fileConfiguration.getIntegerList("DIAGONAL_POINTS"),
min = new ArrayList<>(), max = new ArrayList<>(); min = new ArrayList<>(), max = new ArrayList<>();
int[] loc_n = {loc.getBlockX(), loc.getBlockY() - 1, loc.getBlockZ()}; int[] loc_n = {loc.getBlockX(), loc.getBlockY() - 1, loc.getBlockZ()};
data.add(fromConfig.subList(0, 3)); data.add(fromConfig.subList(0, 3));
data.add(fromConfig.subList(3, 6)); data.add(fromConfig.subList(3, 6));
for (int i = 0; i < 3; i++) { for (int i = 0; i < 3; i++) {
min.add(Math.min(data.get(0).get(i), data.get(1).get(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))); max.add(Math.max(data.get(0).get(i), data.get(1).get(i)));
} }
int c = 0; int c = 0;
for (int k : loc_n) { for (int k : loc_n) {
for (int n = 0; n < 3; n++) { for (int n = 0; n < 3; n++) {
if (k >= min.get(n) && k <= max.get(n)) { if (k >= min.get(n) && k <= max.get(n)) {
++c; ++c;
} }
} }
} }
if (c == 3) { if (c == 3) {
return file.getName().replace("PRISON_", "").replace(".yml", ""); return file.getName().replace("PRISON_", "").replace(".yml", "");
} }
} }
return "Not Prison"; return "Not Prison";
} }
public String getTerritory(String type) { public String getTerritory(String type) {
String prison = getPrison(); String prison = getPrison();
if (!prison.equals("Not Prison") && (type.equals("name") || type.equals("owner"))) { if (!prison.equals("Not Prison") && (type.equals("name") || type.equals("owner"))) {
Location loc = me.getLocation(); Location loc = me.getLocation();
FileConfiguration fileConfiguration = YamlConfiguration.loadConfiguration(new File(PLUGIN_PATH, "PRISON_" + prison + ".yml")); FileConfiguration fileConfiguration = YamlConfiguration.loadConfiguration(new File(PLUGIN_PATH, "PRISON_" + prison + ".yml"));
try { try {
if (fileConfiguration.getConfigurationSection("TERRITORIES").getKeys(false).size() != 0) { if (fileConfiguration.getConfigurationSection("TERRITORIES").getKeys(false).size() != 0) {
for (String terr : fileConfiguration.getConfigurationSection("TERRITORIES").getKeys(false)) { for (String terr : fileConfiguration.getConfigurationSection("TERRITORIES").getKeys(false)) {
List<List<Integer>> data = new ArrayList<>(); List<List<Integer>> data = new ArrayList<>();
List<Integer> fromConfig = fileConfiguration.getIntegerList("TERRITORIES." + terr + ".DIAGONAL_POINTS"), List<Integer> fromConfig = fileConfiguration.getIntegerList("TERRITORIES." + terr + ".DIAGONAL_POINTS"),
min = new ArrayList<>(), max = new ArrayList<>(); min = new ArrayList<>(), max = new ArrayList<>();
int[] loc_n = {loc.getBlockX(), loc.getBlockY() - 1, loc.getBlockZ()}; int[] loc_n = {loc.getBlockX(), loc.getBlockY() - 1, loc.getBlockZ()};
data.add(fromConfig.subList(0, 3)); data.add(fromConfig.subList(0, 3));
data.add(fromConfig.subList(3, 6)); data.add(fromConfig.subList(3, 6));
for (int i = 0; i < 3; i++) { for (int i = 0; i < 3; i++) {
min.add(Math.min(data.get(0).get(i), data.get(1).get(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))); max.add(Math.max(data.get(0).get(i), data.get(1).get(i)));
} }
int c = 0; int c = 0;
for (int k : loc_n) { for (int k : loc_n) {
for (int n = 0; n < 3; n++) { for (int n = 0; n < 3; n++) {
if (k >= min.get(n) && k <= max.get(n)) { if (k >= min.get(n) && k <= max.get(n)) {
++c; ++c;
} }
} }
} }
if (c == 3) { if (c == 3) {
return type.equals("name") ? fileConfiguration.getString("TERRITORIES." + terr + ".NAME") : return type.equals("name") ? fileConfiguration.getString("TERRITORIES." + terr + ".NAME") :
(type.equals("owner") ? fileConfiguration.getString("TERRITORIES." + terr + ".OWNER") : null); (type.equals("owner") ? fileConfiguration.getString("TERRITORIES." + terr + ".OWNER") : null);
} }
} }
} }
} catch (NullPointerException z) { } catch (NullPointerException z) {
} }
} }
return "Not Territory"; return "Not Territory";
} }
public Faction getFaction() { public Faction getFaction() {
return faction; return faction;
} }
public Wallet getWallet() { public Wallet getWallet() {
return wallet; return wallet;
} }
public Stats getStats() { public Stats getStats() {
return stats; return stats;
} }
public void register() throws SQLException { public void register() throws SQLException {
PreparedStatement statement = CONN.prepareStatement("SELECT UUID FROM player_stats WHERE UUID = ?"); PreparedStatement statement = CONN.prepareStatement("SELECT UUID FROM player_stats WHERE UUID = ?");
statement.setString(1, me.getUniqueId().toString()); statement.setString(1, me.getUniqueId().toString());
try (ResultSet resultSet = statement.executeQuery()) { try (ResultSet resultSet = statement.executeQuery()) {
if (!resultSet.next()) { if (!resultSet.next()) {
statement.close(); statement.close();
statement = CONN.prepareStatement( statement = CONN.prepareStatement(
"INSERT INTO player_stats(NAME, UUID, LEVEL, POINTS, FACTION, RESPECT, F_RANG, WALLET, " + "INSERT INTO player_stats(NAME, UUID, LEVEL, POINTS, FACTION, RESPECT, F_RANG, WALLET, " +
"G_TIME, D_BLOCKS, K_PLAYERS, K_MOBS, DEATHS) " "G_TIME, D_BLOCKS, K_PLAYERS, K_MOBS, DEATHS) "
+ "VALUES (?, ?, 1, 0, 'NONE', 0, 0, 0, 0, 0, 0, 0, 0)"); + "VALUES (?, ?, 1, 0, 'NONE', 0, 0, 0, 0, 0, 0, 0, 0)");
statement.setString(1, getPlayer().getName()); statement.setString(1, getPlayer().getName());
statement.setString(2, me.getUniqueId().toString()); statement.setString(2, me.getUniqueId().toString());
statement.executeUpdate(); statement.executeUpdate();
statement.close(); statement.close();
statement = CONN.prepareStatement( statement = CONN.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());
statement.setString(2, me.getUniqueId().toString()); statement.setString(2, me.getUniqueId().toString());
statement.setInt(3, (3600 * 24)); statement.setInt(3, (3600 * 24));
statement.executeUpdate(); statement.executeUpdate();
level = new Level(this); level = new Level(this);
stats = new Stats(this); stats = new Stats(this);
wallet = new Wallet(this); wallet = new Wallet(this);
} }
} }
statement.close(); statement.close();
} }
} }

View File

@@ -1,43 +1,43 @@
package ru.prisonlife.prisonapi.template; package ru.prisonlife.prisonapi.template;
import java.sql.PreparedStatement; import java.sql.PreparedStatement;
import java.sql.ResultSet; import java.sql.ResultSet;
import java.sql.SQLException; import java.sql.SQLException;
import static ru.prisonlife.prisonapi.PrisonAPI.CONN; import static ru.prisonlife.prisonapi.PrisonAPI.CONN;
public class Score { public class Score {
private Prisoner me; private Prisoner me;
Score(Prisoner prisoner) { Score(Prisoner prisoner) {
me = prisoner; me = prisoner;
} }
public double getPoints() throws SQLException { public double getPoints() throws SQLException {
try (PreparedStatement statement = CONN.prepareStatement("SELECT POINTS FROM player_stats WHERE UUID = ?")) { try (PreparedStatement statement = CONN.prepareStatement("SELECT POINTS 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.getDouble(1); return resultSet.getDouble(1);
} else { } else {
return 0d; //TODO нужно или default значение, или exception return 0d; //TODO нужно или default значение, или exception
} }
} }
} }
} }
public void setPoints(double value) throws SQLException { public void setPoints(double value) throws SQLException {
try (PreparedStatement statement = CONN.prepareStatement("UPDATE player_stats SET POINTS = ? WHERE UUID = ?")) { try (PreparedStatement statement = CONN.prepareStatement("UPDATE player_stats SET POINTS = ? WHERE UUID = ?")) {
statement.setDouble(1, value); statement.setDouble(1, value);
statement.setString(2, me.getPlayer().getUniqueId().toString()); statement.setString(2, me.getPlayer().getUniqueId().toString());
statement.executeUpdate(); statement.executeUpdate();
} }
} }
public void addPoints(double value) throws SQLException { public void addPoints(double value) throws SQLException {
setPoints(getPoints() + value); setPoints(getPoints() + value);
} }
} }

View File

@@ -1,47 +1,47 @@
package ru.prisonlife.prisonapi.template; package ru.prisonlife.prisonapi.template;
import java.sql.PreparedStatement; import java.sql.PreparedStatement;
import java.sql.ResultSet; import java.sql.ResultSet;
import java.sql.SQLException; import java.sql.SQLException;
import static ru.prisonlife.prisonapi.PrisonAPI.CONN; import static ru.prisonlife.prisonapi.PrisonAPI.CONN;
public class Wallet { public class Wallet {
private Prisoner me; private Prisoner me;
Wallet(Prisoner prisoner) { Wallet(Prisoner prisoner) {
me = prisoner; me = prisoner;
} }
public int getBalance() throws SQLException { public int getBalance() throws SQLException {
try (PreparedStatement statement = CONN.prepareStatement("SELECT WALLET FROM player_stats WHERE UUID = ?")) { try (PreparedStatement statement = CONN.prepareStatement("SELECT WALLET 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);
} else { } else {
return 0; //TODO нужно или default значение, или exception return 0; //TODO нужно или default значение, или exception
} }
} }
} }
} }
public void addBalance(int value) throws SQLException { public void addBalance(int value) throws SQLException {
setBalance(getBalance() + value); setBalance(getBalance() + value);
} }
public void deposit(int value) throws SQLException { public void deposit(int value) throws SQLException {
setBalance(getBalance() - value); setBalance(getBalance() - value);
} }
public void setBalance(int value) throws SQLException { public void setBalance(int value) throws SQLException {
try (PreparedStatement statement = CONN.prepareStatement("UPDATE player_stats SET WALLET = ? WHERE UUID = ?")) { try (PreparedStatement statement = CONN.prepareStatement("UPDATE player_stats SET WALLET = ? WHERE UUID = ?")) {
statement.setDouble(1, value); statement.setDouble(1, value);
statement.setString(2, me.getPlayer().getUniqueId().toString()); statement.setString(2, me.getPlayer().getUniqueId().toString());
statement.executeUpdate(); statement.executeUpdate();
} }
} }
} }