Hi, I was wondering how you would make all players in a hashmap immune to all threats that could harm them (like pvp, lava, hostile mobs) and make them not place or destroy anything.
Something kind of like this Code:java onEntityDamage(EntityDamageEvent event){ if(event.getEntityType==EntityType.PLAYER){ if(hashmap.contains(event.getEntity().getName()){ event.cancel() } }}
Mathias Eklund remremrem It didn't work, here's my code, and when I'm in the hashmap, I jumped into lava and died. Code:java public class Invincible implements Listener {public static TigreGames plugin;public void onEntityDamage(EntityDamageEvent event1){if(event1.getEntityType()==EntityType.PLAYER) {if(TigreGames.Invinciblemap.containsKey(((PluginBase) event1.getEntity()).getName())) {event1.setCancelled(true);}}}}
SeanyJo remremrem This is what i would use: Code:java public final HashMap<String, String> map = new HashMap<String, String>(); Code:java for(String p : map.keySet()){Bukkit.getPlayer(p).setNoDamageTicks(999999);}
Cycryl So the following code will make it so the players are invincible until they are out of the hashmap? Code:java public class Invincible implements Listener { public static TigreGames plugin; public void onEntityDamage(EntityDamageEvent event1){ for(String p : TigreGames.Invinciblemap.keySet()){ Bukkit.getPlayer(p).setNoDamageTicks(999999); } }}
SeanyJo no u will need this code for that Code:java @EventHandlerpublic void onmove(PlayerMoveEvent){for(Player player : Bukkit.getOnlinePlayers){if(hashmap.containsKey(player)){player.setNoDamageTicks(999999);}else{player.setNoDamageTicks(0)}}}
Mathias Eklund yeah your right... the code should be in a sceduler in the onEnable. didn't think of that...
sgavster The_Doctor_123 Cycryl Mathias Eklund I tried this, registered the events, and I'm still not invincible. Heres the code I have for the damage listener: Code:java public class Invincible implements Listener {public static TigreGames plugin;@EventHandlerpublic void onDamage(EntityDamageEvent e) {for(Player player : Bukkit.getOnlinePlayers()){if(TigreGames.Invinciblemap.containsKey(player)){player.setNoDamageTicks(999999);}else{player.setNoDamageTicks(0);}}}} And heres my event register code: Code:java getServer().getPluginManager().registerEvents(new Invincible(), this);
SeanyJo Can't you just cancel the event? Use if(map.containsKey(e.getPlayer())). And store strings instead of player objects, if(map.containsKey(e.getPlayer.getName()))
SeanyJo I did not bother to read half of the posts here, but in reply to this, you can check if the entity is a player, then cast the entity to a player, like so: Code: @EventHandler public void onEntityDamage(EntityDamageEvent e){ if(e.getEntity() instanceof Player){ Player p = (Player) e.getEntity(); } } Of course, I'm pretty sure you know this already. Just a tip.
Still doesn't work, here's what I have: Code:java @EventHandler public void onDamage(EntityDamageEvent e) { if(e.getEntity() instanceof Player) { Player player = (Player) e.getEntity(); if(TigreGames.Invinciblemap.containsKey(player)){ player.setNoDamageTicks(999999); }else{ player.setNoDamageTicks(0); } } }}
SeanyJo Hold on, can't you just cancel the event? ._. e.setCancelled(); ? I don't see why you're adding the setNoDamageTicks, when you can just cancel it everytime it happens. That, for sure, is more efficient.
Lol, had to read your comment about six times to realize that you mean they would stay invincible even if they were removed from the map if they just stood still Solution: (just what all others said) Code:java @EventHandlerpublic void onDamage(EntityDamageEvent e) {if(e.getEntity() instanceof Player) {Player player = (Player) e.getEntity();if(TigreGames.Invinciblemap.containsKey(player)){e.setCancelled(true); // This line cancels the damage event, meaning if the player is in the hashmap, the damage event will not occur, thus the player will not take any damage}}}} EDIT by Moderator: merged posts, please use the edit button instead of double posting.