From 24e338e28876a8215412cde842edd38db59f10b7 Mon Sep 17 00:00:00 2001 From: M_Viper Date: Tue, 12 Aug 2025 21:53:14 +0000 Subject: [PATCH] Dateien nach "src/main/java/craftersland/games/money" hochladen --- .../games/money/ConfigurationHandler.java | 35 +++++++ .../java/craftersland/games/money/Money.java | 95 +++++++++++++++++++ .../games/money/PlayerListener.java | 70 ++++++++++++++ 3 files changed, 200 insertions(+) create mode 100644 src/main/java/craftersland/games/money/ConfigurationHandler.java create mode 100644 src/main/java/craftersland/games/money/Money.java create mode 100644 src/main/java/craftersland/games/money/PlayerListener.java diff --git a/src/main/java/craftersland/games/money/ConfigurationHandler.java b/src/main/java/craftersland/games/money/ConfigurationHandler.java new file mode 100644 index 0000000..271311d --- /dev/null +++ b/src/main/java/craftersland/games/money/ConfigurationHandler.java @@ -0,0 +1,35 @@ +package net.craftersland.games.money; + +import java.io.File; + +public class ConfigurationHandler { + + private Money money; + + public ConfigurationHandler(Money money) { + this.money = money; + if (!(new File("plugins"+System.getProperty("file.separator")+"Money"+System.getProperty("file.separator")+"config.yml").exists())) { + Money.log.info("No config file found! Creating new one..."); + money.saveDefaultConfig(); + } + try { + money.getConfig().load(new File("plugins"+System.getProperty("file.separator")+"Money"+System.getProperty("file.separator")+"config.yml")); + } catch (Exception e) { + Money.log.info("Could not load config file!"); + e.printStackTrace(); + } + } + + public String getString(String key) { + if (!money.getConfig().contains(key)) { + money.getLogger().severe("Could not locate '"+key+"' in the config.yml inside of the Money folder! (Try generating a new one by deleting the current)"); + return "errorCouldNotLocateInConfigYml:"+key; + } else { + if (key.toLowerCase().contains("color")) { + return "§"+money.getConfig().getString(key); + } + return money.getConfig().getString(key); + } + } + +} diff --git a/src/main/java/craftersland/games/money/Money.java b/src/main/java/craftersland/games/money/Money.java new file mode 100644 index 0000000..eaa6664 --- /dev/null +++ b/src/main/java/craftersland/games/money/Money.java @@ -0,0 +1,95 @@ +package net.craftersland.games.money; + +import java.io.File; +import java.util.concurrent.ExecutorService; +import java.util.concurrent.Executors; +import java.util.logging.Logger; + +import net.craftersland.games.money.database.AccountDatabaseInterface; +import net.craftersland.games.money.database.DatabaseManagerInterface; +import net.craftersland.games.money.database.DatabaseManagerMysql; +import net.craftersland.games.money.database.MoneyMysqlInterface; +import net.milkbowl.vault.economy.Economy; + +import org.bukkit.plugin.PluginManager; +import org.bukkit.plugin.RegisteredServiceProvider; +import org.bukkit.plugin.java.JavaPlugin; + +public final class Money extends JavaPlugin { + + public static Logger log; + public static Economy econ = null; + public static ExecutorService execService = null; + + private ConfigurationHandler configurationHandler; + private DatabaseManagerInterface databaseManager; + private AccountDatabaseInterface moneyDatabaseInterface; + + @Override + public void onEnable(){ + log = getLogger(); + log.info("Loading Money Games/Lobby "+getDescription().getVersion()+"... "); + + //Create Money folder + (new File("plugins"+System.getProperty("file.separator")+"Money")).mkdir(); + + + //Setup Vault for economy and permissions + if (!setupEconomy() ) { + log.severe(String.format("[%s] - Disabled! Vault installed? If yes Economy system installed?)", getDescription().getName())); + getServer().getPluginManager().disablePlugin(this); + return; + } + + //Load Configuration + configurationHandler = new ConfigurationHandler(this); + + //Initiate Threadpool + execService = Executors.newFixedThreadPool(Integer.parseInt(configurationHandler.getString("database.maximumThreads"))); + + //Setup Database + //if (configurationHandler.getString("database.typeOfDatabase").equalsIgnoreCase("mysql")) { + log.info("Using MYSQL as Datasource..."); + databaseManager = new DatabaseManagerMysql(this); + moneyDatabaseInterface = new MoneyMysqlInterface(this); + //} + + //Register Listeners + PluginManager pm = getServer().getPluginManager(); + pm.registerEvents(new PlayerListener(this), this); + + log.info("Money mysql has been successfully loaded!"); + } + + @Override + public void onDisable() { + log.info("Money mysql has been disabled"); + } + + //Methods for setting up Vault + private boolean setupEconomy() { + if (getServer().getPluginManager().getPlugin("Vault") == null) { + return false; + } + RegisteredServiceProvider rsp = getServer().getServicesManager().getRegistration(Economy.class); + if (rsp == null) { + return false; + } + econ = rsp.getProvider(); + return econ != null; + } + + //Getter for Database Interfaces + public AccountDatabaseInterface getMoneyDatabaseInterface() { + return moneyDatabaseInterface; + } + + public ConfigurationHandler getConfigurationHandler() { + return configurationHandler; + } + + public DatabaseManagerInterface getDatabaseManagerInterface() { + return databaseManager; + } + +} diff --git a/src/main/java/craftersland/games/money/PlayerListener.java b/src/main/java/craftersland/games/money/PlayerListener.java new file mode 100644 index 0000000..a92c382 --- /dev/null +++ b/src/main/java/craftersland/games/money/PlayerListener.java @@ -0,0 +1,70 @@ +package net.craftersland.games.money; + +import net.milkbowl.vault.economy.Economy; + +import org.bukkit.Bukkit; +import org.bukkit.event.EventHandler; +import org.bukkit.event.Listener; +import org.bukkit.event.player.PlayerJoinEvent; +import org.bukkit.event.player.PlayerQuitEvent; + +public class PlayerListener implements Listener{ + + private Money money; + public static Economy econ = null; + @SuppressWarnings("unused") + private ConfigurationHandler coHa; + + public PlayerListener(Money money) { + this.money = money; + this.coHa = money.getConfigurationHandler(); + } + + @SuppressWarnings("deprecation") + @EventHandler + public void onLogin(final PlayerJoinEvent event) { + + //Check if player has mysql an account first + if (!money.getMoneyDatabaseInterface().hasAccount(event.getPlayer().getUniqueId())) + { + return; + } + //Added a small delay to prevent the onDisconnect handler overlapping onLogin on a BungeeCord configuration when switching servers. + Bukkit.getServer().getScheduler().scheduleAsyncDelayedTask(money, new Runnable() { + + @Override + public void run() { + + //Set local balance to 0 before depositing the mysql balance + if (Money.econ.getBalance(event.getPlayer()) > 0) + { + Money.econ.withdrawPlayer(event.getPlayer(), Money.econ.getBalance(event.getPlayer())); + } + + //Set mysql balance to local balance + Money.econ.depositPlayer(event.getPlayer(), money.getMoneyDatabaseInterface().getBalance(event.getPlayer().getUniqueId())); + + } + }, 20L); + + } + + @EventHandler + public void onDisconnect(PlayerQuitEvent event) { + + //Check if local balance is 0 + if (Money.econ.getBalance(event.getPlayer()) == 0) + { + return; + } + //Check if player has account and if no create it + if (!money.getMoneyDatabaseInterface().hasAccount(event.getPlayer().getUniqueId())) + { + money.getMoneyDatabaseInterface().createAccount(event.getPlayer().getUniqueId()); + } + //Set local balance on mysql balance + money.getMoneyDatabaseInterface().setBalance(event.getPlayer().getUniqueId(), Money.econ.getBalance(event.getPlayer())); + + } + +}