0

I have a question regarding

I want to output following: When the Player types Hello, the output should be oHell. When the Player types Hello World, the output should be oHello dWorl.

public class ConnectionListener implements Listener {

ArrayList<String> newArrOfStr = new ArrayList<String>();
@EventHandler
public void onMessageSent(AsyncPlayerChatEvent event) {

    Player player = event.getPlayer();
    String output = event.getMessage();
    event.setCancelled(true);
    StringBuffer sb = new StringBuffer(output);
    sb.deleteCharAt(sb.length()-1);
    String[] arrOfStr = output.split(" ");


    int i = 0;
    for (String a : arrOfStr)
        newArrOfStr.add(a);

    player.sendMessage(newArrOfStr(beginning - end));

    newArrOfStr.clear();
Hello >> oHell
Hello World >> oHell dWorl
A BC AAR >> A CB RAA

I just dont understand how to output an Array to a nondefined ending, beacause the Player could send multiple arguments and not only 1, 2 or 3.

The "beginning -end" is the space I mean. I have the complete Array under the tag "newArrOfStr" and now would like to output the single words with a Space inbetween. It is supposed to be one sentence and should not be output like this:

A BC RAA

image

Julian
  • 1
  • 3
  • Can you clarify what your exact question is? Is the problem that you don't know how to send the message to the player? What is your expected behaviour? – Lasslos05 Oct 08 '22 at 20:25
  • The problem is that I don't know how to output an array in the chat without the brackets and commas. – Julian Oct 08 '22 at 20:43

2 Answers2

0

Don't need to make this so complicated. Regex is your friend! Here's all you need:

@EventHandler
public void onMessageSent(AsyncPlayerChatEvent event) {
    event.setMessage(event.getMessage().replaceAll("(\w)\B(\w+)", "$2$1"));
}

Regex explanation:

  • Group 1 (\w): Any word character. This is the first character of each word. Surrounded by parenthesis to make it be a replacement group.
  • \B: Any non-word boundary. Basically this means "stop matching this string group when you hit a word boundary.
  • Group 2: (\w+): That \w is the same as above. + means match one or more. Parenthesis again to group them
  • $2$1 - $2 means the second group, $1 means the first group.
Michael Ziluck
  • 599
  • 6
  • 19
-1

First of all, the solution to your problem: The way to approach this is by declaring a String result and then using a for loop to build the String:

String result = "";
for (String s : newArrOfStr) {
  result = result + s + " ";
}
player.sendMessage(result);

I don't think your Function does what you want it to do, and there is a lot of unused things or other things that are a little bit more complicated than they should be.

  • I don't think you need the ArrayList newArrOfStr at all, if all you want to do is send the message to the player. Instead of looping over the ArrayList newArrOfStr, you can simply just loop over your arrOfStr:

    for (String s : arrOfStr) {

  • If for some other reason you do think you need the ArrayList, instead of adding all the elements with the for loop you have right now, change it to this:

//Bad:
for (String a : arrOfStr)
  newArrOfStr.add(a);
//Good:
newArrOfStr.addAll(Arrays.asList(arrOfStr));
  • Then, you have the unused variable int i = 0. Consider removing it.
  • The StringBuilder you created is also not used.

I tried to write your function in a way that I think it will work:

@EventHandler
public void onMessageSent(AsyncPlayerChatEvent event) {
  event.setCancelled(true);
  Player player = event.getPlayer();
  String output = event.getMessage();
  String[] wordsInOutput = output.split(" ");
  String result = "";
  for (int i = 0; i < wordsInOutput.length; i++) {
    String word = wordsInOutput[i];
    String newWord = word.charAt(word.length() - 1) + word.substring(0, word.length() - 1);
    result = result + newWord + " ";
  }

  player.sendMessage(result);
}

This is the way everyone can understand it, but of course IntelliJ Idea has some suggestions so lets apply those:

@EventHandler
public void onMessageSent(AsyncPlayerChatEvent event) {
  event.setCancelled(true);
  Player player = event.getPlayer();
  String output = event.getMessage();
  String[] wordsInOutput = output.split(" ");
  StringBuilder result = new StringBuilder();
  for (String word : wordsInOutput) {
    String newWord = word.charAt(word.length() - 1) + word.substring(0, word.length() - 1);
    result.append(newWord).append(" ");
  }

  player.sendMessage(result.toString());
}

Now this is a working function. I hope this helps.

Lasslos05
  • 414
  • 2
  • 12