-2

I have been making a very simple bukkit plugin for my server just for personal use. I made a command called players, when someone with the permission ("broadcaster.players"), it would display a list of the servers player. I also wanted to have it display the players gamemode in-game. But I don't know how to put both of them next to each other.

My Code:

if(cmd.getName().equalsIgnoreCase("players")){
  Player player = (Player) sender;
  if(sender instanceof Player){
    if(sender.hasPermission("broadcaster.players")){
      sender.sendMessage(player.getName());
    }
  }
} 

Where it says sender.sendMessage(player.getName());, I would like to display on the same line the gamemode. so I tried:

sender.sendMessage(player.getName()player.getGameMode());

of course this never worked and i am not sure what to do, can anyone suggest to me what i could do, My code is pretty messy but I'm fairly new, so I don't know a lot of things, this is my first coding project and i need tons of help!

Thanks for your patience

spongebob
  • 8,370
  • 15
  • 50
  • 83

4 Answers4

2

To combine two strings in Java, you can use the + operator, for example:

String first = "Hello, ";
String second = "World"!;
String str = first + second;

str is now equal to first + second, which means "Hello, " + "World!", which therefore means that str is now equal to the string "Hello, World!".

In your case, you could do:

sender.sendMessage(player.getName() + player.getGameMode());

But, there would be no space in between them, so, I would recommend adding a space, or a colon between them for readability purposes:

sender.sendMessage(player.getName() + ": " + player.getGameMode());

If the player's name is jojodmo, and their gamemode is SURVIVAL, with sender.sendMessage(player.getName() + player.getGameMode());, the message jojodmoSURVIVAL would be sent.

Yet, with sender.sendMessage(player.getName() + ": " + player.getGameMode());, the message jojodmo: SURVIVAL would be sent.


To get more information on strings in Java, I recommend that you visit the Java Documentation.

Jojodmo
  • 23,357
  • 13
  • 65
  • 107
0

You can concatenate two Strings using the + operator:

sender.sendMessage(player.getName() + ": " + player.getGameMode());
August
  • 12,410
  • 3
  • 35
  • 51
0

Java allows the + operator to concatenate strings:

String hi = "hi";
String zebra = "kangaroo";
System.out.println(hi + ", Mr. " + zebra); // outputs hi, Mr. kangaroo.

Secondly, since String is an object in Java, it has a method, concat(), to concatenate strings to other strings. If you're a big jQuery fan like me, you might want to also consider:

String hi = "hi";
String zebra = "kangaroo";
System.out.println(hi.concat(", Mr. ").concat(zebra)); // outputs hi, Mr. kangaroo.
Taryn
  • 242,637
  • 56
  • 362
  • 405
Shahar
  • 1,687
  • 2
  • 12
  • 18
0

If you want to send him a list of players:

String send = "";
for(Player p1 : Bukkit.getOnlinePlayers() {
send+=p1.getName() + "(" + p1.getGameMode() + ") ";
}
p.sendMessage(send);

This should send to the player a list of all players and their gamemodes.