Solved Prevent damage to players and Villagers

Discussion in 'Plugin Development' started by Zenok, Jan 4, 2014.

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

    Zenok

    I need preventing players from causing damage to other players and villagers, also if players are in the array can not attack. But it returns me nullpointerException error in the line " if (entity.getLastDamageCause().getEntity() instanceof Player) {"

    May which be the error?
    Code:java
    1. @EventHandler
    2. public void onEntityDamage(EntityDamageByEntityEvent event) {
    3.  
    4. LivingEntity entity = (LivingEntity)event.getEntity();
    5.  
    6. if (entity.getLastDamageCause().getEntity() instanceof Player) {
    7. event.setCancelled(true);
    8. } else if(event.getEntityType() == EntityType.VILLAGER) {
    9. event.setCancelled(true);
    10. } else {
    11. Player player = (Player) event.getDamager();
    12. if(Spectators.contains(player.getName())) {
    13. event.setCancelled(true);
    14. }
    15.  
    16. }
    17. }
     
  2. Offline

    valon750

    Zenok

    You'll be looking at...

    if (event.getDamager() istanceof Player)

    To see if the player is what did the damage.

    After that you'll be having to do..

    if (event.getEntity() instanceof Villager)

    to see if it's a villager that was hit.
     
  3. Offline

    Zenok

    Oh, thanks. Solved
     
  4. Offline

    valon750

Thread Status:
Not open for further replies.

Share This Page