I'm getting an issue where when I capture the mob the first time it doesn't really remove it but just respawns it on top of me. I think whats happening is it is doing both methods fine but executing them both so it gets instantly gets despawned then respawned. Could you guys offer any insight on how to fix this? Code:java package com.leland22.plugins; import java.util.ArrayList;import java.util.Arrays;import java.util.List; import org.bukkit.Bukkit;import org.bukkit.ChatColor;import org.bukkit.Location;import org.bukkit.Material;import org.bukkit.World;import org.bukkit.block.Block;import org.bukkit.entity.Entity;import org.bukkit.entity.EntityType;import org.bukkit.entity.LivingEntity;import org.bukkit.entity.Player;import org.bukkit.event.EventHandler;import org.bukkit.event.Listener;import org.bukkit.event.block.Action;import org.bukkit.event.block.BlockPlaceEvent;import org.bukkit.event.player.PlayerInteractEvent;import org.bukkit.inventory.ItemStack;import org.bukkit.inventory.meta.ItemMeta;import org.bukkit.util.BlockIterator; import com.leland22.plugins.Mobtraps; public class PlayerListener implements Listener { public PlayerListener(Mobtraps plugin) {plugin.getServer().getPluginManager().registerEvents(this, plugin);} // Cancels block placement of a renamed trap@EventHandlerpublic void onWebPlacement(BlockPlaceEvent event) {Player p = event.getPlayer();ItemStack item = p.getItemInHand();if (item.getType() == Material.WEB)if (item.hasItemMeta()) {if (item.getItemMeta().hasDisplayName()) {if (item.getItemMeta().getDisplayName().contains("Full Animal Trap"));event.setCancelled(true);}}} @SuppressWarnings("deprecation")@EventHandlerpublic void onRelease(PlayerInteractEvent e){Player p = e.getPlayer();Location loc = p.getLocation();World world = p.getWorld();String animal = p.getItemInHand().getItemMeta().getDisplayName().toString();if(e.getAction().equals(Action.RIGHT_CLICK_AIR) || e.getAction().equals(Action.RIGHT_CLICK_BLOCK)){if(p.getItemInHand().getType().equals(Material.WEB)){if(p.getItemInHand().hasItemMeta()){if(p.getItemInHand().getItemMeta().hasDisplayName()){if(p.getItemInHand().getItemMeta().getDisplayName().contains("Trap")){//splitting the string of the display name to get the id of the mobanimal = animal.substring(animal.indexOf("(") + 1);animal = animal.substring(0, animal.indexOf(")"));String finalanimal = animal.toUpperCase();//actually spawning the mobworld.spawnEntity(loc, EntityType.fromName(finalanimal));changeItemMeta(p);}}}}}} // // Sets the item meta to be an empty trap@SuppressWarnings("deprecation")@EventHandlerpublic void changeItemMeta(Player p){ ItemMeta m = p.getItemInHand().getItemMeta();m.setDisplayName("An empty animal trap");m.setLore(Arrays.asList(ChatColor.GREEN + "An empty animal trap",ChatColor.WHITE + "To capture a animal sneak and right click while looking at it",ChatColor.WHITE + "To release a captured animal right click"));p.getItemInHand().setItemMeta(m);p.updateInventory();} // Gets the entity nearby the player and removes the entity, capturing it@EventHandlerpublic void onCapture(PlayerInteractEvent event ) {Player p = event.getPlayer();if (event.getAction() == Action.RIGHT_CLICK_AIR|| event.getAction() == Action.RIGHT_CLICK_BLOCK) {if (p.isSneaking()) {if (p.getItemInHand().getType() == Material.WEB) {if (p.getItemInHand().hasItemMeta()) {if (p.getItemInHand().getItemMeta().hasDisplayName()) {if (p.getItemInHand().getItemMeta().getDisplayName().contains("An empty animal trap")) { p.getNearbyEntities(2, 2, 2);ArrayList<LivingEntity> livingE = new ArrayList<LivingEntity>();for (Entity e : p.getNearbyEntities(2, 2, 2)) {if (e instanceof LivingEntity) {livingE.add((LivingEntity) e);}} BlockIterator bItr = new BlockIterator(p, 2);Block block;Location loc;int bx, by, bz;double ex, ey, ez;// loop through player's line of sightwhile (bItr.hasNext()) {block = bItr.next();bx = block.getX();by = block.getY();bz = block.getZ();// check for entities near this block in the// line of sightfor (LivingEntity e : livingE) {loc = e.getLocation();ex = loc.getX();ey = loc.getY();ez = loc.getZ();if ((bx - .75 <= ex && ex <= bx + 1.75)&& (bz - .75 <= ez && ez <= bz + 1.75)&& (by - 1 <= ey && ey <= by + 2.5)) {// entity is close enough, set// target and stopString mob = e.getType().toString();System.out.println(mob);fillTrap(mob, p);e.remove();break; }}} }} }}}}} //changing item meta to comply with the captured mob@SuppressWarnings("deprecation")private void fillTrap(String mob, Player p){ItemMeta item = p.getItemInHand().getItemMeta();item.setDisplayName("Full Animal Trap (" + mob + ")");item.setLore(Arrays.asList(ChatColor.GREEN + "A full animal trap containing a",ChatColor.WHITE + "To capture a animal sneak and right click while looking at it",ChatColor.WHITE + "To release a captured animal right click"));p.getItemInHand().setItemMeta(item);p.updateInventory(); } }
In your onRelease method make sure the player is not sneaking. You're executing both listener methods for capturing and spawning the mob. You should also make sure that onRelease only works on trap items that contain "Full Animal Trap" instead of just "Trap".