Now for a new part of the minecraft api - polling events! With this new feature, we can execute code whenever you perform a certain action.
This makes it so you don't have to swap between windows to execute code anymore, you can simply do an action in minecraft and have your code be triggered.
In this specific example, it's setup so when you right click a block while holding a sword, your code is executed.
You'll see the first change is adding for blockhit in mc.player.pollBlockHits():
inside the while loop. For loops are a new type of loop. In this one, each time the loop runs, the blockhit variable contains information on the block you hit, and we can access its position via blockhit.pos
In the script on the right, everytime we right-click a block with a sword, the code executes that turns that block into gold. Try it!
Note! If you get an error here that says AttributeError: CmdPlayer instance has no attribute 'pollBlockHits'
, this means you are using the wrong version of the mcpi library! You need to use the teachcraft version - just copy the mcpi folder from this repository
to be in the same folder as your code.