forked from CreeperMC/creeperpit
soo uhh mariadb thing done and other stuff modified (and not finished)
This commit is contained in:
parent
0950b1b2dd
commit
b1c30df5e6
|
|
@ -4,6 +4,8 @@ import eu.creeper.mc.creeperPit.worldguard.Handler;
|
|||
import eu.creeper.mc.creeperPit.worldguard.Region;
|
||||
import eu.creeper.mc.creeperPit.worldguard.WGManager;
|
||||
import org.bukkit.*;
|
||||
import org.bukkit.configuration.ConfigurationSection;
|
||||
import org.bukkit.configuration.file.FileConfiguration;
|
||||
import org.bukkit.entity.Player;
|
||||
import org.bukkit.event.EventHandler;
|
||||
import org.bukkit.event.Listener;
|
||||
|
|
@ -14,6 +16,7 @@ import org.bukkit.inventory.PlayerInventory;
|
|||
import org.bukkit.plugin.java.JavaPlugin;
|
||||
import org.bukkit.potion.PotionEffect;
|
||||
import org.bukkit.potion.PotionEffectType;
|
||||
import eu.creeper.mc.creeperPit.config.CFGInit;
|
||||
|
||||
import java.util.Objects;
|
||||
|
||||
|
|
@ -27,6 +30,7 @@ public final class Main extends JavaPlugin implements Listener {
|
|||
Bukkit.getPluginManager().registerEvents(this, this);
|
||||
Bukkit.getPluginManager().registerEvents(new Handler(), this);
|
||||
registerRegions(world);
|
||||
initConfig();
|
||||
}
|
||||
|
||||
private World getMainWorld() {
|
||||
|
|
@ -126,4 +130,30 @@ public final class Main extends JavaPlugin implements Listener {
|
|||
player.setHealth(20.0);
|
||||
player.setFoodLevel(20);
|
||||
}
|
||||
|
||||
private void initConfig() {
|
||||
CFGInit cfgInit = new CFGInit(this);
|
||||
cfgInit.itemShop.save("[]");
|
||||
cfgInit.effectShop.save("[]");
|
||||
|
||||
FileConfiguration wgCfgFile = cfgInit.worldguard.get();
|
||||
ConfigurationSection globalSection = wgCfgFile.getConfigurationSection("global");
|
||||
if (globalSection == null) {
|
||||
globalSection = wgCfgFile.createSection("global");
|
||||
|
||||
//coord
|
||||
globalSection.set("x1", true);
|
||||
globalSection.set("y1", true);
|
||||
globalSection.set("z1", true);
|
||||
globalSection.set("x2", true);
|
||||
globalSection.set("y2", true);
|
||||
globalSection.set("z2", true);
|
||||
//can do x thing
|
||||
globalSection.set("canBuild", true);
|
||||
globalSection.set("canBreak", true);
|
||||
globalSection.set("canPvp", true);
|
||||
globalSection.set("canDrop", true);
|
||||
}
|
||||
cfgInit.worldguard.save();
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -7,14 +7,14 @@ import org.bukkit.plugin.java.JavaPlugin;
|
|||
|
||||
import java.io.File;
|
||||
|
||||
public class Init {
|
||||
public class CFGInit {
|
||||
private final Directory dir;
|
||||
|
||||
public final JSON effectShop;
|
||||
public final JSON itemShop;
|
||||
public final YAML worldguard;
|
||||
|
||||
public Init(JavaPlugin plugin) {
|
||||
public CFGInit(JavaPlugin plugin) {
|
||||
this.dir = new Directory(plugin);
|
||||
dir.createFolder("shop");
|
||||
|
||||
|
|
@ -28,7 +28,7 @@ public class JSON {
|
|||
gson.toJson(data, writer);
|
||||
}
|
||||
} catch (Exception e) {
|
||||
plugin.getLogger().severe("Erreur sauvegarde JSON : " + file.getName());
|
||||
plugin.getLogger().severe("COULDNT SAVE FILE " + file.getName());
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
|
@ -43,7 +43,7 @@ public class JSON {
|
|||
return gson.fromJson(reader, clazz);
|
||||
}
|
||||
} catch (Exception e) {
|
||||
plugin.getLogger().severe("Erreur chargement JSON : " + file.getName());
|
||||
plugin.getLogger().severe("COULDNT LOAD FILE " + file.getName() + " (invalid json?)");
|
||||
e.printStackTrace();
|
||||
return defaultValue;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -28,7 +28,7 @@ public class YAML {
|
|||
}
|
||||
config = YamlConfiguration.loadConfiguration(file);
|
||||
} catch (Exception e) {
|
||||
plugin.getLogger().severe("Erreur chargement YAML : " + file.getName());
|
||||
plugin.getLogger().severe("COULDNT LOAD FILE " + file.getName() + " (invalid yaml?)");
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
|
@ -37,7 +37,7 @@ public class YAML {
|
|||
try {
|
||||
config.save(file);
|
||||
} catch (Exception e) {
|
||||
plugin.getLogger().severe("Erreur sauvegarde YAML : " + file.getName());
|
||||
plugin.getLogger().severe("COULDNT SAVE FILE " + file.getName());
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,4 +1,108 @@
|
|||
package eu.creeper.mc.creeperPit.mysql;
|
||||
import java.sql.Connection;
|
||||
import java.sql.DriverManager;
|
||||
import java.sql.PreparedStatement;
|
||||
import java.sql.ResultSet;
|
||||
import java.sql.SQLException;
|
||||
import java.util.UUID;
|
||||
|
||||
/**
|
||||
* MariaDBManager
|
||||
* Gère toute la base de données pour le système de coins
|
||||
*/
|
||||
public class MariaDBManager {
|
||||
private Connection connection;
|
||||
private final String host;
|
||||
private final int port; // 3306 default port
|
||||
|
||||
private final String database;
|
||||
private final String user;
|
||||
private final String password;
|
||||
|
||||
public MariaDBManager(String host, int port, String database, String user, String password) {
|
||||
this.host = host;
|
||||
this.port = port;
|
||||
this.database = database;
|
||||
this.user = user;
|
||||
this.password = password;
|
||||
}
|
||||
|
||||
public void connect() throws SQLException {
|
||||
if (isConnected()) return;
|
||||
|
||||
String url = "jdbc:mariadb://" + host + ":" + port + "/" + database + "?autoReconnect=true";
|
||||
|
||||
connection = DriverManager.getConnection(url, user, password);
|
||||
}
|
||||
public void disconnect() throws SQLException {
|
||||
if (isConnected()) {
|
||||
connection.close();
|
||||
}
|
||||
}
|
||||
public boolean isConnected() throws SQLException {
|
||||
return connection != null && !connection.isClosed();
|
||||
}
|
||||
public Connection getConnection() {
|
||||
return connection;
|
||||
}
|
||||
|
||||
public void createTable() throws SQLException {
|
||||
String sql = """
|
||||
CREATE TABLE IF NOT EXISTS coins (
|
||||
uuid VARCHAR(36) PRIMARY KEY,
|
||||
coins INT NOT NULL
|
||||
);
|
||||
""";
|
||||
connection.prepareStatement(sql).executeUpdate();
|
||||
}
|
||||
|
||||
public void createPlayer(UUID uuid) throws SQLException {
|
||||
if (hasAccount(uuid)) return;
|
||||
|
||||
String sql = "INSERT INTO coins (uuid, coins) VALUES (?, ?)";
|
||||
PreparedStatement ps = connection.prepareStatement(sql);
|
||||
|
||||
ps.setString(1, uuid.toString());
|
||||
ps.setInt(2, 0);
|
||||
|
||||
ps.executeUpdate();
|
||||
}
|
||||
public boolean hasAccount(UUID uuid) throws SQLException {
|
||||
String sql = "SELECT uuid FROM coins WHERE uuid = ?";
|
||||
PreparedStatement ps = connection.prepareStatement(sql);
|
||||
|
||||
ps.setString(1, uuid.toString());
|
||||
|
||||
ResultSet rs = ps.executeQuery();
|
||||
return rs.next();
|
||||
}
|
||||
|
||||
public int getCoins(UUID uuid) throws SQLException {
|
||||
String sql = "SELECT coins FROM coins WHERE uuid = ?";
|
||||
PreparedStatement ps = connection.prepareStatement(sql);
|
||||
|
||||
ps.setString(1, uuid.toString());
|
||||
|
||||
ResultSet rs = ps.executeQuery();
|
||||
if (rs.next()) {
|
||||
return rs.getInt("coins");
|
||||
}
|
||||
|
||||
return -1; // so we can detect that the player isnt found
|
||||
}
|
||||
public void setCoins(UUID uuid, int coins) throws SQLException {
|
||||
String sql = "UPDATE coins SET coins = ? WHERE uuid = ?";
|
||||
PreparedStatement ps = connection.prepareStatement(sql);
|
||||
|
||||
ps.setInt(1, coins);
|
||||
ps.setString(2, uuid.toString());
|
||||
|
||||
ps.executeUpdate();
|
||||
}
|
||||
public void addCoins(UUID uuid, int amount) throws SQLException {
|
||||
setCoins(uuid, getCoins(uuid) + amount);
|
||||
}
|
||||
public void removeCoins(UUID uuid, int amount) throws SQLException {
|
||||
setCoins(uuid, Math.max(0, getCoins(uuid) - amount));
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in New Issue
Block a user