-
Notifications
You must be signed in to change notification settings - Fork 59
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
169 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
55 changes: 55 additions & 0 deletions
55
src/main/java/fr/communaywen/core/commands/MoneyCommand.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
48
src/main/java/fr/communaywen/core/economy/EconomyData.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
54
src/main/java/fr/communaywen/core/economy/EconomyManager.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters