Added support for Ender Chest item lookup.
This commit is contained in:
parent
a5dd9f5060
commit
9d58d8f377
2 changed files with 81 additions and 27 deletions
|
|
@ -1,13 +1,12 @@
|
||||||
package io.github.adrianvic.itemeconomy;
|
package io.github.adrianvic.itemeconomy;
|
||||||
|
|
||||||
import java.util.Arrays;
|
import java.util.*;
|
||||||
import java.util.HashMap;
|
|
||||||
import java.util.Iterator;
|
|
||||||
import java.util.List;
|
|
||||||
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;
|
||||||
import org.bukkit.entity.Player;
|
import org.bukkit.entity.Player;
|
||||||
|
import org.bukkit.inventory.Inventory;
|
||||||
import org.bukkit.inventory.ItemStack;
|
import org.bukkit.inventory.ItemStack;
|
||||||
import org.bukkit.plugin.ServicePriority;
|
import org.bukkit.plugin.ServicePriority;
|
||||||
import org.bukkit.plugin.java.JavaPlugin;
|
import org.bukkit.plugin.java.JavaPlugin;
|
||||||
|
|
@ -22,29 +21,87 @@ public class Main extends JavaPlugin {
|
||||||
super.onDisable();
|
super.onDisable();
|
||||||
}
|
}
|
||||||
|
|
||||||
public static List<ItemStack> getInventory(Player player) {
|
public enum InventoryID {
|
||||||
return Arrays.stream(player.getInventory().getContents()).map((o) -> {
|
INVENTORY,
|
||||||
|
ENDER_CHEST
|
||||||
|
}
|
||||||
|
|
||||||
|
public static List<ItemStack> getInventory(Player player, InventoryID inventory) {
|
||||||
|
Inventory inv = player.getInventory();
|
||||||
|
|
||||||
|
switch (inventory) {
|
||||||
|
case INVENTORY -> {
|
||||||
|
inv = player.getInventory();
|
||||||
|
}
|
||||||
|
case ENDER_CHEST -> {
|
||||||
|
inv = player.getEnderChest();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return Arrays.stream(inv.getContents()).map((o) -> {
|
||||||
return o == null ? new ItemStack(Material.AIR) : o;
|
return o == null ? new ItemStack(Material.AIR) : o;
|
||||||
}).toList();
|
}).toList();
|
||||||
}
|
}
|
||||||
|
|
||||||
public static boolean removeItems(Player player, Material type, int amount) {
|
public static List<ItemStack> getInventory(Player player) {
|
||||||
if (player.getInventory().all(type).values().stream().mapToInt(ItemStack::getAmount).sum() < amount) {
|
return getInventory(player, InventoryID.INVENTORY);
|
||||||
return false;
|
|
||||||
} else {
|
|
||||||
player.getInventory().removeItem(new ItemStack[]{new ItemStack(type, amount)});
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
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);
|
||||||
|
}).mapToInt(ItemStack::getAmount).sum();
|
||||||
|
}
|
||||||
|
|
||||||
|
public static double getBalance(Player player) {
|
||||||
|
Double total = 0.0D;
|
||||||
|
|
||||||
|
for (InventoryID id : InventoryID.values()) {
|
||||||
|
total += getBalance(player, id);
|
||||||
|
}
|
||||||
|
|
||||||
|
return total;
|
||||||
|
}
|
||||||
|
|
||||||
|
public static double getBalance(String player) {
|
||||||
|
return getBalance(Bukkit.getPlayer(player));
|
||||||
|
}
|
||||||
|
|
||||||
|
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);
|
||||||
|
}
|
||||||
|
|
||||||
|
return remaining == 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;
|
||||||
|
}
|
||||||
|
|
||||||
|
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> nope = player.getInventory().addItem(new ItemStack[]{new ItemStack(type, amount)});
|
HashMap<Integer, ItemStack> invOverflow = player.getInventory().addItem(new ItemStack(type, amount));
|
||||||
Iterator var4 = nope.values().iterator();
|
HashMap<Integer, ItemStack> echestOverflow = player.getEnderChest().addItem(new ItemStack(type, invOverflow.values()
|
||||||
|
.stream()
|
||||||
|
.mapToInt(ItemStack::getAmount)
|
||||||
|
.sum()));
|
||||||
|
|
||||||
while(var4.hasNext()) {
|
|
||||||
ItemStack v = (ItemStack)var4.next();
|
for (ItemStack overflow : echestOverflow.values()){
|
||||||
player.getWorld().dropItemNaturally(player.getLocation(), v);
|
player.getWorld().dropItemNaturally(player.getLocation(), overflow);
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -16,7 +16,7 @@ public class VaultLayer implements Economy {
|
||||||
}
|
}
|
||||||
|
|
||||||
public String getName() {
|
public String getName() {
|
||||||
return "ItemEconomy";
|
return "ItemEconomy II";
|
||||||
}
|
}
|
||||||
|
|
||||||
public boolean hasBankSupport() {
|
public boolean hasBankSupport() {
|
||||||
|
|
@ -44,14 +44,11 @@ public class VaultLayer implements Economy {
|
||||||
}
|
}
|
||||||
|
|
||||||
public double getBalance(String playerName) {
|
public double getBalance(String playerName) {
|
||||||
Player player = Bukkit.getPlayer(playerName);
|
return Main.getBalance(playerName);
|
||||||
return player != null ? (double)Main.getInventory(player).stream().filter(Objects::nonNull).filter((i) -> {
|
|
||||||
return i.getType().equals(Config.ITEM);
|
|
||||||
}).mapToInt(ItemStack::getAmount).sum() : 0.0D;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public boolean has(String playerName, double amount) {
|
public boolean has(String playerName, double amount) {
|
||||||
return this.getBalance(playerName) >= amount;
|
return Main.getBalance(playerName) >= amount;
|
||||||
}
|
}
|
||||||
|
|
||||||
public EconomyResponse withdrawPlayer(String playerName, double amount) {
|
public EconomyResponse withdrawPlayer(String playerName, double amount) {
|
||||||
|
|
@ -60,13 +57,13 @@ public class VaultLayer implements Economy {
|
||||||
} else if (amount < 0.0D) {
|
} else if (amount < 0.0D) {
|
||||||
return this.depositPlayer(playerName, -amount);
|
return this.depositPlayer(playerName, -amount);
|
||||||
} else if (!this.has(playerName, amount)) {
|
} else if (!this.has(playerName, amount)) {
|
||||||
return new EconomyResponse(amount, this.getBalance(playerName), ResponseType.FAILURE, "Недостаточно средств");
|
return new EconomyResponse(amount, this.getBalance(playerName), ResponseType.FAILURE, "Insufficient founds.");
|
||||||
} else {
|
} else {
|
||||||
Player player;
|
Player player;
|
||||||
if ((player = Bukkit.getPlayer(playerName)) == null) {
|
if ((player = Bukkit.getPlayer(playerName)) == null) {
|
||||||
return new EconomyResponse(amount, this.getBalance(playerName), ResponseType.FAILURE, "Игрок офлайн");
|
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, "Недостаточно средств") : new EconomyResponse(amount, this.getBalance(playerName), ResponseType.SUCCESS, (String)null);
|
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);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -79,7 +76,7 @@ public class VaultLayer implements Economy {
|
||||||
} else {
|
} else {
|
||||||
Player player;
|
Player player;
|
||||||
if ((player = Bukkit.getPlayer(playerName)) == null) {
|
if ((player = Bukkit.getPlayer(playerName)) == null) {
|
||||||
return new EconomyResponse(amount, this.getBalance(playerName), ResponseType.FAILURE, "Игрок офлайн");
|
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.ITEM, (int)amount);
|
||||||
return new EconomyResponse(amount, this.getBalance(playerName), ResponseType.SUCCESS, (String)null);
|
return new EconomyResponse(amount, this.getBalance(playerName), ResponseType.SUCCESS, (String)null);
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue