Bumped version to 1.1.

Rewrite of config loading to support automatic missing field writing, reloading.

Economy item Material is now provided by Config.

Ender Chest support can now be disabled, `getInventory` (renamed to `getInventoryList`, the old method now returns an instance of Inventory) now returns an empty Inventory instance if Ender Chest is disabled.
This commit is contained in:
天クマ 2026-01-01 15:35:46 -03:00
commit f04001176a
6 changed files with 123 additions and 35 deletions

View file

@ -1,19 +1,22 @@
# ItemEconomy II # 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. 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: Features:
- Item-Based Currency: Set any Minecraft item as your server's currency (default: diamonds). - 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. - 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: ## Configuration Example:
```yaml ```yaml
item: "diamond" # Define the item to be used as currency. item: diamond # Define the item to be used as currency.
singular: "diamond" # Singular form of the currency. singular: diamond # Singular form of the currency.
plural: "diamonds" # Plural form of the currency. plural: diamonds # Plural form of the currency.
format: "{}$" # Customize how the currency is displayed in messages. 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". This configuration will use diamonds as the currency, displayed as {amount}$, e.g., "5 diamonds" or "1 diamond".

View file

@ -2,16 +2,60 @@ package io.github.adrianvic.itemeconomy;
import org.bukkit.Material; import org.bukkit.Material;
import java.util.HashMap;
import java.util.Map;
public class Config { public class Config {
public static Material ITEM; private static Map<String, String> entries = new HashMap<>();
public static String FORMAT; private static UnrealConfig uConf;
public static String PLURAL;
public static String SINGULAR;
public static void loadConfig(UnrealConfig conf) { public static void loadConfig(UnrealConfig conf) {
ITEM = Material.valueOf(((String)conf.get("item")).toUpperCase()); uConf = conf;
FORMAT = (String)conf.get("format"); entries.put("item", "diamond");
PLURAL = (String)conf.get("plural"); entries.put("format", "{}$");
SINGULAR = (String)conf.get("singular"); entries.put("plural", "diamonds");
entries.put("singular", "diamond");
entries.put("ender_chest", "balance");
Map<String, String> missingValues = new HashMap<>();
for (Map.Entry<String, String> 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;
} }
} }

View file

@ -2,6 +2,7 @@ package io.github.adrianvic.itemeconomy;
import java.util.*; import java.util.*;
import io.github.adrianvic.itemeconomy.commands.Reload;
import net.milkbowl.vault.economy.Economy; import net.milkbowl.vault.economy.Economy;
import org.bukkit.Bukkit; import org.bukkit.Bukkit;
import org.bukkit.Material; import org.bukkit.Material;
@ -12,9 +13,17 @@ import org.bukkit.plugin.ServicePriority;
import org.bukkit.plugin.java.JavaPlugin; import org.bukkit.plugin.java.JavaPlugin;
public class Main extends JavaPlugin { public class Main extends JavaPlugin {
private static Main instance;
public void onEnable() { public void onEnable() {
instance = this;
Config.loadConfig(new UnrealConfig(this, this.getDataFolder(), "config.yml")); Config.loadConfig(new UnrealConfig(this, this.getDataFolder(), "config.yml"));
Bukkit.getServicesManager().register(Economy.class, new VaultLayer(), this, ServicePriority.High); Bukkit.getServicesManager().register(Economy.class, new VaultLayer(), this, ServicePriority.High);
getCommand("itecoreload").setExecutor(new Reload());
}
public static JavaPlugin getInstance() {
return instance;
} }
public void onDisable() { public void onDisable() {
@ -26,7 +35,7 @@ public class Main extends JavaPlugin {
ENDER_CHEST ENDER_CHEST
} }
public static List<ItemStack> getInventory(Player player, InventoryID inventory) { public static Inventory getInventory(Player player, InventoryID inventory) {
Inventory inv = player.getInventory(); Inventory inv = player.getInventory();
switch (inventory) { switch (inventory) {
@ -34,22 +43,29 @@ public class Main extends JavaPlugin {
inv = player.getInventory(); inv = player.getInventory();
} }
case ENDER_CHEST -> { case ENDER_CHEST -> {
if (Config.is("ender_chest", "balance")) {
inv = player.getEnderChest(); inv = player.getEnderChest();
} else {
inv = getInstance().getServer().createInventory(null, 9);
}
} }
} }
return Arrays.stream(inv.getContents()).map((o) -> { return inv;
return o == null ? new ItemStack(Material.AIR) : o;
}).toList();
} }
public static List<ItemStack> getInventory(Player player) { public static List<ItemStack> getInventoryList(Player player, InventoryID inventory) {
return getInventory(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<ItemStack> getInventoryList(Player player) {
return getInventoryList(player, InventoryID.INVENTORY);
} }
public static double getBalance(Player player, InventoryID inventory) { public static double getBalance(Player player, InventoryID inventory) {
return (double)getInventory(player, inventory).stream().filter(Objects::nonNull).filter((i) -> { return (double) getInventoryList(player, inventory).stream().filter(Objects::nonNull).filter((i) -> {
return i.getType().equals(Config.ITEM); return i.getType().equals(Config.ecoItem());
}).mapToInt(ItemStack::getAmount).sum(); }).mapToInt(ItemStack::getAmount).sum();
} }
@ -93,8 +109,8 @@ public class Main extends JavaPlugin {
public static void addItems(Player player, Material type, int amount) { public static void addItems(Player player, Material type, int amount) {
HashMap<Integer, ItemStack> invOverflow = player.getInventory().addItem(new ItemStack(type, amount)); HashMap<Integer, ItemStack> invOverflow = getInventory(player, InventoryID.INVENTORY).addItem(new ItemStack(type, amount));
HashMap<Integer, ItemStack> echestOverflow = player.getEnderChest().addItem(new ItemStack(type, invOverflow.values() HashMap<Integer, ItemStack> echestOverflow = getInventory(player, InventoryID.ENDER_CHEST).addItem(new ItemStack(type, invOverflow.values()
.stream() .stream()
.mapToInt(ItemStack::getAmount) .mapToInt(ItemStack::getAmount)
.sum())); .sum()));

View file

@ -1,14 +1,12 @@
package io.github.adrianvic.itemeconomy; package io.github.adrianvic.itemeconomy;
import java.util.List; import java.util.List;
import java.util.Objects;
import net.milkbowl.vault.economy.Economy; import net.milkbowl.vault.economy.Economy;
import net.milkbowl.vault.economy.EconomyResponse; import net.milkbowl.vault.economy.EconomyResponse;
import net.milkbowl.vault.economy.EconomyResponse.ResponseType; import net.milkbowl.vault.economy.EconomyResponse.ResponseType;
import org.bukkit.Bukkit; import org.bukkit.Bukkit;
import org.bukkit.OfflinePlayer; import org.bukkit.OfflinePlayer;
import org.bukkit.entity.Player; import org.bukkit.entity.Player;
import org.bukkit.inventory.ItemStack;
public class VaultLayer implements Economy { public class VaultLayer implements Economy {
public boolean isEnabled() { public boolean isEnabled() {
@ -28,15 +26,15 @@ public class VaultLayer implements Economy {
} }
public String format(double amount) { public String format(double amount) {
return Config.FORMAT.replace("{}", String.valueOf(amount)); return Config.get("format").replace("{}", String.valueOf(amount));
} }
public String currencyNamePlural() { public String currencyNamePlural() {
return Config.PLURAL; return Config.get("plural");
} }
public String currencyNameSingular() { public String currencyNameSingular() {
return Config.SINGULAR; return Config.get("singular");
} }
public boolean hasAccount(String playerName) { public boolean hasAccount(String playerName) {
@ -63,7 +61,7 @@ public class VaultLayer implements Economy {
if ((player = Bukkit.getPlayer(playerName)) == null) { if ((player = Bukkit.getPlayer(playerName)) == null) {
return new EconomyResponse(amount, this.getBalance(playerName), ResponseType.FAILURE, "This player is offline."); return new EconomyResponse(amount, this.getBalance(playerName), ResponseType.FAILURE, "This player is offline.");
} else { } 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) { if ((player = Bukkit.getPlayer(playerName)) == null) {
return new EconomyResponse(amount, this.getBalance(playerName), ResponseType.FAILURE, "This player is offline."); return new EconomyResponse(amount, this.getBalance(playerName), ResponseType.FAILURE, "This player is offline.");
} else { } 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); return new EconomyResponse(amount, this.getBalance(playerName), ResponseType.SUCCESS, (String)null);
} }
} }

View file

@ -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;
}
}
}

View file

@ -1,6 +1,11 @@
name: ItemEconomy name: ItemEconomy
main: io.github.adrianvic.itemeconomy.Main main: io.github.adrianvic.itemeconomy.Main
version: 1.0 version: 1.1
depend: depend:
- Vault - Vault
api-version: '1.21' api-version: '1.21'
commands:
itecoreload:
description: Reloads the config for ItemEconomy
usage: /itecoreload
permission: iteco.reload