Page 1 of 1

Minecraft Pi Edition API

Posted: Mon May 12, 2014 6:30 am
by tewe
Hi!

I am a beginner when it comes to Python, I am trying to learn it using the Minecraft API. I am trying to make a program that places a GoldBlock instead of every block I step on. This is the code that I have written:

Code: Select all

import minecraft.minecraft as minecraft
import minecraft.block as block
import time

mc = minecraft.Minecraft.create()
playerPosition = mc.player.getPos()
post = mc.postToChat
blockType = mc.getBlock(playerPosition.x, playerPosition.y -1, playerPosition.z)

if __name__ == "__main__":


	while ('True'):
		if not blockType == 9 or blockType == 0:
			mc.setBlock(playerPosition.x, playerPosition.y -1, playerPosition.z,block.GOLD_BLOCK)

Code: Select all

	while ('True'):
		if not blockType == 9 or blockType == 0:
			mc.setBlock(playerPosition.x, playerPosition.y -1, playerPosition.z,block.GOLD_BLOCK)
In the later code is a WhileLoop. I am pretty sure that it is this part of the code that is incorrect. When I run the program from LXTerminal it runs properly. But it is only the block that the player stands on when the program executes that is transformed into gold.

I really appreciate all of your help!

Re: Minecraft Pi Edition API

Posted: Mon May 12, 2014 7:33 am
by DougieLawson
You only read mc.getPos() & mc.getBlock() once.

Move those calls inside your while loop so that as you move you re-read the coordinates of the block to turn to gold.

Re: Minecraft Pi Edition API

Posted: Mon May 12, 2014 8:29 am
by elParaguayo
As an aside, your code says:

Code: Select all

while ('True')
True in inverted commas is the string True, rather than the True/False boolean value. You'd therefore normally have:

Code: Select all

while True:
However the effect is the same, as any string other than an empty string is considered True in Python. See https://docs.python.org/2/library/stdtypes.html

Re: Minecraft Pi Edition API

Posted: Mon May 12, 2014 5:06 pm
by tewe
Thank you for the help but it is still not working. I have tried to write another bit of code:

Code: Select all

	while ('True'):
		if not blockType == 9 or blockType == 0:
			mc.setBlock(playerPosition.x, playerPosition.y -1, playerPosition.z,block.GOLD_BLOCK)
                        mc.postToChat ('GOLD!')
I added mc.postToChat ('GOLD!'). That command repeats over and over again. So it is not the loop that is the incorrect part I think.

Re: Minecraft Pi Edition API

Posted: Mon May 12, 2014 6:27 pm
by elParaguayo
Did you see Dougie's comment?

These lines:

Code: Select all

playerPosition = mc.player.getPos()
blockType = mc.getBlock(playerPosition.x, playerPosition.y -1, playerPosition.z)
needs to be inside the While loop.

you're checking whether blockType is equal to a certain value, but you're not updating blockType to reflect the fact that your player is moving.

Re: Minecraft Pi Edition API

Posted: Mon May 12, 2014 6:51 pm
by MattHawkinsUK
Also you probably want to use getTilePos rather than getPos as it will give you integers rather than floats.

Code: Select all

playerPosition = mc.player.getTilePos()
blockType = mc.getBlock(playerPosition.x, playerPosition.y - 1, playerPosition.z)