How can i prevent an ender crystal from exploding?

Discussion in 'Plugin Development' started by spiroulis, Jan 20, 2015.

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

    spiroulis

    The title pretty much says it all. I tried doing this:
    Code:
    @EventHandler
        public void CrystalInteraction(EntityDamageEvent event){
        Entity entity = event.getEntity();
            if(entity.getType() == EntityType.ENDER_CRYSTAL){
                if(event.getCause() == DamageCause.PROJECTILE){
                    if(entity.getLocation() == new Location(entity.getWorld(), main.getConfig().getDouble("FirstCrystal.x"), main.getConfig().getDouble("FirstCrystal.y"), main.getConfig().getDouble("FirstCrystal.z")) || entity.getLocation() == new Location(entity.getWorld(), main.getConfig().getDouble("SecondCrystal.x"), main.getConfig().getDouble("SecondCrystal.y"), main.getConfig().getDouble("SecondCrystal.z")) || entity.getLocation() == new Location(entity.getWorld(), main.getConfig().getDouble("ThirdCrystal.x"), main.getConfig().getDouble("ThirdCrystal.y"), main.getConfig().getDouble("ThirdCrystal.z")) || entity.getLocation() == new Location(entity.getWorld(), main.getConfig().getDouble("FourthCrystal.x"), main.getConfig().getDouble("FourthCrystal.y"), main.getConfig().getDouble("FourthCrystal.z"))){
                        event.setCancelled(true);
                    }
                }
            }
        }
    but it didnt do much... Thanks in advance :)
     
  2. Offline

    Burnett

    @spiroulis Does this even trigger at all? Debug messages. Also what about EntityExplodeEvent. I think it might fit the description more :confused:
     
  3. Offline

    adam753

    @spiroulis
    Well the first thing that stands out is the fact that you're using == to compare two locations which will never be the same instance. You should use equals() instead.
     
  4. Offline

    spiroulis

    @adam753 @Burnett hmm i changed the event to EntityExplodeEvent and i changed == to equals(). But still, it wont do anything after the location check. Any ideas?
     
  5. Offline

    Burnett

    adam753 likes this.
  6. Offline

    spiroulis

    @Burnett i did use debug messages and as i said earlier it triggers and works but only until i compare the locations.
    Code:
    @EventHandler
        public void CrystalInteraction(EntityExplodeEvent event){
        Entity entity = event.getEntity();
        Bukkit.broadcastMessage("Message");
            if(entity.getType() == EntityType.ENDER_CRYSTAL){
                Bukkit.broadcastMessage("Message"); //works until here
                    if(entity.getLocation().equals(new Location(entity.getWorld(), main.getConfig().getDouble("FirstCrystal.x"), main.getConfig().getDouble("FirstCrystal.y"), main.getConfig().getDouble("FirstCrystal.z")))){
                        Bukkit.broadcastMessage("NOT TODAY");
                        event.setCancelled(true);
                }
            }
        }
     
  7. Offline

    drpk

    @spiroulis how are you saving the locations in the config?
     
  8. Offline

    Burnett

    @spiroulis ok, so debug more... Print out what you are trying to compare (the world, x, y and z) and compare them manually.
     
  9. Offline

    spiroulis

    @Burnett so i removed the location check and it didnt explode but it did dissapear though
     
  10. Offline

    TheMintyMate

    @spiroulis
    @adam753

    For future reference, it is much easier to do this (to determine entity type) :
    Code:
    //this is an example:
    @EventHandler
    public void damage(EntityDamageEvent event){
       Entity entity = event.getEntity();
       if (entity instanceof Player){   /* instanceof compares the two classes for a relationship eg: the entity is also a player */
          Player player = (Player)entity; /* cast to player, as we know it can from our if test */
          //do stuff eg:
          event.setCancelled(true);
       }
    }
    Hope this helped somewhat,
    - Minty
     
Thread Status:
Not open for further replies.

Share This Page