From 1a166b50e835b9e0c2a8ada54864b1d46479da42 Mon Sep 17 00:00:00 2001 From: M_Viper Date: Thu, 7 Aug 2025 21:26:50 +0000 Subject: [PATCH] Dateien nach "src/main/java/net/viper/money" hochladen --- .../java/net/viper/money/CommandHandler.java | 137 +++++++++ .../java/net/viper/money/ConfigHandler.java | 213 ++++++++++++++ .../java/net/viper/money/InterestHandler.java | 97 ++++++ src/main/java/net/viper/money/Money.java | 209 +++++++++++++ .../java/net/viper/money/Placeholders.java | 88 ++++++ .../java/net/viper/money/PlayerListener.java | 276 ++++++++++++++++++ .../java/net/viper/money/SoundHandler.java | 48 +++ 7 files changed, 1068 insertions(+) create mode 100644 src/main/java/net/viper/money/CommandHandler.java create mode 100644 src/main/java/net/viper/money/ConfigHandler.java create mode 100644 src/main/java/net/viper/money/InterestHandler.java create mode 100644 src/main/java/net/viper/money/Money.java create mode 100644 src/main/java/net/viper/money/Placeholders.java create mode 100644 src/main/java/net/viper/money/PlayerListener.java create mode 100644 src/main/java/net/viper/money/SoundHandler.java diff --git a/src/main/java/net/viper/money/CommandHandler.java b/src/main/java/net/viper/money/CommandHandler.java new file mode 100644 index 0000000..df4a4a6 --- /dev/null +++ b/src/main/java/net/viper/money/CommandHandler.java @@ -0,0 +1,137 @@ +package net.craftersland.money; + +import org.bukkit.Sound; +import org.bukkit.command.Command; +import org.bukkit.command.CommandExecutor; +import org.bukkit.command.CommandSender; +import org.bukkit.entity.Player; + +public class CommandHandler implements CommandExecutor { + + private final Money money; + + public CommandHandler(Money money) { + this.money = money; + } + + @Override + public boolean onCommand(final CommandSender sender, final Command command, final String cmdlabel, final String[] args) { + Player p; + if (cmdlabel.equalsIgnoreCase("meb") || cmdlabel.equalsIgnoreCase("bank")) { + if (args.length == 0) { + if (sender instanceof Player) { + p = (Player) sender; + sendHelp(p); + return true; + } else { + sendConsoleHelp(sender); + return false; + } + } else if (args.length == 1) { + if (args[0].equalsIgnoreCase("reload")) { + money.getReloadCmd().runCmd(sender); + } else if (args[0].equalsIgnoreCase("help")) { + if (sender instanceof Player) { + p = (Player) sender; + sendHelp(p); + return true; + } else { + sendConsoleHelp(sender); + return false; + } + } else if (args[0].equalsIgnoreCase("balance")) { + money.getBalanceCmd().runUserCmd(sender); + } else if (args[0].equalsIgnoreCase("interest")) { + money.getInterestCmd().runUserCmd(sender); + } else { + if (sender instanceof Player) { + p = (Player) sender; + sendHelp(p); + return false; + } else { + sendConsoleHelp(sender); + return false; + } + } + } else if (args.length == 2) { + if (args[0].equalsIgnoreCase("balance")) { + money.getBalanceCmd().runAdminCmd(sender, args); + } else if (args[0].equalsIgnoreCase("deposit")) { + money.getDepositCmd().runUserCmd(sender, args); + } else if (args[0].equalsIgnoreCase("withdraw")) { + money.getWithdrawCmd().runUserCmd(sender, args); + } else { + if (sender instanceof Player) { + p = (Player) sender; + sendHelp(p); + return true; + } else { + sendConsoleHelp(sender); + return false; + } + } + } else if (args.length == 3) { + if (args[0].equalsIgnoreCase("set")) { + money.getSetCmd().runCmd(sender, args); + } else if (args[0].equalsIgnoreCase("deposit")) { + money.getDepositCmd().runAdminCmd(sender, args); + } else if (args[0].equalsIgnoreCase("withdraw")) { + money.getWithdrawCmd().runAdminCmd(sender, args); + } else { + if (sender instanceof Player) { + p = (Player) sender; + sendHelp(p); + return true; + } else { + sendConsoleHelp(sender); + return false; + } + } + } + } + + return false; + } + + public void sendHelp(Player p) { + if (money.is19Server) { + p.playSound(p.getLocation(), Sound.BLOCK_ANVIL_LAND, 1.0F, 1.0F); + } else { + p.playSound(p.getLocation(), Sound.valueOf("ANVIL_LAND"), 1.0F, 1.0F); + } + for (String s : money.getConfigurationHandler().getStringList("chatMessages.playerHelpMessage.Title")) { + p.sendMessage(s.replace('&', '§')); + } + if (p.hasPermission("MysqlEconomyBank.admin")) { + for (String s : money.getConfigurationHandler().getStringList("chatMessages.playerHelpMessage.Admin")) { + p.sendMessage(s.replace('&', '§')); + } + } else if (p.hasPermission("MysqlEconomyBank.balance") || p.hasPermission("MysqlEconomyBank.deposit") || p.hasPermission("MysqlEconomyBank.withdraw")) { + if (p.hasPermission("MysqlEconomyBank.balance")) { + for (String s : money.getConfigurationHandler().getStringList("chatMessages.playerHelpMessage.BalancePerm")) { + p.sendMessage(s.replace('&', '§')); + } + } + if (p.hasPermission("MysqlEconomyBank.deposit")) { + for (String s : money.getConfigurationHandler().getStringList("chatMessages.playerHelpMessage.DepositPerm")) { + p.sendMessage(s.replace('&', '§')); + } + } + if (p.hasPermission("MysqlEconomyBank.withdraw")) { + for (String s : money.getConfigurationHandler().getStringList("chatMessages.playerHelpMessage.WithdrawPerm")) { + p.sendMessage(s.replace('&', '§')); + } + } + } else { + for (String s : money.getConfigurationHandler().getStringList("chatMessages.playerHelpMessage.NoPerms")) { + p.sendMessage(s.replace('&', '§')); + } + } + } + + public void sendConsoleHelp(CommandSender sender) { + for (String s : money.getConfigurationHandler().getStringList("chatMessages.consoleHelpMsg")) { + sender.sendMessage(s.replace('&', '§')); + } + } +} diff --git a/src/main/java/net/viper/money/ConfigHandler.java b/src/main/java/net/viper/money/ConfigHandler.java new file mode 100644 index 0000000..8b09d92 --- /dev/null +++ b/src/main/java/net/viper/money/ConfigHandler.java @@ -0,0 +1,213 @@ +package net.craftersland.money; + +import net.md_5.bungee.api.ChatMessageType; +import net.md_5.bungee.api.chat.TextComponent; + +import java.io.File; +import java.text.DecimalFormat; +import java.util.ArrayList; +import java.util.List; + +import org.bukkit.ChatColor; +import org.bukkit.entity.Player; + +public class ConfigHandler { + + private Money money; + + // Config Generation and load + public ConfigHandler(Money money) { + this.money = money; + loadConfig(); + } + + public void loadConfig() { + // Create MysqlPlayerDataBridge folder + File pluginFolder = new File("plugins" + System.getProperty("file.separator") + Money.instance.getDescription().getName()); + if (!pluginFolder.exists()) { + pluginFolder.mkdir(); + } + + File configFile = new File("plugins" + System.getProperty("file.separator") + Money.instance.getDescription().getName() + System.getProperty("file.separator") + "config.yml"); + if (!configFile.exists()) { + Money.log.info("No config file found! Creating new one..."); + money.saveDefaultConfig(); + } + + try { + Money.log.info("Loading the config file..."); + money.getConfig().load(configFile); + } catch (Exception e) { + Money.log.severe("Could not load the config file! You need to regenerate the config! Error: " + e.getMessage()); + e.printStackTrace(); + } + } + + // Get config contents by strings + 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.instance.getDescription().getName() + " folder! (Try generating a new one by deleting the current)"); + return "errorCouldNotLocateInConfigYml:" + key; + } else { + return money.getConfig().getString(key); + } + } + + public String getStringWithColor(String key) { + if (!money.getConfig().contains(key)) { + money.getLogger().severe("Could not locate " + key + " in the config.yml inside of the " + Money.instance.getDescription().getName() + " folder! (Try generating a new one by deleting the current)"); + return "errorCouldNotLocateInConfigYml:" + key; + } else { + return money.getConfig().getString(key).replaceAll("&", "§"); + } + } + + public List getStringList(String key) { + if (!money.getConfig().contains(key)) { + money.getLogger().severe("Could not locate " + key + " in the config.yml inside of the " + Money.instance.getDescription().getName() + " folder! (Try generating a new one by deleting the current)"); + return null; + } else { + return money.getConfig().getStringList(key); + } + } + + public Integer getInteger(String key) { + if (!money.getConfig().contains(key)) { + money.getLogger().severe("Could not locate " + key + " in the config.yml inside of the " + Money.instance.getDescription().getName() + " folder! (Try generating a new one by deleting the current)"); + return null; + } else { + return money.getConfig().getInt(key); + } + } + + public Boolean getBoolean(String key) { + if (!money.getConfig().contains(key)) { + money.getLogger().severe("Could not locate " + key + " in the config.yml inside of the " + Money.instance.getDescription().getName() + " folder! (Try generating a new one by deleting the current)"); + return null; + } else { + return money.getConfig().getBoolean(key); + } + } + + // Send player messages using the config predefined messages + public void printMessage(Player player, String messageKey, String amount, Player player2, String player2Name) { + if (money.getConfig().contains(messageKey)) { + List message = new ArrayList<>(); + message.add(money.getConfig().getString(messageKey)); + + // if message is set to '' cancel the message + if (getString(messageKey).equals("")) { + return; + } + + DecimalFormat f = new DecimalFormat("#,##0.00"); + + if (amount != null && !amount.equals("")) { + Double amountDouble = Double.parseDouble(amount); + if (amountDouble.toString().endsWith(".0")) { + DecimalFormat fr = new DecimalFormat("#,##0"); + message.set(0, message.get(0).replaceAll("%amount", fr.format(amountDouble))); + } else { + message.set(0, message.get(0).replaceAll("%amount", f.format(amountDouble))); + } + } + + if (player2 != null) { + if (!player2Name.equals("")) { + message.set(0, message.get(0).replaceAll("%player2", player2Name)); + } + if (money.getMoneyDatabaseInterface().hasAccount(player2)) { + double balance = money.getMoneyDatabaseInterface().getBalance(player2); + if (Double.toString(balance).endsWith(".0")) { + DecimalFormat fr = new DecimalFormat("#,##0"); + message.set(0, message.get(0).replaceAll("%balance", fr.format(balance))); + } else { + message.set(0, message.get(0).replaceAll("%balance", f.format(balance))); + } + } + } + + message.set(0, message.get(0).replaceAll("%pocket", "" + Money.econ.getBalance(player))); + message.set(0, message.get(0).replaceAll("%player", player.getName())); + // Message format + player.sendMessage(parseFormattingCodes(getString("chatMessages.prefix")) + parseFormattingCodes(message.get(0))); + for (int i = 1; i < message.size(); i++) { + player.sendMessage(parseFormattingCodes(message.get(i))); + } + + } else { + money.getLogger().severe("Could not locate '" + messageKey + "' in the config.yml inside of the MysqlEconomyBank folder!"); + player.sendMessage(ChatColor.DARK_RED + "" + ChatColor.BOLD + ">> " + ChatColor.RED + "Could not locate '" + messageKey + "' in the config.yml inside of the MysqlEconomyBank folder!"); + } + } + + // Color and Format codes support + public String parseFormattingCodes(String message) { + message = message.replaceAll("&0", ChatColor.BLACK + ""); + message = message.replaceAll("&1", ChatColor.DARK_BLUE + ""); + message = message.replaceAll("&2", ChatColor.DARK_GREEN + ""); + message = message.replaceAll("&3", ChatColor.DARK_AQUA + ""); + message = message.replaceAll("&4", ChatColor.DARK_RED + ""); + message = message.replaceAll("&5", ChatColor.DARK_PURPLE + ""); + message = message.replaceAll("&6", ChatColor.GOLD + ""); + message = message.replaceAll("&7", ChatColor.GRAY + ""); + message = message.replaceAll("&8", ChatColor.DARK_GRAY + ""); + message = message.replaceAll("&9", ChatColor.BLUE + ""); + message = message.replaceAll("&a", ChatColor.GREEN + ""); + message = message.replaceAll("&b", ChatColor.AQUA + ""); + message = message.replaceAll("&c", ChatColor.RED + ""); + message = message.replaceAll("&d", ChatColor.LIGHT_PURPLE + ""); + message = message.replaceAll("&e", ChatColor.YELLOW + ""); + message = message.replaceAll("&f", ChatColor.WHITE + ""); + message = message.replaceAll("&l", ChatColor.BOLD + ""); + message = message.replaceAll("&o", ChatColor.ITALIC + ""); + message = message.replaceAll("&m", ChatColor.STRIKETHROUGH + ""); + message = message.replaceAll("&n", ChatColor.UNDERLINE + ""); + message = message.replaceAll("&k", ChatColor.MAGIC + ""); + message = message.replaceAll("&r", ChatColor.RESET + ""); + return message; + } + + // Send action bar message. + public void actionBarMessage(Player player, String messageKey) { + if (money.getConfig().contains(messageKey)) { + List message = new ArrayList<>(); + message.add(money.getConfig().getString(messageKey)); + + // if message is set to '' cancel the message + if (message.get(0).equals("")) { + return; + } + + // replace placeholders + DecimalFormat f = new DecimalFormat("#,##0.00"); + double bankBalance = money.getMoneyDatabaseInterface().getBalance(player); + double pocketBalance = Money.econ.getBalance(player); + + if (Double.toString(bankBalance).endsWith(".0")) { + DecimalFormat fr = new DecimalFormat("#,##0"); + message.set(0, message.get(0).replaceAll("%bankBalance", fr.format(bankBalance))); + } else { + message.set(0, message.get(0).replaceAll("%bankBalance", f.format(bankBalance))); + } + + if (Double.toString(pocketBalance).endsWith(".0")) { + DecimalFormat fr = new DecimalFormat("#,##0"); + message.set(0, message.get(0).replaceAll("%pocketBalance", fr.format(pocketBalance))); + } else { + message.set(0, message.get(0).replaceAll("%pocketBalance", f.format(pocketBalance))); + } + + // send the action bar message using Spigot API (native) + if (player != null) { + player.spigot().sendMessage(ChatMessageType.ACTION_BAR, + TextComponent.fromLegacyText(parseFormattingCodes(message.get(0)))); + } + + } else { + money.getLogger().severe("Could not locate '" + messageKey + "' in the config.yml inside of the MysqlEconomyBank folder!"); + player.sendMessage(ChatColor.DARK_RED + "" + ChatColor.BOLD + ">> " + ChatColor.RED + "Could not locate '" + messageKey + "' in the config.yml inside of the MysqlEconomyBank folder!"); + } + } + +} diff --git a/src/main/java/net/viper/money/InterestHandler.java b/src/main/java/net/viper/money/InterestHandler.java new file mode 100644 index 0000000..2b880cd --- /dev/null +++ b/src/main/java/net/viper/money/InterestHandler.java @@ -0,0 +1,97 @@ +package net.craftersland.money; + +import java.util.ArrayList; +import java.util.List; + +import org.bukkit.Bukkit; +import org.bukkit.entity.Player; +import org.bukkit.scheduler.BukkitTask; + +public class InterestHandler { + + private Money pl; + private int taskID = -1; + private long lastInterestTime; + + public InterestHandler(Money pl) { + this.pl = pl; + interestTask(); + } + + public String getNextInterestTime() { + String timeString = "Error"; + long timeLeftMills = System.currentTimeMillis() - lastInterestTime; + long timecountSecPass = timeLeftMills / 1000; + long timecountSec = pl.getConfigurationHandler().getInteger("general.interest.interestTime") * 60 - timecountSecPass; + int days = 0; + int hours = 0; + int minutes = 0; + int seconds = 0; + if (timecountSec >= 86400) { //It's atleast one day + days = (int) (timecountSec / 86400); + timecountSec = timecountSec % 86400; + } + if (timecountSec >= 3600) { + hours = (int) (timecountSec / 3600); + timecountSec = timecountSec % 3600; + } + if (timecountSec >= 60) { + minutes = (int) (timecountSec / 60); + timecountSec = timecountSec % 60; + } + if (timecountSec > 0) { + seconds = (int) timecountSec; + } + + if (days != 0) { + timeString = days + " days " + hours + " hours " + minutes + " min " + seconds + " sec"; + } else if (days == 0 && hours == 0 && minutes == 0) { + timeString = seconds + " sec"; + } else if (days == 0 && hours == 0) { + timeString = minutes + " min " + seconds + " sec"; + } else if (days == 0) { + timeString = hours + " hours " + minutes + " min " + seconds + " sec"; + } + return timeString; + } + + public void resetTask() { + if (taskID != -1) { + Bukkit.getScheduler().cancelTask(taskID); + } + interestTask(); + } + + //Interest task + private void interestTask() { + if (pl.getConfigurationHandler().getBoolean("general.interest.enabled") == true) { + Money.log.info("Interest task started. Iterest will be given every " + pl.getConfigurationHandler().getInteger("general.interest.interestTime") + " minutes."); + BukkitTask task = Bukkit.getScheduler().runTaskTimerAsynchronously(pl, new Runnable() { + + public void run() { + lastInterestTime = System.currentTimeMillis(); + List onlinePlayers = new ArrayList(Bukkit.getOnlinePlayers()); + if (onlinePlayers.isEmpty() == false) { + for (Player p : onlinePlayers) { + Double intPercentage = Double.parseDouble(pl.getConfigurationHandler().getString("general.interest.percentageAmount").replace("%", "")); + Double balance = pl.getMoneyDatabaseInterface().getBalance(p); + + if (balance < pl.getConfigurationHandler().getInteger("general.maxBankLimitMoney")) { + Double interest = (balance / 100) * intPercentage; + + pl.getMoneyDatabaseInterface().setBalance(p, balance + interest); + pl.getConfigurationHandler().printMessage(p, "chatMessages.interest", interest.toString(), p, p.getName()); + } + } + onlinePlayers.clear(); + } + } + + }, 20L, pl.getConfigurationHandler().getInteger("general.interest.interestTime") * (60 * 20L)); + taskID = task.getTaskId(); + } else { + Money.log.info("Interest task is disabled."); + } + } + +} diff --git a/src/main/java/net/viper/money/Money.java b/src/main/java/net/viper/money/Money.java new file mode 100644 index 0000000..ea6db1a --- /dev/null +++ b/src/main/java/net/viper/money/Money.java @@ -0,0 +1,209 @@ +package net.craftersland.money; + +import java.io.File; +import java.util.HashSet; +import java.util.Set; +import java.util.UUID; +import java.util.logging.Logger; + +import net.craftersland.money.commands.*; +import net.craftersland.money.database.*; +import net.milkbowl.vault.economy.Economy; +import net.milkbowl.vault.permission.Permission; + +import org.bukkit.Bukkit; +import org.bukkit.event.HandlerList; +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 Money instance; + public static Economy econ = null; + public static Permission perms = null; + public boolean is14Server = true; + public boolean is19Server = true; + public boolean is13Server = false; + public Set cooldown = new HashSet<>(); + + private static ConfigHandler cH; + private DatabaseManagerInterface databaseManager; + private AccountDatabaseInterface moneyDatabaseInterface; + private boolean enabled = false; + private static SoundHandler sH; + private static ReloadCmd rCmd; + private static BalanceCmd bCmd; + private static SetCmd sCmd; + private static DepositCmd dCmd; + private static WithdrawCmd wCmd; + private static InterestHandler iH; + private static InterestCmd iCmd; + + @Override + public void onEnable() { + log = getLogger(); + instance = this; + getMcVersion(); + + // Setup Vault for economy and permissions + if (!setupEconomy()) { + log.severe("Warning! Vault installed? If yes Economy system installed?"); + getServer().getPluginManager().disablePlugin(this); + return; + } + setupPermissions(); + + // Load Configuration + cH = new ConfigHandler(this); + sH = new SoundHandler(this); + + // Setup Database + if (cH.getString("database.typeOfDatabase").equalsIgnoreCase("mysql")) { + log.info("Using MySQL as Datasource..."); + databaseManager = new DatabaseManagerMysql(this); + moneyDatabaseInterface = new MoneyMysqlInterface(this); + } else { + // FlatFile + File accountsFolder = new File("plugins" + File.separator + "MysqlEconomyBank" + File.separator + "Accounts"); + if (!accountsFolder.exists()) { + accountsFolder.mkdirs(); + } + log.info(instance.getDescription().getName() + " loaded successfully!"); + databaseManager = new DatabaseManagerFlatFile(this); + moneyDatabaseInterface = new MoneyFlatFileInterface(this); + } + + // Init Commands and Interest + rCmd = new ReloadCmd(this); + bCmd = new BalanceCmd(this); + sCmd = new SetCmd(this); + dCmd = new DepositCmd(this); + wCmd = new WithdrawCmd(this); + iH = new InterestHandler(this); + iCmd = new InterestCmd(this); + + // Register Events and Commands + PluginManager pm = getServer().getPluginManager(); + pm.registerEvents(new PlayerListener(this), this); + + CommandHandler cH = new CommandHandler(this); + getCommand("meb").setExecutor(cH); + getCommand("bank").setExecutor(cH); + + // PlaceholderAPI support + if (isPlaceHoldersAPIinstalled()) { + new Placeholders(this).register(); + log.info("PlaceholdersAPI detected and placeholders activated!"); + } + + enabled = true; + log.info(instance.getDescription().getName() + " has been successfully loaded!"); + } + + @Override + public void onDisable() { + if (enabled) { + Bukkit.getScheduler().cancelTasks(this); + HandlerList.unregisterAll(this); + if (databaseManager.getConnection() != null) { + log.info("Closing MySQL connection..."); + databaseManager.closeDatabase(); + } + } + log.info(instance.getDescription().getName() + " is disabled!"); + } + + private boolean getMcVersion() { + String[] serverVersion = Bukkit.getBukkitVersion().split("-"); + String version = serverVersion[0]; + + if (version.matches("1\\.7\\.10|1\\.7\\.9|1\\.7\\.5|1\\.7\\.2|1\\.8\\.8|1\\.8\\.7|1\\.8\\.3|1\\.8\\.4|1\\.8")) { + is19Server = false; + is13Server = false; + is14Server = false; + return true; + } else if (version.matches("1\\.9(\\.\\d+)?")) { + is19Server = true; + is13Server = false; + is14Server = false; + return true; + } else if (version.matches("1\\.1[0-2](\\.\\d+)?")) { + is19Server = true; + is13Server = false; + is14Server = false; + return true; + } else if (version.matches("1\\.13(\\.\\d+)?")) { + is19Server = true; + is13Server = true; + is14Server = false; + return true; + } else if (version.matches("1\\.14(\\.\\d+)?|1\\.15(\\.\\d+)?|1\\.16(\\.\\d+)?")) { + is19Server = true; + is13Server = true; + is14Server = true; + return true; + } else { + is19Server = true; + is13Server = true; + is14Server = true; + } + return false; + } + + 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(); + log.info("Using economy system: " + rsp.getProvider().getName()); + return econ != null; + } + + private boolean setupPermissions() { + RegisteredServiceProvider rsp = getServer().getServicesManager().getRegistration(Permission.class); + perms = rsp.getProvider(); + log.info("Using permission system: " + rsp.getProvider().getName()); + return perms != null; + } + + public boolean isPlaceHoldersAPIinstalled() { + return getServer().getPluginManager().getPlugin("PlaceholderAPI") != null; + } + + // Getter + public AccountDatabaseInterface getMoneyDatabaseInterface() { + return moneyDatabaseInterface; + } + public ConfigHandler getConfigurationHandler() { + return cH; + } + public DatabaseManagerInterface getDatabaseManagerInterface() { + return databaseManager; + } + public SoundHandler getSoundHandler() { + return sH; + } + public ReloadCmd getReloadCmd() { + return rCmd; + } + public BalanceCmd getBalanceCmd() { + return bCmd; + } + public SetCmd getSetCmd() { + return sCmd; + } + public DepositCmd getDepositCmd() { + return dCmd; + } + public WithdrawCmd getWithdrawCmd() { + return wCmd; + } + public InterestHandler getInterestHandler() { + return iH; + } + public InterestCmd getInterestCmd() { + return iCmd; + } +} diff --git a/src/main/java/net/viper/money/Placeholders.java b/src/main/java/net/viper/money/Placeholders.java new file mode 100644 index 0000000..d69dbd7 --- /dev/null +++ b/src/main/java/net/viper/money/Placeholders.java @@ -0,0 +1,88 @@ +package net.craftersland.money; + +import java.text.DecimalFormat; + +import org.bukkit.entity.Player; + +import me.clip.placeholderapi.expansion.PlaceholderExpansion; + +public class Placeholders extends PlaceholderExpansion { + + Money pl; + + public Placeholders(Money plugin) { + this.pl = plugin; + } + + @Override + public boolean persist(){ + return true; + } + + @Override + public boolean canRegister(){ + return true; + } + + @Override + public String getAuthor(){ + return pl.getDescription().getAuthors().toString(); + } + + /** + * The placeholder identifier should go here. + *
This is what tells PlaceholderAPI to call our onRequest + * method to obtain a value if a placeholder starts with our + * identifier. + *
This must be unique and can not contain % or _ + * + * @return The identifier in {@code %_%} as String. + */ + @Override + public String getIdentifier(){ + return "economybank"; + } + + @Override + public String getVersion(){ + return pl.getDescription().getVersion(); + } + + /** + * This is the method called when a placeholder with our identifier + * is found and needs a value. + *
We specify the value identifier in this method. + *
Since version 2.9.1 can you use OfflinePlayers in your requests. + * + * @param player + * A {@link org.bukkit.Player Player}. + * @param identifier + * A String containing the identifier/value. + * + * @return possibly-null String of the requested identifier. + */ + @Override + public String onPlaceholderRequest(Player player, String identifier){ + if (player == null) { + return "Not online!"; + } + + // %someplugin_placeholder1% + if (identifier.equals("balance")) { + Double bal = pl.getMoneyDatabaseInterface().getBalance(player); + DecimalFormat f = new DecimalFormat("#,##0.00"); + + if (bal.toString().endsWith(".0")) { + DecimalFormat fr = new DecimalFormat("#,##0"); + return fr.format(bal); + } else { + return f.format(bal); + } + } + + // We return null if an invalid placeholder (f.e. %someplugin_placeholder3%) + // was provided + return null; + } + +} diff --git a/src/main/java/net/viper/money/PlayerListener.java b/src/main/java/net/viper/money/PlayerListener.java new file mode 100644 index 0000000..7c40921 --- /dev/null +++ b/src/main/java/net/viper/money/PlayerListener.java @@ -0,0 +1,276 @@ +package net.craftersland.money; + +import java.text.DecimalFormat; +import java.util.HashSet; +import java.util.Set; +import java.util.UUID; + +import net.milkbowl.vault.economy.Economy; + +import org.bukkit.Bukkit; +import org.bukkit.ChatColor; +import org.bukkit.Effect; +import org.bukkit.Material; +import org.bukkit.block.Block; +import org.bukkit.block.Sign; +import org.bukkit.entity.Player; +import org.bukkit.event.EventHandler; +import org.bukkit.event.Listener; +import org.bukkit.event.block.BlockBreakEvent; +import org.bukkit.event.block.SignChangeEvent; +import org.bukkit.event.player.PlayerInteractEvent; + +public class PlayerListener implements Listener { + + private Money money; + public static Economy econ = null; + private Set safety = new HashSet(); + + public PlayerListener(Money money) { + this.money = money; + } + + private boolean isEventSafe(final UUID pU) { + if (safety.contains(pU)) { + return false; + } + safety.add(pU); + Bukkit.getScheduler().runTaskLaterAsynchronously(money, new Runnable() { + @Override + public void run() { + safety.remove(pU); + } + }, 2L); + return true; + } + + private boolean isSign(Material sourceMaterial) { + if (money.is14Server == false && sourceMaterial.equals(Material.valueOf("WALL_SIGN")) || money.is13Server == false && sourceMaterial.equals(Material.valueOf("SIGN_POST")) || money.is13Server == true && money.is14Server == false && sourceMaterial.equals(Material.valueOf("SIGN"))) { + return true; + } else if (money.is14Server) { + if (sourceMaterial == Material.ACACIA_SIGN || sourceMaterial == Material.ACACIA_WALL_SIGN) { + return true; + } else if (sourceMaterial == Material.BIRCH_SIGN || sourceMaterial == Material.BIRCH_WALL_SIGN) { + return true; + } else if (sourceMaterial == Material.DARK_OAK_SIGN || sourceMaterial == Material.DARK_OAK_WALL_SIGN) { + return true; + } else if (sourceMaterial == Material.JUNGLE_SIGN || sourceMaterial == Material.JUNGLE_WALL_SIGN) { + return true; + } else if (sourceMaterial == Material.OAK_SIGN || sourceMaterial == Material.OAK_WALL_SIGN) { + return true; + } else if (sourceMaterial == Material.SPRUCE_SIGN || sourceMaterial == Material.SPRUCE_WALL_SIGN) { + return true; + } + } + return false; + } + + @EventHandler + public void onClick(final PlayerInteractEvent event) { + final Player p = event.getPlayer(); + + if (event.getClickedBlock() != null) { + if (isSign(event.getClickedBlock().getType())) { + if (isEventSafe(event.getPlayer().getUniqueId()) == true) { + final Sign sign = (Sign) event.getClickedBlock().getState(); + Bukkit.getScheduler().runTaskAsynchronously(money, new Runnable() { + @Override + public void run() { + if (sign.getLine(0).contains(convertColorCode(money.getConfigurationHandler().getString("signFormat.signColor")) + ChatColor.BOLD + "[Bank]")) { + if (Money.perms.has(p, "MysqlEconomyBank.use")) { + if (p.isSneaking()) { + money.getConfigurationHandler().printMessage(p, "chatMessages.denyIfSneaking", "0", p, p.getName()); + money.getSoundHandler().sendPlingSound(p); + return; + } + if ((sign.getLine(1).equals(money.getConfigurationHandler().getString("signFormat.balance")))) { + if (money.cooldown.contains(p.getUniqueId())) { + money.getConfigurationHandler().printMessage(p, "chatMessages.tooFastInteraction", "0", p, p.getName()); + money.getSoundHandler().sendPlingSound(p); + return; + } else { + money.cooldown.add(p.getUniqueId()); + Double delayCalc = 20.00 / 1000.00 * Double.parseDouble(money.getConfigurationHandler().getString("general.timeBetweenTwoInteractions")); + int delay = delayCalc.intValue(); + Bukkit.getServer().getScheduler().runTaskLaterAsynchronously(money, new Runnable() { + public void run() { + money.cooldown.remove(p.getUniqueId()); + } + }, delay); + } + money.getMoneyDatabaseInterface().getBalance(p); + money.getConfigurationHandler().printMessage(p, "chatMessages.balance", "0", p, p.getName()); + money.getSoundHandler().sendClickSound(p); + return; + } + if ((sign.getLine(1).equals(money.getConfigurationHandler().getString("signFormat.deposit")))) { + if (money.cooldown.contains(p.getUniqueId())) { + money.getConfigurationHandler().printMessage(p, "chatMessages.tooFastInteraction", "0", p, p.getName()); + money.getSoundHandler().sendPlingSound(p); + return; + } else { + money.cooldown.add(p.getUniqueId()); + Double delayCalc = 20.00 / 1000.00 * Double.parseDouble(money.getConfigurationHandler().getString("general.timeBetweenTwoInteractions")); + int delay = delayCalc.intValue(); + Bukkit.getServer().getScheduler().runTaskLaterAsynchronously(money, new Runnable() { + public void run() { + money.cooldown.remove(p.getUniqueId()); + } + }, delay); + } + final Double amount = Double.parseDouble(sign.getLine(2)); + if (Money.econ.getBalance(p) >= amount) { + Double bankBalance = money.getMoneyDatabaseInterface().getBalance(p); + if (bankBalance + amount > Double.parseDouble(money.getConfigurationHandler().getString("general.maxBankLimitMoney"))) { + money.getConfigurationHandler().printMessage(p, "chatMessages.reachedMaximumMoneyInAccount", amount + "", p, p.getName()); + money.getSoundHandler().sendPlingSound(p); + return; + } + Bukkit.getScheduler().runTask(money, new Runnable() { + @Override + public void run() { + Money.econ.withdrawPlayer(p, amount); + } + }); + money.getMoneyDatabaseInterface().setBalance(p, bankBalance + amount); + money.getConfigurationHandler().printMessage(p, "chatMessages.depositedSuccessfully", amount + "", p, p.getName()); + money.getSoundHandler().sendClickSound(p); + return; + } + money.getConfigurationHandler().printMessage(p, "chatMessages.notEnoughMoneyInPoket", amount + "", p, p.getName()); + money.getSoundHandler().sendPlingSound(p); + return; + } + if ((sign.getLine(1).equals(money.getConfigurationHandler().getString("signFormat.withdraw")))) { + if (money.cooldown.contains(p.getUniqueId())) { + money.getConfigurationHandler().printMessage(p, "chatMessages.tooFastInteraction", "0", p, p.getName()); + money.getSoundHandler().sendPlingSound(p); + return; + } else { + money.cooldown.add(p.getUniqueId()); + Double delayCalc = 20.00 / 1000.00 * Double.parseDouble(money.getConfigurationHandler().getString("general.timeBetweenTwoInteractions")); + int delay = delayCalc.intValue(); + Bukkit.getServer().getScheduler().runTaskLaterAsynchronously(money, new Runnable() { + public void run() { + money.cooldown.remove(p.getUniqueId()); + } + }, delay); + } + Double bankBalance = money.getMoneyDatabaseInterface().getBalance(p); + final Double amount = Double.parseDouble(sign.getLine(2)); + if (bankBalance >= amount) { + if (Money.econ.getBalance(p) + amount > Double.parseDouble(money.getConfigurationHandler().getString("general.maxPocketLimitMoney"))) { + money.getConfigurationHandler().printMessage(p, "chatMessages.reachedMaximumMoneyInPocket", amount + "", p, p.getName()); + money.getSoundHandler().sendPlingSound(p); + return; + } + money.getMoneyDatabaseInterface().setBalance(p, bankBalance - amount); + Bukkit.getScheduler().runTask(money, new Runnable() { + @Override + public void run() { + Money.econ.depositPlayer(p, amount); + } + }); + money.getConfigurationHandler().printMessage(p, "chatMessages.withdrewSuccessfully", amount + "", p, p.getName()); + money.getSoundHandler().sendClickSound(p); + return; + } + money.getConfigurationHandler().printMessage(p, "chatMessages.notEnoughMoneyInAccount", amount + "", p, p.getName()); + money.getSoundHandler().sendPlingSound(p); + return; + } + } else { + money.getConfigurationHandler().printMessage(p, "chatMessages.permissionDeny", "0", p, p.getName()); + money.getSoundHandler().sendPlingSound(p); + } + } + } + }); + } + } + } + } + + @EventHandler + public void onSignChange(SignChangeEvent event) { + Player p = event.getPlayer(); + Block block = event.getBlock(); + Sign sign = (Sign)block.getState(); + if (event.getLine(0).equalsIgnoreCase("[Bank]")) { + if (p.hasPermission("MysqlEconomyBank.create")) { + String rawColor = money.getConfigurationHandler().getString("signFormat.signColor"); + String colorCode = convertColorCode(rawColor); + + event.setLine(0, colorCode + ChatColor.BOLD + "[Bank]"); + + if (event.getLine(1).equalsIgnoreCase("deposit")) { + event.setLine(1, money.getConfigurationHandler().getString("signFormat.deposit")); + if (event.getLine(2).isEmpty()) { + event.setLine(2, "0"); + } + } else if (event.getLine(1).equalsIgnoreCase("withdraw")) { + event.setLine(1, money.getConfigurationHandler().getString("signFormat.withdraw")); + if (event.getLine(2).isEmpty()) { + event.setLine(2, "0"); + } + } else if (event.getLine(1).equalsIgnoreCase("balance")) { + event.setLine(1, money.getConfigurationHandler().getString("signFormat.balance")); + event.setLine(2, ""); + } else { + money.getConfigurationHandler().printMessage(p, "chatMessages.invalidSignType", "0", p, p.getName()); + event.setCancelled(true); + return; + } + money.getConfigurationHandler().printMessage(p, "chatMessages.signCreatedSuccessfully", "0", p, p.getName()); + money.getSoundHandler().sendClickSound(p); + } else { + money.getConfigurationHandler().printMessage(p, "chatMessages.permissionDeny", "0", p, p.getName()); + money.getSoundHandler().sendPlingSound(p); + event.setCancelled(true); + } + } + } + + @EventHandler + public void onBlockBreak(BlockBreakEvent event) { + Player p = event.getPlayer(); + Block block = event.getBlock(); + if (isSign(block.getType())) { + Sign sign = (Sign)block.getState(); + if (sign.getLine(0).equalsIgnoreCase(convertColorCode(money.getConfigurationHandler().getString("signFormat.signColor")) + ChatColor.BOLD + "[Bank]")) { + if (!p.hasPermission("MysqlEconomyBank.break")) { + money.getConfigurationHandler().printMessage(p, "chatMessages.permissionDeny", "0", p, p.getName()); + money.getSoundHandler().sendPlingSound(p); + event.setCancelled(true); + return; + } else { + money.getConfigurationHandler().printMessage(p, "chatMessages.signDestroyedSuccessfully", "0", p, p.getName()); + money.getSoundHandler().sendClickSound(p); + } + } + } + } + + @EventHandler + public void onSignEdit(PlayerInteractEvent event) { + if (event.getClickedBlock() != null && isSign(event.getClickedBlock().getType())) { + Sign sign = (Sign) event.getClickedBlock().getState(); + if (sign.getLine(0).equalsIgnoreCase(convertColorCode(money.getConfigurationHandler().getString("signFormat.signColor")) + ChatColor.BOLD + "[Bank]")) { + event.setCancelled(true); + } + } + } + + private String convertColorCode(String rawColor) { + if (rawColor == null || rawColor.isEmpty()) { + return ""; + } + if (rawColor.startsWith("&")) { + return rawColor.replace('&', '§'); + } else if (rawColor.startsWith("§")) { + return rawColor; + } else { + return "§" + rawColor; + } + } +} diff --git a/src/main/java/net/viper/money/SoundHandler.java b/src/main/java/net/viper/money/SoundHandler.java new file mode 100644 index 0000000..08154ff --- /dev/null +++ b/src/main/java/net/viper/money/SoundHandler.java @@ -0,0 +1,48 @@ +package net.craftersland.money; + +import org.bukkit.Sound; +import org.bukkit.entity.Player; + +public class SoundHandler { + + private Money plugin; + + public SoundHandler(Money m) { + this.plugin = m; + } + + public void sendItemBreakSound(Player p) { + if (plugin.is19Server == true) { + p.playSound(p.getLocation(), Sound.ENTITY_ITEM_BREAK, 1.0F, 1.0F); + } else { + p.playSound(p.getLocation(), Sound.valueOf("ITEM_BREAK"), 1.0F, 1.0F); + } + } + + public void sendClickSound(Player p) { + if (plugin.is19Server == true) { + p.playSound(p.getLocation(), Sound.UI_BUTTON_CLICK, 1.0F, 1.0F); + } else { + p.playSound(p.getLocation(), Sound.valueOf("CLICK"), 1.0F, 1.0F); + } + } + + public void sendLevelUpSound(Player p) { + if (plugin.is19Server == true) { + p.playSound(p.getLocation(), Sound.ENTITY_PLAYER_LEVELUP, 1.0F, 1.0F); + } else { + p.playSound(p.getLocation(), Sound.valueOf("LEVEL_UP"), 1.0F, 1.0F); + } + } + + public void sendPlingSound(Player p) { + if (plugin.is13Server) { + p.playSound(p.getLocation(), Sound.BLOCK_NOTE_BLOCK_PLING, 3.0F, 3.0F); + } else if (plugin.is19Server == true) { + p.playSound(p.getLocation(), Sound.valueOf("BLOCK_NOTE_PLING"), 3.0F, 3.0F); + } else { + p.playSound(p.getLocation(), Sound.valueOf("NOTE_PLING"), 3.0F, 3.0F); + } + } + +}