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
|
|
@ -2,6 +2,8 @@ package io.github.adrianvic.itemeconomy;
|
|||
|
||||
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 net.milkbowl.vault.economy.Economy;
|
||||
import org.bukkit.Bukkit;
|
||||
|
|
@ -13,111 +15,117 @@ import org.bukkit.plugin.ServicePriority;
|
|||
import org.bukkit.plugin.java.JavaPlugin;
|
||||
|
||||
public class Main extends JavaPlugin {
|
||||
private static Main instance;
|
||||
private static Main instance;
|
||||
private static Economy economy;
|
||||
|
||||
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 void onEnable() {
|
||||
instance = this;
|
||||
Config.loadConfig(new UnrealConfig(this, this.getDataFolder(), "config.yml"));
|
||||
economy = new VaultLayer();
|
||||
Bukkit.getServicesManager().register(Economy.class, economy, this, ServicePriority.High);
|
||||
|
||||
public static JavaPlugin getInstance() {
|
||||
return instance;
|
||||
}
|
||||
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 void onDisable() {
|
||||
super.onDisable();
|
||||
}
|
||||
|
||||
public enum InventoryID {
|
||||
INVENTORY,
|
||||
ENDER_CHEST
|
||||
}
|
||||
public static JavaPlugin getInstance() {
|
||||
return instance;
|
||||
}
|
||||
|
||||
public static Inventory getInventory(Player player, InventoryID inventory) {
|
||||
Inventory inv = player.getInventory();
|
||||
public static Economy getEconomy() {
|
||||
return economy;
|
||||
}
|
||||
|
||||
switch (inventory) {
|
||||
case INVENTORY -> {
|
||||
inv = player.getInventory();
|
||||
}
|
||||
case ENDER_CHEST -> {
|
||||
if (Config.is("ender_chest", "balance")) {
|
||||
inv = player.getEnderChest();
|
||||
} else {
|
||||
inv = getInstance().getServer().createInventory(null, 9);
|
||||
public enum InventoryID {
|
||||
INVENTORY,
|
||||
ENDER_CHEST
|
||||
}
|
||||
|
||||
public static Inventory getInventory(Player player, InventoryID inventory) {
|
||||
Inventory inv = player.getInventory();
|
||||
|
||||
switch (inventory) {
|
||||
case INVENTORY -> inv = player.getInventory();
|
||||
case ENDER_CHEST -> {
|
||||
if (Config.is("ender_chest", "balance")) {
|
||||
inv = player.getEnderChest();
|
||||
} else {
|
||||
inv = getInstance().getServer().createInventory(null, 9);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return inv;
|
||||
}
|
||||
return inv;
|
||||
}
|
||||
|
||||
public static List<ItemStack> 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<ItemStack> 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<ItemStack> getInventoryList(Player player) {
|
||||
return getInventoryList(player, InventoryID.INVENTORY);
|
||||
}
|
||||
public static List<ItemStack> getInventoryList(Player player) {
|
||||
return getInventoryList(player, InventoryID.INVENTORY);
|
||||
}
|
||||
|
||||
public static double getBalance(Player player, InventoryID inventory) {
|
||||
return (double) getInventoryList(player, inventory).stream().filter(Objects::nonNull).filter((i) -> {
|
||||
return i.getType().equals(Config.ecoItem());
|
||||
}).mapToInt(ItemStack::getAmount).sum();
|
||||
}
|
||||
public static double getBalance(Player player, InventoryID inventory) {
|
||||
return getInventoryList(player, inventory).stream().filter(Objects::nonNull).filter((i) -> i.getType().equals(Config.ecoItem())).mapToInt(ItemStack::getAmount).sum();
|
||||
}
|
||||
|
||||
public static double getBalance(Player player) {
|
||||
Double total = 0.0D;
|
||||
public static double getBalance(Player player) {
|
||||
double total = 0.0D;
|
||||
|
||||
for (InventoryID id : InventoryID.values()) {
|
||||
total += getBalance(player, id);
|
||||
}
|
||||
for (InventoryID id : InventoryID.values()) {
|
||||
total += getBalance(player, id);
|
||||
}
|
||||
|
||||
return total;
|
||||
}
|
||||
return total;
|
||||
}
|
||||
|
||||
public static double getBalance(String player) {
|
||||
return getBalance(Bukkit.getPlayer(player));
|
||||
}
|
||||
public static double getBalance(String player) {
|
||||
return getBalance(Bukkit.getPlayer(player));
|
||||
}
|
||||
|
||||
public static boolean removeItems(Player player, Material type, int amount) {
|
||||
int remaining = amount;
|
||||
public static boolean removeItems(Player player, Material type, int amount) {
|
||||
int remaining = amount;
|
||||
|
||||
remaining = removeFrom(player.getInventory(), type, remaining);
|
||||
if (remaining > 0) {
|
||||
remaining = removeFrom(player.getEnderChest(), type, remaining);
|
||||
}
|
||||
remaining = removeFrom(player.getInventory(), type, remaining);
|
||||
if (remaining > 0) {
|
||||
remaining = removeFrom(player.getEnderChest(), type, remaining);
|
||||
}
|
||||
|
||||
return remaining == 0;
|
||||
}
|
||||
return remaining == 0;
|
||||
}
|
||||
|
||||
private static int removeFrom(Inventory inv, Material type, int amount) {
|
||||
if (amount <= 0) return 0;
|
||||
private static int removeFrom(Inventory inv, Material type, int amount) {
|
||||
if (amount <= 0) return 0;
|
||||
|
||||
for (ItemStack stack : inv.all(type).values()) {
|
||||
int take = Math.min(stack.getAmount(), amount);
|
||||
stack.setAmount(stack.getAmount() - take);
|
||||
amount -= take;
|
||||
if (amount == 0) break;
|
||||
}
|
||||
for (ItemStack stack : inv.all(type).values()) {
|
||||
int take = Math.min(stack.getAmount(), amount);
|
||||
stack.setAmount(stack.getAmount() - take);
|
||||
amount -= take;
|
||||
if (amount == 0) break;
|
||||
}
|
||||
|
||||
return amount;
|
||||
}
|
||||
return 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> echestOverflow = getInventory(player, InventoryID.ENDER_CHEST).addItem(new ItemStack(type, invOverflow.values()
|
||||
.stream()
|
||||
.mapToInt(ItemStack::getAmount)
|
||||
.sum()));
|
||||
|
||||
|
||||
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> echestOverflow = getInventory(player, InventoryID.ENDER_CHEST).addItem(new ItemStack(type, invOverflow.values()
|
||||
.stream()
|
||||
.mapToInt(ItemStack::getAmount)
|
||||
.sum()));
|
||||
|
||||
|
||||
for (ItemStack overflow : echestOverflow.values()){
|
||||
player.getWorld().dropItemNaturally(player.getLocation(), overflow);
|
||||
}
|
||||
}
|
||||
for (ItemStack overflow : echestOverflow.values()) {
|
||||
player.getWorld().dropItemNaturally(player.getLocation(), overflow);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue