From d22aa9f189572f829e4612e8433a261fc574e4f5 Mon Sep 17 00:00:00 2001 From: Gabriel Tofvesson Date: Thu, 20 May 2021 00:36:30 +0200 Subject: [PATCH] Implement stack replacement for throwables --- .../listener/StackReplaceListener.java | 23 +++++++++++++++++++ 1 file changed, 23 insertions(+) diff --git a/src/dev/w1zzrd/invtweaks/listener/StackReplaceListener.java b/src/dev/w1zzrd/invtweaks/listener/StackReplaceListener.java index 6b07e44..5f1501b 100644 --- a/src/dev/w1zzrd/invtweaks/listener/StackReplaceListener.java +++ b/src/dev/w1zzrd/invtweaks/listener/StackReplaceListener.java @@ -2,9 +2,12 @@ package dev.w1zzrd.invtweaks.listener; import org.bukkit.Bukkit; import org.bukkit.Material; +import org.bukkit.entity.Player; +import org.bukkit.entity.ThrowableProjectile; import org.bukkit.event.EventHandler; import org.bukkit.event.Listener; import org.bukkit.event.block.BlockPlaceEvent; +import org.bukkit.event.entity.ProjectileLaunchEvent; import org.bukkit.event.player.PlayerItemBreakEvent; import org.bukkit.inventory.EquipmentSlot; import org.bukkit.inventory.ItemStack; @@ -51,6 +54,26 @@ public class StackReplaceListener implements Listener { logger.fine(LOG_PLUGIN_NAME + " Moved tool into empty hand for player " + event.getPlayer().getName()); } + @EventHandler + public void onPlayerThrowSnowballEvent(final ProjectileLaunchEvent event) { + if (event.getEntity() instanceof final ThrowableProjectile projectile && + projectile.getShooter() instanceof final Player thrower) { + + final PlayerInventory inventory = thrower.getInventory(); + final ItemStack stack = projectile.getItem(); + + final EquipmentSlot slot; + if (inventory.getItemInMainHand().getType() == stack.getType()) + slot = EquipmentSlot.HAND; + else if (inventory.getItemInOffHand().getType() == stack.getType()) + slot = EquipmentSlot.OFF_HAND; + else return; + + if (findAndMoveSimilarStack(stack, slot, inventory, CompareFunc.defaultFunc())) + logger.fine(LOG_PLUGIN_NAME + " Moved snowball into empty hand for player " + thrower.getName()); + } + } + /** * Attempt to find and move a similar stack in the inventory to the one given, using the supplied comparison * function