mirror of
https://github.com/CitizensDev/Citizens2.git
synced 2024-11-22 02:25:57 +01:00
Rewrite itemaction to use the provided item array
This commit is contained in:
parent
40a60fbc8f
commit
c683b752a5
@ -10,7 +10,6 @@ import org.bukkit.ChatColor;
|
||||
import org.bukkit.Material;
|
||||
import org.bukkit.entity.Entity;
|
||||
import org.bukkit.entity.HumanEntity;
|
||||
import org.bukkit.inventory.Inventory;
|
||||
import org.bukkit.inventory.InventoryHolder;
|
||||
import org.bukkit.inventory.ItemStack;
|
||||
import org.bukkit.inventory.meta.Damageable;
|
||||
@ -109,12 +108,10 @@ public class ItemAction extends NPCShopAction {
|
||||
if (!(entity instanceof InventoryHolder))
|
||||
return 0;
|
||||
|
||||
Inventory source = ((InventoryHolder) entity).getInventory();
|
||||
List<Integer> req = items.stream().map(ItemStack::getAmount).collect(Collectors.toList());
|
||||
List<Integer> has = items.stream().map(i -> 0).collect(Collectors.toList());
|
||||
ItemStack[] contents = source.getContents();
|
||||
for (int i = 0; i < contents.length; i++) {
|
||||
ItemStack toMatch = contents[i];
|
||||
for (int i = 0; i < inventory.length; i++) {
|
||||
ItemStack toMatch = inventory[i];
|
||||
if (toMatch == null || toMatch.getType() == Material.AIR || tooDamaged(toMatch))
|
||||
continue;
|
||||
|
||||
@ -126,9 +123,9 @@ public class ItemAction extends NPCShopAction {
|
||||
int remaining = req.get(j);
|
||||
int taken = toMatch.getAmount() > remaining ? remaining : toMatch.getAmount();
|
||||
has.set(j, has.get(j) + taken);
|
||||
if (toMatch.getAmount() - taken <= 0) {
|
||||
if (toMatch.getAmount() - taken <= 0)
|
||||
break;
|
||||
}
|
||||
|
||||
toMatch.setAmount(toMatch.getAmount() - taken);
|
||||
}
|
||||
}
|
||||
@ -140,10 +137,9 @@ public class ItemAction extends NPCShopAction {
|
||||
public Transaction grant(Entity entity, ItemStack[] inventory, int repeats) {
|
||||
if (!(entity instanceof InventoryHolder))
|
||||
return Transaction.fail();
|
||||
Inventory source = ((InventoryHolder) entity).getInventory();
|
||||
return Transaction.create(() -> {
|
||||
int free = 0;
|
||||
for (ItemStack stack : source.getContents()) {
|
||||
for (ItemStack stack : inventory) {
|
||||
if (stack == null || stack.getType() == Material.AIR) {
|
||||
free++;
|
||||
continue;
|
||||
@ -152,11 +148,26 @@ public class ItemAction extends NPCShopAction {
|
||||
return free >= items.size() * repeats;
|
||||
}, () -> {
|
||||
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++) {
|
||||
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 (!(acc instanceof CompoundTag) || !(cmp instanceof CompoundTag))
|
||||
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;
|
||||
}
|
||||
|
||||
acc = ((CompoundTag) acc).getValue().get(parts[i]);
|
||||
cmp = ((CompoundTag) cmp).getValue().get(parts[i]);
|
||||
continue;
|
||||
@ -207,8 +219,25 @@ public class ItemAction extends NPCShopAction {
|
||||
return Transaction.create(() -> containsItems(inventory, repeats, false), () -> {
|
||||
containsItems(inventory, repeats, true);
|
||||
}, () -> {
|
||||
((InventoryHolder) entity).getInventory()
|
||||
.addItem(items.stream().map(ItemStack::clone).toArray(ItemStack[]::new));
|
||||
for (ItemStack item : 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;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user