Invincible

Discussion in 'Plugin Development' started by SeanyJo, Oct 7, 2013.

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

    SeanyJo

    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.
     
  2. Offline

    Mathias Eklund

    Check for the event of everything and if the player is in the hashmap cancel the event.
     
  3. Offline

    remremrem

    Something kind of like this
    Code:java
    1. onEntityDamage(EntityDamageEvent event){
    2. if(event.getEntityType==EntityType.PLAYER){
    3. if(hashmap.contains(event.getEntity().getName()){
    4. event.cancel()
    5. }
    6. }
    7. }
     
    Mathias Eklund likes this.
  4. Offline

    Mathias Eklund

  5. Offline

    SeanyJo

    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
    1. public class Invincible implements Listener {
    2. public static TigreGames plugin;
    3. public void onEntityDamage(EntityDamageEvent event1){
    4. if(event1.getEntityType()==EntityType.PLAYER) {
    5. if(TigreGames.Invinciblemap.containsKey(((PluginBase) event1.getEntity()).getName())) {
    6. event1.setCancelled(true);
    7. }
    8. }
    9. }
    10. }
     
  6. Offline

    Cycryl

    SeanyJo
    remremrem
    This is what i would use:
    Code:java
    1. public final HashMap<String, String> map = new HashMap<String, String>();

    Code:java
    1. for(String p : map.keySet()){
    2. Bukkit.getPlayer(p).setNoDamageTicks(999999);
    3. }
     
    remremrem likes this.
  7. Offline

    SeanyJo

    Cycryl
    So the following code will make it so the players are invincible until they are out of the hashmap?
    Code:java
    1. public class Invincible implements Listener {
    2. public static TigreGames plugin;
    3. public void onEntityDamage(EntityDamageEvent event1){
    4. for(String p : TigreGames.Invinciblemap.keySet()){
    5. Bukkit.getPlayer(p).setNoDamageTicks(999999);
    6. }
    7. }
    8. }
    9.  
     
  8. Offline

    sgavster

    Did you register your events?...
     
  9. Offline

    The_Doctor_123

    SeanyJo
    The static variable "plugin" was never initialized. So it's kinda useless..
     
  10. Offline

    Cycryl

    SeanyJo
    no u will need this code for that
    Code:java
    1. @EventHandler
    2. public void onmove(PlayerMoveEvent){
    3. for(Player player : Bukkit.getOnlinePlayers){
    4. if(hashmap.containsKey(player)){
    5. player.setNoDamageTicks(999999);
    6. }else{
    7. player.setNoDamageTicks(0)
    8. }
    9. }
    10. }
     
  11. Offline

    Mathias Eklund

    They can just not move, and they will be invincible all the time :) Until moved.
     
    whitehooder likes this.
  12. Offline

    Cycryl

    Mathias Eklund
    yeah your right... the code should be in a sceduler in the onEnable.

    didn't think of that...
     
    Mathias Eklund likes this.
  13. Offline

    SeanyJo

    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
    1. public class Invincible implements Listener {
    2. public static TigreGames plugin;
    3. @EventHandler
    4. public void onDamage(EntityDamageEvent e) {
    5. for(Player player : Bukkit.getOnlinePlayers()){
    6. if(TigreGames.Invinciblemap.containsKey(player)){
    7. player.setNoDamageTicks(999999);
    8. }else{
    9. player.setNoDamageTicks(0);
    10. }
    11. }
    12. }
    13. }

    And heres my event register code:
    Code:java
    1. getServer().getPluginManager().registerEvents(new Invincible(), this);
     
  14. Offline

    xTrollxDudex

    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()))
     
  15. Offline

    SeanyJo

    Would I use getHandlers() or get Entity()? getPlayer() isn't for this type of event.
     
  16. Offline

    Compressions

  17. Offline

    xigsag

    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.
     
  18. Offline

    SeanyJo

    Still doesn't work, here's what I have:
    Code:java
    1. @EventHandler
    2. public void onDamage(EntityDamageEvent e) {
    3. if(e.getEntity() instanceof Player) {
    4. Player player = (Player) e.getEntity();
    5. if(TigreGames.Invinciblemap.containsKey(player)){
    6. player.setNoDamageTicks(999999);
    7. }else{
    8. player.setNoDamageTicks(0);
    9. }
    10. }
    11. }
    12. }
     
  19. Offline

    xigsag

    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.
     
  20. Offline

    Mathias Eklund

    yup. Told him that at first. Don't know why he ignored it though :) His fault
     
  21. Offline

    xigsag

    Mathias Eklund Haha, I was wondering why he was still using the nodamageticks thing.
     
  22. Offline

    Cycryl

  23. Offline

    whitehooder

    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
    1. @EventHandler
    2. public void onDamage(EntityDamageEvent e) {
    3. if(e.getEntity() instanceof Player) {
    4. Player player = (Player) e.getEntity();
    5. if(TigreGames.Invinciblemap.containsKey(player)){
    6. 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
    7. }
    8. }
    9. }
    10. }
    11.  


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

    SeanyJo

Thread Status:
Not open for further replies.

Share This Page