While I can't change the API for Minecraft: Pi edition I can change RaspberryJuice - so I decided to add functions to allow you to find out where the player is looking. You can download the RaspberryJuice plugin Canarymod and Bukkit.
I have also created a new Adventures in Minecraft starterkit which includes the new version of RapsberryJuice and everything you need to use the new api functions.
The 3 new functions in the api are:
- player.getRotation() - return the angle of rotation between 0 and 360
- player.getPitch() - returns the angle of pitch between -90 and 90
- player.getDirection() - returns a unit-vector of x,y,z pointing in the direction the player is facing
The functions also work with the entities as well so you can use entity.getRotation(), entity.getPitch() and entity.getDirection()
Here are the couple of code examples I demo in the video.
Get the players rotation and pitch angles:
#import the minecraft module import mcpi.minecraft as minecraft #create a connection to minecraft mc = minecraft.Minecraft.create() while True: #get the players rotational angle angle = mc.player.getRotation() #get the player up and down angle pitch = mc.player.getPitch() mc.postToChat(str(angle) + " : " + str(pitch))
Create a block in front of the player using getDirection():
import mcpi.minecraft as minecraft import mcpi.block as block import time #how far in front of the player the block will be BLOCKDISTANCE = 5 mc = minecraft.Minecraft.create() while True: #get the position pos = mc.player.getPos() #get the direction direction = mc.player.getDirection() #calc the position of the block in front of the player x = round(pos.x + (direction.x * BLOCKDISTANCE)) y = round(pos.y + (direction.y * BLOCKDISTANCE) + 1) z = round(pos.z + (direction.z * BLOCKDISTANCE)) mc.setBlock(x,y,z,block.DIAMOND_BLOCK) time.sleep(0.1) mc.setBlock(x,y,z,block.AIR)I hope you find the new api functions useful.
I've posted some code on the Adventures in Minecraft forum, showing how you can approximate direction in Minecraft Pi Edition. It's very rough, its only an approximation based on monitoring changes in the player position, but it's good enough for some purposes.
ReplyDeleteDavid Whale
@whaleygeek
And here is a link to David's post - http://www.stuffaboutcode.com/p/adventures-in-minecraft-forum.html?place=msg%2Fadventures-in-minecraft-forum%2Fxj6_42BrBAY%2FrEuwnB5gv98J
DeleteI have installed the latest version of RasperryJuice on my server but where can I download the updated Python module with the new functions from?
ReplyDeleteI got mine from https://github.com/brooksc/mcpipy
Thanks.
You can get it from the raspberry juice git hub repo - https://github.com/zhuowei/RaspberryJuice/tree/master/src/main/resources/mcpi/api/python/mcpi
DeleteIs there a way to do this on the original minecraft pi, that comes with the Raspberry pi
ReplyDeleteNo unfortunately not.
Deleteoh well... Going to look into david's toutorial. See if it works.
ReplyDelete