why was this moved?? im dumb ig
This commit is contained in:
@@ -1,82 +0,0 @@
|
||||
package com.alttd.commands.database;
|
||||
|
||||
import com.alttd.VillagerUI;
|
||||
import com.alttd.config.Config;
|
||||
import com.alttd.util.Logger;
|
||||
import org.bukkit.Bukkit;
|
||||
|
||||
import java.sql.Connection;
|
||||
import java.sql.DriverManager;
|
||||
import java.sql.SQLException;
|
||||
|
||||
public class Database {
|
||||
|
||||
private static Database instance = null;
|
||||
public static Connection connection = null;
|
||||
|
||||
private Database() {
|
||||
|
||||
}
|
||||
|
||||
public static Database getDatabase(){
|
||||
if (instance == null)
|
||||
instance = new Database();
|
||||
return (instance);
|
||||
}
|
||||
|
||||
public void init() {
|
||||
try {
|
||||
openConnection();
|
||||
} catch (SQLException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
|
||||
// Tables
|
||||
createUserPointsTable();
|
||||
}
|
||||
|
||||
/**
|
||||
* Opens the connection if it's not already open.
|
||||
* @throws SQLException If it can't create the connection.
|
||||
*/
|
||||
private void openConnection() throws SQLException {
|
||||
if (connection != null && !connection.isClosed()) {
|
||||
return;
|
||||
}
|
||||
|
||||
synchronized (this) {
|
||||
if (connection != null && !connection.isClosed()) {
|
||||
return;
|
||||
}
|
||||
try {
|
||||
Class.forName("com.mysql.cj.jdbc.Driver");
|
||||
} catch (ClassNotFoundException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
|
||||
connection = DriverManager.getConnection(
|
||||
"jdbc:mysql://" + Config.IP + ":" + Config.PORT + "/" + Config.DATABASE_NAME +
|
||||
"?autoReconnect=true&useSSL=false&allowPublicKeyRetrieval=true",
|
||||
Config.USERNAME, Config.PASSWORD);
|
||||
}
|
||||
}
|
||||
|
||||
private static void createUserPointsTable() {
|
||||
try {
|
||||
String sql = "CREATE TABLE IF NOT EXISTS user_points(" +
|
||||
"UUID varchar(36) NOT NULL, " +
|
||||
"points int NOT NULL, " +
|
||||
"villager_type varchar(128) NOT NULL, " +
|
||||
"PRIMARY KEY (UUID), " +
|
||||
"UNIQUE KEY (villager_type)" +
|
||||
")";
|
||||
connection.prepareStatement(sql).executeUpdate();
|
||||
} catch (SQLException e) {
|
||||
e.printStackTrace();
|
||||
Logger.severe("Error while trying to create user point table");
|
||||
Logger.severe("Shutting down VillagerUI");
|
||||
Bukkit.getPluginManager().disablePlugin(VillagerUI.getInstance());
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,125 +0,0 @@
|
||||
package com.alttd.commands.database;
|
||||
|
||||
import com.alttd.objects.EconUser;
|
||||
import com.alttd.objects.VillagerType;
|
||||
import com.alttd.util.Logger;
|
||||
import it.unimi.dsi.fastutil.objects.Object2IntMap;
|
||||
import it.unimi.dsi.fastutil.objects.Object2ObjectArrayMap;
|
||||
|
||||
import java.sql.PreparedStatement;
|
||||
import java.sql.ResultSet;
|
||||
import java.sql.SQLException;
|
||||
import java.util.UUID;
|
||||
|
||||
public class Queries {
|
||||
/**
|
||||
* NOTE: run async
|
||||
* Get the points a user has for a villager type
|
||||
*
|
||||
* @param uuid Uuid for the user you want to get the points for
|
||||
* @param villagerType Type of villager you want to get the points for
|
||||
* @return The amount of points a user has for the given villager type
|
||||
*/
|
||||
public static int getUserPoints(UUID uuid, VillagerType villagerType) {
|
||||
String sql = "SELECT points FROM user_points " +
|
||||
"WHERE UUID = ? " +
|
||||
"AND villager_type = ?;";
|
||||
int points = 0;
|
||||
|
||||
try {
|
||||
PreparedStatement preparedStatement = Database.connection.prepareStatement(sql);
|
||||
preparedStatement.setString(1, uuid.toString());
|
||||
preparedStatement.setString(2, villagerType.getName());
|
||||
|
||||
ResultSet resultSet = preparedStatement.executeQuery();
|
||||
if (resultSet != null && resultSet.next()) {
|
||||
points = resultSet.getInt("points");
|
||||
}
|
||||
} catch (SQLException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
return (points);
|
||||
}
|
||||
|
||||
/**
|
||||
* NOTE: run async
|
||||
* Add a specified amount of points to the user for the given villager type
|
||||
*
|
||||
* @param uuid Uuid for the user you want to add the points to
|
||||
* @param villagerType Type of villager you want to add the points to
|
||||
* @param points The amount of points to add
|
||||
* @return success
|
||||
*/
|
||||
public static boolean updateUserPoints(UUID uuid, String villagerType, int points) {
|
||||
String sql = "UPDATE Points = SET user_points = user_points + ? " +
|
||||
"WHERE UUID = ? " +
|
||||
"AND villager_type = ?;";
|
||||
|
||||
try {
|
||||
PreparedStatement preparedStatement = Database.connection.prepareStatement(sql);
|
||||
preparedStatement.setInt(1, points);
|
||||
preparedStatement.setString(2, uuid.toString());
|
||||
preparedStatement.setString(3, villagerType);
|
||||
|
||||
if (preparedStatement.executeUpdate() == 0)
|
||||
return createUserPointsEntry(uuid, villagerType, points);
|
||||
} catch (SQLException e) {
|
||||
e.printStackTrace();
|
||||
Logger.warning("Unable to add % points to %" +
|
||||
" for villager type %", String.valueOf(points), uuid.toString(), villagerType);
|
||||
return (false);
|
||||
}
|
||||
return (true);
|
||||
}
|
||||
|
||||
/**
|
||||
* NOTE: run async
|
||||
* Create a new user entry
|
||||
*
|
||||
* @param uuid Uuid for the user you want to create an entry for
|
||||
* @param villagerType Type of villager you want to use in that entry
|
||||
* @param points The amount of points to set the start to
|
||||
* @return success
|
||||
*/
|
||||
public static boolean createUserPointsEntry(UUID uuid, String villagerType, int points) {
|
||||
String sql = "INSERT INTO Points " +
|
||||
"(uuid, villager_type, points) " +
|
||||
"(?, ?, ?)";
|
||||
|
||||
try {
|
||||
PreparedStatement preparedStatement = Database.connection.prepareStatement(sql);
|
||||
preparedStatement.setString(1, uuid.toString());
|
||||
preparedStatement.setString(2, villagerType);
|
||||
preparedStatement.setInt(3, points);
|
||||
|
||||
return (preparedStatement.execute());
|
||||
} catch (SQLException e) {
|
||||
e.printStackTrace();
|
||||
Logger.warning("Unable to create point entry for %" +
|
||||
" for villager type %", uuid.toString(), villagerType);
|
||||
return (false);
|
||||
}
|
||||
}
|
||||
|
||||
public static EconUser getEconUser(UUID uuid) {
|
||||
String sql = "SELECT * FROM POINTS WHERE uuid = ?";
|
||||
|
||||
try {
|
||||
PreparedStatement preparedStatement = Database.connection.prepareStatement(sql);
|
||||
preparedStatement.setString(1, uuid.toString());
|
||||
|
||||
ResultSet resultSet = preparedStatement.executeQuery();
|
||||
Object2ObjectArrayMap<String, Integer> points = new Object2ObjectArrayMap<>();
|
||||
while (resultSet.next()) {
|
||||
points.put(
|
||||
resultSet.getString("villager_type"),
|
||||
resultSet.getInt("points"));
|
||||
}
|
||||
return (new EconUser(uuid, points));
|
||||
|
||||
} catch (SQLException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
return (null);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user