Reputation: 2188
Basically, I have a game and this is how I manage who's the owner and who's a moderator.
public static final String[] ADMINS = {"test", "jonathan", "bob"};
And that line is in Settings.java
In Commands.java how would I make a command
case "giveadmin":
//write the users name to list
return true;
That physically changes the array and adds the name to it. For example, say I used the giveadmin command on a player named jerry. How would I add code so that in Settings.java it changes this array
public static final String[] ADMINS = {"test", "jonathan", "bob"};
To this
public static final String[] ADMINS = {"test", "jonathan", "bob", "jerry"};
And no I just don't wanna append the name like every other array, I want to physically see the name jerry added to the array when I open the class.
Upvotes: 0
Views: 215
Reputation: 893
A program that changes its own source code is a very bad idea. Not only would it be difficult to do this in the first place, you wouldn't be able maintain a consistent game. Whenever an admin is added, the code would have to be recompiled (time consuming process) and then the game relaunched.
A better way is to store all of this information in a config/XML file. Something of the format
ADMINS= test, jonathan, bob
MODS= kyle, zach, bill
With this, you can read the file line by line at startup, and String.split() on each line to parse the names into an array.
When you add an admin, you just have to create a new array and copy old values with the new value appended and write it to the appropriate spot in the config file.
Upvotes: 1
Reputation: 21773
I would not recommend doing this. Instead, write a file which is a delimited (by new lines for example) list of all admin usernames.
When your program starts up, read the file and create an ArrayList<String>
(or other collection) of every name in the file.
When you want to add a new name, open up the file, append it to the end, save it, and also add it to the ArrayList<String>
. You must open the file up again in case the file has been modified concurrently by another instance of your program (or otherwise).
This has the advantage that non-technical people who can't read java can still edit the admins text file with notepad.
Upvotes: 1