Solved Message to String or char array

Discussion in 'Plugin Development' started by Forseth11, Mar 28, 2013.

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

    Forseth11

    Hello, I am having trouble putting the player's message into a char array.
    This is what I'm using, and it won't work:
    Code:
    char[] Cmessage = event.getMessage().toCharArray();
    When I convert this char array to another string and send a message to a player I get this:
    What am I doing wrong, and how do I fix it?

    (I can use a string conversion or char array conversion.)
     
  2. Offline

    chasechocolate

    How are you converting? Use new String(Cmessage).
     
  3. Offline

    Forseth11

    chasechocolate
    No, the way I am converting this:
    Code:
    char[] Cmessage = event.getMessage().toCharArray();
    won't work.
    The way I convert it back to string is:
    Code:
    String test = Cmessage.toString();
     
  4. Offline

    Devil0s

    I don't know whether there is a standard methode without taking any look to Java but you can use a for array to turn the char array into a String.

    Here is an example code:
    Code:
            String before = "Hello guys. this is a test.";
         
         
            char[] befores = before.toCharArray();
         
            String after = "";
         
            for(int i = 0; i < befores.length; i++) {
                after += befores[i];
             
            }
    You can't convert an array (a collection of objects) into one non array object. You have to use a an object inside the array and convert it. Object for object. Best way is the for loop.
    Why do you need a character array?
     
  5. Offline

    Forseth11

    Devil0s Okay, now how come it doesn't work with
    Code:
    event.getMessage()
     
  6. Offline

    Devil0s

    What doesn't work with
    Code:
    event.getMessage();
    ??

    This should work if you really want a char array.
    Code:
            char[] messageChars = event.getMessage().toCharArray(); //variables should be start lowercase
            String message = "";
            for(int i = 0; i < messageChars.length; i++) {
                message += messageChars[i];
            }
    If event.getMessage() doesn't work then are you sure that the event has a message??
    Try
    Code:
    event.getPlayer().sendMessage("Test: " + event.getMessage());
    What can you see?
     
  7. Offline

    RingOfStorms

    Why take the string, convert it to char array, then convert it back again just to send the original string? Am I missing something here? Just use the original string.
     
  8. Offline

    Tirelessly

    You're printing the memory address or whatever it is java uses specifically.
     
  9. Offline

    Forseth11

    RingOfStorms I need to put it back to string to send a player message.
     
  10. Offline

    RingOfStorms

    Why did you turn it into a char array to start with then?

    player.sendMessage(event.getMessage());

    event.getMessage() returns a string, which is what all of the message functions take as a parameter, why would you convert a string into a char array then convert it back to a string?
     
  11. Offline

    Forseth11

    I need to take the massage and only use the first word, and delete anything after the first space.

    This might help.
    My code:

    Code:
    public class CommandListener implements Listener{
        @EventHandler(priority = EventPriority.HIGHEST)
        public void onPlayerCommand(PlayerCommandPreprocessEvent event){
            if(event.isCancelled()){ return;}
            Player player = event.getPlayer();
            String message = event.getMessage();
            char[] Cmessage = event.getMessage().toCharArray();
            String test = Cmessage.toString();
            player.sendMessage(test);
            String finalCommand = "/000111000111000111000111-00223399-00";
            player.sendMessage("testing1");
            done:
            for(int a = 0; a <= event.getMessage().length(); a++){
                player.sendMessage("testing2");
                if(Cmessage[a] == ' '){
                    int b = a -1;
                    for(int c = 0; c <= b; c++){
                        player.sendMessage("testing3");
                        if(Cmessage[b] != ' '){
                        finalCommand += Cmessage[b];
                        }
                        else{
                            break done;
                        }
                    }
                }
            }
            if(finalCommand == "/000111000111000111000111-00223399-00"){
                finalCommand = event.getMessage();
            }
           
           
           
           
            if(event.getMessage().equalsIgnoreCase("/say")){
                player.sendMessage(ChatColor.RED + "Sorry this command has been disabled.");
                event.setCancelled(true);
            }
        }
    }
    I have put some unnecessary things in there for testing.

    There is something with the for loop that makes event.getPlayer() not work.

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

    Devil0s

    You should split it and just use the first String object.
    Code:
    String[] messageParts = message.split(" ");
    youWantedPart = messageParts[0];
     
  13. Offline

    RingOfStorms

    Well im not gonna read that crazy code lol, but if you just want to get a massage you could probably go to a local area for that ^_^.

    To get the first word from a string just do:

    Code:java
    1.  
    2. String message = "Cheese is so awesome!";
    3. String message = message.split(" ")[0];
    4. broadcast(message); //Outputs: "Cheese"
    5.  


    Edit: ninja'd by Devil0s
     
  14. Offline

    Forseth11

    THANK YOU Devil0s. I didn't know there was a split function. It works!!! :D

    P.S. I officially feel stupid! :oops:
     
Thread Status:
Not open for further replies.

Share This Page