Answer from an npc if an entity is alive

Discussion in 'Spigot Plugin Development' started by Froyzz, May 25, 2016.

  1. Hey guys!
    I did an specific entity who respawn every 1 day, so i want to create an npc ( with citizens api )who say if the entity is alive or not.
    But i don't know how to add an event who check if the specific entity is alive or not in my event who say the message when i right click on the npc. May you help me please :(
    I need to define herobrine but i can't because my event is NPCRightClickEvent and not living entity
    Code (Text):

        @EventHandler
        public void onRightClickEntity(NPCRightClickEvent e){
                Player p = e.getClicker();
                NPC npc = e.getNPC();
                Entity herobrine = ???
                      if (npc.getFullName().equalsIgnoreCase("Boss-Info")){
                          if (herobrine.isAlive){
                            p.sendMessage("win");
                          } else {
                              p.sendMessage("lose");
                          }
                        }
                    }
                        }
     
     
    #1 Froyzz, May 25, 2016
    Last edited: May 25, 2016
  2. You could make another event for EntityDeathEvent and check if the "Boss" was the entity that was killed; if it was, then make a boolean variable that switches from false to true and then in your original EntityClickEvent just check the boolean variable.
     
  3. Code (Text):

        @EventHandler
        public void onBossDeath(EntityDeathEvent e){
            Entity herobrine = e.getEntity();
            if (herobrine instanceof PigZombie){
                if (herobrine.isCustomNameVisible());
                if (herobrine.getWorld().getName().equalsIgnoreCase("Herobrine"));
                Entity boss = e.getEntity();

               
            }
           
        }
        @EventHandler
        public void onRightClickEntity(NPCRightClickEvent e){
                Player p = e.getClicker();
                NPC npc = e.getNPC();
                      if (npc.getFullName().equalsIgnoreCase("Boss-Info")){
                          if (boss.isAlive){
                            p.sendMessage("win");
                          } else {
                              p.sendMessage("lose");
                          }
                        }
                    }
                        }
     
    I did this, but now how can i define the herobrine ?
     
  4. What do you mean "define the herobrine"?
     
  5. I mean how i can define the "boss.isAlive){"
    the variable boss does not exist for the second event, just for the first and i want to use the first variable in the second
     
  6. You could create a boolean variable in your main class called "bossAlive = true;". Then, when ever you want to check if the boss is alive or not, just check to see if that variable is true or false.
     
  7. i typethis on my main class
    boolean bossAlive = true;
    and this in my events class
    Code (Text):
                if (herobrine.getWorld().getName().equalsIgnoreCase("Herobrine"));
                Entity boss = e.getEntity();      
               
            }
           
        }
        @EventHandler
        public void onRightClickEntity(NPCRightClickEvent e){
                Player p = e.getClicker();
                NPC npc = e.getNPC();
                        if (npc.getFullName().equalsIgnoreCase("Boss-Info")){
                            if (bossAlive = true);
                            p.sendMessage("win");
                            } else {
                                p.sendMessage("lose");
    It still doesn't works and show me errors
     
  8. By the looks of it, I am going to say you are somewhat new to Java/Spigot and have probably watched videos on youtube about how to make Spigot plugins. Firstly, and I think most people on here agree, YouTube tutorials for Spigot are a bad idea, reading about how to do something and understanding the API will get you much further.

    What you are trying to do is access a variable from another class, there are a couple ways you can go about this. your line here:
    Code (Text):
    if (bossAlive = true);
    has a few things wrong with it.




    Firstly, just typing in bossAlive won't do you any good because there is no bossAlive variable in your event class. You can fix this a couple ways. 1) Make the bossAlive variable in your main class static (not the technically proper way for OOP programming), or make an instance of your main class in your event class (Google how to do that or ask me after if you don't know how). Then you can access the variable by doing:
    Code (Text):
    if (Main.bossAlive == true)




    Secondly, there is a difference between one('=') and two equal signs('=='). One equal sign is an assignment operator, it assigns or gives something a value. Example:
    Code (Text):
    int aNumber = 32;  //One equal sign, assign aNumber to 32

    aNumber = 1; //Changed aNumber to 1
    Two equal signs is a comparative operator, it is the operator you need to use in if-statements because you are trying to check if something is equal to something else. Example:
    Code (Text):
    if (booleanVariable == true) {  //Two equal signs to check if something is equal to somethig else
        //do stuff
    }



    Lastly, you have a semi-colon right after your if-statement without any logic for it to perform. At the moment this line:
    Code (Text):
    if (bossAlive = true);
    does nothing because you have no brackets after the if-statement to do anything.

    In my opinion it seems like you might have to review some of the more basic logic of Java before attempting Spigot (I know it's not the response anyone wants to hear).

    The correct format for your if-statement (assuming you have made the variable static or passed the instance correctly) would be:
    Code (Text):
    if (Main.bossAlive == true) {
        //do stuff
    } else {
        //do other stuff
    }
     
  9. Thank you very mush, i have understand now

    Code (Text):
        @EventHandler
        public void onBossDeath(EntityDeathEvent e){
            Entity herobrine = e.getEntity();
            if (herobrine instanceof PigZombie){
                if (herobrine.isCustomNameVisible()){
                if (herobrine.getWorld().getName().equalsIgnoreCase("world")){
                Entity boss = e.getEntity();
                if (boss.isDead()){
                    Main.bossAlive = false;
                }
                    if (!(boss.isDead())){
                    Main.bossAlive = true;
                    }
                }
                }
                    }
                }
           
           
       

        @EventHandler
        public void onRightClickEntity(NPCRightClickEvent e){
                Player p = e.getClicker();
                NPC npc = e.getNPC();
                        if (npc.getFullName().equalsIgnoreCase("Boss-Info")){
                            if (Main.bossAlive == false) {
                            p.sendMessage("Boss dead (false)");
                            } else {
                                p.sendMessage("Boss alive (true)");
                            }
                            }
                       
                    }    
    There is an error, i think that the event EntityDeathEvent is not the good event for that.
    They always say that the boss is dead, if i change false by true they say me that the boss is alive
     
  10. You made two copies of the event entity:
    Code (Text):
    Entity herobrine = e.getEntity();
    Entity boss = e.getEntity();
    Not an error causing issue, just redundant.

    Perhaps some of your if-statements are not correct and are stopping your code from making it all the way to changing the boolean variable in the OnDeathEvent. Try sending messages after each if-statement to see if you are making all the way down your if-statement chain.
     
  11. No
    The boss is the entity next to all the conditions, so if all the conditions are ok ( entity is pigzombie, name herobrine and in the world world ) so the entity in the entity death event is the boss. And the Entity herobrine is just the entity before the conditions.
    No ?
    In my main it's that:
    public static boolean bossAlive ;
    is public static boolean bossAlive true|false; better ?
     
  12. The two entity variables you have are the same entity. You are just duplicating them with different variable names, but they are being set to the exact same thing: 'e.getEntity()'.

    In your EntityDeathEvent, have your code broadcast a message or send a message to the console after each if-statement. That way you know if you are making it all the way down your if-statement chain.
     
  13. Okay i did, that's works
    But the entity death event works only when the boss die, the npc say me that the boss is died.
    If i reload, they will say me that he is alive and he is not, i kill him and they say me that he is dead
    The problem is, when he is alive i need to change the event so, entity death event is not always active, just when he die it works, but not when i summon him by my egg.
    The broadcast came when i kill him too, they're not spamming, is it possible to refresh the entity death event every 5 seconds ?
     
  14. Ahhhh, ok. All variables get reset upon any server restart unless you save them to a config file. You will have to save your boolean variable to a config file and then load it in onEnable(). That way the boolean variable will stay the same through server restarts.
     
  15. Sure, but there is a second problem, if i summon him, the event doesnt works too, i need to create an other event for that ?
    And how can you save a variable into a config file ?
    And thank you so much man for your great help !
     
  16. If you summon him with a custom command then just make sure to reset the boolean variable. If not, you can make a custom command that just swaps the value of the boolean variable. If you go on google there are many guides for creating config files.
     
  17. Thank you, yes i summon him by a command
     
  18. Code (Text):
                            if (this.nhi.getConfig().getString("Valeur").contains("false")){
    It makes me error in the console, is it normal ?
    i type
    Code (Text):
                    if (this.nhi.getConfig().getString("Valeur") == "false"){
                        this.nhi.getConfig().getString("Valeur").replace("false", "true");
    for the first event
     
  19. You can probably make a new thread for your config issue as this is not related to your original problem.
     
  20. Okay, thank you so much for all