1

I am wondering if its possible to combine two strings that I have set -

What I am trying to do is for when the player types the command "/rules add Rule Goes Here" It will add the text "Rule Goes Here" to the config file as a list, the config file will look like this:

Rules:

  • Rule 1

  • Rule 2

The code that I am trying to use for the command is stated below:

if (args[0].equalsIgnoreCase("add"))
      { 
        ArrayList<String[]> list1 = new ArrayList<String[]>();
        List<String[]> c = plugin.getConfig().getList("rules");

        c.list1.addAll(Arrays.asList(args));

        return true;
      }

The "plugin" is the main class of the plugin, so where everything gets fired up to use. And thats where the config is generated.

I have attempted to use this code that I had found on the bukkit forums - plugin.getConfig().getList("rules").add(args); But no luck as I got the error stating:

The method add(capture#4-of ?) in the type List<capture#4-of ?> is not applicable for the arguments (String[])

Now might be a stupid question to ask, and most likey is, but I am still a beginner of course. If you could please assist me with this, id be very grateful. Thank you!

5
  • 1
    What is plugin? And can you clarify "what the player inputs for the rule" Commented Aug 2, 2016 at 21:55
  • 2
    @cricket_007 Looks like the plugin object for a Minecraft Bukkit plugin to me. Commented Aug 2, 2016 at 21:58
  • 1
    The code you've added doesn't make a lot of sense. You have declared c as a List<?> but the next line is c.list1.add. What are you trying to do here? Commented Aug 2, 2016 at 21:58
  • 1
    Ahh, the smell of a new inexperienced Bukkit programmer ;-D Commented Aug 2, 2016 at 21:58
  • The post is very difficult to understand and the code doesn't help much. Commented Aug 2, 2016 at 22:24

3 Answers 3

2

You need that List<?> to have an actual type, you can't mutate an object that has a wildcard type.

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

1 Comment

Not only this, but it seems that OP thinks list1 is somehow a member of c.
0

This actually is a fun task, although this is nothing you should be doing as a starter of java(writing minecraft plugins without atleast the basics of java won't work)

As for your problem, following will be a way to do this, there might be errors as i do not have a minecraft server I could test it on.

Getting an ArrayList<String> with the rules written in chat by a user is simple:

ArrayList<String> a = new ArrayList<>();
    for(String s : args){
        a.add(s);
    }

Now to add those rules to your config, bukkit does offer a function to set values, set(String, Object) to be precise. Documentation can be found here.

So you could do something like:

plugin.getConfig().set("Rules", a);

If the result is what you want great, if it is not, please be precise on what is wrong with that function.

Also bear in mind that Adding those rules won't do much if no other plugin or your plugin uses "Rules" as the key to access them(to my knowledge atleast).

Edit:

The above is possible, but the following might work as well:

if(plugin.getConfig().getList("rules")){
   plugin.getConfig().getList("rules").add(Arrays.asList(args));
 }

Do not forget that this way can have duplicates! A List and (as far as I know) all it's implementations do not check for duplicate entries!

Comments

0

Maybe this is what you are looking for:

c.list1.addAll(Arrays.asList(args));

But you would need to describe more precisely what you want to do with which lists.

1 Comment

Unfortunately that doesn't solve it, I have edited the post to give an explanation of what I am trying to do.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.