Rewrite itemaction to use the provided item array

This commit is contained in:
fullwall 2024-03-11 20:37:30 +08:00
parent 40a60fbc8f
commit c683b752a5

View File

@ -10,7 +10,6 @@ import org.bukkit.ChatColor;
import org.bukkit.Material; import org.bukkit.Material;
import org.bukkit.entity.Entity; import org.bukkit.entity.Entity;
import org.bukkit.entity.HumanEntity; import org.bukkit.entity.HumanEntity;
import org.bukkit.inventory.Inventory;
import org.bukkit.inventory.InventoryHolder; import org.bukkit.inventory.InventoryHolder;
import org.bukkit.inventory.ItemStack; import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.meta.Damageable; import org.bukkit.inventory.meta.Damageable;
@ -109,12 +108,10 @@ public class ItemAction extends NPCShopAction {
if (!(entity instanceof InventoryHolder)) if (!(entity instanceof InventoryHolder))
return 0; return 0;
Inventory source = ((InventoryHolder) entity).getInventory();
List<Integer> req = items.stream().map(ItemStack::getAmount).collect(Collectors.toList()); List<Integer> req = items.stream().map(ItemStack::getAmount).collect(Collectors.toList());
List<Integer> has = items.stream().map(i -> 0).collect(Collectors.toList()); List<Integer> has = items.stream().map(i -> 0).collect(Collectors.toList());
ItemStack[] contents = source.getContents(); for (int i = 0; i < inventory.length; i++) {
for (int i = 0; i < contents.length; i++) { ItemStack toMatch = inventory[i];
ItemStack toMatch = contents[i];
if (toMatch == null || toMatch.getType() == Material.AIR || tooDamaged(toMatch)) if (toMatch == null || toMatch.getType() == Material.AIR || tooDamaged(toMatch))
continue; continue;
@ -126,9 +123,9 @@ public class ItemAction extends NPCShopAction {
int remaining = req.get(j); int remaining = req.get(j);
int taken = toMatch.getAmount() > remaining ? remaining : toMatch.getAmount(); int taken = toMatch.getAmount() > remaining ? remaining : toMatch.getAmount();
has.set(j, has.get(j) + taken); has.set(j, has.get(j) + taken);
if (toMatch.getAmount() - taken <= 0) { if (toMatch.getAmount() - taken <= 0)
break; break;
}
toMatch.setAmount(toMatch.getAmount() - taken); toMatch.setAmount(toMatch.getAmount() - taken);
} }
} }
@ -140,10 +137,9 @@ public class ItemAction extends NPCShopAction {
public Transaction grant(Entity entity, ItemStack[] inventory, int repeats) { public Transaction grant(Entity entity, ItemStack[] inventory, int repeats) {
if (!(entity instanceof InventoryHolder)) if (!(entity instanceof InventoryHolder))
return Transaction.fail(); return Transaction.fail();
Inventory source = ((InventoryHolder) entity).getInventory();
return Transaction.create(() -> { return Transaction.create(() -> {
int free = 0; int free = 0;
for (ItemStack stack : source.getContents()) { for (ItemStack stack : inventory) {
if (stack == null || stack.getType() == Material.AIR) { if (stack == null || stack.getType() == Material.AIR) {
free++; free++;
continue; continue;
@ -152,11 +148,26 @@ public class ItemAction extends NPCShopAction {
return free >= items.size() * repeats; return free >= items.size() * repeats;
}, () -> { }, () -> {
for (int i = 0; i < repeats; i++) { for (int i = 0; i < repeats; i++) {
source.addItem(items.stream().map(ItemStack::clone).toArray(ItemStack[]::new)); List<ItemStack> toAdd = items.stream().map(ItemStack::clone).collect(Collectors.toList());
for (int j = 0; j < inventory.length; j++) {
if (toAdd.isEmpty())
break;
if (inventory[j] == null || inventory[j].getType() == Material.AIR) {
inventory[j] = toAdd.remove(toAdd.size() - 1);
}
}
} }
}, () -> { }, () -> {
for (int i = 0; i < repeats; i++) { for (int i = 0; i < repeats; i++) {
source.removeItem(items.stream().map(ItemStack::clone).toArray(ItemStack[]::new)); List<ItemStack> toRemove = items.stream().map(ItemStack::clone).collect(Collectors.toList());
for (int j = 0; j < inventory.length; j++) {
if (toRemove.isEmpty())
break;
if (toRemove.get(toRemove.size() - 1).equals(inventory[j])) {
inventory[j] = null;
toRemove.remove(toRemove.size() - 1);
}
}
} }
}); });
} }
@ -184,9 +195,10 @@ public class ItemAction extends NPCShopAction {
if (i < parts.length - 1) { if (i < parts.length - 1) {
if (!(acc instanceof CompoundTag) || !(cmp instanceof CompoundTag)) if (!(acc instanceof CompoundTag) || !(cmp instanceof CompoundTag))
return false; return false;
if (parts[i].equals(acc.getName()) && acc.getName().equals(cmp.getName())) {
if (parts[i].equals(acc.getName()) && acc.getName().equals(cmp.getName()))
continue; continue;
}
acc = ((CompoundTag) acc).getValue().get(parts[i]); acc = ((CompoundTag) acc).getValue().get(parts[i]);
cmp = ((CompoundTag) cmp).getValue().get(parts[i]); cmp = ((CompoundTag) cmp).getValue().get(parts[i]);
continue; continue;
@ -207,8 +219,25 @@ public class ItemAction extends NPCShopAction {
return Transaction.create(() -> containsItems(inventory, repeats, false), () -> { return Transaction.create(() -> containsItems(inventory, repeats, false), () -> {
containsItems(inventory, repeats, true); containsItems(inventory, repeats, true);
}, () -> { }, () -> {
((InventoryHolder) entity).getInventory() for (ItemStack item : items.stream().map(ItemStack::clone).toArray(ItemStack[]::new)) {
.addItem(items.stream().map(ItemStack::clone).toArray(ItemStack[]::new)); for (int i = 0; i < inventory.length; i++) {
ItemStack stack = inventory[i];
if (stack == null || stack.getType() == Material.AIR) {
inventory[i] = item;
break;
}
if (stack.getMaxStackSize() > stack.getAmount() && matches(stack, item)) {
int free = stack.getMaxStackSize() - stack.getAmount();
if (item.getAmount() > free) {
item.setAmount(item.getAmount() - free);
stack.setAmount(stack.getMaxStackSize());
} else {
stack.setAmount(stack.getAmount() + item.getAmount());
break;
}
}
}
}
}); });
} }