Solved Entity Tracking error on custom Entity

Discussion in 'Plugin Development' started by Jogy34, Jul 6, 2013.

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

    Jogy34

    In one of my plugins I'm creating custom projectiles. They are bukkit projectiles though and not NMS projectiles. From NMS stuff it's an EntityItem. They work fine when I'm launching them. They fly how I want them to and work how I want them to. However, if I try to launch one after I had already launched one but before the first one is gone, it throws this error:

    Code:
    "Silently" catching entity tracking error.
    net.minecraft.server.v1_6_R1.ReportedException: Adding entity to track
          at net.minecraft.server.v1_6_R1.EntityTracker.addEntity(EntityTracker.java:116)
          at net.minecraft.server.v1_6_R1.EntityTracker.track(EntityTracker.java:57)
          at net.minecraft.server.v1_6_R1.WorldManager.a(WorldManager.java:18)
          at net.minecraft.server.v1_6_R1.World.a(World.java:957)
          at net.minecraft.server.v1_6_R1.WorldServer.a(WorldServer.java:800)
          at net.minecraft.server.v1_6_R1.World.addEntity(World.java:950)
          at com.xern.jogy34.tardis.tardis.TARDIS.fireTimeWeapon(TARDIS.java:1695)
          at com.xern.jogy34.tardis.general.TARDISMain.onCommand(TARDISMain.java:1383)
          at org.bukkit.command.PluginCommand.execute(PluginCommand.java:44)
          at org.bukkit.command.SimpleCommandMap.dispatch(SimpleCommandMap.java:189)
          at org.bukkit.craftbukkit.v1_6_R1.CraftServer.dispatchCommand(CraftServer.java:523)
          at net.minecraft.server.v1_6_R1.PlayerConnection.handleCommand(PlayerConnection.java:983)
          at net.minecraft.server.v1_6_R1.PlayerConnection.chat(PlayerConnection.java:901)
          at net.minecraft.server.v1_6_R1.PlayerConnection.a(PlayerConnection.java:858)
          at net.minecraft.server.v1_6_R1.Packet3Chat.handle(SourceFile:49)
          at net.minecraft.server.v1_6_R1.NetworkManager.b(NetworkManager.java:293)
          at net.minecraft.server.v1_6_R1.PlayerConnection.d(PlayerConnection.java:118)
          at net.minecraft.server.v1_6_R1.ServerConnection.b(SourceFile:37)
          at net.minecraft.server.v1_6_R1.DedicatedServerConnection.b(SourceFile:30)
          at net.minecraft.server.v1_6_R1.MinecraftServer.t(MinecraftServer.java:590)
          at net.minecraft.server.v1_6_R1.DedicatedServer.t(DedicatedServer.java:226)
          at net.minecraft.server.v1_6_R1.MinecraftServer.s(MinecraftServer.java:486)
          at net.minecraft.server.v1_6_R1.MinecraftServer.run(MinecraftServer.java:419)
          at net.minecraft.server.v1_6_R1.ThreadServerApplication.run(SourceFile:582)
    Caused by: java.lang.IllegalStateException: Entity is already tracked!
          at net.minecraft.server.v1_6_R1.EntityTracker.addEntity(EntityTracker.java:96)
          ... 23 more
    
    My basic code is:
    Code:
    MyProjectile proj = new MyProjectile(nmsWorld);
    proj.setPosition(location); //I have switch cases that I use to determine this and the velocity
    proj.setVelocity(vector); //My own method that sets the motX, motY, motZ which I use to determine how it flys.
    nmsWorld.addEntity(proj);
    
    This works fine for my mob entities and fireworks projectile but not my EntityItem projectiles. Anyone have any thoughts as to what would be causing this? If you want to see more code just ask.
     
    xTrollxDudex likes this.
  2. Offline

    tills13

    What is on and around "com.xern.jogy34.tardis.tardis.TARDIS.fireTimeWeapon(TARDIS.java:1695)"

    By "Caused by: java.lang.IllegalStateException: Entity is already tracked!" it seems the item is automatically tracked when you create it OR you've already added it somewhere else to the tracker.
     
  3. Offline

    SoThatsIt

    Jogy34 Could i please see your MyProjectile class
     
  4. Offline

    Jogy34


    That's where I actually add the entity to the NMS world. And it seems to think that every time I create a new one that it's actually the same one. The error only gets thrown when I try to fire one before the previous one has died.


    The code I showed you wasn't my actual code it was just the basic part of what I was doing. I didn't really want to show you that because in order to show you everything I have to show you 4 different classes.
    Anyways here you go:

    Highest up class:
    Code:java
    1.  
    2. package com.xern.jogy34.tardis.weapons;
    3.  
    4. import net.minecraft.server.v1_6_R1.World;
    5.  
    6. import org.bukkit.ChatColor;
    7. import org.bukkit.Material;
    8. import org.bukkit.inventory.ItemStack;
    9. import org.bukkit.inventory.meta.ItemMeta;
    10.  
    11. public class MagneticBeacon extends TimeWeapon
    12. {
    13. private static ItemStack me;
    14. public MagneticBeacon(World world)
    15. {
    16. super(world, Material.FLINT.getId(), 0);
    17. }
    18.  
    19. @Override
    20. public TimeWeaponType getType()
    21. {
    22. return TimeWeaponType.MAGNETIC_BEACON;
    23. }
    24.  
    25. @Override
    26. public ItemStack getWeaponStack()
    27. {
    28. return me;
    29. }
    30.  
    31. static
    32. {
    33. me = new ItemStack(Material.FLINT);
    34.  
    35. ItemMeta im = me.getItemMeta();
    36. im.setDisplayName(ChatColor.DARK_RED + "BASIC MAGNETIC BEACON");
    37.  
    38. WeaponHelper.addWeapon(MagneticBeacon.class, me, 50);
    39. }
    40. }
    41.  

    All of the overriden methods are for my plugin and don't have to do with EntityItem
    TimeWeapon:
    Code:java
    1.  
    2. package com.xern.jogy34.tardis.weapons;
    3.  
    4. import java.util.UUID;
    5.  
    6. import org.bukkit.Location;
    7. import org.bukkit.util.Vector;
    8.  
    9. import net.minecraft.server.v1_6_R1.EntityItem;
    10. import net.minecraft.server.v1_6_R1.ItemStack;
    11. import net.minecraft.server.v1_6_R1.World;
    12.  
    13. public abstract class TimeWeapon extends EntityItem implements ITimeWeapon
    14. {
    15. public TimeWeapon(World world, int id, int data)
    16. {
    17. super(world);
    18.  
    19. this.uniqueID = UUID.randomUUID();
    20.  
    21. this.id = id;
    22. this.setItemStack(new ItemStack(id, 1, data));
    23. }
    24.  
    25. @Override
    26. public void shoot(double d0, double d1, double d2, float f, float f1)
    27. {
    28. WeaponHelper.shoot(d0, d1, d2, f, f1, this);
    29. }
    30.  
    31. @Override
    32. public void l_()
    33. {
    34. WeaponHelper.l_(this);
    35. }
    36.  
    37. @Override
    38. public void setVelocity(Vector v)
    39. {
    40. this.motX = v.getX();
    41. this.motY = v.getY();
    42. this.motZ = v.getZ();
    43. }
    44.  
    45. @Override
    46. public void setPosition(Location loc)
    47. {
    48. super.setPosition(loc.getX(), loc.getY(), loc.getZ());
    49. }
    50. }
    51.  

    ITimeWeapon (interface):
    Code:java
    1.  
    2. package com.xern.jogy34.tardis.weapons;
    3.  
    4. import org.bukkit.Location;
    5. import org.bukkit.inventory.ItemStack;
    6. import org.bukkit.util.Vector;
    7.  
    8. import net.minecraft.server.v1_6_R1.IProjectile;
    9.  
    10. public interface ITimeWeapon extends IProjectile
    11. {
    12. public TimeWeaponType getType();
    13. public ItemStack getWeaponStack();
    14. public void setVelocity(Vector v);
    15. public void setPosition(Location loc);
    16. }
    17.  

    WeaponHelper class:
    Code:java
    1.  
    2. package com.xern.jogy34.tardis.weapons;
    3.  
    4. import java.util.HashMap;
    5. import java.util.Random;
    6.  
    7. import org.bukkit.Bukkit;
    8. import org.bukkit.Location;
    9. import org.bukkit.craftbukkit.v1_6_R1.CraftWorld;
    10. import org.bukkit.inventory.ItemStack;
    11.  
    12. import com.xern.jogy34.tardis.events.TimeWeaponHitTARDISEvent;
    13. import com.xern.jogy34.tardis.tardis.TARDIS;
    14.  
    15. import net.minecraft.server.v1_6_R1.Entity;
    16. import net.minecraft.server.v1_6_R1.MathHelper;
    17.  
    18. public class WeaponHelper
    19. {
    20. private static final Random rnd = new Random();
    21. public static void l_(Entity e)
    22. {
    23. if(e instanceof ITimeWeapon)
    24. {
    25. ((ITimeWeapon)e).shoot(e.motX, e.motY, e.motZ, 1.5F, 1.0F);
    26. e.move(e.motX, e.motY, e.motZ);
    27. if(e.getBukkitEntity().getTicksLived() > 100) e.die();
    28.  
    29. CraftWorld w = e.world.getWorld();
    30. int x = (int) (e.locX + e.motX);
    31. int y = (int) (e.locY + e.motY);
    32. int z = (int) (e.locZ + e.motZ);
    33. if(w.getBlockAt(x, y, z).getTypeId() != 0 || (e.motX == 0 && e.motY == 0 && e.motZ == 0))
    34. {
    35. TARDIS t = TARDIS.getTardisByLocation(new Location(w, x, y, z), false, false);
    36. if(t == null)
    37. {
    38. e.die();
    39. }
    40. else
    41. {
    42. TimeWeaponHitTARDISEvent event = new TimeWeaponHitTARDISEvent(t, ((ITimeWeapon)e));
    43. Bukkit.getPluginManager().callEvent(event);
    44. e.die();
    45. }
    46. }
    47. }
    48. }
    49.  
    50. public static void shoot(double d0, double d1, double d2, float f, float f1, Entity e)
    51. {
    52. float f2 = MathHelper.sqrt(d0 * d0 + d1 * d1 + d2 * d2);
    53.  
    54. d0 /= (double) f2;
    55. d1 /= (double) f2;
    56. d2 /= (double) f2;
    57. d0 += rnd.nextGaussian() * (double) (rnd.nextBoolean() ? -1 : 1) * 0.007499999832361937D * (double) f1;
    58. d1 += rnd.nextGaussian() * (double) (rnd.nextBoolean() ? -1 : 1) * 0.007499999832361937D * (double) f1;
    59. d2 += rnd.nextGaussian() * (double) (rnd.nextBoolean() ? -1 : 1) * 0.007499999832361937D * (double) f1;
    60. d0 *= (double) f;
    61. d1 *= (double) f;
    62. d2 *= (double) f;
    63. e.motX = d0;
    64. e.motY = d1;
    65. e.motZ = d2;
    66. float f3 = MathHelper.sqrt(d0 * d0 + d2 * d2);
    67.  
    68. e.lastYaw = e.yaw = (float) (Math.atan2(d0, d2) * 180.0D / 3.1415927410125732D);
    69. e.lastPitch = e.pitch = (float) (Math.atan2(d1, (double) f3) * 180.0D / 3.1415927410125732D);
    70. }
    71.  
    72. public static class WeaponStackAndPrice
    73. {
    74. public final ItemStack weaponStack;
    75. public final int price;
    76. public WeaponStackAndPrice(ItemStack weaponStack, int price)
    77. {
    78. this.weaponStack = weaponStack;
    79. this.price = price;
    80. }
    81. }
    82.  
    83. private static HashMap<WeaponStackAndPrice, Class<? extends ITimeWeapon>> allWeapons =
    84. new HashMap<WeaponStackAndPrice, Class<? extends ITimeWeapon>>();
    85.  
    86. public static void addWeapon(Class<? extends ITimeWeapon> weapon, ItemStack weaponStack, int price)
    87. {
    88. allWeapons.put(new WeaponStackAndPrice(weaponStack, price), weapon);
    89. }
    90.  
    91. public static WeaponStackAndPrice[] getAllWeaponStacksAndPrices()
    92. {
    93. return allWeapons.keySet().toArray(new WeaponStackAndPrice[0]);
    94. }
    95.  
    96. public static ItemStack getStackFromWeapon(Class<? extends ITimeWeapon> weapon)
    97. {
    98. for(WeaponStackAndPrice wap : allWeapons.keySet())
    99. {
    100. if(allWeapons.get(wap).equals(weapon))
    101. {
    102. return wap.weaponStack;
    103. }
    104. }
    105. return null;
    106. }
    107.  
    108. public static Class<? extends ITimeWeapon> isWeaponStack(ItemStack is)
    109. {
    110. for(WeaponStackAndPrice w : allWeapons.keySet())
    111. {
    112. ItemStack cur = w.weaponStack;
    113. if(is.getType().equals(cur.getType()))
    114. {
    115. if(is.getItemMeta().equals(cur.getItemMeta()))
    116. {
    117. if(is.getEnchantments().equals(cur.getEnchantments()))
    118. {
    119. return allWeapons.get(w);
    120. }
    121. }
    122. }
    123. }
    124. return null;
    125. }
    126. }
    127.  

    I use this so that I don't have to keep rewriting the same code as not all of my weapon classes come from TimeWeapon as currently one of them is a firework and not an item

    Anyone?

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 3, 2016
  5. Offline

    tills13

    Does NMS automatically add the item to the tracker. It could be not that all the items are the same but that the item has been added already by another section.

    Try printing put all the tracked items and see if they are the same or if it's what I'm thinking.

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 3, 2016
  6. Offline

    Jogy34

    Entities are added to the tracker when you add them into the NMS world. And I already know that the tracker contains an EntityItem that it thinks is the same one as a new one that I try to fire which is one that was previously fired. If it didn't think it was already tracked then it wouldn't be throwing the error and it would actually be fireing and if it didn't think that the one that is already tracked is the one that was previously fired then it would be throwing the error at times when one wasn't out there that was previously fired or it would sometimes allow it to fire when one is already out there.

    Anyone?

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 3, 2016
  7. Offline

    Jogy34

    Anyone have any ideas?

    EDIT: Sorry, I figured it out. I was just being kind of stupid. Turns out the entity tracker checks if an entity is already tracked by its id, not it's UUID its normal id. I was thinking that the id field in EntityItem was the id of the item it was going to render as so I kept setting it to the same thing.
     
Thread Status:
Not open for further replies.

Share This Page