Solved Am I referencing this boolean variable right?

Discussion in 'Plugin Development' started by croc122, Nov 1, 2014.

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

    croc122

    I am new to Bukkit plugin development and still have a lot to learn about Java. I am attempting to write a configurable MobShooter plugin that let's you choose the "ammo" that fires from a bow. Determining the best approach has proven difficult, but I'm determined to learn!

    However, I am stumped as to why my variable is not being recognized within the same class file it is defined. Here is the entire class. On line 24 it gets an error in Netbeans and asks if I want to create a local variable by that name, but as you can see, it already exists in the onEnable function.

    Code:java
    1. package me.croc122.mobshooter;
    2.  
    3. import java.util.ArrayList;
    4. import org.bukkit.configuration.file.FileConfiguration;
    5. import org.bukkit.entity.Entity;
    6.  
    7. public class Mobs {
    8. public void onEnable() {
    9. FileConfiguration config = getConfig();
    10. boolean arrow = config.getBoolean("arrow");
    11. boolean cow = config.getBoolean("cow");
    12. boolean chicken = config.getBoolean("chicken");
    13. boolean sheep = config.getBoolean("sheep");
    14. boolean pig = config.getBoolean("pig");
    15. boolean ocelot = config.getBoolean("ocelot");
    16. boolean wolf = config.getBoolean("wolf");
    17. boolean mooshroom = config.getBoolean("mooshroom");
    18. }
    19.  
    20.  
    21. public void mob0(Entity entity)
    22. {
    23. ArrayList<String> moblist = new ArrayList();
    24. if(arrow = true)
    25. {
    26. moblist.add("arrow");
    27. }
    28. }
    29.  
    30.  
    31. }
     
  2. Offline

    CraftCreeper6

    croc122
    Create your booleans above your onEnable but don't initialize them, then in your onEnable initialize them :)
     
  3. Offline

    croc122

  4. Offline

    Totom3

    croc122 It's because your booleans are local variables. A variable is called local when it is declared within a method. Because of that, they are only accessible from the block where they were created, here the entire method (if they were declaring within a if statement block, they wouldn't be accessible outside of it). If you want to access them outside of the method, you'll have to declare it in the class, in which case they'd be called fields, and you will be able to access them from the class and sometimes from other classes

    Also, what is the mob0() method ?

    EDIT: by using this approach people can set 2 paths in the config to true, and your code might break or not behave correctly. If your ammo is going to be an Entity, you could extract a String from the config, which represents an EntityType. This is a bit more advanced though.
     
  5. Offline

    croc122

    Totom3 the mob0 method is where I am going to add the enabled mobs to an array list so the plugin can know which mobs the server owner wants enabled in the MobShooter menu.

    Also, can you give me an example of using "fields".
     
  6. Offline

    Totom3

    croc122 What CraftCreeper6 basically. You should know how to do this.

    Code:java
    1. class YouClass extends JavaPlugin {
    2. private EntityType myEntityAmmo = EntityType.COW; // A field
    3. private boolean myBoolean; // Another field that is not initialized yet ( = null)
    4. // These two can be accessed by all the methods and fields from this class
    5. }
     
  7. Offline

    croc122

    Totom3 CraftCreeper6 So basically when the plugin is enabled at server startup, I should add all my boolean variables that return true to an ArrayList?
     
  8. Offline

    Watto

    croc122

    Also the if statement 'arrow = true' won't work because 1 equals sign is assigning so technically you're assigning arrow to true which doesn't work anyway because you're doing it in an if statement.

    When checking if a boolean is true you just put it in the if statement like this;

    Code:java
    1. if(arrow)
    2. //this is true
    3.  
    4. if(!arrow)
    5. //this is false
     
  9. Offline

    croc122

    Watto oh thanks I forgot about the double equal sign
     
  10. Offline

    Watto

    croc122
    Don't use the double equal sign though. It's pointless, use the example i provided before.
     
  11. Offline

    Totom3

    I don't get why you'd do that if you can only have 1 'ammo' entity. In which case you should just store an EntityType.
     
  12. Offline

    croc122

    Totom3 well the plugin is called MobShooter, so the plan was to add all the passive mobs as selectable ammo.
     
Thread Status:
Not open for further replies.

Share This Page