From 908149c5ec7d42d356f8e3c4fdd9a03798a7111a Mon Sep 17 00:00:00 2001 From: WildInterloper <156627888+WildInterloper@users.noreply.github.com> Date: Sat, 23 Mar 2024 03:33:38 -0400 Subject: [PATCH] 1.1.1 - Added a sell multiplier feature! --- pom.xml | 2 +- .../AutoSell/EconomyHandler.java | 21 +++++ .../AutoSell/MultiplierManager.java | 87 +++++++++++++++++++ .../AutoSell/SellManager.java | 40 ++++++++- .../nvus/nvus_prison_setup/PrisonSetup.java | 17 +++- src/main/resources/config.yml | 5 ++ src/main/resources/plugin.yml | 3 + 7 files changed, 169 insertions(+), 6 deletions(-) create mode 100644 src/main/java/me/nvus/nvus_prison_setup/AutoSell/EconomyHandler.java create mode 100644 src/main/java/me/nvus/nvus_prison_setup/AutoSell/MultiplierManager.java diff --git a/pom.xml b/pom.xml index 97154b9..2bd6274 100644 --- a/pom.xml +++ b/pom.xml @@ -6,7 +6,7 @@ me.NVus NVus_Prison - 1.1.0 + 1.1.1 jar NVus_PrisonSetup diff --git a/src/main/java/me/nvus/nvus_prison_setup/AutoSell/EconomyHandler.java b/src/main/java/me/nvus/nvus_prison_setup/AutoSell/EconomyHandler.java new file mode 100644 index 0000000..451d03d --- /dev/null +++ b/src/main/java/me/nvus/nvus_prison_setup/AutoSell/EconomyHandler.java @@ -0,0 +1,21 @@ +package me.nvus.nvus_prison_setup.AutoSell; + +import net.milkbowl.vault.economy.Economy; +import org.bukkit.entity.Player; + +public class EconomyHandler { + + private Economy economy; + private MultiplierManager multiplierManager; + + public EconomyHandler(Economy economy, MultiplierManager multiplierManager) { + this.economy = economy; + this.multiplierManager = multiplierManager; + } + + public void giveMoney(Player player, double amount) { + double multiplier = multiplierManager.getPlayerMultiplier(player.getUniqueId()); + economy.depositPlayer(player, amount * multiplier); + } +} + diff --git a/src/main/java/me/nvus/nvus_prison_setup/AutoSell/MultiplierManager.java b/src/main/java/me/nvus/nvus_prison_setup/AutoSell/MultiplierManager.java new file mode 100644 index 0000000..552f4aa --- /dev/null +++ b/src/main/java/me/nvus/nvus_prison_setup/AutoSell/MultiplierManager.java @@ -0,0 +1,87 @@ +package me.nvus.nvus_prison_setup.AutoSell; +import me.nvus.nvus_prison_setup.PrisonSetup; +import org.bukkit.Bukkit; +import org.bukkit.command.Command; +import org.bukkit.command.CommandExecutor; +import org.bukkit.command.CommandSender; +import org.bukkit.entity.Player; +import org.bukkit.scheduler.BukkitRunnable; +import org.bukkit.scheduler.BukkitTask; + +import java.util.HashMap; +import java.util.Map; +import java.util.UUID; + +public class MultiplierManager implements CommandExecutor { + + private final PrisonSetup plugin; + private final Map multipliers = new HashMap<>(); + + // Store the scheduled tasks for removing multipliers + private final Map removalTasks = new HashMap<>(); + + public MultiplierManager(PrisonSetup plugin) { + this.plugin = plugin; // Store the passed plugin instance for later use + } + + @Override + public boolean onCommand(CommandSender sender, Command command, String label, String[] args) { + if (!sender.hasPermission("multiplier.use")) { + sender.sendMessage("You don't have permission to use this command."); + return true; + } + + if (args.length != 3) { + sender.sendMessage("Usage: /multiplier "); + return true; + } + + Player target = Bukkit.getPlayer(args[0]); + if (target == null) { + sender.sendMessage("Player not found."); + return true; + } + + try { + double multiplier = Double.parseDouble(args[1]); + int duration = Integer.parseInt(args[2].replace("m", "")); + applyMultiplier(target, multiplier, duration); + sender.sendMessage("Applied a " + multiplier + "x multiplier to " + target.getName() + " for " + duration + " minutes."); + } catch (NumberFormatException e) { + sender.sendMessage("Invalid number format."); + } + + return true; + } + + private void applyMultiplier(Player player, double multiplier, int duration) { + UUID playerId = player.getUniqueId(); + // Cancel any existing removal task + if (removalTasks.containsKey(playerId)) { + removalTasks.get(playerId).cancel(); + removalTasks.remove(playerId); + } + + // Apply the new multiplier + multipliers.put(playerId, multiplier); + + // Schedule a new removal task + BukkitTask task = new BukkitRunnable() { + @Override + public void run() { + multipliers.remove(playerId); + removalTasks.remove(playerId); + player.sendMessage("Your selling multiplier has expired."); + } + }.runTaskLater(plugin, duration * 60 * 20); // Convert minutes to ticks + + // Store the task for potential cancellation + removalTasks.put(playerId, task); + + player.sendMessage("Applied a " + multiplier + "x multiplier to you for " + duration + " minutes."); + } + + public double getPlayerMultiplier(UUID playerUuid) { + return multipliers.getOrDefault(playerUuid, 1.0); // Default to 1.0, meaning no multiplier + } +} diff --git a/src/main/java/me/nvus/nvus_prison_setup/AutoSell/SellManager.java b/src/main/java/me/nvus/nvus_prison_setup/AutoSell/SellManager.java index c59aacb..4e93375 100644 --- a/src/main/java/me/nvus/nvus_prison_setup/AutoSell/SellManager.java +++ b/src/main/java/me/nvus/nvus_prison_setup/AutoSell/SellManager.java @@ -2,6 +2,7 @@ package me.nvus.nvus_prison_setup.AutoSell; import me.nvus.nvus_prison_setup.Configs.ConfigManager; import me.nvus.nvus_prison_setup.PrisonSetup; +import me.nvus.nvus_prison_setup.AutoSell.MultiplierManager; import net.milkbowl.vault.economy.Economy; import org.bukkit.ChatColor; import org.bukkit.Material; @@ -18,24 +19,46 @@ import java.io.IOException; import java.util.HashMap; import java.util.Map; import java.util.UUID; +import java.util.concurrent.ConcurrentHashMap; +import java.time.LocalDateTime; + public class SellManager implements CommandExecutor { private final HashMap autoSellStatus = new HashMap<>(); private final HashMap prices = new HashMap<>(); - private final ConfigManager configManager; - public SellManager(ConfigManager configManager) { + private final Map playerMultipliers = new ConcurrentHashMap<>(); + private final ConfigManager configManager; + private final MultiplierManager multiplierManager; + + public SellManager(ConfigManager configManager, MultiplierManager multiplierManager) { this.configManager = configManager; + this.multiplierManager = multiplierManager; loadPrices(); } + private static class MultiplierInfo { + double multiplier; + LocalDateTime expiryTime; + + MultiplierInfo(double multiplier, int durationMinutes) { + this.multiplier = multiplier; + this.expiryTime = LocalDateTime.now().plusMinutes(durationMinutes); + } + + boolean isExpired() { + return LocalDateTime.now().isAfter(expiryTime); + } + } + public boolean onCommand(CommandSender sender, Command command, String label, String[] args) { if (!(sender instanceof Player)) { sender.sendMessage("Only players can use this command."); return true; } + Player player = (Player) sender; // Admin Commands: @@ -170,11 +193,20 @@ public class SellManager implements CommandExecutor { private void giveMoney(Player player, Material material, int quantity, double amount) { Economy economy = PrisonSetup.getEconomy(); - economy.depositPlayer(player, amount); + double multiplier = multiplierManager.getPlayerMultiplier(player.getUniqueId()); + double finalAmount = amount * multiplier; // Apply the multiplier + economy.depositPlayer(player, finalAmount); player.sendMessage(ChatColor.translateAlternateColorCodes('&', - String.format("&a&lSold &d%dx &3%s &a&lfor $%.2f", quantity, material.toString().toLowerCase().replaceAll("_", " "), amount))); + String.format("&a&lSold &d%dx &3%s &a&lfor $%.2f", quantity, material.toString().toLowerCase().replaceAll("_", " "), finalAmount))); } +// private void giveMoney(Player player, Material material, int quantity, double amount) { +// Economy economy = PrisonSetup.getEconomy(); +// economy.depositPlayer(player, amount); +// player.sendMessage(ChatColor.translateAlternateColorCodes('&', +// String.format("&a&lSold &d%dx &3%s &a&lfor $%.2f", quantity, material.toString().toLowerCase().replaceAll("_", " "), amount))); +// } + // public void sellItems(Player player) { diff --git a/src/main/java/me/nvus/nvus_prison_setup/PrisonSetup.java b/src/main/java/me/nvus/nvus_prison_setup/PrisonSetup.java index 0999e7a..ee14aec 100644 --- a/src/main/java/me/nvus/nvus_prison_setup/PrisonSetup.java +++ b/src/main/java/me/nvus/nvus_prison_setup/PrisonSetup.java @@ -1,5 +1,6 @@ package me.nvus.nvus_prison_setup; +import me.nvus.nvus_prison_setup.AutoSell.MultiplierManager; import me.nvus.nvus_prison_setup.Configs.ConfigManager; import me.nvus.nvus_prison_setup.Kit.KitManager; import me.nvus.nvus_prison_setup.Configs.SettingsMenu; @@ -48,7 +49,9 @@ import java.sql.Statement; public final class PrisonSetup extends JavaPlugin { + private static PrisonSetup instance; private ConfigManager configManager; + private MultiplierManager multiplierManager; private DatabaseManager dbManager; private GangManager gangManager; @@ -121,9 +124,17 @@ public final class PrisonSetup extends JavaPlugin { // Register the Auto Sell and Sell All Listeners boolean autoSellEnabled = configManager.getConfig("config.yml").getBoolean("AutoSell", true); boolean sellAllEnabled = configManager.getConfig("config.yml").getBoolean("SellAll", true); - SellManager sellManager = new SellManager(configManager); + boolean sellMultiplierEnabled = configManager.getConfig("config.yml").getBoolean("SellMultiplier", true); + + MultiplierManager multiplierManager = new MultiplierManager(this); + SellManager sellManager = new SellManager(configManager,multiplierManager); + this.getCommand("multiplier").setExecutor(multiplierManager); this.getCommand("setprice").setExecutor(sellManager); +// if (sellMultiplierEnabled) { +// this.getCommand("multiplier").setExecutor(new MultiplierManager(this)); +// } + // If they are true, register the commands. if (autoSellEnabled) { // Register the autosell command. @@ -264,4 +275,8 @@ public final class PrisonSetup extends JavaPlugin { public GangManager getGangManager() { return gangManager; } + + public static PrisonSetup getInstance() { + return instance; + } } diff --git a/src/main/resources/config.yml b/src/main/resources/config.yml index dc401ca..3ae6ddc 100644 --- a/src/main/resources/config.yml +++ b/src/main/resources/config.yml @@ -33,6 +33,11 @@ SellAll: true # You can see material prices in the item_pricing.yml file or in-game using /sellprice with an item in your hand. # Only players/prisoners with the permission nvus.prisoner can use the /sellprice command and autosell toggle. +# Should we allow the use of multipliers for selling items? ie 1.5x, 2x etc. +# These are admin commands that can be used by admins or other plugins like InfiniteVouchers etc. to give players a selling +# multiplier for a set amount of time. | /multiplier 1.5 15m +SellMultiplier: True + ######################################################################################## # ARMOR SETTINGS # ######################################################################################## diff --git a/src/main/resources/plugin.yml b/src/main/resources/plugin.yml index 73bb583..cbd1832 100644 --- a/src/main/resources/plugin.yml +++ b/src/main/resources/plugin.yml @@ -59,6 +59,9 @@ commands: usage: | /autosell - Toggle auto selling all eligible items in your inventory. aliases: [ automaticsell ] + multiplier: + description: Apply a selling multiplier to a player. + usage: /multiplier setprice: description: Set the price of the block being held in item_prices.yml. Set the price to 0 to remove it from the list! usage: |