Void in seperate class

Discussion in 'Plugin Development' started by BurnerDiamond, Jan 23, 2015.

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

    BurnerDiamond

    I have a thing which checks if a players health is below zero, than setHealth to 10 and etc.

    Is there any way I can make my own separate class for the void?
     
  2. Offline

    sirrus86

    You mean a check to see if the player is in the void? You could check if their y position is less than 0.
     
  3. Offline

    BurnerDiamond

    I basically want to make a "checker" class. It would check if a players health is ever 0 and if it was, set the health to 20.

    The class would have to be in a seperate one and be able to connect to the main.

    @sirrus86
     
  4. Offline

    CraftCreeper6

    @BurnerDiamond
    You can use instances / constructors to 'connect' with classes. You can simply create a public void and make the params the Player. Check the players health, if below x then set to y.
     
  5. Offline

    SuperOriginal

    @BurnerDiamond Like a utility class? You could most likely just have a static method because you don't need an instance for it
     
  6. Offline

    BurnerDiamond

    May I have an example on how to make an instance for health. I'm not really an expert in this field. Even a tutoiral but an example would be much appreciated!

    Would it be something like:

    Code:
    public void Health(Player p) {
      if(p.getHealth >=1) {
        p.setHealth(20.0);
    
    
    If it is like this how would I make an instance of it to my kit/main class?

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

    CraftCreeper6

    @BurnerDiamond
    Nope, somethings are wrong.
    • Missing two brackets
    • Your method doesn't follow Java Naming Conventions.
    Otherwise, perfect!
     
  8. Offline

    BurnerDiamond

    @CraftCreeper6

    The two brackets is an easy fix.

    The Java Naming Conventions I don't understand

    The p.getHealth is undefined for the type player?
     
  9. Offline

    CraftCreeper6

    @BurnerDiamond
    Could you screenshot the method please? (Hover over the methods error)
     
  10. Offline

    BurnerDiamond

    Last edited by a moderator: Jun 13, 2016
  11. Offline

    CraftCreeper6

    @BurnerDiamond
    What kind of Bukkit are you using? Standard Bukkit or Craftbukkit?

    MyClass myClass = new MyClass();
     
  12. Offline

    BurnerDiamond

    I was using craftbukkit ;)! Changed it ot bukkit.

    Okay. What if I wanted this to work only in a certain world?

    Say that the void only worked in a specific event?

    @CraftCreeper6
     
  13. Offline

    CraftCreeper6

    @BurnerDiamond
    You can check the players world before hand. myPlayer.getWorld().getName();
     
  14. Offline

    BurnerDiamond

    Okay so basically I would do this

    Code:
    if(p.isinworld(sometjimg) {
      Death death = new Death();
    
      
    And it would check everything

    But what if i wanted to check if a player was in world without doing anything=

    So basically

    if(p.isinworld()) {
    Do the instance death;

    And the p.isinworld will work percectly any way?

    @CraftCreeper6
     
  15. Offline

    CraftCreeper6

    @BurnerDiamond
    No, you would need to refer to your method. death.myMethod.
     
  16. Offline

    BurnerDiamond

    Would I also have to register my check player location?

    public void Location(Player p) {
    if(p.getlocation.getworld() == something {
    death.Health;

    @CraftCreeper6
     
  17. Offline

    CraftCreeper6

    @BurnerDiamond
    I do not quite understand what you are saying.
    It should look like this:
    Code:
    public void myMethod(Player player){
    if(player.getWorld().getName().equals("myWorld")){
    if(player.getHealth <= 1){
    player.setHealth(20);
    }
    }
    }
     
  18. Offline

    PreFiXAUT

    @BurnerDiamond 1. Java Naming Conventions for methods and fields -> lowerCamelCase. Start with a lowercase word like "get" and then, every next word is simply put togehter but the first Character uppercase.
    Code:
    public void mysupercoolmethodiwouldliketouse()
    // Should be called
    public void mySuperCoolMethodIWouldLikeToUse()
    Classes/Interfaces/... should use UpperCamelCase -> Basicly the same as lowerCamelCase, just that you start with an UpperCase Character right away.
    2.
    You can't create Methods in a Object you didn't create by yourself (only if you have the SourceCode and other stuff but that doesn't matter now). Use a custom Class like you did and simply call the Method from the Class (Either way use statics or get a Instance of the Class so you can access them).

    Kinda Offtopic but: You should do the Java-Learning somewhere else as in a public Forum ;)
     
  19. Offline

    BurnerDiamond

    @CraftCreeper6

    I' re got the void that is in a seperate class, something like this...

    Code:
    public class Health
    
      public void changeHealth(Player p) {
        //do stuff here
    
    
    The problem is I don't know how to initalze it so that it runs all the time.

    I' ex got my main class up and running but it doesn't seem to work,

    Do I need to put something in my main class?
     
  20. Offline

    1Rogue

    I have a counter-question: Why? How would doing this cause more benefit than complication in your code?
     
  21. Offline

    BurnerDiamond

    @1Rogue

    I just want it to happen all the time. I'll need it for a future plugin. Sure I could just put it in an event handler. But I want it to check every time. I just want it basically looping each time. Or checking each time if a player's health is 3.

    I have three things I could try.

    A bukkit runnable
    Place it in an event handler
    Make a for loop

    Is there no way just to check just if a player's health is 1 and if it is then do my void.

    @1Rogue

    I just want it to happen all the time. I'll need it for a future plugin. Sure I could just put it in an event handler. But I want it to check every time. I just want it basically looping each time. Or checking each time if a player's health is 3.

    I have three things I could try.

    A bukkit runnable
    Place it in an event handler
    Make a for loop

    Is there no way just to check just if a player's health is 1 and if it is then do my void.

    Or maybe I could just make it static...

    I dont' know if I have to join it to the main class though.

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

    Fuzzybear04

    itll cause a lot of lag if you loop through every player on the server like every 1/2 second
     
  23. Offline

    BurnerDiamond

  24. Offline

    Fuzzybear04

    using the EntityDamageByEntityEvent, check if the damage dealt is more than what the player getting hit's health is / the players health is less than the damage, then if it is cancel the event, set their health to 20 and tp them somewhere?
     
  25. Offline

    BurnerDiamond

    @Fuzzybear04

    That'so what I though in the first place but since I'm fairly new to Java I thought there was a way for me to constantly initialze it after a couple ticks.

    Now that I think about it an EntityDamageEvent should work.

    I could just do the entity damage event. Check if it's a player and pass an instance of my method to the event.

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 13, 2016
Thread Status:
Not open for further replies.

Share This Page