Skip to content

Commit f040011

Browse files
committed
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.
1 parent 3d5acde commit f040011

File tree

6 files changed

+123
-35
lines changed

6 files changed

+123
-35
lines changed

README.md

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,22 @@
11
# ItemEconomy II
2-
ItemEconomy II is a fork of [ItemEconomy](https://modrinth.com/plugin/itemeconomy), keeping it updated to later versions of Minecraft.
2+
ItemEconomy II is a fork of [ItemEconomy](https://modrinth.com/plugin/itemeconomy), keeping it updated to later versions of Minecraft and adding new features.
33

44
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.
55
Features:
66

77
- Item-Based Currency: Set any Minecraft item as your server's currency (default: diamonds).
8-
- Vault Integration: Fully compatible with Vault, enabling seamless interaction with other economy-based plugins.
8+
- VaultUnlocked Integration: Fully compatible with VaultUnlocked, enabling seamless interaction with other economy-based plugins.
9+
- Simple logic: Just checks if the user has the item/how many when queried.
910
- Customizable Formatting: Define how your currency is displayed, including singular and plural forms.
11+
- Ender Chest support: Items on Ender Chests are counted in the user balance.
1012

1113
## Configuration Example:
1214
```yaml
13-
item: "diamond" # Define the item to be used as currency.
14-
singular: "diamond" # Singular form of the currency.
15-
plural: "diamonds" # Plural form of the currency.
15+
item: diamond # Define the item to be used as currency.
16+
singular: diamond # Singular form of the currency.
17+
plural: diamonds # Plural form of the currency.
1618
format: "{}$" # Customize how the currency is displayed in messages.
19+
ender_chest: balance # Either none or balance
1720
```
1821
This configuration will use diamonds as the currency, displayed as {amount}$, e.g., "5 diamonds" or "1 diamond".
1922

src/main/java/io/github/adrianvic/itemeconomy/Config.java

Lines changed: 55 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,60 @@
22

33
import org.bukkit.Material;
44

5+
import java.util.HashMap;
6+
import java.util.Map;
7+
58
public class Config {
6-
public static Material ITEM;
7-
public static String FORMAT;
8-
public static String PLURAL;
9-
public static String SINGULAR;
10-
11-
public static void loadConfig(UnrealConfig conf) {
12-
ITEM = Material.valueOf(((String)conf.get("item")).toUpperCase());
13-
FORMAT = (String)conf.get("format");
14-
PLURAL = (String)conf.get("plural");
15-
SINGULAR = (String)conf.get("singular");
16-
}
9+
private static Map<String, String> entries = new HashMap<>();
10+
private static UnrealConfig uConf;
11+
12+
public static void loadConfig(UnrealConfig conf) {
13+
uConf = conf;
14+
entries.put("item", "diamond");
15+
entries.put("format", "{}$");
16+
entries.put("plural", "diamonds");
17+
entries.put("singular", "diamond");
18+
entries.put("ender_chest", "balance");
19+
20+
Map<String, String> missingValues = new HashMap<>();
21+
22+
for (Map.Entry<String, String> e : entries.entrySet()) {
23+
String val = (String) conf.get(e.getKey());
24+
25+
if (val != null) {
26+
entries.put(e.getKey(), val);
27+
} else {
28+
missingValues.put(e.getKey(), e.getValue());
29+
}
30+
}
31+
32+
missingValues.forEach((key, value) -> {
33+
conf.put(key, value);
34+
Main.getInstance().getLogger().info("Generating new config entry that was missing: %s: %s".formatted(key, value));
35+
});
36+
conf.save();
37+
}
38+
39+
public static String get(String entry) {
40+
return entries.get(entry);
41+
}
42+
43+
public static boolean is(String entry, String value) {
44+
return entries.get(entry).equals(value);
45+
}
46+
47+
public static UnrealConfig getuConf() {
48+
return uConf;
49+
}
50+
51+
public static Material ecoItem() {
52+
try {
53+
return Material.valueOf(Config.get("item").toUpperCase());
54+
} catch (IllegalArgumentException e) {
55+
Main.getInstance().getLogger().warning("Invalid item was set as economy item, disabling.");
56+
Main.getInstance().getServer().getPluginManager().disablePlugin(Main.getInstance());
57+
}
58+
59+
return Material.DIAMOND;
60+
}
1761
}

src/main/java/io/github/adrianvic/itemeconomy/Main.java

Lines changed: 27 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import java.util.*;
44

5+
import io.github.adrianvic.itemeconomy.commands.Reload;
56
import net.milkbowl.vault.economy.Economy;
67
import org.bukkit.Bukkit;
78
import org.bukkit.Material;
@@ -12,9 +13,17 @@
1213
import org.bukkit.plugin.java.JavaPlugin;
1314

1415
public class Main extends JavaPlugin {
16+
private static Main instance;
17+
1518
public void onEnable() {
19+
instance = this;
1620
Config.loadConfig(new UnrealConfig(this, this.getDataFolder(), "config.yml"));
1721
Bukkit.getServicesManager().register(Economy.class, new VaultLayer(), this, ServicePriority.High);
22+
getCommand("itecoreload").setExecutor(new Reload());
23+
}
24+
25+
public static JavaPlugin getInstance() {
26+
return instance;
1827
}
1928

2029
public void onDisable() {
@@ -26,30 +35,37 @@ public enum InventoryID {
2635
ENDER_CHEST
2736
}
2837

29-
public static List<ItemStack> getInventory(Player player, InventoryID inventory) {
38+
public static Inventory getInventory(Player player, InventoryID inventory) {
3039
Inventory inv = player.getInventory();
3140

3241
switch (inventory) {
3342
case INVENTORY -> {
3443
inv = player.getInventory();
3544
}
3645
case ENDER_CHEST -> {
37-
inv = player.getEnderChest();
46+
if (Config.is("ender_chest", "balance")) {
47+
inv = player.getEnderChest();
48+
} else {
49+
inv = getInstance().getServer().createInventory(null, 9);
50+
}
3851
}
3952
}
4053

41-
return Arrays.stream(inv.getContents()).map((o) -> {
42-
return o == null ? new ItemStack(Material.AIR) : o;
43-
}).toList();
54+
return inv;
55+
}
56+
57+
public static List<ItemStack> getInventoryList(Player player, InventoryID inventory) {
58+
Inventory inv = getInventory(player, inventory);
59+
return Arrays.stream(inv.getContents()).map((o) -> o == null ? new ItemStack(Material.AIR) : o).toList();
4460
}
4561

46-
public static List<ItemStack> getInventory(Player player) {
47-
return getInventory(player, InventoryID.INVENTORY);
62+
public static List<ItemStack> getInventoryList(Player player) {
63+
return getInventoryList(player, InventoryID.INVENTORY);
4864
}
4965

5066
public static double getBalance(Player player, InventoryID inventory) {
51-
return (double)getInventory(player, inventory).stream().filter(Objects::nonNull).filter((i) -> {
52-
return i.getType().equals(Config.ITEM);
67+
return (double) getInventoryList(player, inventory).stream().filter(Objects::nonNull).filter((i) -> {
68+
return i.getType().equals(Config.ecoItem());
5369
}).mapToInt(ItemStack::getAmount).sum();
5470
}
5571

@@ -93,8 +109,8 @@ private static int removeFrom(Inventory inv, Material type, int amount) {
93109

94110

95111
public static void addItems(Player player, Material type, int amount) {
96-
HashMap<Integer, ItemStack> invOverflow = player.getInventory().addItem(new ItemStack(type, amount));
97-
HashMap<Integer, ItemStack> echestOverflow = player.getEnderChest().addItem(new ItemStack(type, invOverflow.values()
112+
HashMap<Integer, ItemStack> invOverflow = getInventory(player, InventoryID.INVENTORY).addItem(new ItemStack(type, amount));
113+
HashMap<Integer, ItemStack> echestOverflow = getInventory(player, InventoryID.ENDER_CHEST).addItem(new ItemStack(type, invOverflow.values()
98114
.stream()
99115
.mapToInt(ItemStack::getAmount)
100116
.sum()));

src/main/java/io/github/adrianvic/itemeconomy/VaultLayer.java

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,12 @@
11
package io.github.adrianvic.itemeconomy;
22

33
import java.util.List;
4-
import java.util.Objects;
54
import net.milkbowl.vault.economy.Economy;
65
import net.milkbowl.vault.economy.EconomyResponse;
76
import net.milkbowl.vault.economy.EconomyResponse.ResponseType;
87
import org.bukkit.Bukkit;
98
import org.bukkit.OfflinePlayer;
109
import org.bukkit.entity.Player;
11-
import org.bukkit.inventory.ItemStack;
1210

1311
public class VaultLayer implements Economy {
1412
public boolean isEnabled() {
@@ -28,15 +26,15 @@ public int fractionalDigits() {
2826
}
2927

3028
public String format(double amount) {
31-
return Config.FORMAT.replace("{}", String.valueOf(amount));
29+
return Config.get("format").replace("{}", String.valueOf(amount));
3230
}
3331

3432
public String currencyNamePlural() {
35-
return Config.PLURAL;
33+
return Config.get("plural");
3634
}
3735

3836
public String currencyNameSingular() {
39-
return Config.SINGULAR;
37+
return Config.get("singular");
4038
}
4139

4240
public boolean hasAccount(String playerName) {
@@ -63,7 +61,7 @@ public EconomyResponse withdrawPlayer(String playerName, double amount) {
6361
if ((player = Bukkit.getPlayer(playerName)) == null) {
6462
return new EconomyResponse(amount, this.getBalance(playerName), ResponseType.FAILURE, "This player is offline.");
6563
} else {
66-
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);
64+
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);
6765
}
6866
}
6967
}
@@ -78,7 +76,7 @@ public EconomyResponse depositPlayer(String playerName, double amount) {
7876
if ((player = Bukkit.getPlayer(playerName)) == null) {
7977
return new EconomyResponse(amount, this.getBalance(playerName), ResponseType.FAILURE, "This player is offline.");
8078
} else {
81-
Main.addItems(player, Config.ITEM, (int)amount);
79+
Main.addItems(player, Config.ecoItem(), (int)amount);
8280
return new EconomyResponse(amount, this.getBalance(playerName), ResponseType.SUCCESS, (String)null);
8381
}
8482
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package io.github.adrianvic.itemeconomy.commands;
2+
3+
import io.github.adrianvic.itemeconomy.Config;
4+
import io.github.adrianvic.itemeconomy.Main;
5+
import io.github.adrianvic.itemeconomy.UnrealConfig;
6+
import org.bukkit.command.Command;
7+
import org.bukkit.command.CommandExecutor;
8+
import org.bukkit.command.CommandSender;
9+
import org.jetbrains.annotations.NotNull;
10+
11+
public class Reload implements CommandExecutor {
12+
13+
@Override
14+
public boolean onCommand(@NotNull CommandSender commandSender, @NotNull Command command, @NotNull String s, @NotNull String @NotNull [] strings) {
15+
try {
16+
Config.loadConfig(new UnrealConfig(Main.getInstance(), Main.getInstance().getDataFolder(), "config.yml"));
17+
return true;
18+
} catch (Exception e) {
19+
return false;
20+
}
21+
}
22+
}

src/main/resources/plugin.yml

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
11
name: ItemEconomy
22
main: io.github.adrianvic.itemeconomy.Main
3-
version: 1.0
3+
version: 1.1
44
depend:
55
- Vault
66
api-version: '1.21'
7+
commands:
8+
itecoreload:
9+
description: Reloads the config for ItemEconomy
10+
usage: /itecoreload
11+
permission: iteco.reload

0 commit comments

Comments
 (0)