Compare the String argument in command


I’m begginer in sponge plugin development and i don’t find in the net how to compare a String argument.

My code:
` @Override
public CommandResult execute(CommandSource src, CommandContext args) throws CommandException {
ArrayList axeLoc = new ArrayList<>();
ArrayList allieLoc = new ArrayList<>();
Random r = new Random();

	if(!(src instanceof Player)) {
		src.sendMessage(Text.of("§cLa console ou les commandes blocs ne peuvent pas utiliser cette commande!"));
	Player p = (Player) src;
	String team = args.<String>getOne("team").get();
	//That's here
	if(team.equals(Text.of("axe"))) {
		p.sendMessage(Text.of("§aVous avez choisis la team§c§l Axe§a."));
		axeLoc.add(new Location<>(p.getWorld(), -1694, 5, 505));
		axeLoc.add(new Location<>(p.getWorld(), -1622, 5, 503));
		axeLoc.add(new Location<>(p.getWorld(), -1648, 5, 507));
		axeLoc.add(new Location<>(p.getWorld(), -1631, 5, 505));
		axeLoc.add(new Location<>(p.getWorld(), -1615, 5, 503));
		axeLoc.add(new Location<>(p.getWorld(), -1602, 5, 527));
		axeLoc.add(new Location<>(p.getWorld(), -1612, 5, 522));
		axeLoc.add(new Location<>(p.getWorld(), -1622, 5, 527));
		axeLoc.add(new Location<>(p.getWorld(), -1656, 5, 530));
		axeLoc.add(new Location<>(p.getWorld(), -1676, 5, 526));
		axeLoc.add(new Location<>(p.getWorld(), -1692, 5, 528));

	if(team.equals(Text.of("allie"))) {
		p.sendMessage(Text.of("§aVous avez choisis la team§2§l Allié§a."));
		allieLoc.add(new Location<>(p.getWorld(), -1609, 19, 643));
		allieLoc.add(new Location<>(p.getWorld(), -1676, 19, 643));
		allieLoc.add(new Location<>(p.getWorld(), -1637, 19, 643));
		allieLoc.add(new Location<>(p.getWorld(), -1603, 19, 643));
	return CommandResult.success();

I want to get the String argument /join , if this argument equals to “allie” of “axe” i want to do things.
Thank’s you,


You are attempting to compare a string to a text.
You need to compare a string to a string. Just having the “axe” and not the Text.of(“axe”) will do it.

If you have a text object and trying to compare that to a string, you can use

Text text;

In direct response to your question, it’s standard Java - you can use the equals method correctly for comparison. In a majority of cases, equals requires the object types to be the same. In your code, for some reason your calling equals with a Text argument on an object that’s known to be a String. If you’re experiencing difficulty with this, then you need to review your core Java - this is not a Sponge issue.

However, there are a number of problems here that should be addressed.

  • There’s no reason to define two lists here. It’s generally good practice to only define objects when you need them - even if the command does run successfully, you’ve added at least one ArrayList instance that hasn’t been touched for the garbage collection to handle later on. Ideally, this entire list of locations thing should be done in a completely different way (more on that later).
  • You should store the Random instance statically in your class and reuse it multiple times. There are both performance and logical reasons to do this, so it’s worth a google search if you’re interested.
  • In the check to see if the source is a player, the last line is simply CommandResult.success(). This is wrong for two reasons:
    • This statement doesn’t do anything. Your IDE should display this.
    • Returning success is simply wrong - the command was not successful. Instead, you should throw a CommandException with the Text message you send to the user above.
  • Your if-statements should really be an if-else - any performance impact is negligible, but it should be made clear in the code that those two statements can’t both be true.
  • As stated above, your main problem with equals is the fact that team is a String and your passing in a Text object as the arguments. However, you also need to consider their case sensitivity as well. This is where equalsIgnoreCase should be used.
  • Beyond creating a bunch of Location objects, there are two concerns I have here:
    • The world is retrieved from the player. If the player is in a different world, this could teleport them somewhere you don’t intend where you don’t want them to be.
    • The coordinates are hard-coded in. It’s much better to have things like this retrieved from a config file than encode them directly into the plugin.
  • A better way to do this, if you must, is to use something like a switch statement on a random number and select the location from there to avoid creating multiple unused Location objects.

At a guess, it seems like you’re creating some type of WWII themed battle minigame. When you’re working with having multiplayer teams and things like that, I would recommend looking into more of a queue system so that players aren’t stuck in the area or something until the battle starts. I would also try to ensure that players don’t spawn on top of each other as well.

I would recommend taking a step back and try to redesign things here. Use a config file to allow those locations to be editable and avoid recreating them over and over again. I start a lot of my projects by walking through the plugin from a user perspective and mapping out exactly how I, as a user, would expect it to work and what features I have. Give that a go and look into the above points and see where that takes you. Good luck!

1 Like

Thank you very much, I will try to take into account what you told me. I do not use a configuration file since this plugin is just a test to understand how the commands work.