Split command from string?

Discussion in 'Plugin Development' started by AppleMen, Dec 2, 2015.

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

    AppleMen

    Hello,


    I have made a list in my config.yml which contains commands (that are randomly chosen) to execute when someone votes (1 command). However, I want to be able to put a second command after the 1st one and execute that one as well.

    This is my config now:
    Code:
    # [Name]|[Command](|[amount]) (+[name]|[2nd command](|[amount]))
      Voterewards:
      - Creeper Spawner|give %player% 52:50 1 #Creeper
      - Skeleton Spawner|give %player% 52:51 1 #Skeleton
    
    Look close to the description above. It now splits the name of the reward, the command and the amount. But how can I split it to have multiple commands on 1 line?

    This is my code now: (It does look messy indeed, just trying many ways...)
    Code:
    String[] splitCommand = cmd.split("\\|");
                                String rewardName = splitCommand[0];
                                String newCmd = splitCommand[1];
                                String amount = "";
    
                                /*if(splitCommand.length == 2) {
                                    if(splitCommand[2] != null)
                                        amount = splitCommand[2];
                                }*/
    
                                System.out.println(splitCommand.length);
    
                                String[] splitSecondCmd;
                                String secondRewardName = "";
                                String secondAmount = "";
                                if(splitCommand.length > 2 && splitCommand[3] != null) {
                                    splitSecondCmd = newCmd.split("\\+");
                                    secondRewardName = splitSecondCmd[0];
                                    String secondNewCmd = splitSecondCmd[1];
                                    secondAmount = splitSecondCmd[2];
    
                                    Bukkit.dispatchCommand(Bukkit.getConsoleSender(), newCmd.replaceAll("%player%", p.getName()));
                                    Bukkit.dispatchCommand(Bukkit.getConsoleSender(), secondNewCmd.replaceAll("%player%", p.getName()));
                                } else {
                                    Bukkit.dispatchCommand(Bukkit.getConsoleSender(), newCmd.replaceAll("%player%", p.getName()));
                                }
    
     
  2. Offline

    CraftCreeper6

  3. Offline

    AppleMen

    I am for-looping over the prices. It takes 1 randomly and it will be executed. I am looking for an option so I can put 2 commands 'toghetter' to be executed both.
     
  4. Offline

    CraftCreeper6

    @AppleMen
    Select more than one is what I meant. For loop over again.

    EDIT: Two prizes together? Set your key as the prize name and put a list of the prizes inside it, then for loop over the keys instead, which ever is selected get the list for it.
     
  5. Offline

    AppleMen

    And how would I look to the split? Or specify the command
     
  6. Offline

    CraftCreeper6

  7. Offline

    AppleMen

    I want to get the 2 commands. How would I do that with a for-loop?

    So this list:
    Code:
    Voterewards:
      - Diamond sword|give %player% diamond_sword 1
      - Creeper Spawner|give %player% 52:50 1 #Creeper
      - Skeleton Spawner|give %player% 52:51 1 #Skeleton
      - Other commands
    
    And when it randomly chooses the Creeper Spawner for example, it should also select the Skeleton Spawner because I want to have them toghetter for example.
     
  8. Offline

    Scimiguy

    Just put all your options into an array, grab a random one.
    Remove that element, then grab another random one
     
  9. Offline

    CraftCreeper6

  10. Offline

    AppleMen

    I am stuck.. So I tried a different way of doing this:

    I have a string in my config which could contain multiple commands:
    Code:
    name:Name command:msg %player% 1stcommand|msg %player 2ndcommand
    It contains a name and a command with semicolons. Now I want to be able to get the parts behind 'name:' and 'command:'. And if the command part contains multiple commands (split by an '|' sign) I want to get both commands.

    How can I do this?
     
  11. Offline

    mcdorli

    String[] strings = string.split("|");
    if (strings.length > 1) {
    do your thing
    }
     
  12. Offline

    AppleMen

    How can I get the part behind 'name:' and 'command:'?
     
  13. Offline

    mcdorli

    Split, where there is a space, then you probably know, where everything is
    0.: the name
    1.: commands
    then split the commands, and voilá, you're done.
     
Thread Status:
Not open for further replies.

Share This Page