Craft repair cancelled

This commit is contained in:
Roch 2022-10-25 17:22:36 +02:00
parent b7ba87b58d
commit bc59bb1788

View File

@ -1,11 +1,12 @@
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.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,117 +16,160 @@ 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.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import java.util.Arrays;
import java.util.Objects;
public class ItemListener implements Listener {
// Aye
public ItemListener() {
//RFG//MMOItems.log(" §b>§a>§e> §7Registering Listeners");
// Aye
public ItemListener() {
//RFG//MMOItems.log(" §b>§a>§e> §7Registering Listeners");
// Register Reforger Listeners
Bukkit.getPluginManager().registerEvents(new RFGKeepName(), MMOItems.plugin);
Bukkit.getPluginManager().registerEvents(new RFGKeepLore(), MMOItems.plugin);
Bukkit.getPluginManager().registerEvents(new RFGKeepEnchantments(), MMOItems.plugin);
Bukkit.getPluginManager().registerEvents(new RFGKeepExternalSH(), MMOItems.plugin);
Bukkit.getPluginManager().registerEvents(new RFGKeepGems(), MMOItems.plugin);
Bukkit.getPluginManager().registerEvents(new RFGKeepModifications(), MMOItems.plugin);
Bukkit.getPluginManager().registerEvents(new RFGKeepSoulbound(), MMOItems.plugin);
Bukkit.getPluginManager().registerEvents(new RFGKeepUpgrades(), MMOItems.plugin);
Bukkit.getPluginManager().registerEvents(new RFGKeepRNG(), MMOItems.plugin);
// Register Reforger Listeners
Bukkit.getPluginManager().registerEvents(new RFGKeepName(), MMOItems.plugin);
Bukkit.getPluginManager().registerEvents(new RFGKeepLore(), MMOItems.plugin);
Bukkit.getPluginManager().registerEvents(new RFGKeepEnchantments(), MMOItems.plugin);
Bukkit.getPluginManager().registerEvents(new RFGKeepExternalSH(), MMOItems.plugin);
Bukkit.getPluginManager().registerEvents(new RFGKeepGems(), MMOItems.plugin);
Bukkit.getPluginManager().registerEvents(new RFGKeepModifications(), MMOItems.plugin);
Bukkit.getPluginManager().registerEvents(new RFGKeepSoulbound(), MMOItems.plugin);
Bukkit.getPluginManager().registerEvents(new RFGKeepUpgrades(), MMOItems.plugin);
Bukkit.getPluginManager().registerEvents(new RFGKeepRNG(), MMOItems.plugin);
// Amount ones
Bukkit.getPluginManager().registerEvents(new RFGKeepDurability(), MMOItems.plugin);
Bukkit.getPluginManager().registerEvents(new RFFKeepAmount(), MMOItems.plugin);
Bukkit.getPluginManager().registerEvents(new RFFKeepSkins(), MMOItems.plugin);
// Amount ones
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);
@EventHandler(ignoreCancelled = true)
private void itemPickup(EntityPickupItemEvent e) {
if (!e.getEntity().getType().equals(EntityType.PLAYER)) return;
inv.setResult(air);
Bukkit.getScheduler().runTaskLater(MMOItems.plugin, () -> {
boolean repairDisabled = Arrays.stream(inv.getMatrix())
.filter(Objects::nonNull)
.filter(itemStack -> !itemStack.getType().isAir())
.allMatch(itemStack -> {
final NBTItem nbtItem = NBTItem.get(itemStack);
return nbtItem.hasTag("MMOITEMS_DISABLE_REPAIRING") && nbtItem.getBoolean("MMOITEMS_DISABLE_REPAIRING");
});
if (repairDisabled) {
inv.setItem(0, air);
player.updateInventory();
System.out.println("YEAH");
return;
}
System.out.println("NOPE");
// TODO: repair
}, 1);
}
ItemStack newItem = modifyItem(e.getItem().getItemStack(), (Player) e.getEntity(), "pickup");
if (newItem != null) e.getItem().setItemStack(newItem);
}
@EventHandler(ignoreCancelled = true)
private void itemPickup(EntityPickupItemEvent e) {
if (!e.getEntity().getType().equals(EntityType.PLAYER)) return;
@EventHandler(ignoreCancelled = true)
private void itemCraft(CraftItemEvent e) {
if(!(e.getWhoClicked() instanceof Player)) return;
ItemStack newItem = modifyItem(e.getCurrentItem(), (Player) e.getWhoClicked(), "craft");
if (newItem != null) e.setCurrentItem(newItem);
}
ItemStack newItem = modifyItem(e.getItem().getItemStack(), (Player) e.getEntity(), "pickup");
if (newItem != null) e.getItem().setItemStack(newItem);
}
@EventHandler(ignoreCancelled = true)
private void inventoryMove(InventoryClickEvent e) {
if (e.getInventory().getType() != InventoryType.CRAFTING || !(e.getWhoClicked() instanceof Player)) return;
ItemStack newItem = modifyItem(e.getCurrentItem(), (Player) e.getWhoClicked(), "click");
if (newItem != null) e.setCurrentItem(newItem);
}
@EventHandler(ignoreCancelled = true)
private void itemCraft(CraftItemEvent e) {
if (!(e.getWhoClicked() instanceof Player)) return;
ItemStack newItem = modifyItem(e.getCurrentItem(), (Player) e.getWhoClicked(), "craft");
if (newItem != null) e.setCurrentItem(newItem);
}
@EventHandler(ignoreCancelled = true)
private void dropItem(PlayerDropItemEvent event) {
NBTItem nbt = NBTItem.get(event.getItemDrop().getItemStack());
if (!MMOItems.plugin.getConfig().getBoolean("soulbound.can-drop") && nbt.hasTag("MMOITEMS_SOULBOUND"))
event.setCancelled(true);
}
@EventHandler(ignoreCancelled = true)
private void inventoryMove(InventoryClickEvent e) {
if (e.getInventory().getType() != InventoryType.CRAFTING || !(e.getWhoClicked() instanceof Player)) return;
ItemStack newItem = modifyItem(e.getCurrentItem(), (Player) e.getWhoClicked(), "click");
if (newItem != null) e.setCurrentItem(newItem);
}
@EventHandler(priority = EventPriority.HIGH, ignoreCancelled = true)
public void playerJoin(PlayerJoinEvent event) {
Player player = event.getPlayer();
@EventHandler(ignoreCancelled = true)
private void dropItem(PlayerDropItemEvent event) {
NBTItem nbt = NBTItem.get(event.getItemDrop().getItemStack());
if (!MMOItems.plugin.getConfig().getBoolean("soulbound.can-drop") && nbt.hasTag("MMOITEMS_SOULBOUND"))
event.setCancelled(true);
}
ItemStack newItem = modifyItem(player.getEquipment().getHelmet(), player, "join");
if(newItem != null) player.getEquipment().setHelmet(newItem);
newItem = modifyItem(player.getEquipment().getChestplate(), player, "join");
if(newItem != null) player.getEquipment().setChestplate(newItem);
newItem = modifyItem(player.getEquipment().getLeggings(), player, "join");
if(newItem != null) player.getEquipment().setLeggings(newItem);
newItem = modifyItem(player.getEquipment().getBoots(), player, "join");
if(newItem != null) player.getEquipment().setBoots(newItem);
@EventHandler(priority = EventPriority.HIGH, ignoreCancelled = true)
public void playerJoin(PlayerJoinEvent event) {
Player player = event.getPlayer();
for (int j = 0; j < 9; j++) {
newItem = modifyItem(player.getInventory().getItem(j), player, "join");
if (newItem != null) player.getInventory().setItem(j, newItem);
}
ItemStack newItem = modifyItem(player.getEquipment().getHelmet(), player, "join");
if (newItem != null) player.getEquipment().setHelmet(newItem);
newItem = modifyItem(player.getEquipment().getChestplate(), player, "join");
if (newItem != null) player.getEquipment().setChestplate(newItem);
newItem = modifyItem(player.getEquipment().getLeggings(), player, "join");
if (newItem != null) player.getEquipment().setLeggings(newItem);
newItem = modifyItem(player.getEquipment().getBoots(), player, "join");
if (newItem != null) player.getEquipment().setBoots(newItem);
newItem = modifyItem(player.getEquipment().getItemInOffHand(), player, "join");
if(newItem != null) player.getEquipment().setItemInOffHand(newItem);
}
for (int j = 0; j < 9; j++) {
newItem = modifyItem(player.getInventory().getItem(j), player, "join");
if (newItem != null) player.getInventory().setItem(j, newItem);
}
@Nullable private ItemStack modifyItem(@Nullable ItemStack stack, @NotNull Player player, @NotNull String reason) {
//RFG//MMOItems.log("§8Reforge §cMOD§7 Modifying " + SilentNumbers.getItemName(stack) + " §7due to§3 " + reason);
newItem = modifyItem(player.getEquipment().getItemInOffHand(), player, "join");
if (newItem != null) player.getEquipment().setItemInOffHand(newItem);
}
// Sleep on metaless stacks
if (stack == null) { return null; }
if (!stack.hasItemMeta()) { return null; }
@Nullable
private ItemStack modifyItem(@Nullable ItemStack stack, @NotNull Player player, @NotNull String reason) {
//RFG//MMOItems.log("§8Reforge §cMOD§7 Modifying " + SilentNumbers.getItemName(stack) + " §7due to§3 " + reason);
// Create a reforger to look at it
MMOItemReforger mod = new MMOItemReforger(stack);
// Sleep on metaless stacks
if (stack == null) {
return null;
}
if (!stack.hasItemMeta()) {
return null;
}
// Shouldn't update? I sleep
if (!mod.shouldReforge(reason)) { return null; }
// Create a reforger to look at it
MMOItemReforger mod = new MMOItemReforger(stack);
// All right update then
mod.setPlayer(player);
if (!mod.reforge(MMOItems.plugin.getLanguage().revisionOptions)) {
// Shouldn't update? I sleep
if (!mod.shouldReforge(reason)) {
return null;
}
return null; }
// All right update then
mod.setPlayer(player);
if (!mod.reforge(MMOItems.plugin.getLanguage().revisionOptions)) {
// Drop all those items
for (ItemStack drop : player.getInventory().addItem(
mod.getReforgingOutput().toArray(new ItemStack[0])).values()) {
return null;
}
// Not air right
if (SilentNumbers.isAir(drop)) { continue; }
// Drop all those items
for (ItemStack drop : player.getInventory().addItem(
mod.getReforgingOutput().toArray(new ItemStack[0])).values()) {
// Drop to the world
player.getWorld().dropItem(player.getLocation(), drop); }
// Not air right
if (SilentNumbers.isAir(drop)) {
continue;
}
// That's it
return mod.getResult();
}
// Drop to the world
player.getWorld().dropItem(player.getLocation(), drop);
}
// That's it
return mod.getResult();
}
}