Solved Help with data storage in class

Discussion in 'Plugin Development' started by AsapRocky, Jan 15, 2017.

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

    AsapRocky

    Hello!
    I am new to programming and I'm currently learning Java and at the same time training by programming bukkit plugins.
    Here is my problem:

    I want to make a practice type plugin.Where you duel someone, get teleported to an arena, fight, respawn, etc.
    I made a Class named DuelState where I want all data stored for every player, and access the data later on in any other class of the plugin.

    Problem is how do I do this? How do I access all the data (boolean inFight, boolean negotiating, int bestOf, etc..) of a player? Like a hashmap where the key is a player's name, and the values are all the data stated above.
    Example: Later on I want to get or set the inFight of a player named "Notch" in the class Main.

    Here is the code:
    Code:
    public class DuelState {
    
       
        Player player1;
        Player player2;
        boolean inFight;
        boolean negotiating;
        int bestOf;
        String kitType;
       
       
        public DuelState(Player player1, Player player2, boolean inFight, boolean negotiating, int bestOf, String kitType) {
            this.player1 = player1;
            this.player2 = player2;
            this.inFight = inFight;
            this.negotiating = negotiating;
            this.bestOf = bestOf;
            this.kitType = kitType;
           
           
        }
       
        public void setInFight(Player p) {
            this.player1 = p;
            this.inFight = true;
        }
       
        public boolean isInFight() {
            return inFight;
        }
       
        public boolean isNegotiating() {
            return negotiating;
        }
       
        public int getBestOf() {
            return bestOf;
        }
       
        public String getkitType() {
            return kitType;
        }
       
    }
    Help would be much appreciated as I am currently blocked on this problem.
    Cheers!
     
  2. Offline

    JanTuck

    Well i believe you already know how to make a new duel state
    Code:java
    1.  
    2. DuelState duelState = new DuelState( ... );
    3.  


    You could do this in different ways like making a function for each info you want to get like.

    I would recommend.
    Code:java
    1.  
    2. public Player getPlayerOne()
    3. {
    4. return player1;
    5. }
    6.  

    And retrieve it like this.
    Code:java
    1.  
    2. Player playerOne = duelSate.getPlayerOne();
    3.  

    And then changing all the info to a private variable.

    Or you could make the player1 variable public.
    Code:java
    1.  
    2. public Player player1;
    3.  

    And retrieve it like this.
    Code:java
    1.  
    2. Player playerOne = duelState.player1
    3.  
     
  3. Offline

    AsapRocky

    Thank you for the fast response!
    I still have one question as I don't understand very well.
    How would I instantiate the data for player "Notch" in the class Main, and then modify lets say that same player's inFight boolean variable in the class DuelCommand ?
     
  4. Offline

    JanTuck

    @AsapRocky

    You would have to store the DuelState with the players uuid or something. Like in a hashmap.
     
  5. Offline

    AsapRocky

    Okay I understand now!
    Thank you so very much!
     
  6. Offline

    kameronn

    @AsapRocky
    Use a map when you're storing the duelstate with the uuid
     
Thread Status:
Not open for further replies.

Share This Page