Minecraft – execute /list command

minecraft-commandsminecraft-java-editionminecraft-java-edition-server

I was setting up a welcome message to display to players that join my server when I came across something unexpected. I tried to run this command

execute @a ~ ~ ~ /list

I wanted every player to get a message in chat telling them who is online however when I run this command nothing appears in chat and the command block output just lists online players. Here is the output I got when I tested it just now with just me online:

[18:46:29] JakepLol

So my question is, why does this execute the command as if it is the command block running it and is there any other way I can achieve this?

Best Answer

Player List

The way that /execute works is not that the player runs the command, but that it sets the player as the effective "command sender", while keeping the command block or the opped user who ran the /execute command as the true command sender. This allows /execute to run op commands that the player could not run, but also means that the player does not see the output of these /execute commands, even if it was targeted to them.

However, you are not out of luck. Using the /tellraw command, you can display any message you want, including a selector.

/tellraw @a ["",{"text":"Welcome to the server! Here is a list of all online players: "},{"selector":"@a"}]

This command is special, because it allows you to format a message using JSON (JavaScript Object Notation) to allow colored text, scoreboard values, and selectors, and much more to be displayed in a chat message. There are a few similar commands, but /tellraw is the one you need.

You probably want to customize the text component to your liking, so here is a guide to creating the text components manually and here is a website that will allow you to create them automatically.

Player Count

To reproduce the /list command further using this method, you can use the /stats command, combined with a scoreboard. Create a new scoreboard of any name (PlayerCount), and set a fake player (#Count)'s score on that board to 0.

/scoreboard players set #Count PlayerCount 0

Now, it is time to use /stats. Within your command chain, before your /tellraw command, place a new command block with the following command:

/testfor @a

Then, stand on top of the command and run:

/stats block ~ ~-1 ~ set AffectedEntities #Count PlayerCount

Now, you will notice that each time your command chain runs, the player count score will be updated. You can edit your /tellraw text component to display this count. Here is an example:

/tellraw @a ["",{"text":"Welcome to the server! There are "},{"score":{"name":"#Count","objective":"PlayerCount"}},{"text":" players online now: "},{"selector":"@a"}]