diff --git a/README.md b/README.md index f2fa45e..2da59ea 100644 --- a/README.md +++ b/README.md @@ -1,19 +1,22 @@ # ItemEconomy II -ItemEconomy II is a fork of [ItemEconomy](https://modrinth.com/plugin/itemeconomy), keeping it updated to later versions of Minecraft. +ItemEconomy II is a fork of [ItemEconomy](https://modrinth.com/plugin/itemeconomy), keeping it updated to later versions of Minecraft and adding new features. This PaperMC plugin integrates with Vault to provide a unique, item-based economy system for your Minecraft server. Instead of relying solely on virtual balances, players use in-game items as physical currency, adding a layer of immersion and realism to your economy. Features: - Item-Based Currency: Set any Minecraft item as your server's currency (default: diamonds). -- Vault Integration: Fully compatible with Vault, enabling seamless interaction with other economy-based plugins. +- VaultUnlocked Integration: Fully compatible with VaultUnlocked, enabling seamless interaction with other economy-based plugins. +- Simple logic: Just checks if the user has the item/how many when queried. - Customizable Formatting: Define how your currency is displayed, including singular and plural forms. +- Ender Chest support: Items on Ender Chests are counted in the user balance. ## Configuration Example: ```yaml -item: "diamond" # Define the item to be used as currency. -singular: "diamond" # Singular form of the currency. -plural: "diamonds" # Plural form of the currency. +item: diamond # Define the item to be used as currency. +singular: diamond # Singular form of the currency. +plural: diamonds # Plural form of the currency. format: "{}$" # Customize how the currency is displayed in messages. +ender_chest: balance # Either none or balance ``` This configuration will use diamonds as the currency, displayed as {amount}$, e.g., "5 diamonds" or "1 diamond". diff --git a/src/main/java/io/github/adrianvic/itemeconomy/Config.java b/src/main/java/io/github/adrianvic/itemeconomy/Config.java index 2f2bb4f..f564ac4 100644 --- a/src/main/java/io/github/adrianvic/itemeconomy/Config.java +++ b/src/main/java/io/github/adrianvic/itemeconomy/Config.java @@ -2,16 +2,60 @@ package io.github.adrianvic.itemeconomy; import org.bukkit.Material; -public class Config { - public static Material ITEM; - public static String FORMAT; - public static String PLURAL; - public static String SINGULAR; +import java.util.HashMap; +import java.util.Map; - public static void loadConfig(UnrealConfig conf) { - ITEM = Material.valueOf(((String)conf.get("item")).toUpperCase()); - FORMAT = (String)conf.get("format"); - PLURAL = (String)conf.get("plural"); - SINGULAR = (String)conf.get("singular"); - } +public class Config { + private static Map entries = new HashMap<>(); + private static UnrealConfig uConf; + + public static void loadConfig(UnrealConfig conf) { + uConf = conf; + entries.put("item", "diamond"); + entries.put("format", "{}$"); + entries.put("plural", "diamonds"); + entries.put("singular", "diamond"); + entries.put("ender_chest", "balance"); + + Map missingValues = new HashMap<>(); + + for (Map.Entry e : entries.entrySet()) { + String val = (String) conf.get(e.getKey()); + + if (val != null) { + entries.put(e.getKey(), val); + } else { + missingValues.put(e.getKey(), e.getValue()); + } + } + + missingValues.forEach((key, value) -> { + conf.put(key, value); + Main.getInstance().getLogger().info("Generating new config entry that was missing: %s: %s".formatted(key, value)); + }); + conf.save(); + } + + public static String get(String entry) { + return entries.get(entry); + } + + public static boolean is(String entry, String value) { + return entries.get(entry).equals(value); + } + + public static UnrealConfig getuConf() { + return uConf; + } + + public static Material ecoItem() { + try { + return Material.valueOf(Config.get("item").toUpperCase()); + } catch (IllegalArgumentException e) { + Main.getInstance().getLogger().warning("Invalid item was set as economy item, disabling."); + Main.getInstance().getServer().getPluginManager().disablePlugin(Main.getInstance()); + } + + return Material.DIAMOND; + } } diff --git a/src/main/java/io/github/adrianvic/itemeconomy/Main.java b/src/main/java/io/github/adrianvic/itemeconomy/Main.java index 219be38..4ff0506 100644 --- a/src/main/java/io/github/adrianvic/itemeconomy/Main.java +++ b/src/main/java/io/github/adrianvic/itemeconomy/Main.java @@ -2,6 +2,7 @@ package io.github.adrianvic.itemeconomy; import java.util.*; +import io.github.adrianvic.itemeconomy.commands.Reload; import net.milkbowl.vault.economy.Economy; import org.bukkit.Bukkit; import org.bukkit.Material; @@ -12,9 +13,17 @@ import org.bukkit.plugin.ServicePriority; import org.bukkit.plugin.java.JavaPlugin; public class Main extends JavaPlugin { + private static Main instance; + public void onEnable() { + instance = this; Config.loadConfig(new UnrealConfig(this, this.getDataFolder(), "config.yml")); Bukkit.getServicesManager().register(Economy.class, new VaultLayer(), this, ServicePriority.High); + getCommand("itecoreload").setExecutor(new Reload()); + } + + public static JavaPlugin getInstance() { + return instance; } public void onDisable() { @@ -26,7 +35,7 @@ public class Main extends JavaPlugin { ENDER_CHEST } - public static List getInventory(Player player, InventoryID inventory) { + public static Inventory getInventory(Player player, InventoryID inventory) { Inventory inv = player.getInventory(); switch (inventory) { @@ -34,22 +43,29 @@ public class Main extends JavaPlugin { inv = player.getInventory(); } case ENDER_CHEST -> { - inv = player.getEnderChest(); + if (Config.is("ender_chest", "balance")) { + inv = player.getEnderChest(); + } else { + inv = getInstance().getServer().createInventory(null, 9); + } } } - return Arrays.stream(inv.getContents()).map((o) -> { - return o == null ? new ItemStack(Material.AIR) : o; - }).toList(); + return inv; } - public static List getInventory(Player player) { - return getInventory(player, InventoryID.INVENTORY); + public static List getInventoryList(Player player, InventoryID inventory) { + Inventory inv = getInventory(player, inventory); + return Arrays.stream(inv.getContents()).map((o) -> o == null ? new ItemStack(Material.AIR) : o).toList(); + } + + public static List getInventoryList(Player player) { + return getInventoryList(player, InventoryID.INVENTORY); } public static double getBalance(Player player, InventoryID inventory) { - return (double)getInventory(player, inventory).stream().filter(Objects::nonNull).filter((i) -> { - return i.getType().equals(Config.ITEM); + return (double) getInventoryList(player, inventory).stream().filter(Objects::nonNull).filter((i) -> { + return i.getType().equals(Config.ecoItem()); }).mapToInt(ItemStack::getAmount).sum(); } @@ -93,8 +109,8 @@ public class Main extends JavaPlugin { public static void addItems(Player player, Material type, int amount) { - HashMap invOverflow = player.getInventory().addItem(new ItemStack(type, amount)); - HashMap echestOverflow = player.getEnderChest().addItem(new ItemStack(type, invOverflow.values() + HashMap invOverflow = getInventory(player, InventoryID.INVENTORY).addItem(new ItemStack(type, amount)); + HashMap echestOverflow = getInventory(player, InventoryID.ENDER_CHEST).addItem(new ItemStack(type, invOverflow.values() .stream() .mapToInt(ItemStack::getAmount) .sum())); diff --git a/src/main/java/io/github/adrianvic/itemeconomy/VaultLayer.java b/src/main/java/io/github/adrianvic/itemeconomy/VaultLayer.java index 665dc8c..1dc88eb 100644 --- a/src/main/java/io/github/adrianvic/itemeconomy/VaultLayer.java +++ b/src/main/java/io/github/adrianvic/itemeconomy/VaultLayer.java @@ -1,14 +1,12 @@ package io.github.adrianvic.itemeconomy; import java.util.List; -import java.util.Objects; import net.milkbowl.vault.economy.Economy; import net.milkbowl.vault.economy.EconomyResponse; import net.milkbowl.vault.economy.EconomyResponse.ResponseType; import org.bukkit.Bukkit; import org.bukkit.OfflinePlayer; import org.bukkit.entity.Player; -import org.bukkit.inventory.ItemStack; public class VaultLayer implements Economy { public boolean isEnabled() { @@ -28,15 +26,15 @@ public class VaultLayer implements Economy { } public String format(double amount) { - return Config.FORMAT.replace("{}", String.valueOf(amount)); + return Config.get("format").replace("{}", String.valueOf(amount)); } public String currencyNamePlural() { - return Config.PLURAL; + return Config.get("plural"); } public String currencyNameSingular() { - return Config.SINGULAR; + return Config.get("singular"); } public boolean hasAccount(String playerName) { @@ -63,7 +61,7 @@ public class VaultLayer implements Economy { if ((player = Bukkit.getPlayer(playerName)) == null) { return new EconomyResponse(amount, this.getBalance(playerName), ResponseType.FAILURE, "This player is offline."); } else { - return !Main.removeItems(player, Config.ITEM, (int)amount) ? new EconomyResponse(amount, this.getBalance(playerName), ResponseType.FAILURE, "Insufficient founds.") : new EconomyResponse(amount, this.getBalance(playerName), ResponseType.SUCCESS, (String)null); + return !Main.removeItems(player, Config.ecoItem(), (int)amount) ? new EconomyResponse(amount, this.getBalance(playerName), ResponseType.FAILURE, "Insufficient founds.") : new EconomyResponse(amount, this.getBalance(playerName), ResponseType.SUCCESS, (String)null); } } } @@ -78,7 +76,7 @@ public class VaultLayer implements Economy { if ((player = Bukkit.getPlayer(playerName)) == null) { return new EconomyResponse(amount, this.getBalance(playerName), ResponseType.FAILURE, "This player is offline."); } else { - Main.addItems(player, Config.ITEM, (int)amount); + Main.addItems(player, Config.ecoItem(), (int)amount); return new EconomyResponse(amount, this.getBalance(playerName), ResponseType.SUCCESS, (String)null); } } diff --git a/src/main/java/io/github/adrianvic/itemeconomy/commands/Reload.java b/src/main/java/io/github/adrianvic/itemeconomy/commands/Reload.java new file mode 100644 index 0000000..804e6ed --- /dev/null +++ b/src/main/java/io/github/adrianvic/itemeconomy/commands/Reload.java @@ -0,0 +1,22 @@ +package io.github.adrianvic.itemeconomy.commands; + +import io.github.adrianvic.itemeconomy.Config; +import io.github.adrianvic.itemeconomy.Main; +import io.github.adrianvic.itemeconomy.UnrealConfig; +import org.bukkit.command.Command; +import org.bukkit.command.CommandExecutor; +import org.bukkit.command.CommandSender; +import org.jetbrains.annotations.NotNull; + +public class Reload implements CommandExecutor { + + @Override + public boolean onCommand(@NotNull CommandSender commandSender, @NotNull Command command, @NotNull String s, @NotNull String @NotNull [] strings) { + try { + Config.loadConfig(new UnrealConfig(Main.getInstance(), Main.getInstance().getDataFolder(), "config.yml")); + return true; + } catch (Exception e) { + return false; + } + } +} diff --git a/src/main/resources/plugin.yml b/src/main/resources/plugin.yml index 60f5404..eeb2443 100644 --- a/src/main/resources/plugin.yml +++ b/src/main/resources/plugin.yml @@ -1,6 +1,11 @@ name: ItemEconomy main: io.github.adrianvic.itemeconomy.Main -version: 1.0 +version: 1.1 depend: - Vault api-version: '1.21' +commands: + itecoreload: + description: Reloads the config for ItemEconomy + usage: /itecoreload + permission: iteco.reload \ No newline at end of file