Skip to content

Commit

Permalink
Système d'économie (#58)
Browse files Browse the repository at this point in the history
  • Loading branch information
Xernas78 authored Jul 5, 2024
2 parents d9025b8 + 4c1dfff commit 5d88104
Show file tree
Hide file tree
Showing 5 changed files with 169 additions and 5 deletions.
14 changes: 9 additions & 5 deletions src/main/java/fr/communaywen/core/AywenCraftPlugin.java
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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");
Expand Down Expand Up @@ -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() {
Expand All @@ -83,6 +86,7 @@ public TeamManager getTeamManager() {
public static AywenCraftPlugin getInstance() {
return instance;
}

/**
* Format a permission with the permission prefix.
*
Expand Down
55 changes: 55 additions & 0 deletions src/main/java/fr/communaywen/core/commands/MoneyCommand.java
Original file line number Diff line number Diff line change
@@ -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 <player> <amount>]");
return true;
}
}
48 changes: 48 additions & 0 deletions src/main/java/fr/communaywen/core/economy/EconomyData.java
Original file line number Diff line number Diff line change
@@ -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<UUID, Double> balances) {
for (Map.Entry<UUID, Double> entry : balances.entrySet()) {
config.set(entry.getKey().toString(), entry.getValue());
}
try {
config.save(file);
} catch (IOException e) {
e.printStackTrace();
}
}

public Map<UUID, Double> loadBalances() {
Map<UUID, Double> balances = new HashMap<>();
for (String key : config.getKeys(false)) {
balances.put(UUID.fromString(key), config.getDouble(key));
}
return balances;
}
}
54 changes: 54 additions & 0 deletions src/main/java/fr/communaywen/core/economy/EconomyManager.java
Original file line number Diff line number Diff line change
@@ -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<UUID, Double> 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);
}
}
3 changes: 3 additions & 0 deletions src/main/resources/plugin.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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 <player> <amount>]

permissions:
ayw.admin.version:
Expand Down

0 comments on commit 5d88104

Please sign in to comment.