0

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.

10
  • You actually want the program to modify its own source code? Commented Jan 24, 2013 at 0:44
  • 7
    @Jonathan: Ok, but that's probably not what you want. Even if you could do that, it wouldn't have any useful effect, because you'd also have to recompile etc. You should strongly consider using a database/config-file. Commented Jan 24, 2013 at 0:46
  • @Jonathan: Do you want this to happen at pre-compile time? or at run time? Commented Jan 24, 2013 at 0:47
  • 6
    When 3 people tell you the same thing, you should consider listening :/ Commented Jan 24, 2013 at 0:49
  • 1
    @mre: dude not called for. Commented Jan 24, 2013 at 1:09

2 Answers 2

1

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.

Sign up to request clarification or add additional context in comments.

Comments

1

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.