Question

it happens that I'm making a bukkit arena plugin. I have an Arena class, with an enum for its status. The default status is Status.disabled. I have a method that I call on the on-Enable, which sets the arena to Idle Status. The Problem is that after I call the method for every arena, the arena appears as Disabled status again. Here is part of my Arena class:

public class Arena {
    protected final Main main;

    public static enum Status{
        Idle,
        InLobby,
        InCountdown,
        InGame,
        Disabled;
    }

    private Status status = Status.Disabled;
    String name;
    Location lobby;
    Location start;
    Location end;
    List<Location> checkpoints = new ArrayList<Location>();
    int lives;
    int time;

    public Arena(Main main, String name, Location lobby, Location start, List<Location> checkpoints, Location end, int lives, int time)
    {
        this.main = main;
        this.name = name;
        this.lobby = lobby;
        this.start = start;
        this.checkpoints = checkpoints;
        this.end = end;
        this.lives = lives;
        this.time = time;
    }

    public void setStatus(Status statuss){
        this.status = statuss;
    }

    public final Status getStatus(){
        return status;
    }

    public final void onEnable(){
        this.status = Status.Idle;
        System.out.println(this.getStatus() + " Trying status set on OnEnable. This is made from an Arena class.");
        //The message above says status is Idle...
    }
}

Now, here's my onEnable method, which is on my Main class that extends to JavaPlugin:

ArenaManager manager;
public void onEnable(){
    PluginDescriptionFile pdfFile;
    pdfFile = this.getDescription();
    getLogger().info(pdfFile.getName() + pdfFile.getVersion() + " has been enabled!");
    manager = new ArenaManager(this);
    PluginManager pm = this.getServer().getPluginManager();
    pm.registerEvents(new events(this), this);
    getConfig().options().copyDefaults(true);
    saveDefaultConfig();
    for(Arena arena : manager.getAllArenas()){
        arena.onEnable();
    }
    getLogger().info("Plugin contains " + manager.getAllArenas().size() + " arena(s)!");
    foo();
}

public void foo(){
    for(Arena a : manager.getAllArenas()){
        System.out.println("foo " + a.getStatus());
    }
}

My method called "foo" checks the status for all arenas. The arenas appear as disabled when I try "foo". If this is useful, each arena is saved to a file, so to get all arenas, I use my arenamanager. The method from the arenamanager used in the onEnable is:

public List<Arena> getAllArenas(){
    List<Arena> arenas = new ArrayList<Arena>();
    File[] allfiles = new File(main.getDataFolder() + File.separator + "Courses" + File.separator).listFiles();
    assert allfiles != null;
    for(File f : allfiles){
        FileConfiguration con = YamlConfiguration.loadConfiguration(f);
        String name = con.getString("Name");
        Arena a = getCourse(name);
        arenas.add(a);
    }
    return arenas;
}

I tried printing out the status as well from the PlayerInteractEvent (on a sign), but the arena still shows as disabled. Any idea on how to fix this? There are no errors on the console by the way. Thanks for the help :)

Was it helpful?

Solution

In short:

The status variable does not change because you are not saving a reference to the List of Arena objects that are returned from manager.getAllArenas() that you use to set the Status on. You want to look at your code and find a way to save the state of the Arena object. There are a couple different ways to do this, but I am going to let you decide what is best for you.

The long explanation is below:

When you create an Object in Java, you must hold the object (reference it) somewhere if you want to keep using the same object and save the state.

In your ArenaManager.onEnable() method you call manager.getAllArenas() and set the status field on each Arena object to Status.Idle. Here:

public void onEnable(){
    ...
    for(Arena arena : manager.getAllArenas()){
        arena.onEnable();
    }
    ...
    foo();
}

The logic in manager.getAllArenas() is essentially

1. Create new List<Arena>
2. Get the name of all the Arena objects from File
3. For every File object, create a new Arena object
4. Add new Arena object to List created in step 1.

You do not save this list anywhere when you call manager.getAllArenas() in the ArenaManager.onEnable() method, so once you exit the for loop, all the Arena objects you set the Status on in the Arena.onEnable() method.

When you call foo() later in the Arena.onEnable(). You create a brand new list when you call manager.getAllArenas() again, so this list was not effected by your for-loop that set the status to Status.Idle

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top