Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package ua.end433.endiumeconomy;
- import me.clip.placeholderapi.expansion.PlaceholderExpansion;
- import me.clip.placeholderapi.PlaceholderAPI;
- import org.bukkit.Bukkit;
- import org.bukkit.OfflinePlayer;
- import org.bukkit.command.Command;
- import org.bukkit.command.CommandSender;
- import org.bukkit.configuration.file.FileConfiguration;
- import org.bukkit.configuration.file.YamlConfiguration;
- import org.bukkit.plugin.java.JavaPlugin;
- import org.bukkit.ChatColor;
- import org.jetbrains.annotations.NotNull;
- import java.io.File;
- import java.io.IOException;
- public class EndiumEconomy extends JavaPlugin {
- private File vaultsFolder;
- @Override
- public void onEnable() {
- saveDefaultConfig();
- getLogger().info(getMessage("plugin-enabled"));
- // Register PlaceholderAPI expansion
- if (Bukkit.getPluginManager().getPlugin("PlaceholderAPI") != null) {
- new CurrencyExpansion(getDataFolder()).register();
- getLogger().info(getMessage("placeholderapi-registered"));
- } else {
- getLogger().warning(getMessage("placeholderapi-not-found"));
- }
- // Creating vaults folder
- vaultsFolder = new File(getDataFolder(), "vaults");
- if (!vaultsFolder.exists()) {
- if (vaultsFolder.mkdirs()) {
- getLogger().info(getMessage("vaults-folder-created"));
- } else {
- getLogger().warning(getMessage("vaults-folder-creation-failed"));
- }
- }
- }
- @Override
- public boolean onCommand(CommandSender sender, Command command, String label, String[] args) {
- if (command.getName().equalsIgnoreCase("eec")) {
- if (args.length < 1) {
- sender.sendMessage(getMessage("not-enough-arguments"));
- return true;
- }
- String subCommand = args[0].toLowerCase();
- if (subCommand.equals("create")) {
- return handleCreate(sender, args);
- } else if (subCommand.equals("addm")) {
- return handleAddMoney(sender, args);
- } else if (subCommand.equals("removem")) {
- return handleRemoveMoney(sender, args);
- } else if (subCommand.equals("reload")) {
- return handleReload(sender);
- } else if (subCommand.equals("remove")) {
- return handleRemoveVault(sender, args);
- } else {
- sender.sendMessage(getMessage("unknown-command"));
- return true;
- }
- }
- return false;
- }
- private String getMessage(String key) {
- return ChatColor.translateAlternateColorCodes('&', getConfig().getString("messages." + key, "Missing message: " + key));
- }
- private boolean handleCreate(CommandSender sender, String[] args) {
- if (args.length < 2) {
- sender.sendMessage(getMessage("not-enough-arguments"));
- return true;
- }
- String vaultName = args[1];
- File vaultFile = new File(vaultsFolder, vaultName + ".yml");
- if (vaultFile.exists()) {
- sender.sendMessage(getMessage("vault-exists").replace("{vault}", vaultName));
- return true;
- }
- try {
- if (vaultFile.createNewFile()) {
- sender.sendMessage(getMessage("vault-created").replace("{vault}", vaultName));
- } else {
- sender.sendMessage(getMessage("vault-create-failed").replace("{vault}", vaultName));
- }
- } catch (IOException e) {
- sender.sendMessage(getMessage("error").replace("{error}", e.getMessage()));
- e.printStackTrace();
- }
- return true;
- }
- private boolean handleAddMoney(CommandSender sender, String[] args) {
- if (args.length < 4) {
- sender.sendMessage(getMessage("not-enough-arguments"));
- return true;
- }
- String vaultName = args[1];
- String playerName = args[2];
- int amount;
- try {
- amount = Integer.parseInt(args[3]);
- } catch (NumberFormatException e) {
- sender.sendMessage(getMessage("invalid-amount"));
- return true;
- }
- File vaultFile = new File(vaultsFolder, vaultName + ".yml");
- if (!vaultFile.exists()) {
- sender.sendMessage(getMessage("vault-not-found").replace("{vault}", vaultName));
- return true;
- }
- FileConfiguration vaultConfig = YamlConfiguration.loadConfiguration(vaultFile);
- int currentBalance = vaultConfig.getInt(playerName, 0);
- vaultConfig.set(playerName, currentBalance + amount);
- try {
- vaultConfig.save(vaultFile);
- sender.sendMessage(getMessage("player-balance-updated")
- .replace("{player}", playerName)
- .replace("{vault}", vaultName));
- } catch (IOException e) {
- sender.sendMessage(getMessage("error").replace("{error}", e.getMessage()));
- e.printStackTrace();
- }
- return true;
- }
- private boolean handleRemoveMoney(CommandSender sender, String[] args) {
- if (args.length < 4) {
- sender.sendMessage(getMessage("not-enough-arguments"));
- return true;
- }
- String vaultName = args[1];
- String playerName = args[2];
- int amount;
- try {
- amount = Integer.parseInt(args[3]);
- } catch (NumberFormatException e) {
- sender.sendMessage(getMessage("invalid-amount"));
- return true;
- }
- File vaultFile = new File(vaultsFolder, vaultName + ".yml");
- if (!vaultFile.exists()) {
- sender.sendMessage(getMessage("vault-not-found").replace("{vault}", vaultName));
- return true;
- }
- FileConfiguration vaultConfig = YamlConfiguration.loadConfiguration(vaultFile);
- int currentBalance = vaultConfig.getInt(playerName, 0);
- if (currentBalance < amount) {
- sender.sendMessage(getMessage("player-insufficient-balance")
- .replace("{player}", playerName)
- .replace("{vault}", vaultName));
- return true;
- }
- vaultConfig.set(playerName, currentBalance - amount);
- try {
- vaultConfig.save(vaultFile);
- sender.sendMessage(getMessage("player-balance-updated")
- .replace("{player}", playerName)
- .replace("{vault}", vaultName));
- } catch (IOException e) {
- sender.sendMessage(getMessage("error").replace("{error}", e.getMessage()));
- e.printStackTrace();
- }
- return true;
- }
- private boolean handleReload(CommandSender sender) {
- reloadConfig();
- sender.sendMessage(getMessage("plugin-reloaded"));
- return true;
- }
- private boolean handleRemoveVault(CommandSender sender, String[] args) {
- if (args.length < 2) {
- sender.sendMessage(getMessage("not-enough-arguments"));
- return true;
- }
- String vaultName = args[1];
- File vaultFile = new File(vaultsFolder, vaultName + ".yml");
- if (!vaultFile.exists()) {
- sender.sendMessage(getMessage("vault-not-found").replace("{vault}", vaultName));
- return true;
- }
- if (vaultFile.delete()) {
- sender.sendMessage(getMessage("vault-removed").replace("{vault}", vaultName));
- } else {
- sender.sendMessage(getMessage("vault-remove-failed").replace("{vault}", vaultName));
- }
- return true;
- }
- @Override
- public void onDisable() {
- getLogger().info(getMessage("plugin-disabled"));
- }
- }
- class CurrencyExpansion extends PlaceholderExpansion {
- private final File pluginFolder;
- public CurrencyExpansion(File pluginFolder) {
- this.pluginFolder = pluginFolder;
- }
- @Override
- public @NotNull String getIdentifier() {
- return "eec";
- }
- @Override
- public @NotNull String getAuthor() {
- return "end433";
- }
- @Override
- public @NotNull String getVersion() {
- return "1.00";
- }
- @Override
- public boolean persist() {
- return true;
- }
- @Override
- public boolean canRegister() {
- return true;
- }
- @Override
- public String onRequest(OfflinePlayer player, @NotNull String params) {
- String[] args = params.split("_");
- if (args.length != 1) {
- return null; // Invalid placeholder format
- }
- String vaultName = args[0];
- File vaultFile = new File(pluginFolder, "vaults/" + vaultName + ".yml");
- if (!vaultFile.exists()) {
- return "0"; // Vault not found
- }
- FileConfiguration vaultConfig = YamlConfiguration.loadConfiguration(vaultFile);
- String playerName = player.getName();
- if (playerName == null) {
- return "0"; // Player not found
- }
- int balance = vaultConfig.getInt(playerName, 0);
- return String.valueOf(balance);
- }
- }
Add Comment
Please, Sign In to add comment