SilverCrafter7
Posts: 6
Joined: Fri Jul 29, 2016 11:32 pm

Minecraft BlockEvents

Mon Aug 01, 2016 2:14 am

Hi I'm trying to get this to work, but I'm not sure how to use the block event type correctly. Thoughts?

Code: Select all

from mcpi.minecraft import Minecraft
from time import sleep

mc = Minecraft.create()
myCoords = mc.player.getPos()
blockEvent = mc.events.pollBlockHits()

DIRT = 3
blockEventType = blockEvent.HIT

if blockEventType == True:
    mc.postToChat("Hey! Don't Hit me!")

scotty101
Posts: 3958
Joined: Fri Jun 08, 2012 6:03 pm

Re: Minecraft BlockEvents

Mon Aug 01, 2016 9:19 am

You need to put the check for an event inside a loop otherwise it only happens once.

Also you set blockEventType to be blockEvent.HIT which means that your if statement will never be true.

If you describe what you want, I can help you with an example.

I'm guessing that you want to have the message "Hey! Don't Hit me!" appear when you hit a dirt block. Is that correct?
Electronic and Computer Engineer
Pi Interests: Home Automation, IOT, Python and Tkinter

scotty101
Posts: 3958
Joined: Fri Jun 08, 2012 6:03 pm

Re: Minecraft BlockEvents

Mon Aug 01, 2016 9:28 am

Here is a simple example of reading block hits

Code: Select all

import mcpi.minecraft as minecraft
import time

mc.minecraft.Minecraft.create()

while True:
    hits = mc.events.pollBlockHits()
    for hit in hits:
        print("You hit block ({},{},{})".format(hit.pos.x,hit.pos.y,hit.pos.z))
    time.sleep(0.1)
Electronic and Computer Engineer
Pi Interests: Home Automation, IOT, Python and Tkinter

SilverCrafter7
Posts: 6
Joined: Fri Jul 29, 2016 11:32 pm

Re: Minecraft BlockEvents

Mon Aug 01, 2016 2:20 pm

thx for the help I changed print to mc.postToChat

Return to “Python”