User avatar
tewe
Posts: 37
Joined: Tue Apr 22, 2014 2:49 pm

Minecraft Pi Edition API

Mon May 12, 2014 6:30 am

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!
I'm a swedish viking with big beard, I like to eat raspberry pi.

User avatar
DougieLawson
Posts: 36330
Joined: Sun Jun 16, 2013 11:19 pm
Location: Basingstoke, UK
Contact: Website Twitter

Re: Minecraft Pi Edition API

Mon May 12, 2014 7:33 am

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.
Note: Having anything humorous in your signature is completely banned on this forum. Wear a tin-foil hat and you'll get a ban.

Any DMs sent on Twitter will be answered next month.

This is a doctor free zone.

User avatar
elParaguayo
Posts: 1943
Joined: Wed May 16, 2012 12:46 pm
Location: London, UK

Re: Minecraft Pi Edition API

Mon May 12, 2014 8:29 am

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
RPi Information Screen: plugin based system for displaying weather, travel information, football scores etc.

User avatar
tewe
Posts: 37
Joined: Tue Apr 22, 2014 2:49 pm

Re: Minecraft Pi Edition API

Mon May 12, 2014 5:06 pm

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.
I'm a swedish viking with big beard, I like to eat raspberry pi.

User avatar
elParaguayo
Posts: 1943
Joined: Wed May 16, 2012 12:46 pm
Location: London, UK

Re: Minecraft Pi Edition API

Mon May 12, 2014 6:27 pm

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.
RPi Information Screen: plugin based system for displaying weather, travel information, football scores etc.

User avatar
MattHawkinsUK
Posts: 538
Joined: Tue Jan 10, 2012 8:48 pm
Location: UK
Contact: Website

Re: Minecraft Pi Edition API

Mon May 12, 2014 6:51 pm

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)
My Raspberry Pi blog and home of the BerryClip Add-on board : http://www.raspberrypi-spy.co.uk/
Follow me on Google+, Facebook, Pinterest and Twitter (@RPiSpy)

Return to “Python”