MMOItems id check, durability error fixed and cancel on mc+mmo repair

This commit is contained in:
Roch Blonndiaux 2022-10-27 16:34:47 +02:00
parent c7d38fb88e
commit f3af637d79

View File

@ -61,42 +61,57 @@ public class ItemListener implements Listener {
final Player player = (Player) e.getView().getPlayer(); final Player player = (Player) e.getView().getPlayer();
final CraftingInventory inv = e.getInventory(); final CraftingInventory inv = e.getInventory();
final ItemStack air = new ItemStack(Material.AIR); final ItemStack air = new ItemStack(Material.AIR);
final ItemStack originalResult = inv.getResult();
//inv.setResult(air); inv.setResult(air);
Bukkit.getScheduler().runTaskLater(MMOItems.plugin, () -> { Bukkit.getScheduler().runTaskLater(MMOItems.plugin, () -> {
List<ItemStack> items = Arrays.stream(inv.getMatrix()) List<ItemStack> items = Arrays.stream(inv.getMatrix())
.filter(Objects::nonNull) .filter(Objects::nonNull)
.filter(itemStack -> !itemStack.getType().isAir()) .filter(itemStack -> !itemStack.getType().isAir())
.collect(Collectors.toList()); .collect(Collectors.toList());
long mmoItemsCount = items.stream()
.filter(itemStack -> NBTItem.get(itemStack).hasTag("MMOITEMS_ITEM_ID"))
.count();
// If there are no MMOItems in the crafting matrix, do nothing
if (mmoItemsCount == 0) {
inv.setResult(originalResult);
player.updateInventory();
return;
}
// If both items are not MMO items or if they don't have the same id return
if (mmoItemsCount == 1
|| !NBTItem.get(items.get(0)).getString("MMOITEMS_ITEM_ID").equals(NBTItem.get(items.get(1)).getString("MMOITEMS_ITEM_ID"))) {
inv.setResult(air);
player.updateInventory();
return;
}
inv.setResult(originalResult);
// Is repair disabled in config?
boolean repairDisabled = items.stream() boolean repairDisabled = items.stream()
.allMatch(itemStack -> { .allMatch(itemStack -> {
final NBTItem nbtItem = NBTItem.get(itemStack); final NBTItem nbtItem = NBTItem.get(itemStack);
return nbtItem.hasTag("MMOITEMS_DISABLE_REPAIRING") && nbtItem.getBoolean("MMOITEMS_DISABLE_REPAIRING"); return nbtItem.hasTag("MMOITEMS_DISABLE_REPAIRING") && nbtItem.getBoolean("MMOITEMS_DISABLE_REPAIRING");
}); });
boolean repairPartiallyDisabled = items.stream()
.anyMatch(itemStack -> { // Does the item have a MMO durability tag?
final NBTItem nbtItem = NBTItem.get(itemStack);
return nbtItem.hasTag("MMOITEMS_DISABLE_REPAIRING") && nbtItem.getBoolean("MMOITEMS_DISABLE_REPAIRING");
});
boolean hasCustomDurability = items.stream().allMatch(itemStack -> new DurabilityItem(player, itemStack).isValid()); boolean hasCustomDurability = items.stream().allMatch(itemStack -> new DurabilityItem(player, itemStack).isValid());
ItemStack result = null; if (repairDisabled)
if (repairDisabled || repairPartiallyDisabled) inv.setResult(air);
result = air;
else if (hasCustomDurability) { else if (hasCustomDurability) {
result = items.get(0); DurabilityItem durabilityItem = new DurabilityItem(player, items.get(0));
DurabilityItem durabilityItem = new DurabilityItem(player, result); int summedDurability = items.stream()
int durability = items.stream()
.map(itemStack -> new DurabilityItem(player, itemStack)) .map(itemStack -> new DurabilityItem(player, itemStack))
.map(DurabilityItem::getDurability) .map(DurabilityItem::getDurability)
.reduce(0, Integer::sum); .reduce(0, Integer::sum);
durabilityItem.addDurability(durabilityItem.getMaxDurability() - Math.min(durabilityItem.getMaxDurability(), durability)); int finalDurability = durabilityItem.getMaxDurability() - Math.min(durabilityItem.getMaxDurability(), summedDurability);
result = durabilityItem.toItem(); if (finalDurability > 0)
durabilityItem.addDurability(finalDurability);
inv.setResult(durabilityItem.toItem());
} }
if (repairDisabled || hasCustomDurability) {
inv.setResult(result);
player.updateInventory(); player.updateInventory();
}
}, 1); }, 1);
} }