You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: questions/amountitems.md
+41-2Lines changed: 41 additions & 2 deletions
Original file line number
Diff line number
Diff line change
@@ -2,17 +2,56 @@
2
2
3
3
## Java
4
4
5
+
### Clear
6
+
5
7
Using the [`clear`](https://minecraft.wiki/Commands/clear) command with a `[<maxCount>]` of 0 will return the amount of items a player has, without actually clearing any of them.
6
-
That means we can use `execute store` to save that number somewhere and then execute depending on that:
8
+
That means we can use [`execute store`](https://minecraft.wiki/w/Commands/execute#Store_subcommand) to save that number somewhere and then execute depending on that:
9
+
10
+
# Setup
11
+
scoreboard objectives add diamonds dummy
7
12
13
+
# Commands
8
14
execute store result score @s diamonds run clear @s diamond 0
9
15
execute if score @s diamonds matches 5 run say I have exactly 5 diamonds in my inventory.
10
16
execute if score @s diamonds matches 1..4 run say I have somewhere between 1 and 4 diamonds in my inventory.
11
17
execute if score @s diamonds matches 10.. run say I have 10 or more diamonds in my inventory.
12
18
execute if score @s diamonds matches ..20 run say I have 20 or less diamonds in my inventory.
13
19
20
+
### Execute if items
21
+
22
+
Since version 1.20.5 you can also use [`execute if items`](https://minecraft.wiki/w/Commands/execute#(if|unless)_items) to count the number of items.
23
+
24
+
The `if items` subcommand, when executed, returns the number of items that meet the specified conditions. For a quick example, running this command will show the count of all items in the player's inventory (except for armor and left hand slots):
25
+
26
+
# In chat
27
+
execute if items entity @s container.* *
28
+
29
+
You can find out more details on how to detect specific items here: [Detect a specific item](/wiki/questions/detectitem)
30
+
31
+
Below is an example for getting the amount of a custom item and executing some command depending on the result:
32
+
33
+
# Example item
34
+
give @s diamond[minecraft:custom_data={diamond:true},minecraft:item_name="'Custom Diamond'"]
35
+
36
+
# Command blocks
37
+
execute as @a store result score @s diamonds if items entity @s container.* *[custom_data~{diamond:true}]
38
+
execute as @a[scores={diamonds=5}] run say I have exactly 5 coins.
39
+
execute as @a[scores={diamonds=1..4}] run say I have somewhere between 1 and 4 coins.
40
+
execute as @a[scores={diamonds=10..}] run say I have 10 or more coins.
41
+
execute as @a[scores={diamonds=..20}] run say I have 20 or less coins.
42
+
43
+
Although you can use `/clear` on a player, `if items` can also count the number of items in a chest, shulker_box and other containers, can also count the number of items in a players ender_chest.
44
+
45
+
Here are some examples for this:
46
+
47
+
# Counting items in chest or any container
48
+
execute store result score #container diamonds if items block <pos> container.* *[custom_data~{diamond:true}]
49
+
50
+
# Counting items in ender_chest
51
+
execute as @a store result score @s diamonds if items entity @s enderchest.* *[custom_data~{diamond:true}]
52
+
14
53
## Bedrock
15
54
16
55
In the **1.18.20 beta** they added the [`hasitem`](https://minecraft.wiki/wiki/Target_selectors#Selecting_targets_by_items) target selector, which allows you to check for specific amounts (as [ranges](/wiki/questions/range)) of items in entities inventories. Below is an example, check the link above for more information.
17
56
18
-
execute @a[hasitem={item:apple,quantity=5..}] ~~~ run say I have 5 or more apples in my inventory
57
+
execute @a[hasitem={item:apple,quantity=5..}] ~~~ run say I have 5 or more apples in my inventory
Copy file name to clipboardExpand all lines: questions/areas.md
+108-5Lines changed: 108 additions & 5 deletions
Original file line number
Diff line number
Diff line change
@@ -2,17 +2,20 @@
2
2
3
3
_Java Syntax, but this can be applied to Bedrock just as well by changing the selector arguments to Bedrock Syntax._
4
4
5
-
This mostly comes up as a question to change the gamemode in a certain area (e.g. spawn, safe zones, etc), so we will focus on that, but this can be applied to any use case. For questions to do something once a player enters a single area, [look here](/wiki/questions/runonce).
5
+
This mostly comes up as a question to change the gamemode in a certain area (e.g. spawn, safe zones, etc.), so we will focus on that, but this can be applied to any use case. For questions to do something once a player enters a single area, [look here](/wiki/questions/runonce).
6
6
7
-
The naive approach would be to put everyone in a radius X around the worldspawn (e.g.at 0 0 0) to adventure mode and everyone outside of it to survival mode.
7
+
The naive approach would be to put everyone in a radius X around the worldspawn (e.g.at 0 0 0) to adventure mode and everyone outside of it to survival mode.
8
8
9
9
gamemode adventure @a[x=0,y=0,z=0,distance=..X]
10
10
gamemode survival @a[x=0,y=0,z=0,distance=X..]
11
11
12
12
This method quickly falls apart if you have a non-spherical or non-box shaped area or you want this to apply to more than one area, because a player will always be outside of the other areas, even if they are inside one, so will always end up in survival.
13
13
14
-
The best way to make sure players are in one of multiple areas without overwriting each other, is to use a tag: So instead of applying the desired effect to each area individually, you tag all players that are in one of the areas and apply the effect once to all of them (or everyone else).
14
+
## Hardcoded locations
15
15
16
+
The best way to make sure players are in one of multiple areas without overwriting each other, is to use a tag: So instead of applying the desired effect to each area individually, you tag all players that are in one of the areas and apply the effect once to all of them (or everyone else). But this method requires a separate command block for each location. For a large number of locations, use the anchor entity method.
17
+
18
+
# Command blocks / tick function
16
19
tag @a remove inArea
17
20
tag @a[x=0,y=0,z=0,distance=..X] add inArea
18
21
tag @a[x=100,y=64,z=100,dx=70,dy=16,dz=28] add in Area
@@ -25,5 +28,105 @@ To prevent chatspam for the players and unnecessary gamemode changes, we suggest
If for some reason you want to keep commandblockoutput on and don't want your output to be spammed by this system, check out this post by /u/Afanofall23:
If for some reason you want to keep commandBlockOutput on and don't want your output to be spammed by this system, check out [this post](https://www.reddit.com/r/MinecraftCommands/comments/mw11xm/do_something_to_players_in_multiple_specific) by [u/Afanofall23](https://www.reddit.com/u/Afanofall23).
32
+
33
+
## Anchor entities
34
+
35
+
If you need to check if the player is in one of several spherical areas, for example to switch gamemode to adventure, then you can use some kind of entity as an anchor to check if the player is nearby. For versions before 1.17 you can use armor_stand or area_effect_cloud, but since version 1.17 it is strongly recommended to use an [marker entity](https://minecraft.wiki/w/Marker) to mark a place.
36
+
37
+
# Summon marker
38
+
summon marker <pos> {Tags:["adventure_area"]}
39
+
40
+
# Command blocks / tick function
41
+
execute as @a[gamemode=survival] at @s if entity @e[type=marker,tag=adventure_area,distance=..X,limit=1] run gamemode adventure
42
+
execute as @a[gamemode=adventure] at @s unless entity @e[type=marker,tag=adventure_area,distance=..X,limit=1] run gamemode survival
43
+
44
+
`X - distance at which players should be switched into adventure mode.`
45
+
46
+
To make placing markers more convenient, you can give a spawn_egg containing a marker with the tag:
47
+
48
+
# 1.17 - 1.20.4
49
+
give @s bat_spawn_egg{EntityTag:{id:"minecraft:marker",Tags:["adventure_area"]},display:{Name:'{"text":"Adventure Area Marker","italic":false}'}}
50
+
51
+
# 1.20.5+
52
+
give @s minecraft:bat_spawn_egg[entity_data={id:"minecraft:marker",Tags:["adventure_area"]},item_name='"Adventure Area Marker"']
53
+
54
+
## Block layer
55
+
56
+
If you need to execute a command when a player enters a very randomly shaped area, then you can place under the map, for example, at a height of Y=-63, a layer of some block that you will check under the player.
57
+
58
+
For example, you want to create an area on your map where the player will be detected if the player is not sneaking. This example will check the red_concrete block at Y=-63 for this area:
59
+
60
+
# Command block / tick function (1.20.5+)
61
+
execute as @a at @s if predicate {condition:"entity_properties",entity:"this",predicate:{flags:{is_sneaking:false}}} if block ~ -63 ~ minecraft:red_concrete run say You have been found!
62
+
63
+
## Predicates
64
+
65
+
If you need to check multiple cubic, spherical or cylindrical areas, you can also use [predicates](https://minecraft.wiki/w/Predicate) in datapack or command blocks (1.20.5+).
66
+
67
+
In a predicate, you can use the `minecraft:alternative` (1.14-1.19.4) or `minecraft:any_of` (1.20+) condition to check multiple areas in one predicate using the `minecraft:location_check` condition.
68
+
69
+
Below is an example of a predicate for checking three cubic areas:
0 commit comments