Solved Why this detection doesn't work ?

Discussion in 'Spigot Plugin Development' started by punisher5, May 31, 2018.

  1. Hi i'm trying to detect if the cause of the enderman's death is a player.

    Why this lines doesn't work ?

    if(e.getEntity().getType() == EntityType.ENDERMAN && e.getEntity().getWorld().equals(Bukkit.getWorld("world_the_end"))) {
    Player p = e.getEntity().getKiller();
    if(e.getEntity().getLastDamageCause().getEntity().equals(p)) {
  2. Well by defining p as e.getEntity().getKiller() you are already assuming that the player is the killer of the enderman, so why would you want to check afterwards?
    • Agree Agree x 1
  3. Not sure if that was intentional, but that code would only work if the player is in the end.
    Code (Java):
    Please, can you post your full class? We don't know what even you're using.
  4. Of course the player is in the end ^^
    sorry for not clarifying it.

    here is my full class, I'm trying to spawn endermite when a enderman is killed by a player and there is many type of endermite with different spawn rate:
  5. Well it would only work if the endermen is in the end..

    As I mentioned earlier the line 23 in your pastebin is unnecessary.
  6. I know that xD I'm trying in the end
  7. I just realized what you tried to do, and boy oh boy. What the hell is this mess? When you are trying to randomize which type of Endermite is spawned don't use tons of the same event, use the same "event method" for all of the Endermites.
  8. xDD but how ? I use a new random for each ?
  9. I think it would be better to test the world's and entities through their UUIDs rather than their object instances.

    Try this code and tell me if it works out for you:

    Code (Java):

           public void onEndermanDeath(EntityDeathEvent e) {
               if(e.getEntity().getType() == EntityType.ENDERMAN && e.getEntity().getWorld().getUID().toString().equals(Bukkit.getWorld("world_the_end").getUID().toString())) {
                   Player p = e.getEntity().getKiller();
                   if(e.getEntity().getLastDamageCause().getEntity().getUniqueId().toString().equals(p.getUniqueId().toString())) {
                       int x = e.getEntity().getLocation().getBlockX();
                       int y = e.getEntity().getLocation().getBlockY() + 1;
                       int z = e.getEntity().getLocation().getBlockZ();
                       Location loc = new Location (e.getEntity().getWorld(), x, y, z);        
                       Random rand = new Random();
                       int randomNumber = rand.nextInt(100);
                       if(randomNumber <= 7) {
                           Endermite endermite = (Endermite)e.getEntity().getWorld().spawnEntity(loc, EntityType.ENDERMITE);
                           endermite.setCustomName("ยง4Death Machine");
                           endermite.addPotionEffect(new PotionEffect(PotionEffectType.INCREASE_DAMAGE, 9999999, 1));
                           endermite.addPotionEffect(new PotionEffect(PotionEffectType.REGENERATION, 9999999, 2));
                           endermite.addPotionEffect(new PotionEffect(PotionEffectType.HEALTH_BOOST, 9999999, 4));
                           Bukkit.broadcastMessage("patate 1111");
    Also take into consideration that you set the endermite spawn rate to very low. If you want to be 100% certain that the code is working, just for testing purposes, I would use "if (true)" rather than "if (randomNumber <=7)". Then when you are certain it works, you can switch back.
  10. Through this line the following if is unnecessary
    • Agree Agree x 2
  11. Choco


    So a few things here:
    1. 50% of this line is redundant or unnecessary
    2. Comparing a UUID is far faster than comparing the contents of a String. UUID is 2 longs (easy == comparison), Strings must compare each and every individual character, which for a UUID, is 36 characters
    3. Bukkit#getWorld() can return null if said world does not exist. Easy NullPointerException if the name of the world was changed
    4. Get the world's name and check it instead. Otherwise, if you only care about it being an end world, check its environment (World#getEnvironment() == Environment.THE_END)
    Code (Java):
    if (e.getEntity().getWorld().getEnvironment() == Environment.THE_END) {


    // Or, if you want a very specific world,
    if (e.getEntity().getWorld().getName().equals("world_the_end")) {

    • Agree Agree x 1
  12. Thnx a lot you three :)
    • Agree Agree x 1
  13. Yeah. However I am not entirely used to java and I was unsure that comparing UUIDInstance1 == UUIDInstance2 would only compare the pointers to those UUIDs rather than the UUID's themselves. Also, I am not sure what goes on under the hood of UUIDInstance1.equals(UUIDInstance2), so I played it safe and compared the strings.
  14. Choco


    == compares the memory addresses of two objects (i.e. compares their pointers, yes), though a World instance should be comparable using == as well. If that's the case, its UUID will also be comparable with == as it will be the same UUID. Nevertheless, to be safe, UUID#equals() should be used as it compares the underlying longs representing it. See Java's source for UUID
    Code (Java):
        public boolean equals(Object obj) {
            if (!(obj instanceof UUID)) return false;
            return (((UUID) obj).mostSigBits == mostSigBits &&
                ((UUID) obj).leastSigBits == leastSigBits);
    Now you know that UUID comparison is faster than its String counterpart ;)
    • Agree Agree x 1
  15. Yeah, I pulled this up here and they said the way to compare values is to compare them with ".equals"

    Thanks for that code snippet btw :)