Allow Spaces in arguments

Discussion in 'Plugin Development' started by #define, Apr 2, 2012.

Thread Status:
Not open for further replies.
  1. Hi, i'm making a message broker and I want my admins to have spaces in their message's.

    now the problem is it won't accept them.

    I tried adding "\"" + args[0] + "\"" but didn't work.

    Hope you can help:

    here I handle the command:

    Code:
     @Override
        public boolean onCommand(CommandSender sender, Command cmd, String lbl, String[] args) {
        Player player = null;
     
        if (sender instanceof Player)
        player = (Player) sender;
     
        if(!cmd.getName().equalsIgnoreCase("addmsg"))
        return true;
     
        if (player == null) {
        sender.sendMessage("Deze command kan alleen uitgevoerd worden door een speler");
        return true;
        }
     
     
        try {
        int id = broker.count + 1;
        Statement stmt = broker.con.createStatement();
        stmt.executeUpdate("INSERT INTO msg(id,color,msg) VALUES('" + id + "','" + args[1] + "','" + "\"" + args[0] + "\"" + "')");
        stmt.close();
        broker.roll();
        return true;
        } catch (SQLException e) {
        e.printStackTrace();
        }
        return false;
        }
    Thanks!
     
  2. Offline

    surtic

    When you know that the subargs all for the Messages are you can do it like this.

    Code:
    String message = "";
     
                for ( String string : subargs ) {
                    message = message + string + " ";
                }
     
    #define likes this.
  3. The problem is that there also is a color args. so args[0] is the Message and args[1] is the color how can I make args[0] be This Server is awesome! (with spaces) because when I do that now I get This in msg and Server in color.


    Thanks

    I tried your method, but it didn't worked.

    I have two parameters one a int and one a string.

    Thanks

    So i tried this:

    Code:
    package com.cj4.mbroker;
     
    import java.sql.Connection;
    import java.sql.DriverManager;
    import java.sql.SQLException;
    import java.sql.Statement;
     
    import org.bukkit.ChatColor;
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandExecutor;
    import org.bukkit.command.CommandSender;
    import org.bukkit.entity.Player;
     
    public class addCommand implements CommandExecutor {
            private MBroker broker;
        public addCommand(MBroker mb){
            this.broker = mb;
     
        }
       
        @Override
        public boolean onCommand(CommandSender sender, Command cmd, String lbl, String[] args) {
        Player player = null;
     
        if (sender instanceof Player)
        player = (Player) sender;
     
        if(!cmd.getName().equalsIgnoreCase("addmsg"))
        return true;
     
        if (player == null) {
        sender.sendMessage("Deze command kan alleen uitgevoerd worden door een speler");
        return true;
        }
     
        try {
         
     
           
        int id = broker.count + 1;
        Statement stmt = broker.con.createStatement();
        stmt.executeUpdate("INSERT INTO msg(id,color,msg) VALUES('" + id + "','" + args[1] + "','" +args[0]  + "')");
        stmt.close();
        broker.roll();
        return true;
        } catch (SQLException e) {
        e.printStackTrace();
        }
        return false;
        }
     
    }
    
    and then input in my mc chat 'Arno Cornette' 1

    and then i get this error:
    Code:
    2012-04-02 20:05:31 [SEVERE] java.sql.SQLException: 2 values for 3 columns
    2012-04-02 20:05:31 [SEVERE]    at org.sqlite.NativeDB.throwex(NativeDB.java:210)
    2012-04-02 20:05:31 [SEVERE]    at org.sqlite.NativeDB._exec(Native Method)
    2012-04-02 20:05:31 [SEVERE]    at org.sqlite.Stmt.executeUpdate(Stmt.java:152)
    2012-04-02 20:05:31 [SEVERE]    at com.cj4.mbroker.addCommand.onCommand(addCommand.java:42)
    2012-04-02 20:05:31 [SEVERE]    at org.bukkit.command.PluginCommand.execute(PluginCommand.java:40)
    2012-04-02 20:05:31 [SEVERE]    at org.bukkit.command.SimpleCommandMap.dispatch(SimpleCommandMap.java:166)
    2012-04-02 20:05:31 [SEVERE]    at org.bukkit.craftbukkit.CraftServer.dispatchCommand(CraftServer.java:473)
    2012-04-02 20:05:31 [SEVERE]    at net.minecraft.server.NetServerHandler.handleCommand(NetServerHandler.java:821)
    2012-04-02 20:05:31 [SEVERE]    at net.minecraft.server.NetServerHandler.chat(NetServerHandler.java:781)
    2012-04-02 20:05:31 [SEVERE]    at net.minecraft.server.NetServerHandler.a(NetServerHandler.java:764)
    2012-04-02 20:05:31 [SEVERE]    at net.minecraft.server.Packet3Chat.handle(Packet3Chat.java:33)
    2012-04-02 20:05:31 [SEVERE]    at net.minecraft.server.NetworkManager.b(NetworkManager.java:229)
    2012-04-02 20:05:31 [SEVERE]    at net.minecraft.server.NetServerHandler.a(NetServerHandler.java:113)
    2012-04-02 20:05:31 [SEVERE]    at net.minecraft.server.NetworkListenThread.a(NetworkListenThread.java:78)
    2012-04-02 20:05:31 [SEVERE]    at net.minecraft.server.MinecraftServer.w(MinecraftServer.java:554)
    2012-04-02 20:05:31 [SEVERE]    at net.minecraft.server.MinecraftServer.run(MinecraftServer.java:452)
    2012-04-02 20:05:31 [SEVERE]    at net.minecraft.server.ThreadServerApplication.run(SourceFile:490)
    Thanks!

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: May 25, 2016
  4. Offline

    surtic

    First of all, you musst test your Params before you add it in your Statement!

    You have really only two args? Test it with args.length() your log says that the Statment are not corekt how it look? I think you make some mistakes... like ID and Color Number as a String.
     
    #define likes this.
  5. Fixed it. It is working I just replace all _ with " " :)

    Thanks for the effort.
     
Thread Status:
Not open for further replies.

Share This Page