I'm trying to activate some command blocks only if there's more than one player online in my vanilla server. Is there a way to accomplish this with command blocks?
5 Answers
You probably want to use the testfor command with some type of selector.
The easiest thing to do would be to use the @a
selector with a redstone comparator. The @a
selector will return the count by means of the signal strength, which can be compared to a known strength. If you want to check for more than one (2 or more), then it's as simple as C=rr>
where C
is the command block, =
is the comparator, r
is redstone dust, and >
is the repeater. (Thanks to @MBraedley for the suggestion)
You could also probably use the c
selector here, for count, but I'm not 100% sure how that works in a conditional.
The name
selector would also do it if you want to be sure there are other people around with names that are not yours, but that won't work if you need it to not trigger if only one non-you person is on the server.
-
1I don't think you need the count selector.
testfor @a
should be all that's needed. Commented Nov 25, 2013 at 12:15 -
@MBraedley Won't that return true if there is only one player? The OP said "more than one". Actually, if there are no players the command block probably won't run because the chunk won't be loaded unless there's some mod involved... Commented Nov 25, 2013 at 13:41
-
3It will return the count through a comparator by means of the signal strength. If you want to check for more than one (2 or more), then it's as simple as
C=rr>
where C is the command block, = is the comparator, r is redstone dust, and > is the repeater. Commented Nov 25, 2013 at 13:57 -
I'll try these alternatives and give you a feedback asap. Thanks Commented Nov 25, 2013 at 16:41
Yes, this is very possible and quite easy.
To set things up, run this command:
/scoreboard objectives add players trigger
Then, set up some chain command blocks with these commands:
scoreboard players enable @a players
execute @a ~ ~ ~ /trigger players set 0
scoreboard players enable @e players
execute @e ~ ~ ~ /trigger players add 1
Then, when you run them, it will set the scoreboard "players" to the amount of players online.
You can also use the c tag:
How it works:
/testfor @a[c=!1]
Then it tests there isn't exactly one player online. It will also workif no players are online, but noone can see that. Only in the server data. You place a comparator next to the commandblock and then you can do what you want, but when you want a redstone line, you have to place a repeater.
-
1This will not work. The
c
parameter does not accept the NOT operator. Commented Jul 24, 2016 at 1:19
Saw this awhile back. This is most definitely possible in the newer versions of Minecraft, with many different possible solutions in fact. I've used at least 3 different setups while trying to make maps, none of which were actually released.
The newest version of my player counter is a 2 command block setup. I compressed it into a builder command for simplicity: http://pastebin.com/kktVdDKe
/summon FallingSand ~ ~1 ~ {
TileID:152,
Time:1000,
DropItem:0,
Riding:{
id:FallingSand,
TileEntityData:{
Command:fill ~ ~1 ~ ~ ~ - 11 ~ air 0 replace
},
TileID:137,
Time:1000,
DropItem:0,
Riding:{
id:FallingSand,
TileEntityData:{
Command:"say Done building. In total 21 blocks were placed. In JSON the initial command block contained 175 lines of data, together making 5,773 typed characters. This script was made by Wesley J. B."
},
TileID:137,
Time:1000,
DropItem:0,
Riding:{
id:FallingSand,
TileID:152,
Time:1000,
DropItem:0,
Riding:{
id:FallingSand,
TileEntityData:{
Command:gamerule commandBlockOutput false
},
TileID:137,
Time:1000,
DropItem:0,
Riding:{
id:FallingSand,
TileID:137,
Time:1000,
DropItem:0,
TileEntityData:{
Command:summon FallingSand ~2 ~10 ~ {
TileID:152,
Time:1000,
DropItem:0,
Riding:{
id:FallingSand,
TileID:137,
Time:1000,
DropItem:0,
TileEntityData:{
Command:"fill ~ ~1 ~ ~ ~-1 ~ air"
},
Riding:{
id:FallingSand,
TileID:152,
Time:1000,
DropItem:0,
Riding:{
id:FallingSand,
TileID:137,
Time:1000,
DropItem:0,
TileEntityData:{
Command:summon FallingSand ~ 254 ~ {
TileID:152,
Time:1000,
DropItem:0,
Riding:{
id:FallingSand,
TileID:137,
Time:1000,
DropItem:0,
TileEntityData:{
Command:"fill ~ ~1 ~ ~ ~-1 ~ air"
},
Riding:{
id:FallingSand,
TileID:152,
Time:1000,
DropItem:0
}
}
}
},
}
}
}
}
},
Riding:{
id:FallingSand,
TileID:152,
Time:1000,
DropItem:0,
Riding:{
id:FallingSand,
TileID:137,
Time:1000,
DropItem:0,
TileEntityData:{
Command:summon FallingSand ~1 ~10 ~ {
TileID:137,
Time:1000,
DropItem:0,
TileEntityData:{
Command:scoreboard players reset Players
},
Riding:{
id:FallingSand,
TileID:1,
Time:1000,
DropItem:0,
}
}
},
Riding:{
id:FallingSand,
TileID:137,
Time:1000,
DropItem:0,
TileEntityData:{
Command:summon FallingSand ~3 ~10 ~ {
TileID:137,
Time:1000,
DropItem:0,
TileEntityData:{
Command:execute @a ~ ~ ~ scoreboard players add Players vars 1
},
Riding:{
id:FallingSand,
TileID:1,
Time:1000,
DropItem:0,
}
}
},
Riding:{
id:FallingSand,
TileID:152,
Time:1000,
DropItem:0,
Riding:{
id:FallingSand,
TileID:137,
Time:1000,
DropItem:0,
TileEntityData:{
Command:scoreboard objectives setdisplay sidebar vars
},
Riding:{
id:FallingSand,
TileID:137,
Time:1000,
DropItem:0,
TileEntityData:{
Command:scoreboard objectives add vars dummy
},
Riding:{
id:FallingSand,
TileID:152,
Time:1000,
DropItem:0,
}
}
}
}
}
}
}
}
}
}
}
}
}
The basis is simple, before every check, reset player "Players" and then execute at every player online to add 1 to "Players."
For your application, add another command block that fires after the base that tests for the score of "Players" to be greater than 1. This can be done using the scoreboard command. If you want the command, I'll whip it together for you, but as I just finished putting my counter into a building block for simplicity, I need to take a small break.
Repeat Unconditional Always active: /testfor @a[c=!1] In front of the arrow on the command block: /yourCommand
-
1This will not work. The
c
parameter does not accept the NOT operator. Commented Jul 24, 2016 at 1:18