This commit is contained in:
YuTian 2024-07-21 16:05:51 +08:00
parent 6a85d4e129
commit 914a063a83
5 changed files with 4 additions and 129 deletions

View File

@ -1,14 +1,11 @@
package com.io.yutian.auchestshop.command.subs;
import com.io.yutian.auchestshop.shop.Shop;
import com.io.yutian.auchestshop.util.ItemStackUtil;
import com.io.yutian.aulib.command.CommandContext;
import com.io.yutian.aulib.command.ICommand;
import com.io.yutian.aulib.command.argument.Argument;
import com.io.yutian.aulib.command.argument.ArgumentTypes;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
import org.bukkit.inventory.ItemStack;
import java.util.ArrayList;
import java.util.List;
@ -27,10 +24,6 @@ public class CommandTest extends ICommand {
CommandSender sender = commandContext.getSender();
String type = commandContext.getArgumentsValue("type").getString();
if (type.equalsIgnoreCase("eq")) {
Player player = (Player) sender;
ItemStack itemStack = player.getInventory().getItemInMainHand();
ItemStack itemStack1 = player.getInventory().getItemInOffHand();
sender.sendMessage("result: "+ItemStackUtil.matches(itemStack, itemStack1));
}
}

View File

@ -6,9 +6,9 @@ import com.io.yutian.auchestshop.AuChestShop;
import com.io.yutian.auchestshop.database.SQLIO;
import com.io.yutian.auchestshop.listener.ShopListener;
import com.io.yutian.auchestshop.shop.*;
import com.io.yutian.auchestshop.util.ItemStackUtil;
import com.io.yutian.auchestshop.util.ShopUtil;
import com.io.yutian.aulib.lang.Lang;
import com.io.yutian.aulib.util.ItemStackUtil;
import org.apache.commons.lang.StringUtils;
import org.bukkit.Chunk;
import org.bukkit.Location;

View File

@ -1,100 +0,0 @@
package com.io.yutian.auchestshop.util;
import com.io.yutian.aulib.nbt.NBTCompound;
import com.io.yutian.aulib.nbt.NBTItem;
import com.io.yutian.aulib.util.LangUtil;
import org.bukkit.Bukkit;
import org.bukkit.Material;
import org.bukkit.inventory.Inventory;
import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.meta.ItemMeta;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import java.util.ArrayList;
import java.util.List;
public class ItemStackUtil {
public static List<ItemStack> mergeItems(ItemStack item, int amount) {
int maxStackSize = item.getMaxStackSize();
int totalAmount = amount;
int remainder = totalAmount % maxStackSize;
int quotient = totalAmount / maxStackSize;
List<ItemStack> items = new ArrayList<>();
for (int i = 0; i < quotient; i++) {
ItemStack itemStack = item.clone();
itemStack.setAmount(maxStackSize);
items.add(itemStack);
}
ItemStack itemStack = item.clone();
itemStack.setAmount(remainder);
items.add(itemStack);
return items;
}
public static boolean matches(@Nullable ItemStack original, @Nullable ItemStack tester) {
if (original == null || tester == null) {
return false;
}
original = original.clone();
original.setAmount(1);
tester = tester.clone();
tester.setAmount(1);
Material comparisonType = original.getType().isLegacy() ? Bukkit.getUnsafe().fromLegacy(original.getData(), true) : original.getType();
boolean flag = comparisonType == tester.getType() && original.getDurability() == tester.getDurability() && original.hasItemMeta() == tester.hasItemMeta() && (!original.hasItemMeta() || Bukkit.getItemFactory().equals(original.getItemMeta(), tester.getItemMeta()));
if (!flag) {
return false;
}
NBTItem nbtOriginal = new NBTItem(original);
NBTItem nbtTester = new NBTItem(tester);
NBTCompound nbtCompound0 = nbtOriginal.getTag();
NBTCompound nbtCompound1 = nbtTester.getTag();
return nbtCompound0.equals(nbtCompound1);
}
public static int countItems(Inventory inventory, @NotNull ItemStack item) {
if (inventory == null) {
return 0;
}
int count = 0;
for (ItemStack itemStack : inventory) {
if (itemStack == null || itemStack.getType() == Material.AIR) {
continue;
}
if (matches(item, itemStack)) {
count+= itemStack.getAmount();
}
}
return count;
}
public static int countSpace(Inventory inventory, @NotNull ItemStack item) {
if (inventory == null) {
return 0;
}
int count = 0;
int itemMaxStackSize = item.getMaxStackSize();
for (ItemStack itemStack : inventory) {
if (itemStack == null || itemStack.getType() == Material.AIR) {
count+=itemMaxStackSize;
} else if (matches(item, itemStack)) {
count+=itemStack.getAmount() >= itemMaxStackSize ? 0 : itemMaxStackSize - itemStack.getAmount();
}
}
return count;
}
public static String getItemName(ItemStack itemStack) {
ItemMeta meta = itemStack.getItemMeta();
if (meta != null && meta.hasDisplayName()) {
return meta.getDisplayName();
}
Material material = itemStack.getType();
String id = material.name().toLowerCase();
boolean isBlock = material.isBlock();
String prefix = isBlock ? "block" : "item";
return LangUtil.getLang(prefix+".minecraft."+id);
}
}

View File

@ -7,7 +7,6 @@ import org.bukkit.Tag;
import org.bukkit.block.Block;
import org.bukkit.block.data.BlockData;
import org.bukkit.block.data.Directional;
import org.bukkit.inventory.Inventory;
import org.bukkit.inventory.ItemStack;
import org.jetbrains.annotations.NotNull;
@ -22,25 +21,6 @@ public class ShopUtil {
SHOPABLES.add(Material.TRAPPED_CHEST);
}
public static void takeShopItemStack(Inventory inventory, ItemStack itemStack, int amount) {
int index = -1;
int a = amount;
while (index < inventory.getSize() && a > 0) {
++index;
ItemStack i = inventory.getItem(index);
if (i != null && ItemStackUtil.matches(itemStack, i)) {
int k = i.getAmount();
if (k < a) {
i.setType(Material.AIR);
} else {
int l = k - a;
i.setAmount(l);
}
a -= k;
}
}
}
public static boolean isShopItem(ItemStack itemStack) {
NBTItem nbtItem = new NBTItem(itemStack);
if (!nbtItem.has("item_type", NBTString.TYPE_ID)) {

View File

@ -4,6 +4,8 @@ import com.io.yutian.auchestshop.AuChestShop;
import com.io.yutian.auchestshop.shop.Shop;
import com.io.yutian.auchestshop.shop.ShopType;
import com.io.yutian.aulib.lang.Lang;
import com.io.yutian.aulib.util.InventoryUtil;
import com.io.yutian.aulib.util.ItemStackUtil;
import com.io.yutian.aulib.util.PlayerInventoryUtil;
import net.milkbowl.vault.economy.EconomyResponse;
import org.bukkit.entity.Player;
@ -36,7 +38,7 @@ public class TradeHelper {
return;
}
List<ItemStack> itemStacks = ItemStackUtil.mergeItems(itemStack, amount);
ShopUtil.takeShopItemStack(shopInventory, itemStack, amount);
InventoryUtil.takeItemStack(shopInventory, itemStack, amount);
for (ItemStack item : itemStacks) {
PlayerInventoryUtil.giveItemStack(player, item);
}