diff --git a/src/main/java/fr/communaywen/core/AywenCraftPlugin.java b/src/main/java/fr/communaywen/core/AywenCraftPlugin.java index 0abe38e3..5c96c6e6 100644 --- a/src/main/java/fr/communaywen/core/AywenCraftPlugin.java +++ b/src/main/java/fr/communaywen/core/AywenCraftPlugin.java @@ -1,16 +1,13 @@ package fr.communaywen.core; import dev.xernas.menulib.MenuLib; -import fr.communaywen.core.commands.RulesCommand; -import fr.communaywen.core.commands.TeamCommand; +import fr.communaywen.core.commands.*; +import fr.communaywen.core.economy.EconomyManager; import fr.communaywen.core.listeners.ChatListener; import fr.communaywen.core.teams.TeamManager; -import fr.communaywen.core.commands.ProutCommand; import fr.communaywen.core.utils.DiscordWebhook; import fr.communaywen.core.utils.MOTDChanger; -import fr.communaywen.core.commands.VersionCommand; import fr.communaywen.core.utils.PermissionCategory; -import fr.communaywen.core.commands.RTPCommand; import org.bukkit.command.PluginCommand; import org.bukkit.configuration.file.FileConfiguration; import org.bukkit.configuration.file.YamlConfiguration; @@ -26,6 +23,7 @@ public final class AywenCraftPlugin extends JavaPlugin { private TeamManager teamManager; private FileConfiguration bookConfig; private static AywenCraftPlugin instance; + private EconomyManager economyManager; private void loadBookConfig() { File bookFile = new File(getDataFolder(), "rules.yml"); @@ -70,10 +68,15 @@ public void onEnable() { this.getCommand("rtp").setExecutor(new RTPCommand(this)); getServer().getPluginManager().registerEvents(new AntiTrampling(),this); saveDefaultConfig(); + + // Initialiser EconomyManager et enregistrer la commande money + economyManager = new EconomyManager(getDataFolder()); + this.getCommand("money").setExecutor(new MoneyCommand(economyManager)); } @Override public void onDisable() { + // Logic to save data if necessary } public TeamManager getTeamManager() { @@ -83,6 +86,7 @@ public TeamManager getTeamManager() { public static AywenCraftPlugin getInstance() { return instance; } + /** * Format a permission with the permission prefix. * diff --git a/src/main/java/fr/communaywen/core/commands/MoneyCommand.java b/src/main/java/fr/communaywen/core/commands/MoneyCommand.java new file mode 100644 index 00000000..e65196fe --- /dev/null +++ b/src/main/java/fr/communaywen/core/commands/MoneyCommand.java @@ -0,0 +1,55 @@ +package fr.communaywen.core.commands; + +import fr.communaywen.core.economy.EconomyManager; +import org.bukkit.Bukkit; +import org.bukkit.command.Command; +import org.bukkit.command.CommandExecutor; +import org.bukkit.command.CommandSender; +import org.bukkit.entity.Player; + +public class MoneyCommand implements CommandExecutor { + private final EconomyManager economyManager; + + public MoneyCommand(EconomyManager economyManager) { + this.economyManager = economyManager; + } + + @Override + public boolean onCommand(CommandSender sender, Command command, String label, String[] args) { + if (!(sender instanceof Player)) { + sender.sendMessage("This command can only be run by a player."); + return true; + } + + Player player = (Player) sender; + + if (args.length == 0) { + player.sendMessage("Balance: " + economyManager.getBalance(player)); + return true; + } + + if (args.length == 3 && args[0].equalsIgnoreCase("transfer")) { + Player target = Bukkit.getPlayer(args[1]); + double amount; + + try { + amount = Double.parseDouble(args[2]); + } catch (NumberFormatException e) { + player.sendMessage("Invalid amount."); + return true; + } + + if (target != null && economyManager.transferBalance(player, target, amount)) { + player.sendMessage("Transferred " + amount + " to " + target.getName()); + target.sendMessage("Received " + amount + " from " + player.getName()); + } else { + player.sendMessage("Transfer failed."); + } + + return true; + } + + player.sendMessage("Usage: /money [transfer ]"); + return true; + } +} diff --git a/src/main/java/fr/communaywen/core/economy/EconomyData.java b/src/main/java/fr/communaywen/core/economy/EconomyData.java new file mode 100644 index 00000000..56927fee --- /dev/null +++ b/src/main/java/fr/communaywen/core/economy/EconomyData.java @@ -0,0 +1,48 @@ +package fr.communaywen.core.economy; + +import org.bukkit.Bukkit; +import org.bukkit.configuration.file.FileConfiguration; +import org.bukkit.configuration.file.YamlConfiguration; +import org.bukkit.entity.Player; + +import java.io.File; +import java.io.IOException; +import java.util.HashMap; +import java.util.Map; +import java.util.UUID; + +public class EconomyData { + private final File file; + private final FileConfiguration config; + + public EconomyData(File dataFolder) { + file = new File(dataFolder, "economy.yml"); + if (!file.exists()) { + try { + file.createNewFile(); + } catch (IOException e) { + e.printStackTrace(); + } + } + config = YamlConfiguration.loadConfiguration(file); + } + + public void saveBalances(Map balances) { + for (Map.Entry entry : balances.entrySet()) { + config.set(entry.getKey().toString(), entry.getValue()); + } + try { + config.save(file); + } catch (IOException e) { + e.printStackTrace(); + } + } + + public Map loadBalances() { + Map balances = new HashMap<>(); + for (String key : config.getKeys(false)) { + balances.put(UUID.fromString(key), config.getDouble(key)); + } + return balances; + } +} diff --git a/src/main/java/fr/communaywen/core/economy/EconomyManager.java b/src/main/java/fr/communaywen/core/economy/EconomyManager.java new file mode 100644 index 00000000..f9d1a151 --- /dev/null +++ b/src/main/java/fr/communaywen/core/economy/EconomyManager.java @@ -0,0 +1,54 @@ +package fr.communaywen.core.economy; + +import org.bukkit.Bukkit; +import org.bukkit.entity.Player; + +import java.io.File; +import java.util.HashMap; +import java.util.Map; +import java.util.UUID; + +public class EconomyManager { + private final Map balances; + private final EconomyData economyData; + + public EconomyManager(File dataFolder) { + this.economyData = new EconomyData(dataFolder); + this.balances = economyData.loadBalances(); + } + + public double getBalance(Player player) { + return balances.getOrDefault(player.getUniqueId(), 0.0); + } + + public void addBalance(Player player, double amount) { + UUID uuid = player.getUniqueId(); + balances.put(uuid, getBalance(player) + amount); + saveBalances(); + } + + public boolean withdrawBalance(Player player, double amount) { + UUID uuid = player.getUniqueId(); + double balance = getBalance(player); + if (balance >= amount) { + balances.put(uuid, balance - amount); + saveBalances(); + return true; + } else { + return false; + } + } + + public boolean transferBalance(Player from, Player to, double amount) { + if (withdrawBalance(from, amount)) { + addBalance(to, amount); + return true; + } else { + return false; + } + } + + private void saveBalances() { + economyData.saveBalances(balances); + } +} diff --git a/src/main/resources/plugin.yml b/src/main/resources/plugin.yml index f1d6871d..1bcef36a 100644 --- a/src/main/resources/plugin.yml +++ b/src/main/resources/plugin.yml @@ -21,6 +21,9 @@ commands: description: Téléportation aléatoire permission: ayw.command.rtp aliases: randomteleportation + money: + description: Manage your money + usage: /money [transfer ] permissions: ayw.admin.version: