Bumped version to 1.2.
Added `/balance` and `/pay` commands and a config option to enable them. Added safeIs method to Config to match values without case sensitivity. Added getCurrencyText method to Config to generate a formatted string from a currency amount. Value 'format' from config file now supports amount-name replacement. Main now stores the instance of VaultLayer for further use.
This commit is contained in:
parent
f04001176a
commit
717f1b9a1a
8 changed files with 266 additions and 98 deletions
|
|
@ -9,15 +9,18 @@ Features:
|
||||||
- Simple logic: Just checks if the user has the item/how many when queried.
|
- 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.
|
- Ender Chest support: Items on Ender Chests are counted in the user balance.
|
||||||
|
- Built-int optional balance and pay commands with support for permissions.
|
||||||
|
|
||||||
## 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: "{} $" # {} will be replaced with the amount and $ either with singular or plural
|
||||||
ender_chest: balance # Either none or balance
|
ender_chest: balance # Either none or balance
|
||||||
|
commands: true # Disabling this will disable /balance and /pay
|
||||||
```
|
```
|
||||||
|
|
||||||
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".
|
||||||
|
|
||||||
## Usage:
|
## Usage:
|
||||||
|
|
|
||||||
|
|
@ -3,6 +3,7 @@ package io.github.adrianvic.itemeconomy;
|
||||||
import org.bukkit.Material;
|
import org.bukkit.Material;
|
||||||
|
|
||||||
import java.util.HashMap;
|
import java.util.HashMap;
|
||||||
|
import java.util.Locale;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
|
|
||||||
public class Config {
|
public class Config {
|
||||||
|
|
@ -16,6 +17,7 @@ public class Config {
|
||||||
entries.put("plural", "diamonds");
|
entries.put("plural", "diamonds");
|
||||||
entries.put("singular", "diamond");
|
entries.put("singular", "diamond");
|
||||||
entries.put("ender_chest", "balance");
|
entries.put("ender_chest", "balance");
|
||||||
|
entries.put("commands", "true");
|
||||||
|
|
||||||
Map<String, String> missingValues = new HashMap<>();
|
Map<String, String> missingValues = new HashMap<>();
|
||||||
|
|
||||||
|
|
@ -44,6 +46,16 @@ public class Config {
|
||||||
return entries.get(entry).equals(value);
|
return entries.get(entry).equals(value);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public static boolean safeIs(String entry, String value) {
|
||||||
|
return is(entry.toLowerCase(Locale.ROOT), value.toLowerCase(Locale.ROOT));
|
||||||
|
}
|
||||||
|
|
||||||
|
public static String getCurrencyText(int amount) {
|
||||||
|
return entries.get("format")
|
||||||
|
.replace("{}", String.valueOf(amount))
|
||||||
|
.replace("$", (amount != 1) ? entries.get("plural") : entries.get("singular"));
|
||||||
|
}
|
||||||
|
|
||||||
public static UnrealConfig getuConf() {
|
public static UnrealConfig getuConf() {
|
||||||
return uConf;
|
return uConf;
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -2,6 +2,8 @@ package io.github.adrianvic.itemeconomy;
|
||||||
|
|
||||||
import java.util.*;
|
import java.util.*;
|
||||||
|
|
||||||
|
import io.github.adrianvic.itemeconomy.commands.Balance;
|
||||||
|
import io.github.adrianvic.itemeconomy.commands.Pay;
|
||||||
import io.github.adrianvic.itemeconomy.commands.Reload;
|
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;
|
||||||
|
|
@ -14,20 +16,31 @@ import org.bukkit.plugin.java.JavaPlugin;
|
||||||
|
|
||||||
public class Main extends JavaPlugin {
|
public class Main extends JavaPlugin {
|
||||||
private static Main instance;
|
private static Main instance;
|
||||||
|
private static Economy economy;
|
||||||
|
|
||||||
public void onEnable() {
|
public void onEnable() {
|
||||||
instance = this;
|
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);
|
economy = new VaultLayer();
|
||||||
|
Bukkit.getServicesManager().register(Economy.class, economy, this, ServicePriority.High);
|
||||||
|
|
||||||
getCommand("itecoreload").setExecutor(new Reload());
|
getCommand("itecoreload").setExecutor(new Reload());
|
||||||
|
if (Config.safeIs("commands", "true")) {
|
||||||
|
getCommand("balance").setExecutor(new Balance());
|
||||||
|
getCommand("pay").setExecutor(new Pay());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public void onDisable() {
|
||||||
|
super.onDisable();
|
||||||
}
|
}
|
||||||
|
|
||||||
public static JavaPlugin getInstance() {
|
public static JavaPlugin getInstance() {
|
||||||
return instance;
|
return instance;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void onDisable() {
|
public static Economy getEconomy() {
|
||||||
super.onDisable();
|
return economy;
|
||||||
}
|
}
|
||||||
|
|
||||||
public enum InventoryID {
|
public enum InventoryID {
|
||||||
|
|
@ -39,9 +52,7 @@ public class Main extends JavaPlugin {
|
||||||
Inventory inv = player.getInventory();
|
Inventory inv = player.getInventory();
|
||||||
|
|
||||||
switch (inventory) {
|
switch (inventory) {
|
||||||
case INVENTORY -> {
|
case INVENTORY -> inv = player.getInventory();
|
||||||
inv = player.getInventory();
|
|
||||||
}
|
|
||||||
case ENDER_CHEST -> {
|
case ENDER_CHEST -> {
|
||||||
if (Config.is("ender_chest", "balance")) {
|
if (Config.is("ender_chest", "balance")) {
|
||||||
inv = player.getEnderChest();
|
inv = player.getEnderChest();
|
||||||
|
|
@ -64,13 +75,11 @@ public class Main extends JavaPlugin {
|
||||||
}
|
}
|
||||||
|
|
||||||
public static double getBalance(Player player, InventoryID inventory) {
|
public static double getBalance(Player player, InventoryID inventory) {
|
||||||
return (double) getInventoryList(player, inventory).stream().filter(Objects::nonNull).filter((i) -> {
|
return getInventoryList(player, inventory).stream().filter(Objects::nonNull).filter((i) -> i.getType().equals(Config.ecoItem())).mapToInt(ItemStack::getAmount).sum();
|
||||||
return i.getType().equals(Config.ecoItem());
|
|
||||||
}).mapToInt(ItemStack::getAmount).sum();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public static double getBalance(Player player) {
|
public static double getBalance(Player player) {
|
||||||
Double total = 0.0D;
|
double total = 0.0D;
|
||||||
|
|
||||||
for (InventoryID id : InventoryID.values()) {
|
for (InventoryID id : InventoryID.values()) {
|
||||||
total += getBalance(player, id);
|
total += getBalance(player, id);
|
||||||
|
|
@ -107,7 +116,6 @@ public class Main extends JavaPlugin {
|
||||||
return amount;
|
return amount;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
public static void addItems(Player player, Material type, int amount) {
|
public static void addItems(Player player, Material type, int amount) {
|
||||||
HashMap<Integer, ItemStack> invOverflow = getInventory(player, InventoryID.INVENTORY).addItem(new ItemStack(type, amount));
|
HashMap<Integer, ItemStack> invOverflow = getInventory(player, InventoryID.INVENTORY).addItem(new ItemStack(type, amount));
|
||||||
HashMap<Integer, ItemStack> echestOverflow = getInventory(player, InventoryID.ENDER_CHEST).addItem(new ItemStack(type, invOverflow.values()
|
HashMap<Integer, ItemStack> echestOverflow = getInventory(player, InventoryID.ENDER_CHEST).addItem(new ItemStack(type, invOverflow.values()
|
||||||
|
|
|
||||||
|
|
@ -26,7 +26,7 @@ public class VaultLayer implements Economy {
|
||||||
}
|
}
|
||||||
|
|
||||||
public String format(double amount) {
|
public String format(double amount) {
|
||||||
return Config.get("format").replace("{}", String.valueOf(amount));
|
return Config.getCurrencyText((int) amount);
|
||||||
}
|
}
|
||||||
|
|
||||||
public String currencyNamePlural() {
|
public String currencyNamePlural() {
|
||||||
|
|
|
||||||
|
|
@ -0,0 +1,49 @@
|
||||||
|
package io.github.adrianvic.itemeconomy.commands;
|
||||||
|
|
||||||
|
import io.github.adrianvic.itemeconomy.Config;
|
||||||
|
import io.github.adrianvic.itemeconomy.Main;
|
||||||
|
import org.bukkit.Bukkit;
|
||||||
|
import org.bukkit.command.Command;
|
||||||
|
import org.bukkit.command.CommandExecutor;
|
||||||
|
import org.bukkit.command.CommandSender;
|
||||||
|
import org.bukkit.command.TabCompleter;
|
||||||
|
import org.bukkit.entity.Player;
|
||||||
|
import org.jetbrains.annotations.NotNull;
|
||||||
|
import org.jetbrains.annotations.Nullable;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.stream.Collectors;
|
||||||
|
|
||||||
|
public class Balance implements CommandExecutor, TabCompleter {
|
||||||
|
@Override
|
||||||
|
public boolean onCommand(@NotNull CommandSender commandSender, @NotNull Command command, @NotNull String s, @NotNull String @NotNull [] strings) {
|
||||||
|
if ((commandSender.isOp() || commandSender.hasPermission("iteco.balance.others")) && strings.length > 0) {
|
||||||
|
double amount = Main.getEconomy().getBalance(strings[0]);
|
||||||
|
commandSender.sendMessage("%s has %s.".formatted(
|
||||||
|
strings[0],
|
||||||
|
Config.getCurrencyText((int) amount)
|
||||||
|
));
|
||||||
|
} else {
|
||||||
|
if (commandSender instanceof Player player) {
|
||||||
|
double amount = Main.getEconomy().getBalance(player);
|
||||||
|
commandSender.sendMessage("You have %s.".formatted(
|
||||||
|
Config.getCurrencyText((int) amount)
|
||||||
|
));
|
||||||
|
} else {
|
||||||
|
commandSender.sendMessage("One must be a player to have a balance.");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public @Nullable List<String> onTabComplete(@NotNull CommandSender sender, @NotNull Command command, @NotNull String label, @NotNull String @NotNull [] args) {
|
||||||
|
if ((sender.isOp() || sender.hasPermission("iteco.balance.others")) && args.length == 1) {
|
||||||
|
return Bukkit.getOnlinePlayers()
|
||||||
|
.stream()
|
||||||
|
.map(Player::getName)
|
||||||
|
.collect(Collectors.toList());
|
||||||
|
}
|
||||||
|
return List.of();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -0,0 +1,74 @@
|
||||||
|
package io.github.adrianvic.itemeconomy.commands;
|
||||||
|
|
||||||
|
import io.github.adrianvic.itemeconomy.Config;
|
||||||
|
import io.github.adrianvic.itemeconomy.Main;
|
||||||
|
import net.milkbowl.vault.economy.EconomyResponse;
|
||||||
|
import org.bukkit.Bukkit;
|
||||||
|
import org.bukkit.command.Command;
|
||||||
|
import org.bukkit.command.CommandExecutor;
|
||||||
|
import org.bukkit.command.CommandSender;
|
||||||
|
import org.bukkit.command.TabCompleter;
|
||||||
|
import org.bukkit.entity.Player;
|
||||||
|
import org.jetbrains.annotations.NotNull;
|
||||||
|
import org.jetbrains.annotations.Nullable;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.stream.Collectors;
|
||||||
|
|
||||||
|
public class Pay implements CommandExecutor, TabCompleter {
|
||||||
|
@Override
|
||||||
|
public boolean onCommand(@NotNull CommandSender commandSender, @NotNull Command command, @NotNull String s, @NotNull String @NotNull [] strings) {
|
||||||
|
if (strings.length < 2) {
|
||||||
|
commandSender.sendMessage(command.getUsage());
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
int amount;
|
||||||
|
|
||||||
|
try {
|
||||||
|
amount = Integer.parseInt(strings[1]);
|
||||||
|
String amountString = Config.getCurrencyText(amount);
|
||||||
|
|
||||||
|
if (commandSender instanceof Player player && Main.getEconomy().has(player, amount)) {
|
||||||
|
if (Bukkit.getPlayer(strings[0]) instanceof Player target) {
|
||||||
|
EconomyResponse withdrawResponse = Main.getEconomy().withdrawPlayer(player.getName(), amount);
|
||||||
|
if (withdrawResponse.transactionSuccess()) {
|
||||||
|
EconomyResponse depositResponse = Main.getEconomy().depositPlayer(target.getName(), amount);
|
||||||
|
if (depositResponse.transactionSuccess()) {
|
||||||
|
commandSender.sendMessage("Transaction of %s to %s was successfully realized.".formatted(amountString, target.getName()));
|
||||||
|
target.sendMessage("You received %s from %s.".formatted(amountString, player.getName()));
|
||||||
|
} else {
|
||||||
|
commandSender.sendMessage("Could not realize transaction: %s".formatted(depositResponse.errorMessage));
|
||||||
|
Main.getEconomy().depositPlayer(player.getName(), amount);
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
commandSender.sendMessage("Could not realize transaction: %s".formatted(withdrawResponse.errorMessage));
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
commandSender.sendMessage("Could not find target player.");
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
commandSender.sendMessage("You don't have enough money.");
|
||||||
|
}
|
||||||
|
} catch (NumberFormatException nfe) {
|
||||||
|
commandSender.sendMessage("The amount you tried to pay is not valid.");
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public @Nullable List<String> onTabComplete(@NotNull CommandSender sender, @NotNull Command command, @NotNull String label, @NotNull String @NotNull [] args) {
|
||||||
|
if (sender instanceof Player player) {
|
||||||
|
if (args.length == 1) {
|
||||||
|
return Bukkit.getOnlinePlayers()
|
||||||
|
.stream()
|
||||||
|
.map(Player::getName)
|
||||||
|
.collect(Collectors.toList());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return List.of();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -2,3 +2,4 @@ item: "diamond"
|
||||||
singular: "diamond"
|
singular: "diamond"
|
||||||
plural: "diamonds"
|
plural: "diamonds"
|
||||||
format: "{} $"
|
format: "{} $"
|
||||||
|
commands: "true"
|
||||||
|
|
@ -1,11 +1,32 @@
|
||||||
name: ItemEconomy
|
name: ItemEconomy
|
||||||
main: io.github.adrianvic.itemeconomy.Main
|
main: io.github.adrianvic.itemeconomy.Main
|
||||||
version: 1.1
|
version: 1.2
|
||||||
depend:
|
depend: [Vault]
|
||||||
- Vault
|
|
||||||
api-version: '1.21'
|
api-version: '1.21'
|
||||||
commands:
|
commands:
|
||||||
itecoreload:
|
itecoreload:
|
||||||
description: Reloads the config for ItemEconomy
|
description: Reloads the config for ItemEconomy
|
||||||
usage: /itecoreload
|
usage: "/itecoreload"
|
||||||
permission: iteco.reload
|
permission: iteco.reload
|
||||||
|
default: op
|
||||||
|
balance:
|
||||||
|
description: Prints your balance
|
||||||
|
usage: "/balance"
|
||||||
|
aliases: [bal]
|
||||||
|
permission: iteco.balance
|
||||||
|
pay:
|
||||||
|
description: Transfers money from your balance to other player
|
||||||
|
usage: "/pay <player> <amount>"
|
||||||
|
permission: iteco.pay
|
||||||
|
permissions:
|
||||||
|
iteco.reload:
|
||||||
|
description: "Permission to use the command '/itecoreload'."
|
||||||
|
default: false
|
||||||
|
iteco.balance:
|
||||||
|
description: "Permission to use the command '/balance'."
|
||||||
|
default: true
|
||||||
|
children:
|
||||||
|
iteco.balance.others: false
|
||||||
|
iteco.pay:
|
||||||
|
description: "Permission to use the command '/pay'."
|
||||||
|
default: true
|
||||||
Loading…
Add table
Add a link
Reference in a new issue