123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174 |
- package me.km.playerbank;
- import java.sql.ResultSet;
- import java.sql.SQLException;
- import java.util.UUID;
- import me.km.KajetansMod;
- import me.km.databank.IStatement;
- public class PlayerBank
- {
- public PlayerBank()
- {
- KajetansMod.databank.execute("CREATE TABLE IF NOT EXISTS players ("
- + "id INT NOT NULL AUTO_INCREMENT PRIMARY KEY, "
- + "uuid_1 BIGINT NOT NULL, "
- + "uuid_2 BIGINT NOT NULL, "
- + "name VARCHAR(20) NOT NULL, "
- + "UNIQUE INDEX (uuid_1, uuid_2));");
- }
-
- private final IStatement add = KajetansMod.databank.createStatement(
- "INSERT INTO players (uuid_1,uuid_2,name) VALUES (?,?,?);");
-
- public boolean add(UUID uuid, String name)
- {
- try
- {
- add.validate();
- add.setLong(1, uuid.getLeastSignificantBits());
- add.setLong(2, uuid.getMostSignificantBits());
- add.setString(3, name);
- add.executeUpdate();
- return true;
- }
- catch(SQLException ex)
- {
- ex.printStackTrace();
- return false;
- }
- }
-
- private final IStatement changeName = KajetansMod.databank.createStatement(
- "UPDATE players SET name=? WHERE uuid_1=? AND uuid_2=?;");
-
- public void changeName(UUID uuid, String name)
- {
- try
- {
- changeName.validate();
- changeName.setString(1, name);
- changeName.setLong(2, uuid.getLeastSignificantBits());
- changeName.setLong(3, uuid.getMostSignificantBits());
- changeName.executeUpdate();
- }
- catch(SQLException ex)
- {
- ex.printStackTrace();
- }
- }
-
- private final IStatement getId = KajetansMod.databank.createStatement(
- "SELECT id FROM players WHERE uuid_1=? AND uuid_2=?;");
-
- public int getId(UUID uuid)
- {
- try
- {
- getId.validate();
- getId.setLong(1, uuid.getLeastSignificantBits());
- getId.setLong(2, uuid.getMostSignificantBits());
- try(ResultSet rs = getId.executeQuery())
- {
- if(rs.next())
- {
- return rs.getInt(1);
- }
- return -1;
- }
- catch(SQLException ex)
- {
- ex.printStackTrace();
- }
- }
- catch(SQLException ex)
- {
- ex.printStackTrace();
- }
- return -1;
- }
-
- private final IStatement getUUIDfromID = KajetansMod.databank.createStatement(
- "SELECT uuid_1,uuid_2 FROM players WHERE id=?;");
-
- public UUID getUUIDfromID(int id)
- {
- try
- {
- getUUIDfromID.validate();
- getUUIDfromID.setInt(1, id);
- try(ResultSet rs = getUUIDfromID.executeQuery())
- {
- if(rs.next())
- {
- return new UUID(rs.getLong(2), rs.getLong(1));
- }
- }
- catch(SQLException ex)
- {
- ex.printStackTrace();
- }
- }
- catch(SQLException ex)
- {
- ex.printStackTrace();
- }
- return null;
- }
-
- private final IStatement getNamefromID = KajetansMod.databank.createStatement(
- "SELECT name FROM players WHERE id=?;");
-
- public String getNamefromID(int id)
- {
- try
- {
- getNamefromID.validate();
- getNamefromID.setInt(1, id);
- try(ResultSet rs = getNamefromID.executeQuery())
- {
- if(rs.next())
- {
- return rs.getString(1);
- }
- }
- catch(SQLException ex)
- {
- ex.printStackTrace();
- }
- }
- catch(SQLException ex)
- {
- ex.printStackTrace();
- }
- return "";
- }
-
- private final IStatement getUUID = KajetansMod.databank.createStatement(
- "SELECT uuid_1,uuid_2 FROM players WHERE name=?;");
-
- public UUID getUUID(String name)
- {
- try
- {
- getUUID.validate();
- getUUID.setString(1, name);
- try(ResultSet rs = getUUID.executeQuery())
- {
- if(rs.next())
- {
- return new UUID(rs.getLong(2), rs.getLong(1));
- }
- }
- catch(Exception ex)
- {
- ex.printStackTrace();
- }
- }
- catch(SQLException ex)
- {
- ex.printStackTrace();
- }
- return null;
- }
- }
|