Give the player an item (can be applied to mobs).
This function requires 3 arguements
Mob target - Who does this code affect
Item - Name of the item
Amount - How many of this item does the player recieve
#Custom function - copy and paste into your own code
def set_slot(slot, item, amount):
command = "replaceitem entity @p slot.hotbar "
command += str(slot) + " " + item + " " + str(amount)
player.execute(command)
#Example of running the function
set_slot(0, "diamond_sword", 1)
Give the player an item in a certain slot. replace the item in that slot already.
This is a custom function that must be copied in to the top of your code.
This code requires the following arguments to use:
Slot number
Item name (In speech marks)
Amount
Clears all items from the players inventory
#Custom function - Copy and paste this code
def clear_item(item, amount):
player.execute("clear @p " + item + " 0 " + str(amount))
#Example of running the function
clear_item("diamond", 3)
A simple function that can be added to remove a certian item from the players inventory.
A player can hold a maximum of 2,386 of a given item (if you want to remove the item complelely)
There is no way to directly see the player inventory in minecraft education edition. The tools below are work arounds to this problem by using the in built minecraft commands (these can be triggered in python but are one way only, this check if the command works, a block is placed in the world and python check if the block exisits). To get this working you must:
Have cheats enabled
Be in the area where the blocks are placed or create a ticking zone (see bottom of this page)
Copy the functions to the top your code
#Custom function - copy and paste this code
def has_item(item, amount, x, y, z):
blocks.place(AIR, world(x, y, z))
command = "execute @p[hasitem={item=" + item
command += ",quantity=" + str(amount) + "..}] ~ ~ ~ setblock "
command += str(x) + " " + str(y) + " " + str(z) + " diamond_block"
player.execute(command)
loops.pause(200)
if blocks.test_for_block(DIAMOND_BLOCK, world(x, y, z)):
return True
else:
return False
#Example of using the function
if has_item("diamond",5,0,0,0) == True:
player.say("Has enough diamonds")
else:
player.say("Not enough diamonds")
The code on the left does the following:
Places a air block in the world (reset)
Runs a minecraft command (not python) to see if the player has a diamond and then places a diamond_block if true
wait for the command to run (200ms)
Checks if their is a diamond block at that set position and outputs a diffrent responce depending if the answer is yes or no).
Running the function
This function requires:
the item name (in speech marks)
amount the player must at least have
X,Y,Z world cords to place the checker block
#Custom function - copy and paste this code
def is_holding(item, x, y, z):
blocks.place(AIR, world(x, y, z))
command = "execute @p[hasitem={item=" + item
command += ",location=slot.weapon.mainhand}] ~ ~ ~ setblock "
command += str(x) + " " + str(y) + " " + str(z) + " diamond_block"
player.execute(command)
loops.pause(200)
if blocks.test_for_block(DIAMOND_BLOCK, world(x, y, z)):
return True
else:
return False
#Example of running the function
if is_holding("snowball",0,0,0) == True:
player.say("Holding snowball")
else:
player.say("No snowball")
This code check if the player has an item in their active hotbar slot.
This code work simliar to checking if the player has an item.
Running the function
This function requires:
the item name (in speech marks)
X,Y,Z world cords to place the checker block
A ticking area, is an area that is always loaded by the game. This is required to use the inventory exploit on this page.
Run this command (cheat will need to be enabled). This will set up a 3 chunk radius around the player that will remain loaded at all times.