More item tweaks

This commit is contained in:
fullwall 2023-06-19 02:27:13 +08:00
parent ad442e4bd3
commit c001b65988
2 changed files with 25 additions and 21 deletions

View File

@ -348,15 +348,17 @@ public class Citizens extends JavaPlugin implements CitizensPlugin {
} }
public void onDependentPluginDisable() { public void onDependentPluginDisable() {
storeNPCs(false); if (enabled) {
saveOnDisable = false; storeNPCs(false);
saveOnDisable = false;
}
} }
@Override @Override
public void onDisable() { public void onDisable() {
if (!enabled) { if (!enabled)
return; return;
}
Bukkit.getPluginManager().callEvent(new CitizensDisableEvent()); Bukkit.getPluginManager().callEvent(new CitizensDisableEvent());
Editor.leaveAll(); Editor.leaveAll();
despawnNPCs(saveOnDisable); despawnNPCs(saveOnDisable);
@ -388,7 +390,8 @@ public class Citizens extends JavaPlugin implements CitizensPlugin {
e.printStackTrace(); e.printStackTrace();
} }
Messaging.severeTr(Messages.CITIZENS_INCOMPATIBLE, getDescription().getVersion(), mcVersion); Messaging.severeTr(Messages.CITIZENS_INCOMPATIBLE, getDescription().getVersion(), mcVersion);
enabled = true; NMS.shutdown();
CitizensAPI.shutdown();
Bukkit.getPluginManager().disablePlugin(this); Bukkit.getPluginManager().disablePlugin(this);
return; return;
} }
@ -626,6 +629,7 @@ public class Citizens extends JavaPlugin implements CitizensPlugin {
} }
} }
saves.loadInto(npcRegistry); saves.loadInto(npcRegistry);
shops.load(); shops.load();

View File

@ -2,7 +2,6 @@ package net.citizensnpcs.trait.shop;
import java.util.Arrays; import java.util.Arrays;
import java.util.List; import java.util.List;
import java.util.function.BiFunction;
import java.util.function.Consumer; import java.util.function.Consumer;
import java.util.stream.Collectors; import java.util.stream.Collectors;
import java.util.stream.IntStream; import java.util.stream.IntStream;
@ -52,7 +51,7 @@ public class ItemAction extends NPCShopAction {
this.items = items; this.items = items;
} }
private boolean containsItems(Inventory source, int repeats, BiFunction<ItemStack, Integer, ItemStack> filter) { private boolean containsItems(Inventory source, int repeats, boolean modify) {
List<Integer> req = items.stream().map(i -> i.getAmount() * repeats).collect(Collectors.toList()); List<Integer> req = items.stream().map(i -> i.getAmount() * repeats).collect(Collectors.toList());
ItemStack[] contents = source.getContents(); ItemStack[] contents = source.getContents();
for (int i = 0; i < contents.length; i++) { for (int i = 0; i < contents.length; i++) {
@ -61,6 +60,7 @@ public class ItemAction extends NPCShopAction {
continue; continue;
if (tooDamaged(toMatch)) if (tooDamaged(toMatch))
continue; continue;
toMatch = toMatch.clone();
for (int j = 0; j < items.size(); j++) { for (int j = 0; j < items.size(); j++) {
if (toMatch == null) if (toMatch == null)
break; break;
@ -70,14 +70,21 @@ 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();
ItemStack res = filter.apply(toMatch, taken);
if (res == null) { if (toMatch.getAmount() == taken) {
source.clear(i); toMatch = null;
} else { } else {
source.setItem(i, res); toMatch.setAmount(toMatch.getAmount() - taken);
}
if (modify) {
if (toMatch == null) {
source.clear(i);
} else {
source.setItem(i, toMatch.clone());
}
} }
req.set(j, remaining - taken); req.set(j, remaining - taken);
toMatch = res;
} }
} }
return req.stream().collect(Collectors.summingInt(n -> n)) <= 0; return req.stream().collect(Collectors.summingInt(n -> n)) <= 0;
@ -196,16 +203,9 @@ public class ItemAction extends NPCShopAction {
return Transaction.fail(); return Transaction.fail();
Inventory source = ((InventoryHolder) entity).getInventory(); Inventory source = ((InventoryHolder) entity).getInventory();
return Transaction.create(() -> { return Transaction.create(() -> {
return containsItems(source, repeats, (stack, taken) -> stack); return containsItems(source, repeats, false);
}, () -> { }, () -> {
containsItems(source, repeats, (stack, taken) -> { containsItems(source, repeats, true);
if (stack.getAmount() == taken) {
return null;
} else {
stack.setAmount(stack.getAmount() - taken);
return stack;
}
});
}, () -> { }, () -> {
source.addItem(items.stream().map(ItemStack::clone).toArray(ItemStack[]::new)); source.addItem(items.stream().map(ItemStack::clone).toArray(ItemStack[]::new));
}); });