Solved How can I create an arg with multiple words

Discussion in 'Plugin Development' started by xMinecraft, Jan 25, 2013.

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

    xMinecraft

    Hello.
    I try create a command with multiple args but I want this:

    /mycommand arg1 arg2 Hello world!

    I want that "Hello world!" be only one arg.
    So.. the all args working but I don't know: How can I create an arg with multiple words?
    Regards.
     
  2. Offline

    Rprrr

    I have no idea what you mean.
    An argument is one word. You cannot change it to multiple words. Please explain what you're wanting to do.
     
  3. Offline

    xMinecraft

    /mycommand arg arg Hello world!

    How i make Hello world! not be an argument.

    I do not know if I explained well
     
  4. Offline

    skipperguy12

    xMinecraft
    I'm not the best with strings, but a simple work around is that if you know exactly how many args your going to use, you can do

    if(args.length == #)

    and now it will give an error when trying to add more than or less than the number of args.
     
  5. Offline

    xMinecraft

    Sorry but no
    I've the code working but on the latest I want put "Hello world!" but I give error "Internal error"
     
  6. Offline

    Rprrr

    1. Explain what the command should do.
    2. Show us your code.
     
  7. Offline

    william9518

    use a string builder and a for loop to check for 2 spaces. make a string of everything after the second space. that will be ur "hello world" part.
     
  8. Offline

    xMinecraft

    So?:

    Code:java
    1. StringBuilder message = new StringBuilder();
    2. if (args.length > 3) {
    3. message.append(args[2]);
    4. for (int i = 3; i < args.length; i++) {
    5. message.append(" ");
    6. message.append(args[i]);
    7. }
    8. }[/i]
     
  9. Offline

    william9518

    Code:java
    1. StringBuilder string = new StringBuilder();
    2. for (int s = 3; s < args.length; s++) {
    3. if (s > 3) string.append(" ");
    4. string.append(args[s]);
    5. }
    6. String final = string.toString();
    7. [/s]
     
  10. Offline

    xMinecraft

    I write the command:

    /rename 1 1 red Rename item

    And nothing..

    main.java
    Code:java
    1.  
    2. package com.gmail.lopezitospriter.ItemName;
    3. import java.util.logging.Logger;
    4. import org.bukkit.plugin.java.JavaPlugin;
    5. public class main extends JavaPlugin {
    6. private Logger log = Logger.getLogger("Minecraft");
    7. @Override
    8. public void onEnable() {
    9. log.info("[" + getDescription().getName() + "] esta habilitado!");
    10. getCommand("rename").setExecutor(new Comando());
    11. }
    12. @Override
    13. public void onDisable() {
    14. log.info("[" + getDescription().getName() + "] esta deshabilitado! :/");
    15. }
    16.  
    17. }
    18.  

    Comando.java

    Code:java
    1.  
    2. package com.gmail.lopezitospriter.ItemName;
    3.  
    4. import org.bukkit.ChatColor;
    5. import org.bukkit.Material;
    6. import org.bukkit.command.Command;
    7. import org.bukkit.command.CommandExecutor;
    8. import org.bukkit.command.CommandSender;
    9. import org.bukkit.entity.Player;
    10. import org.bukkit.inventory.ItemStack;
    11. import org.bukkit.inventory.PlayerInventory;
    12. import org.bukkit.inventory.meta.ItemMeta;
    13.  
    14. public class Comando implements CommandExecutor {
    15.  
    16. @Override
    17. public boolean onCommand(CommandSender sender, Command cmd, String commandLabel, String[] args){
    18. if(commandLabel.equalsIgnoreCase("rename")) {
    19.  
    20. if (args.length > 4) {
    21. if(sender instanceof Player){
    22. Player p = (Player)sender;
    23. if(p.isOp()){
    24. String arg1 = args[0];
    25. String arg4 = args[2].toUpperCase();
    26. String arg3 = args[3];
    27. PlayerInventory inventory = p.getInventory();
    28. ItemStack itemstack = new ItemStack(Material.matchMaterial(arg1), Integer.parseInt(args[1]));
    29. ItemMeta test = (ItemMeta) itemstack.getItemMeta();
    30. StringBuilder string = new StringBuilder();
    31. for (int s = 4; s < args.length; s++) {
    32. if (s > 4) string.append(" ");
    33. string.append(args[s]);
    34. }
    35. String newnameitem = string.toString();
    36. test.setDisplayName(ChatColor.valueOf(arg4) + newnameitem);
    37. itemstack.setItemMeta(test);
    38. inventory.addItem(itemstack);
    39. p.sendMessage(ChatColor.GREEN + "You rename: " + arg1 + " to " + ChatColor.valueOf(arg4) + arg3);
    40. p.sendMessage(ChatColor.GREEN + "You see inventory!");
    41. }
    42. }
    43. return true;
    44. }
    45. }
    46. return true;
    47. }
    48.  
    49. }
    50. [/s]
     
  11. Offline

    william9518

    plugin.yml?
     
  12. Offline

    xMinecraft

    Code:
    name: ItemRename
    author: Lopezito
    version: 1.0
    description: Rename Items
    main: com.gmail.lopezitospriter.ItemName.main
    commands:
      rename:
        description: Rename Items
        usage: /rename iditem many color newname
        permission: itemrename.rename
        aliases: [rn, xre]
    permissions:
        itemrename.rename:
            description: Can use /rename
            default: op
    
     
  13. Offline

    fireblast709

    your problem probably is that you use args.length > 4 instead args.length > 3 ;D.
     
  14. Offline

    xMinecraft

    Change the number but my command not work.
    My plugin on the log server haven't error

    I remove
    1. if(sender instanceof Player){

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: May 31, 2016
Thread Status:
Not open for further replies.

Share This Page