fixed unidentified item interactions

This commit is contained in:
Indyuce 2021-12-20 13:18:01 +01:00
parent 60d0065706
commit f1d529a6c7
2 changed files with 54 additions and 41 deletions

View File

@ -1,15 +1,17 @@
package net.Indyuce.mmoitems.listener; package net.Indyuce.mmoitems.listener;
import net.Indyuce.mmoitems.MMOItems;
import io.lumine.mythic.lib.MythicLib; import io.lumine.mythic.lib.MythicLib;
import io.lumine.mythic.lib.api.item.NBTItem; import io.lumine.mythic.lib.api.item.NBTItem;
import net.Indyuce.mmoitems.MMOItems;
import net.Indyuce.mmoitems.api.interaction.util.DurabilityItem; import net.Indyuce.mmoitems.api.interaction.util.DurabilityItem;
import org.bukkit.Keyed; import org.bukkit.Keyed;
import org.bukkit.entity.ArmorStand; import org.bukkit.entity.ArmorStand;
import org.bukkit.entity.LivingEntity; import org.bukkit.entity.LivingEntity;
import org.bukkit.entity.Player; import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler; import org.bukkit.event.EventHandler;
import org.bukkit.event.EventPriority;
import org.bukkit.event.Listener; import org.bukkit.event.Listener;
import org.bukkit.event.block.BlockBreakEvent;
import org.bukkit.event.enchantment.EnchantItemEvent; import org.bukkit.event.enchantment.EnchantItemEvent;
import org.bukkit.event.entity.EntityDamageByEntityEvent; import org.bukkit.event.entity.EntityDamageByEntityEvent;
import org.bukkit.event.entity.EntityDamageEvent; import org.bukkit.event.entity.EntityDamageEvent;
@ -28,9 +30,8 @@ import org.bukkit.inventory.ItemStack;
public class DisableInteractions implements Listener { public class DisableInteractions implements Listener {
// anvils
@EventHandler @EventHandler
public void a(InventoryClickEvent event) { public void anvilInteractions(InventoryClickEvent event) {
Inventory inv = event.getClickedInventory(); Inventory inv = event.getClickedInventory();
if (inv == null || inv.getType() != InventoryType.ANVIL || event.getSlotType() != SlotType.RESULT) if (inv == null || inv.getType() != InventoryType.ANVIL || event.getSlotType() != SlotType.RESULT)
return; return;
@ -41,9 +42,8 @@ public class DisableInteractions implements Listener {
event.setCancelled(true); event.setCancelled(true);
} }
// grindstone
@EventHandler @EventHandler
public void b(InventoryClickEvent event) { public void grindstoneInteractions(InventoryClickEvent event) {
if (MythicLib.plugin.getVersion().isBelowOrEqual(1, 13)) if (MythicLib.plugin.getVersion().isBelowOrEqual(1, 13))
return; return;
@ -55,9 +55,8 @@ public class DisableInteractions implements Listener {
event.setCancelled(true); event.setCancelled(true);
} }
// smithing table
@EventHandler @EventHandler
public void c(InventoryClickEvent event) { public void smithingTableInteractions(InventoryClickEvent event) {
if (MythicLib.plugin.getVersion().isBelowOrEqual(1, 15)) if (MythicLib.plugin.getVersion().isBelowOrEqual(1, 15))
return; return;
@ -69,34 +68,46 @@ public class DisableInteractions implements Listener {
event.setCancelled(true); event.setCancelled(true);
} }
// enchanting tables
@EventHandler @EventHandler
public void d(EnchantItemEvent event) { public void enchantTablesInteractions(EnchantItemEvent event) {
if (isDisabled(NBTItem.get(event.getItem()), "enchant")) if (isDisabled(NBTItem.get(event.getItem()), "enchant"))
event.setCancelled(true); event.setCancelled(true);
} }
// smelting
@EventHandler @EventHandler
public void e(FurnaceSmeltEvent event) { public void furnaceInteractions(FurnaceSmeltEvent event) {
if (isDisabled(NBTItem.get(event.getSource()), "smelt")) if (isDisabled(NBTItem.get(event.getSource()), "smelt"))
event.setCancelled(true); event.setCancelled(true);
} }
// interaction /**
@EventHandler * Disables both click interactions if the corresponding
public void f(PlayerInteractEvent event) { * option was found on the item
* <p>
* Also prevents interactions with UNIDENTIFIED items
*/
@EventHandler(priority = EventPriority.LOW)
public void clickInteractions(PlayerInteractEvent event) {
if (!event.hasItem()) if (!event.hasItem())
return; return;
NBTItem item = NBTItem.get(event.getItem()); NBTItem item = NBTItem.get(event.getItem());
if (item.getBoolean("MMOITEMS_DISABLE_INTERACTION")) if (item.getBoolean("MMOITEMS_DISABLE_INTERACTION") || item.hasTag("MMOITEMS_UNIDENTIFIED_ITEM"))
event.setCancelled(true);
}
/**
* Prevents unidentified tools from breaking blocks
*/
@EventHandler(priority = EventPriority.LOW)
public void miningInteractions(BlockBreakEvent event) {
NBTItem item = NBTItem.get(event.getPlayer().getInventory().getItemInMainHand());
if (item.hasTag("MMOITEMS_UNIDENTIFIED_ITEM"))
event.setCancelled(true); event.setCancelled(true);
} }
// interaction (entity)
@EventHandler @EventHandler
public void g(PlayerInteractEntityEvent event) { public void entityInteractions(PlayerInteractEntityEvent event) {
if (event.getRightClicked() instanceof ArmorStand) if (event.getRightClicked() instanceof ArmorStand)
return; return;
@ -106,17 +117,15 @@ public class DisableInteractions implements Listener {
event.setCancelled(true); event.setCancelled(true);
} }
// interaction (consume)
@EventHandler @EventHandler
public void h(PlayerItemConsumeEvent event) { public void consumeInteractions(PlayerItemConsumeEvent event) {
NBTItem item = NBTItem.get(event.getItem()); NBTItem item = NBTItem.get(event.getItem());
if (item.getBoolean("MMOITEMS_DISABLE_INTERACTION")) if (item.getBoolean("MMOITEMS_DISABLE_INTERACTION"))
event.setCancelled(true); event.setCancelled(true);
} }
// workbench
@EventHandler @EventHandler
public void i(CraftItemEvent event) { public void workbenchInteractions(CraftItemEvent event) {
if (event.getRecipe() instanceof Keyed) if (event.getRecipe() instanceof Keyed)
if (((Keyed) event.getRecipe()).getKey().getNamespace().equals("mmoitems")) { if (((Keyed) event.getRecipe()).getKey().getNamespace().equals("mmoitems")) {
String craftingPerm = NBTItem.get(event.getCurrentItem()).getString("MMOITEMS_CRAFT_PERMISSION"); String craftingPerm = NBTItem.get(event.getCurrentItem()).getString("MMOITEMS_CRAFT_PERMISSION");
@ -136,14 +145,14 @@ public class DisableInteractions implements Listener {
event.setCancelled(true); event.setCancelled(true);
} }
// preventing the player from shooting the arrow
@EventHandler @EventHandler
public void j(EntityShootBowEvent event) { public void shootBowInteractions(EntityShootBowEvent event) {
if (!(event.getEntity() instanceof Player)) if (!(event.getEntity() instanceof Player))
return; return;
DurabilityItem durItem = new DurabilityItem(((Player) event.getEntity()).getPlayer(), event.getBow()); DurabilityItem durItem = new DurabilityItem(((Player) event.getEntity()).getPlayer(), event.getBow());
// Cannot shoot a broken bow
if (durItem.isBroken()) if (durItem.isBroken())
event.setCancelled(true); event.setCancelled(true);
@ -156,9 +165,29 @@ public class DisableInteractions implements Listener {
if (stack == null) if (stack == null)
return; return;
// Cannot shoot arrow?
NBTItem arrow = NBTItem.get(stack); NBTItem arrow = NBTItem.get(stack);
if (arrow.hasType() && MMOItems.plugin.getConfig().getBoolean("disable-interactions.arrow-shooting") if (arrow.hasType() && (MMOItems.plugin.getConfig().getBoolean("disable-interactions.arrow-shooting")
|| arrow.getBoolean("MMOITEMS_DISABLE_ARROW_SHOOTING")) || arrow.getBoolean("MMOITEMS_DISABLE_ARROW_SHOOTING")))
event.setCancelled(true);
}
@EventHandler(priority = EventPriority.LOW)
public void attackInteractions(EntityDamageByEntityEvent event) {
if (event.getDamage() == 0 || event.getCause() != EntityDamageEvent.DamageCause.ENTITY_ATTACK || !(event.getEntity() instanceof LivingEntity)
|| !(event.getDamager() instanceof Player) || event.getEntity().hasMetadata("NPC") || event.getDamager().hasMetadata("NPC"))
return;
Player player = (Player) event.getDamager();
ItemStack item = player.getInventory().getItemInMainHand();
DurabilityItem durItem = new DurabilityItem(player, item);
// If weapon is broken don't do damage
if (durItem.isBroken())
event.setCancelled(true);
// Prevent unidentified weapons from being used
if (durItem.getNBTItem().hasTag("MMOITEMS_UNIDENTIFIED_ITEM"))
event.setCancelled(true); event.setCancelled(true);
} }
@ -182,19 +211,4 @@ public class DisableInteractions implements Listener {
return nbt.hasType() && MMOItems.plugin.getConfig().getBoolean("disable-interactions." + type) return nbt.hasType() && MMOItems.plugin.getConfig().getBoolean("disable-interactions." + type)
|| nbt.getBoolean("MMOITEMS_DISABLE_" + type.toUpperCase().replace("-", "_") + "ING"); || nbt.getBoolean("MMOITEMS_DISABLE_" + type.toUpperCase().replace("-", "_") + "ING");
} }
// If weapon is broken don't do damage
@EventHandler
public void playerAttack(EntityDamageByEntityEvent event) {
if (event.getDamage() == 0 || event.getCause() != EntityDamageEvent.DamageCause.ENTITY_ATTACK || !(event.getEntity() instanceof LivingEntity)
|| !(event.getDamager() instanceof Player) || event.getEntity().hasMetadata("NPC") || event.getDamager().hasMetadata("NPC"))
return;
Player player = (Player) event.getDamager();
ItemStack item = player.getInventory().getItemInMainHand();
DurabilityItem durItem = new DurabilityItem(player, item);
if (durItem.isBroken())
event.setCancelled(true);
}
} }

View File

@ -167,6 +167,5 @@ public class PlayerListener implements Listener {
@EventHandler @EventHandler
public void registerInventoryUpdates2(PlayerItemHeldEvent event) { public void registerInventoryUpdates2(PlayerItemHeldEvent event) {
PlayerData.get(event.getPlayer()).getInventory().scheduleUpdate(); PlayerData.get(event.getPlayer()).getInventory().scheduleUpdate();
} }
} }