Compare commits

..

5 Commits

9 changed files with 344 additions and 6 deletions

13
.gitignore vendored Normal file
View File

@ -0,0 +1,13 @@
# Ignore compiled class files
*.class
# Ignore Maven target directory
target/
out/
# Ignore IntelliJ IDEA project files
.idea/
*.iml
# Ignore Mac OS metadata
.DS_Store

View File

@ -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() {
@ -58,12 +62,7 @@ public final class Main extends JavaPlugin implements Listener {
public void onJoin(PlayerJoinEvent event) {
Player player = event.getPlayer();
// event.setJoinMessage(ChatColor.GREEN + "" + ChatColor.BOLD + "[+] " + ChatColor.RESET + ChatColor.YELLOW + player.getName());
if (player.hasPlayedBefore()) {
event.setJoinMessage(null);
} else {
event.setJoinMessage(ChatColor.GREEN + "" + ChatColor.BOLD + "[+] " + ChatColor.RESET + ChatColor.YELLOW + player.getName());
}
event.setJoinMessage(ChatColor.GREEN + "" + ChatColor.BOLD + "[+] " + ChatColor.RESET + ChatColor.YELLOW + player.getName());
setupPlayer(player);
}
@ -131,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();
}
}

View File

@ -0,0 +1,30 @@
package eu.creeper.mc.creeperPit.config;
import eu.creeper.mc.creeperPit.config.manager.Directory;
import eu.creeper.mc.creeperPit.config.manager.JSON;
import eu.creeper.mc.creeperPit.config.manager.YAML;
import org.bukkit.plugin.java.JavaPlugin;
import java.io.File;
public class CFGInit {
private final Directory dir;
public final JSON effectShop;
public final JSON itemShop;
public final YAML worldguard;
public CFGInit(JavaPlugin plugin) {
this.dir = new Directory(plugin);
dir.createFolder("shop");
File shopCfgs = dir.getSubFolder("shop");
File file = new File(shopCfgs, "effects.json");
this.effectShop = new JSON(plugin, file);
file = new File(shopCfgs, "items.json");
this.itemShop = new JSON(plugin, file);
file = new File(dir.getPluginFolder(), "worldguard.yml");
this.worldguard = new YAML(plugin, file);
}
}

View File

@ -0,0 +1,40 @@
package eu.creeper.mc.creeperPit.config.manager;
import org.bukkit.plugin.java.JavaPlugin;
import java.io.File;
public class Directory {
private final JavaPlugin plugin;
public Directory(JavaPlugin plugin) {
this.plugin = plugin;
}
public File getPluginFolder() {
File folder = plugin.getDataFolder();
if (!folder.exists()) {
folder.mkdirs();
}
return folder;
}
public File createFolder(String name) {
File folder = new File(plugin.getDataFolder(), name);
if (!folder.exists()) {
folder.mkdirs();
}
return folder;
}
public File getSubFolder(String name) {
File sub = new File(getPluginFolder(), name);
if (!sub.exists()) {
sub.mkdirs();
}
return sub;
}
public boolean exists(String name) {
return new File(getPluginFolder(), name).exists();
}
}

View File

@ -0,0 +1,51 @@
package eu.creeper.mc.creeperPit.config.manager;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import org.bukkit.plugin.java.JavaPlugin;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
public class JSON {
private final JavaPlugin plugin;
private final File file;
private final Gson gson;
public JSON(JavaPlugin plugin, File file) {
this.plugin = plugin;
this.file = file;
this.gson = new GsonBuilder().setPrettyPrinting().create();
}
public void save(Object data) {
try {
if (!plugin.getDataFolder().exists()) {
plugin.getDataFolder().mkdirs();
}
try (FileWriter writer = new FileWriter(file)) {
gson.toJson(data, writer);
}
} catch (Exception e) {
plugin.getLogger().severe("COULDNT SAVE FILE " + file.getName());
e.printStackTrace();
}
}
public <T> T load(Class<T> clazz, T defaultValue) {
try {
if (!file.exists()) {
save(defaultValue);
return defaultValue;
}
try (FileReader reader = new FileReader(file)) {
return gson.fromJson(reader, clazz);
}
} catch (Exception e) {
plugin.getLogger().severe("COULDNT LOAD FILE " + file.getName() + " (invalid json?)");
e.printStackTrace();
return defaultValue;
}
}
}

View File

@ -0,0 +1,48 @@
package eu.creeper.mc.creeperPit.config.manager;
import org.bukkit.configuration.file.FileConfiguration;
import org.bukkit.configuration.file.YamlConfiguration;
import org.bukkit.plugin.java.JavaPlugin;
import java.io.File;
public class YAML {
private final JavaPlugin plugin;
private final File file;
private FileConfiguration config;
public YAML(JavaPlugin plugin, File file) {
this.plugin = plugin;
this.file = file;
load();
}
public void load() {
try {
if (!plugin.getDataFolder().exists()) {
plugin.getDataFolder().mkdirs();
}
if (!file.exists()) {
file.createNewFile();
}
config = YamlConfiguration.loadConfiguration(file);
} catch (Exception e) {
plugin.getLogger().severe("COULDNT LOAD FILE " + file.getName() + " (invalid yaml?)");
e.printStackTrace();
}
}
public void save() {
try {
config.save(file);
} catch (Exception e) {
plugin.getLogger().severe("COULDNT SAVE FILE " + file.getName());
e.printStackTrace();
}
}
public FileConfiguration get() {
return config;
}
}

View File

@ -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));
}
}

View File

@ -8,10 +8,16 @@ import java.util.List;
public class WGManager implements Listener {
private static final List<Region> regions = new ArrayList<>();
public static List<Region> getRegions() {
return regions;
}
public static void registerRegion(Region region) {
regions.add(region);
}
public static void unregisterRegion(Region region) {
regions.remove(region);
}
public static Region getRegionAt(Location loc) {
for (Region region : regions) {

View File

@ -6,6 +6,27 @@ prefix: CPit
authors: [ Befaci, creeper ]
description: CreeperPit gameplay manager
permissions:
creeperpit.admin:
default: "op"
description: "Is an administrator of CreeperPit"
creeperpit.worldguard.place:
default: "op"
description: "Can bypass the place WorldGuard's security"
creeperpit.worldguard.break:
default: "op"
description: "Can bypass the breaking WorldGuard's security"
creeperpit.worldguard.pvp:
default: "op"
description: "Can bypass the PVP WorldGuard's security"
creeperpit.worldguard.drop:
default: "op"
description: "Can bypass the dropping WorldGuard's security"
creeperpit.worldguard.*:
default: "op"
description: "Can bypass every WorldGuard security"
children:
- creeperpit.worldguard.break
- creeperpit.worldguard.place
- creeperpit.worldguard.drop
- creeperpit.worldguard.pvp