Need Help with Variables!

Discussion in 'Plugin Development' started by yellowface7, Dec 9, 2018.

  1. Offline

    yellowface7

    Hello! I am new to Bukkit and Java but I am no stranger to modding games. I am more used to JavaScript and other languages.

    The plugin I am making will have an integer variable that will be called to a lot of listeners. How should I handle implementing it? Should I put the variable in it's own package? Should I create an integer variable in the main java file? Or is there something else I need to do?

    Another question I have is after I register this variable, how should I call it in listeners? Should I do a public int variable name in the listener and have the modifiers at the respected spots? I am quite confused here.

    Some help here would be appreciated. :3
     
  2. Online

    timtower Ninja on the waves Moderator

  3. Offline

    yellowface7

    My plan is to make a class for each player action and each of those classes will contain listeners. I am currently on the first class. I have the listener complete but I do not have the variable registered yet.
     
  4. Offline

    DutchJellyV2

    First of all Id like to clarify that I might be misunderstanding this.

    To use a variable across more listenerclasses, I'd firstly store the variable privately in the (main) class that constructs the listenerclasses into objects and registers them to the server. Doing this you can make the constructors of the listenerclasses accept the variable stored in your main class. This way you can pass the integer value stored in your main class to the listenerclasses using their constructor.

    Passing your integer value to a listenerclass should be along the lines of:

    Code:
     
    private int yourprivateint;
    @Override
    onEnable() {
    ...getPluginManager.registerEvents(this, new listenerclass(yourPrivateInt)) ;
    } 
    
    This probably isnt a good explanation and therefor I suggest looking up what constructors are and how objects work.

    Disclaimer: its 3:34 am and Im typing this hopefully helpful response on my phone out of boredom.

    Verstuurd vanaf mijn COL-L29 met Tapatalk
     
    Last edited: Dec 22, 2018
  5. Offline

    yellowface7

    Wow! That really helps clear things up! I should read up more on constructors.
     
  6. Offline

    yellowface7

    Okay, so I had a crash course on Java and I have a question...

    Did I set this up correctly?

    On my main class, I have this:

    Code:
        public void registerEvents() {
            PluginManager pm = getServer().getPluginManager();
           
            pm.registerEvents(new BlockBreak(), this);
        }
        public int getCredit() {
            return Credit;
        }
    
        public void setCredit(int credit) {
            Credit = credit;
        }
        private int Credit;
    }
    This is what is in my block breaking listener for wood:

    Code:
    public class BlockBreak implements Listener {
    
        private int Credit;
    
        @EventHandler
        public void onBlockBreak(BlockBreakEvent event) {
            Block block = event.getBlock();
            Material material = block.getType();
           
            if(material != Material.WOOD) {
                setCredit(+1);
            }
        }
    
        public int getCredit() {
            return Credit;
        }
    
        public void setCredit(int credit) {
            Credit = credit;
        }
    }
    I feel like I may be doing something wrong here.
    Would this store the value for each individual player?
    Would this save the value?

    Background: This plugin will watch the players' stats in their early days
    on the server, then it will assign the player to a town with a job according
    to their highest score in the stats. The system will tally up their score and
    place similarly scored players in the same town.

    Credit is the score.
     
  7. Online

    timtower Ninja on the waves Moderator

    @yellowface7 HashMap will be able to store everything for each individual player.
     
  8. Offline

    DutchJellyV2

    Setcredit(+1) doesn't increment the value but sets it to 1. It also would increment the variable in your eventhandle class. I'm assuming that you want to store the credits in the main class. To do that, you could pass an instance of the main class called mainClass to your eventClass instance. So to increment the credits in the main class you'll have to do something along the lines of:
    Code:
    mainClass.setCredit(mainClass.getCredit()+1);
    
    To store a credit integer for each player you could use HashMap to map a player's uuid (the key in the hashmap) to an integer value (the value that is mapped to a key).

    I'd like to add that I think you're making alot of progress! Good luck with the next bit!
     

Share This Page