How would I create a XP bottling system using command blocks?
What I want to do: If a player is holding a glass_bottle
and standing 2 blocks away from some XP orbs, it will /kill 3 XPOrb, give the player 1 experience_bottle
and remove 1 glass_bottle
from inventory
This is what I have as a setup so far:
Working, on repeat command blocks:
scoreboard players set @a NotHoldingItem 0 {SelectedItem:{id:minecraft:glass_bottle}}
scoreboard players set @a NotHoldingItem 1
Not working part, the part I need help with - I don't know if this is the right way to do it anyways (again on repeat command blocks):
execute @a[score_NotHoldingItem=0,r=2] ~ ~ ~ /scoreboard players add @e[type=XPOrb] 1 xp
(add score to xp in range of 2 blocks of player holding a bottle)
execute @e[score_xp=4] ~ ~ ~ give @p[score_NotHoldingItem=0] minecraft:experience_bottle
-->(Chain):
kill @e[score_xp_min=4,c=3]
-->(Chain):
clear @p[score_NotHoldingItem=0] minecraft:glass_bottle 1
Thank you.
(Repeat) execute @a[score_NotHoldingItem=0,r=2] ~ ~ ~ /scoreboard players add @e[type=XPOrb] 1 xp (add score to xp in range of 2 blocks of player holding a bottle)
r=2
will select players within two blocks on the command block (or whatever else is running the command) to execute from. You should move the r=2
to the second selector in order to add the score to XPOrbs within 2 blocks of a player holding the bottle:
<count>
also comes before <objective>
in scoreboard players add
's syntax:
execute @a[score_NotHoldingItem=0] ~ ~ ~ /scoreboard players add @e[type=XPOrb,r=2] xp 1
(Repeat) execute @e[score_xp=4] ~ ~ ~ give @p[score_NotHoldingItem=0] minecraft:experience_bottle
This will execute even if there's just one XP orb near the player. If you want it to only activate when there's at least 3 XP orbs, then you'll need to set up a count.
Set up a dummy objective called something like "NearbyXp" (/scoreboard objectives add NearbyXp dummy
) then add the following commands to your clock, before this give command:
scoreboard players set @a[score_NearbyXp_min=1] NearbyXp 0
execute @e[type=XPOrb] ~ ~ ~ scoreboard players add @a[score_NotHoldingItem=0,r=2] 1 NearbyXp
This makes all XP orbs add 1 NearbyXp
to players holding bottles within 2 blocks of them. E.G: If there are 7 XP orbs near you, they'll each add 1 to your NearbyXp
score, setting it to 7.
Then, on your commands such as this one where you're giving the experience bottle, you should change the player selector to @p[score_NotHoldingItem=0,score_NearbyXp_min=3]
so that only players with 3 XP orbs around them will get the bottle.
After doing this you should also be able to get rid of any of the command related to the xp
score, as this will detect when players have 3 XP orbs around them instead.
kill @e[score_xp_min=4,c=3]
This should be executed from the player if you want to remove the 3 nearest XP orbs to the player, rather than 3 nearest to the command block.
clear @p[score_NotHoldingItem=0] minecraft:glass_bottle 1
This clears all glass bottles with a data value of 1, rather than 1 glass bottle as I assume you want here.
To guarantee that the order the commands are executed in stays the same, you should only be using one repeat command block and the rest chains.
Your commands in the end should look something like:
scoreboard players set @a NotHoldingItem 1
scoreboard players set @a NotHoldingItem 0 {SelectedItem:{id:minecraft:glass_bottle}}
scoreboard players set @a[score_NearbyXp_min=1] NearbyXp 0
execute @e[type=XPOrb] ~ ~ ~ scoreboard players add @a[score_NotHoldingItem=0,r=2] NearbyXp 1
give @p[score_NotHoldingItem=0,score_NearbyXp_min=3] minecraft:experience_bottle
execute @a[score_NotHoldingItem=0,score_NearbyXp_min=3] ~ ~ ~ kill @e[type=XPOrb,c=3]
clear @a[score_NotHoldingItem=0,score_NearbyXp_min=3] minecraft:glass_bottle 0 1
Only the first one should be repeating, the rest should be chains following onto it, like so:
Take note that:
- XP orbs vary in value, so someone could bottle 3 experience or 7431 experience and get the same bottle.
- It's hard to stand within 2 blocks of multiple XP orbs without picking some up, I'd recommend increasing
r=2
tor=5