Changing a boolean from another class

Discussion in 'Plugin Development' started by xXMaTTHDXx, Aug 9, 2013.

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

    xXMaTTHDXx

    Well I am trying to do exactly what the title explains, I have a boolean in my game class like this
    Code:
    public class game {
        private OITC plugin;
       
        public game(OITC plugin){
          this.plugin = plugin;
        }
        public void StartGame(boolean start)
        {
        start = false;
        }
    }
    What I need to do is turn that start variable to true in my main class here
    Code:
    @EventHandler
        public void onJoin(PlayerJoinEvent e){
            Player p = e.getPlayer();
            if(Bukkit.getServer().getOnlinePlayers().length >= 1){
               
                Bukkit.getScheduler().scheduleSyncRepeatingTask(this, new Runnable(){
     
                    @Override
                    public void run() {
                        timer--;
                        if(timer <=0){
                            Bukkit.getScheduler().cancelTasks(plugin);
                            timer = 10;
                            //need to turn true here!
                           
                        }
                    }
                   
                   
                },0L , 20L);
               
            }
    Any ideas, Thanks!
     
  2. Offline

    Everdras

    In the method StartGame, the boolean start is in the local scope. It does not exist in the main class and it will be garbage collected once the method exits. Literally, all the method does is this:

    1. Create local variable named 'start'
    2. Since it's a primitive type (boolean), it's passed in by value. So the value is copied into 'start'
    3. You assign 'start' to false.
    4. The method returns and 'start' passes out of scope. Its memory is deallocated and returned.

    So not only does start not do anything, it's never accessible outside of the method and it's never saved anywhere that lives beyond the method.

    I think an easier way to fix your problem would be to ask: What are you trying to do here, in general terms? Describe what this part of your plugin is doing.
     
  3. Offline

    xXMaTTHDXx

    Well I recently learnt(Practicing) how to make plugins like Infected or one in the chamber is what I am doing right now. I have notes for my steps that I am following, here they are.
    1. make a game class with Boolean start then Listen for on join(no game started)

    2. Check when the player joins if there is enough players to start a game

    3. If there is use SyncRepatingTask to count down to start game

    4. Set Boolean start to true

    5. For storing respawns use CodPlayers class

    6. Then if in mid game and someone dies or leaves check if there is enough players to play if so continue if not stop the game.

    Everdras I am basicly trying to make the skeleton to my game or one in the chamber.
     
  4. Offline

    Everdras

    Code:java
    1. public class Game {
    2. private boolean started;
    3.  
    4. public Game() {
    5. started = false;
    6. }
    7.  
    8. public boolean isStarted() {
    9. return started;
    10. }
    11.  
    12. public void setStarted(final boolean started) {
    13. this.started = started;
    14. }
    15. }


    Would be your Game class.

    To set the game's state to started, simply call setStarted(true);

    The layout of the event listener class should look like this:

    Code:java
    1. public class PlayerJoinGameListener implements Listener {
    2. private Game currentGame;
    3.  
    4. public PlayerJoinGameListener(Game g) {
    5. this.currentGame = g;
    6. }
    7.  
    8. @EventHandler
    9. public void onPlayerJoin(PlayerJoinEvent e) {
    10. //whatever your game-beginning logic is
    11. currentGame.setStarted(true);
    12. }
    13. }
     
    xXMaTTHDXx likes this.
  5. Offline

    xXMaTTHDXx

    Thanks so much Everdras! I am going to jot all this down so I can continue making the plugin!
     
  6. Offline

    flaaghara

    In class game have a boolean instance field named start. Create accessor and mutator methods for start and in your main class write and/or read that boolean.
     
  7. Offline

    xXMaTTHDXx

    Thanks for the help but Everdras beat you to it :p
     
Thread Status:
Not open for further replies.

Share This Page