Error with entities

Discussion in 'Plugin Development' started by leland22, Jun 4, 2014.

Thread Status:
Not open for further replies.
  1. Offline

    leland22

    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
    1. package com.leland22.plugins;
    2.  
    3. import java.util.ArrayList;
    4. import java.util.Arrays;
    5. import java.util.List;
    6.  
    7. import org.bukkit.Bukkit;
    8. import org.bukkit.ChatColor;
    9. import org.bukkit.Location;
    10. import org.bukkit.Material;
    11. import org.bukkit.World;
    12. import org.bukkit.block.Block;
    13. import org.bukkit.entity.Entity;
    14. import org.bukkit.entity.EntityType;
    15. import org.bukkit.entity.LivingEntity;
    16. import org.bukkit.entity.Player;
    17. import org.bukkit.event.EventHandler;
    18. import org.bukkit.event.Listener;
    19. import org.bukkit.event.block.Action;
    20. import org.bukkit.event.block.BlockPlaceEvent;
    21. import org.bukkit.event.player.PlayerInteractEvent;
    22. import org.bukkit.inventory.ItemStack;
    23. import org.bukkit.inventory.meta.ItemMeta;
    24. import org.bukkit.util.BlockIterator;
    25.  
    26. import com.leland22.plugins.Mobtraps;
    27.  
    28. public class PlayerListener implements Listener {
    29.  
    30.  
    31.  
    32. public PlayerListener(Mobtraps plugin) {
    33. plugin.getServer().getPluginManager().registerEvents(this, plugin);
    34. }
    35.  
    36. // Cancels block placement of a renamed trap
    37. @EventHandler
    38. public void onWebPlacement(BlockPlaceEvent event) {
    39. Player p = event.getPlayer();
    40. ItemStack item = p.getItemInHand();
    41. if (item.getType() == Material.WEB)
    42. if (item.hasItemMeta()) {
    43. if (item.getItemMeta().hasDisplayName()) {
    44. if (item.getItemMeta().getDisplayName().contains("Full Animal Trap"));
    45. event.setCancelled(true);
    46. }
    47. }
    48. }
    49.  
    50. @SuppressWarnings("deprecation")
    51. @EventHandler
    52. public void onRelease(PlayerInteractEvent e){
    53. Player p = e.getPlayer();
    54. Location loc = p.getLocation();
    55. World world = p.getWorld();
    56. String animal = p.getItemInHand().getItemMeta().getDisplayName().toString();
    57. if(e.getAction().equals(Action.RIGHT_CLICK_AIR) || e.getAction().equals(Action.RIGHT_CLICK_BLOCK)){
    58. if(p.getItemInHand().getType().equals(Material.WEB)){
    59. if(p.getItemInHand().hasItemMeta()){
    60. if(p.getItemInHand().getItemMeta().hasDisplayName()){
    61. if(p.getItemInHand().getItemMeta().getDisplayName().contains("Trap")){
    62. //splitting the string of the display name to get the id of the mob
    63. animal = animal.substring(animal.indexOf("(") + 1);
    64. animal = animal.substring(0, animal.indexOf(")"));
    65. String finalanimal = animal.toUpperCase();
    66. //actually spawning the mob
    67. world.spawnEntity(loc, EntityType.fromName(finalanimal));
    68. changeItemMeta(p);
    69. }
    70. }
    71. }
    72. }
    73. }
    74. }
    75.  
    76. // // Sets the item meta to be an empty trap
    77. @SuppressWarnings("deprecation")
    78. @EventHandler
    79. public void changeItemMeta(Player p){
    80.  
    81. ItemMeta m = p.getItemInHand().getItemMeta();
    82. m.setDisplayName("An empty animal trap");
    83. m.setLore(Arrays.asList(ChatColor.GREEN + "An empty animal trap",
    84. ChatColor.WHITE + "To capture a animal sneak and right click while looking at it",
    85. ChatColor.WHITE + "To release a captured animal right click"));
    86. p.getItemInHand().setItemMeta(m);
    87. p.updateInventory();
    88. }
    89.  
    90.  
    91. // Gets the entity nearby the player and removes the entity, capturing it
    92. @EventHandler
    93. public void onCapture(PlayerInteractEvent event ) {
    94. Player p = event.getPlayer();
    95. if (event.getAction() == Action.RIGHT_CLICK_AIR
    96. || event.getAction() == Action.RIGHT_CLICK_BLOCK) {
    97. if (p.isSneaking()) {
    98. if (p.getItemInHand().getType() == Material.WEB) {
    99. if (p.getItemInHand().hasItemMeta()) {
    100. if (p.getItemInHand().getItemMeta().hasDisplayName()) {
    101. if (p.getItemInHand().getItemMeta()
    102. .getDisplayName()
    103. .contains("An empty animal trap")) {
    104.  
    105. p.getNearbyEntities(2, 2, 2);
    106. ArrayList<LivingEntity> livingE = new ArrayList<LivingEntity>();
    107. for (Entity e : p.getNearbyEntities(2, 2, 2)) {
    108. if (e instanceof LivingEntity) {
    109. livingE.add((LivingEntity) e);
    110. }
    111. }
    112.  
    113. BlockIterator bItr = new BlockIterator(p, 2);
    114. Block block;
    115. Location loc;
    116. int bx, by, bz;
    117. double ex, ey, ez;
    118. // loop through player's line of sight
    119. while (bItr.hasNext()) {
    120. block = bItr.next();
    121. bx = block.getX();
    122. by = block.getY();
    123. bz = block.getZ();
    124. // check for entities near this block in the
    125. // line of sight
    126. for (LivingEntity e : livingE) {
    127. loc = e.getLocation();
    128. ex = loc.getX();
    129. ey = loc.getY();
    130. ez = loc.getZ();
    131. if ((bx - .75 <= ex && ex <= bx + 1.75)
    132. && (bz - .75 <= ez && ez <= bz + 1.75)
    133. && (by - 1 <= ey && ey <= by + 2.5)) {
    134. // entity is close enough, set
    135. // target and stop
    136. String mob = e.getType().toString();
    137. System.out.println(mob);
    138. fillTrap(mob, p);
    139. e.remove();
    140. break;
    141.  
    142. }
    143. }
    144. }
    145.  
    146. }
    147. }
    148.  
    149. }
    150. }
    151. }
    152. }
    153. }
    154.  
    155. //changing item meta to comply with the captured mob
    156. @SuppressWarnings("deprecation")
    157. private void fillTrap(String mob, Player p){
    158. ItemMeta item = p.getItemInHand().getItemMeta();
    159. item.setDisplayName("Full Animal Trap (" + mob + ")");
    160. item.setLore(Arrays.asList(ChatColor.GREEN + "A full animal trap containing a",
    161. ChatColor.WHITE + "To capture a animal sneak and right click while looking at it",
    162. ChatColor.WHITE + "To release a captured animal right click"));
    163. p.getItemInHand().setItemMeta(item);
    164. p.updateInventory();
    165.  
    166.  
    167. }
    168.  
    169.  
    170. }
    171.  
     
  2. Offline

    LazyLemons

    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".
     
  3. Offline

    leland22

Thread Status:
Not open for further replies.

Share This Page