Merge remote-tracking branch 'origin/master'

# Conflicts:
#	MMOItems-Dist/src/main/java/net/Indyuce/mmoitems/listener/ItemListener.java
This commit is contained in:
Indyuce 2022-11-03 18:11:32 +01:00
commit b62b303d5c
6 changed files with 135 additions and 40 deletions

View File

@ -169,7 +169,7 @@ public class Elements extends ItemStat<RandomElementListData, ElementListData> i
// Try to find every existing element
for (Element element : Element.values())
for (ElementStatType statType : ElementStatType.values()) {
final String path = "MMOITEMS_" + element.getName() + "_" + statType.name();
final String path = "MMOITEMS_" + statType.getConcatenatedTagPath(element);
ItemTag tag = ItemTag.getTagAtPath(path, storedTags);
if (tag != null)
elements.setStat(element, statType, (double) tag.getValue());

View File

@ -62,6 +62,6 @@ public class RepairPower extends DoubleStat implements ConsumableItemInteraction
}
// vanilla durability
return RepairUtils.repairPower(playerData, target, consumable, repairPower);
return RepairUtils.repairVanillaItem(playerData, target, consumable, -1, repairPower);
}
}

View File

@ -25,7 +25,7 @@ import org.jetbrains.annotations.Nullable;
public class RepairPowerPercent extends DoubleStat implements ConsumableItemInteraction {
public RepairPowerPercent() {
super("REPAIR_PERCENT", Material.DAMAGED_ANVIL, "Repair Percentage",
new String[] { "The percentage of total durability to repair", "When dropped onto an item." },
new String[]{"The percentage of total durability to repair", "When dropped onto an item."},
new String[]{"consumable"});
}
@ -39,7 +39,7 @@ public class RepairPowerPercent extends DoubleStat implements ConsumableItemInte
Player player = playerData.getPlayer();
final String type = "MMOITEMS_REPAIR_TYPE";
if((target.hasTag(type) || consumable.getNBTItem().hasTag(type)) &&
if ((target.hasTag(type) || consumable.getNBTItem().hasTag(type)) &&
!target.getString(type).equals(consumable.getNBTItem().getString(type))) {
Message.UNABLE_TO_REPAIR.format(ChatColor.RED, "#item#", MMOUtils.getDisplayName(target.getItem())).send(player);
player.getPlayer().playSound(player.getPlayer().getLocation(), Sound.ENTITY_VILLAGER_NO, 1, 1.5f);
@ -54,9 +54,10 @@ public class RepairPowerPercent extends DoubleStat implements ConsumableItemInte
DurabilityItem durItem = new DurabilityItem(player, target);
if (durItem.getDurability() < durItem.getMaxDurability()) {
target.getItem().setItemMeta(durItem.addDurability((int) (durItem.getMaxDurability()*called.getRepairedPercent())).toItem().getItemMeta());
int repaired = (int) (durItem.getMaxDurability() * repairPower / 100);
target.getItem().setItemMeta(durItem.addDurability(repaired).toItem().getItemMeta());
Message.REPAIRED_ITEM
.format(ChatColor.YELLOW, "#item#", MMOUtils.getDisplayName(target.getItem()), "#amount#", "" + called.getRepairedPercent())
.format(ChatColor.YELLOW, "#item#", MMOUtils.getDisplayName(target.getItem()), "#amount#", String.valueOf(repaired))
.send(player);
CustomSoundListener.playConsumableSound(consumable.getItem(), player);
}
@ -64,6 +65,6 @@ public class RepairPowerPercent extends DoubleStat implements ConsumableItemInte
}
// vanilla durability
return RepairUtils.repairPower(playerData, target, consumable, repairPower);
return RepairUtils.repairVanillaItem(playerData, target, consumable, repairPower, -1);
}
}

View File

@ -33,7 +33,8 @@ public class ElementListData implements Mergeable {
public void merge(StatData data) {
Validate.isTrue(data instanceof ElementListData, "Cannot merge two different stat data types");
ElementListData extra = (ElementListData) data;
extra.stats.forEach((key, value) -> stats.put(key, value));
//Includes old values if any, fixes stacking of element double values I believe - Kilo
extra.stats.forEach((key, value) -> stats.put(key, value + stats.getOrDefault(key,0.0)));
}
@NotNull

View File

@ -10,7 +10,6 @@ import org.bukkit.Bukkit;
import org.bukkit.ChatColor;
import org.bukkit.entity.Player;
import org.bukkit.inventory.meta.Damageable;
import org.bukkit.inventory.meta.ItemMeta;
import org.jetbrains.annotations.NotNull;
/**
@ -25,27 +24,40 @@ public class RepairUtils {
throw new IllegalStateException("Utility class");
}
public static boolean repairPower(@NotNull PlayerData playerData, @NotNull NBTItem target, @NotNull Consumable consumable, double repairPower) {
final Player player = playerData.getPlayer();
if (!target.getBoolean("Unbreakable") && target.getItem().hasItemMeta() && target.getItem().getItemMeta() instanceof Damageable
&& ((Damageable) target.getItem().getItemMeta()).getDamage() > 0) {
RepairItemEvent called = new RepairItemEvent(playerData, consumable.getMMOItem(), target, repairPower);
Bukkit.getPluginManager().callEvent(called);
if (called.isCancelled())
return false;
ItemMeta meta = target.getItem().getItemMeta();
((Damageable) meta).setDamage(Math.max(0, ((Damageable) meta).getDamage() - called.getRepaired()));
target.getItem().setItemMeta(meta);
Message.REPAIRED_ITEM.format(ChatColor.YELLOW,
"#item#",
MMOUtils.getDisplayName(target.getItem()),
"#amount#",
String.valueOf(called.getRepaired() == -1 ? called.getRepairedPercent() : called.getRepaired()))
.send(player);
CustomSoundListener.playConsumableSound(consumable.getItem(), player);
return true;
}
return false;
/**
* Repairs a vanilla item.
* It will not work with MMOItems items.
*
* @param playerData The player data.
* @param target The target item.
* @param consumable The consumable item.
* @param repairPercentage The repair percentage. (set to -1 to disable)
* @param repairUses The repair uses. (set to -1 to disable)
* @return True if the item was repaired.
*/
public static boolean repairVanillaItem(@NotNull PlayerData playerData, @NotNull NBTItem target, @NotNull Consumable consumable, double repairPercentage, int repairUses) {
final Player player = playerData.getPlayer();
final boolean percentage = repairPercentage > 0;
if (target.getBoolean("Unbreakable")
|| !target.getItem().hasItemMeta()
|| !(target.getItem().getItemMeta() instanceof Damageable meta)
|| ((Damageable) target.getItem().getItemMeta()).getDamage() <= 0)
return false;
RepairItemEvent called = percentage ? new RepairItemEvent(playerData, consumable.getMMOItem(), target, repairPercentage) : new RepairItemEvent(playerData, consumable.getMMOItem(), target, repairUses);
Bukkit.getPluginManager().callEvent(called);
if (called.isCancelled())
return false;
int repaired = percentage ? (int) ((repairPercentage / 100) * target.getItem().getType().getMaxDurability()) : repairUses;
meta.setDamage(Math.max(0, meta.getDamage() - repaired));
target.getItem().setItemMeta(meta);
Message.REPAIRED_ITEM.format(ChatColor.YELLOW,
"#item#",
MMOUtils.getDisplayName(target.getItem()),
"#amount#",
String.valueOf(repaired))
.send(player);
CustomSoundListener.playConsumableSound(consumable.getItem(), player);
return true;
}
}

View File

@ -1,11 +1,13 @@
package net.Indyuce.mmoitems.listener;
import io.lumine.mythic.lib.api.item.NBTItem;
import io.lumine.mythic.lib.api.util.ui.SilentNumbers;
import net.Indyuce.mmoitems.MMOItems;
import net.Indyuce.mmoitems.api.interaction.util.DurabilityItem;
import net.Indyuce.mmoitems.api.util.MMOItemReforger;
import io.lumine.mythic.lib.api.item.NBTItem;
import net.Indyuce.mmoitems.listener.reforging.*;
import org.bukkit.Bukkit;
import org.bukkit.Material;
import org.bukkit.entity.EntityType;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
@ -15,13 +17,19 @@ import org.bukkit.event.entity.EntityPickupItemEvent;
import org.bukkit.event.inventory.CraftItemEvent;
import org.bukkit.event.inventory.InventoryClickEvent;
import org.bukkit.event.inventory.InventoryType;
import org.bukkit.event.inventory.PrepareItemCraftEvent;
import org.bukkit.event.player.PlayerDropItemEvent;
import org.bukkit.event.player.PlayerJoinEvent;
import org.bukkit.inventory.CraftingInventory;
import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.meta.Damageable;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import java.util.Arrays;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;
public class ItemListener implements Listener {
@ -44,7 +52,67 @@ public class ItemListener implements Listener {
Bukkit.getPluginManager().registerEvents(new RFGKeepDurability(), MMOItems.plugin);
Bukkit.getPluginManager().registerEvents(new RFFKeepAmount(), MMOItems.plugin);
Bukkit.getPluginManager().registerEvents(new RFFKeepSkins(), MMOItems.plugin);
}
@EventHandler
private void onItemCraftRepair(PrepareItemCraftEvent e) {
if (!(e.getView().getPlayer() instanceof Player) || !e.isRepair())
return;
final Player player = (Player) e.getView().getPlayer();
final CraftingInventory inv = e.getInventory();
final ItemStack air = new ItemStack(Material.AIR);
final ItemStack originalResult = inv.getResult();
inv.setResult(air);
Bukkit.getScheduler().runTaskLater(MMOItems.plugin, () -> {
List<ItemStack> items = Arrays.stream(inv.getMatrix())
.filter(Objects::nonNull)
.filter(itemStack -> !itemStack.getType().isAir())
.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()
.allMatch(itemStack -> {
final NBTItem nbtItem = NBTItem.get(itemStack);
return nbtItem.hasTag("MMOITEMS_DISABLE_REPAIRING") && nbtItem.getBoolean("MMOITEMS_DISABLE_REPAIRING");
});
// Does the item have a MMO durability tag?
boolean hasCustomDurability = items.stream().allMatch(itemStack -> new DurabilityItem(player, itemStack).isValid());
if (repairDisabled)
inv.setResult(air);
else if (hasCustomDurability) {
DurabilityItem durabilityItem = new DurabilityItem(player, items.get(0));
int summedDurability = items.stream()
.map(itemStack -> new DurabilityItem(player, itemStack))
.map(DurabilityItem::getDurability)
.reduce(0, Integer::sum);
int finalDurability = durabilityItem.getMaxDurability() - Math.min(durabilityItem.getMaxDurability(), summedDurability);
if (finalDurability > 0)
durabilityItem.addDurability(finalDurability);
inv.setResult(durabilityItem.toItem());
}
player.updateInventory();
}, 1);
}
@EventHandler(ignoreCancelled = true)
@ -103,27 +171,40 @@ public class ItemListener implements Listener {
//RFG//MMOItems.log("§8Reforge §cMOD§7 Modifying " + SilentNumbers.getItemName(stack) + " §7due to§3 " + reason);
// Sleep on metaless stacks
if (stack == null || !stack.hasItemMeta())
if (stack == null) {
return null;
}
if (!stack.hasItemMeta()) {
return null;
}
// Create a reforger to look at it
MMOItemReforger mod = new MMOItemReforger(stack);
// Shouldn't update? I sleep
if (!mod.shouldReforge(reason)) return null;
if (!mod.shouldReforge(reason)) {
return null;
}
// All right update then
mod.setPlayer(player);
if (!mod.reforge(MMOItems.plugin.getLanguage().revisionOptions))
if (!mod.reforge(MMOItems.plugin.getLanguage().revisionOptions)) {
return null;
}
// Drop all those items
player.getInventory()
.addItem(mod.getReforgingOutput().toArray(new ItemStack[0]))
.values()
.stream()
.filter(drop -> !SilentNumbers.isAir(drop))
.forEach(drop -> player.getWorld().dropItem(player.getLocation(), drop));
for (ItemStack drop : player.getInventory().addItem(
mod.getReforgingOutput().toArray(new ItemStack[0])).values()) {
// Not air right
if (SilentNumbers.isAir(drop)) {
continue;
}
// Drop to the world
player.getWorld().dropItem(player.getLocation(), drop);
}
// That's it
return mod.getResult();