mirror of
https://github.com/EarthMC/QuickShop-Reremake.git
synced 2026-11-26 02:19:48 -05:00
New matcher system with item matching fixed
Former-commit-id: fb3867fde4cdc34638f994bb3be4a634eda057d5
This commit is contained in:
1 parent
87012da888
commit
5f839a52db
9 files changed
+43
-52
No files matched your search
@@ -52,7 +52,7 @@ public class ArmorStandDisplayItem implements DisplayItem {
|
||||
|
||||
ArmorStandDisplayItem(@NotNull Shop shop) {
|
||||
this.shop = shop;
|
||||
this.originalItemStack = shop.getItem().clone();
|
||||
this.originalItemStack = new ItemStack(shop.getItem());
|
||||
this.originalItemStack.setAmount(1);
|
||||
}
|
||||
|
||||
|
||||
@@ -102,7 +102,7 @@ public class ContainerShop implements Shop {
|
||||
this.location = location;
|
||||
this.price = price;
|
||||
this.moderator = moderator;
|
||||
this.item = item.clone();
|
||||
this.item = new ItemStack(item);
|
||||
this.plugin = QuickShop.instance;
|
||||
this.item.setAmount(1);
|
||||
this.shopType = type;
|
||||
@@ -260,7 +260,7 @@ public class ContainerShop implements Shop {
|
||||
/** @return The durability of the item */
|
||||
@Override
|
||||
public short getDurability() {
|
||||
return (short) ((Damageable) Objects.requireNonNull(this.item.getItemMeta())).getDamage();
|
||||
return (short) ((Damageable) this.item.getItemMeta()).getDamage();
|
||||
}
|
||||
|
||||
/** @return The name of the player who owns the shop. */
|
||||
@@ -314,7 +314,7 @@ public class ContainerShop implements Shop {
|
||||
ItemStack[] contents = p.getInventory().getContents();
|
||||
for (int i = 0; amount1 > 0 && i < contents.length; i++) {
|
||||
ItemStack stack = contents[i];
|
||||
if (stack == null) {
|
||||
if (stack == null || stack.getType() == Material.AIR) {
|
||||
continue; // No item
|
||||
}
|
||||
if (matches(stack)) {
|
||||
@@ -347,7 +347,7 @@ public class ContainerShop implements Shop {
|
||||
ItemStack item = playerContents[i];
|
||||
if (item != null && this.matches(item)) {
|
||||
// Copy it, we don't want to interfere
|
||||
item = item.clone();
|
||||
item = new ItemStack(item);
|
||||
// Amount = total, item.getAmount() = how many items in the
|
||||
// stack
|
||||
int stackSize = Math.min(amount1, item.getAmount());
|
||||
@@ -526,7 +526,7 @@ public class ContainerShop implements Shop {
|
||||
ArrayList<ItemStack> floor = new ArrayList<>(5);
|
||||
Inventory pInv = p.getInventory();
|
||||
if (this.isUnlimited()) {
|
||||
ItemStack item = this.item.clone();
|
||||
ItemStack item = new ItemStack(this.item);
|
||||
while (amount > 0) {
|
||||
int stackSize = Math.min(amount, this.item.getMaxStackSize());
|
||||
item.setAmount(stackSize);
|
||||
@@ -538,9 +538,9 @@ public class ContainerShop implements Shop {
|
||||
for (int i = 0; amount > 0 && i < chestContents.length; i++) {
|
||||
// Can't clone it here, it could be null
|
||||
ItemStack item = chestContents[i];
|
||||
if (item != null && this.matches(item)) {
|
||||
if (item != null && item.getType() != Material.AIR && this.matches(item)) {
|
||||
// Copy it, we don't want to interfere
|
||||
item = item.clone();
|
||||
item = new ItemStack(item);
|
||||
// Amount = total, item.getAmount() = how many items in the
|
||||
// stack
|
||||
int stackSize = Math.min(amount, item.getAmount());
|
||||
|
||||
@@ -133,7 +133,7 @@ public interface DisplayItem {
|
||||
* @return New itemStack with protect flag.
|
||||
*/
|
||||
static ItemStack createGuardItemStack(@NotNull ItemStack itemStack, @NotNull Shop shop) {
|
||||
itemStack = itemStack.clone();
|
||||
itemStack = new ItemStack(itemStack);
|
||||
itemStack.setAmount(1);
|
||||
ItemMeta iMeta = itemStack.getItemMeta();
|
||||
if (QuickShop.instance.getConfig().getBoolean("shop.display-item-use-name")) {
|
||||
|
||||
@@ -46,7 +46,7 @@ public class Info {
|
||||
this.action = action;
|
||||
this.last = last;
|
||||
if (item != null) {
|
||||
this.item = item.clone();
|
||||
this.item = new ItemStack(item);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -60,7 +60,7 @@ public class Info {
|
||||
this.action = action;
|
||||
this.last = last;
|
||||
if (item != null) {
|
||||
this.item = item.clone();
|
||||
this.item = new ItemStack(item);
|
||||
}
|
||||
if (shop != null) {
|
||||
this.shop = shop.clone();
|
||||
|
||||
@@ -52,7 +52,7 @@ public class InventoryPreview implements Listener {
|
||||
* @param player Target player.
|
||||
*/
|
||||
public InventoryPreview(@NotNull ItemStack itemStack, @NotNull Player player) {
|
||||
this.itemStack = itemStack.clone();
|
||||
this.itemStack = new ItemStack(itemStack);
|
||||
this.player = player;
|
||||
if (Objects.requireNonNull(this.itemStack.getItemMeta()).hasLore()) {
|
||||
ItemMeta itemMeta = this.itemStack.getItemMeta();
|
||||
|
||||
@@ -54,7 +54,7 @@ public class RealDisplayItem implements DisplayItem {
|
||||
*/
|
||||
RealDisplayItem(@NotNull Shop shop) {
|
||||
this.shop = shop;
|
||||
this.originalItemStack = shop.getItem().clone();
|
||||
this.originalItemStack = new ItemStack(shop.getItem());
|
||||
this.originalItemStack.setAmount(1);
|
||||
|
||||
// this.displayLoc = shop.getLocation().clone().add(0.5, 1.2, 0.5);
|
||||
|
||||
@@ -46,7 +46,6 @@ public class ItemMatcher {
|
||||
* @return true if the itemstacks match. (Material, durability, enchants, name)
|
||||
*/
|
||||
public boolean matches(@Nullable ItemStack requireStack, @Nullable ItemStack givenStack) {
|
||||
|
||||
if (requireStack == givenStack) {
|
||||
return true; // Referring to the same thing, or both are null.
|
||||
}
|
||||
@@ -57,10 +56,10 @@ public class ItemMatcher {
|
||||
return false; // One of them is null (Can't be both, see above)
|
||||
}
|
||||
|
||||
requireStack = requireStack.clone();
|
||||
requireStack.setAmount(1);
|
||||
givenStack = givenStack.clone();
|
||||
givenStack.setAmount(1);
|
||||
//requireStack = requireStack.clone();
|
||||
//requireStack.setAmount(1);
|
||||
//givenStack = givenStack.clone();
|
||||
//givenStack.setAmount(1);
|
||||
|
||||
// if (plugin.getConfig().getBoolean("shop.strict-matches-check")) {
|
||||
// Util.debugLog("Execute strict match check...");
|
||||
@@ -71,37 +70,22 @@ public class ItemMatcher {
|
||||
// }
|
||||
switch (plugin.getConfig().getInt("matcher.work-type")) {
|
||||
case 1:
|
||||
return requireStack.isSimilar(givenStack);
|
||||
case 2:
|
||||
return requireStack.equals(givenStack);
|
||||
return requireStack.isSimilar(givenStack);
|
||||
case 0:
|
||||
default:
|
||||
;
|
||||
}
|
||||
|
||||
if (!typeMatches(requireStack, givenStack)) {
|
||||
if (requireStack.getType() != givenStack.getType()) {
|
||||
Util.debugLog("Type not match.");
|
||||
return false;
|
||||
}
|
||||
|
||||
// if (requireStack.hasItemMeta() != givenStack.hasItemMeta()) {
|
||||
// Util.debugLog("Meta not matched");
|
||||
// return false;
|
||||
|
||||
if (requireStack.hasItemMeta()) {
|
||||
if (!givenStack.hasItemMeta()) {
|
||||
Util.debugLog("Meta not match.");
|
||||
return false;
|
||||
}
|
||||
return itemMetaMatcher.matches(requireStack, givenStack);
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
private boolean typeMatches(ItemStack requireStack, ItemStack givenStack) {
|
||||
return requireStack.getType().equals(givenStack.getType());
|
||||
return itemMetaMatcher.matches(requireStack, givenStack);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
class ItemMetaMatcher {
|
||||
private boolean repaircost;
|
||||
private boolean attributes;
|
||||
@@ -202,27 +186,36 @@ class ItemMetaMatcher {
|
||||
private boolean repairCostMatches(ItemMeta required, ItemMeta test) {
|
||||
Util.debugLog("Matching: Repair Cost");
|
||||
boolean requiredIs = required instanceof Repairable;
|
||||
if (!canMatches(requiredIs, test instanceof Repairable)) {
|
||||
return false;
|
||||
} else if (!repaircost || !requiredIs) {
|
||||
return true;
|
||||
}
|
||||
|
||||
boolean requiredHas = requiredIs && ((Repairable) required).hasRepairCost();
|
||||
return repaircost ? (canMatches(requiredIs, test instanceof Repairable)
|
||||
&& canMatches(requiredHas, ((Repairable) test).hasRepairCost()) ?
|
||||
return canMatches(requiredHas, ((Repairable) test).hasRepairCost()) ?
|
||||
|
||||
(requiredHas
|
||||
? ((Repairable) required).getRepairCost() == ((Repairable) test).getRepairCost()
|
||||
: true)
|
||||
:
|
||||
|
||||
false)
|
||||
: true;
|
||||
false;
|
||||
}
|
||||
|
||||
private boolean potionMatches(ItemMeta required, ItemMeta test) {
|
||||
Util.debugLog("Matching: Potion");
|
||||
boolean requiredIs = required instanceof PotionMeta;
|
||||
if (!canMatches(requiredIs, test instanceof PotionMeta)) {
|
||||
return false;
|
||||
} else if (!potions || !requiredIs) {
|
||||
return true;
|
||||
}
|
||||
|
||||
boolean requiredHasColor = requiredIs && ((PotionMeta) required).hasColor();
|
||||
boolean requiredHasCustomEffects = requiredIs && ((PotionMeta) required).hasCustomEffects();
|
||||
|
||||
return potions ? (canMatches(requiredIs, test instanceof PotionMeta)
|
||||
&& canMatches(requiredHasColor, ((PotionMeta) test).hasColor())
|
||||
return canMatches(requiredHasColor, ((PotionMeta) test).hasColor())
|
||||
&& canMatches(requiredHasCustomEffects, ((PotionMeta) test).hasCustomEffects()) ?
|
||||
|
||||
(requiredHasColor
|
||||
@@ -238,8 +231,7 @@ class ItemMetaMatcher {
|
||||
.equals(((PotionMeta) test).getBasePotionData())))
|
||||
:
|
||||
|
||||
false)
|
||||
: true;
|
||||
false;
|
||||
}
|
||||
|
||||
boolean matches(ItemStack requiredStack, ItemStack testStack) {
|
||||
|
||||
@@ -183,7 +183,7 @@ public class Util {
|
||||
int items = 0;
|
||||
for (ItemStack iStack : inv.getStorageContents()) {
|
||||
//noinspection ConstantConditions
|
||||
if (iStack == null) {
|
||||
if (iStack == null || iStack.getType() == Material.AIR) {
|
||||
continue;
|
||||
}
|
||||
if (plugin.getItemMatcher().matches(item, iStack)) {
|
||||
|
||||
@@ -319,10 +319,9 @@ effect:
|
||||
onclick: true
|
||||
#Item matcher, don't touch it if you didn't know how to use it.
|
||||
matcher:
|
||||
#Matcher type
|
||||
#0=QuickShop user-friendly matcher.
|
||||
#1=Bukkit standard item matcher.
|
||||
#2=Java equals matcher.
|
||||
# Matcher type
|
||||
#0= QuickShop item matcher with configurable options below.
|
||||
#1= Bukkit item matcher, can be more accurate.
|
||||
work-type: 0
|
||||
#For Item
|
||||
item:
|
||||
|
||||
Reference in new issue
Block a user