Jump to content

[1.8] Get Position of Block / IBlockState


McPlayHD

Recommended Posts

I'm trying to get the position of the placed block out of the PlaceEvent. How is this possible?

 

@SubscribeEvent
public void onBlockPlace(PlaceEvent e) {
if(e.player == plugin.getPlayer()) {
	if(e.placedBlock == Blocks.redstone_torch.getDefaultState()) {
		BlockPos bp = ?;
	}
}
}

Link to comment
Share on other sites

Did you try looking at the source code of the

BlockEvent.PlaceEvent

class or the fields/methods you can access on the instance you've been provided? Use the

BlockEvent#pos

field (in 1.8.9 and earlier) or

BlockEvent#getPos

method (in 1.9) to get the

BlockPos

of any

BlockEvent

subclass (e.g.

BlockEvent.PlaceEvent

).

 

An

IBlockState

represents a specific state of a

Block

. If you only care about the

Block

itself, use

IBlockState#getBlock

to get the

Block

and compare that instead of the state.

 

Are you sure you want to compare the player with

==

? A new instance is created every time a player respawns, so

plugin

(whatever it is) will have to maintain a reference to the current player instance. You may want to store and compare the player's

UUID

instead: use

Entity#getUniqueID

to get it and

Object#equals

to compare it.

 

Edit: Always specify the Minecraft version you're using in the thread title.

Please don't PM me to ask for help. Asking your question in a public thread preserves it for people who are having the same problem in the future.

Link to comment
Share on other sites

So I tried it without the player == player but not even this code works:

 

@SubscribeEvent
public void onBlockPlace(PlaceEvent e) {
plugin.sendMessage("ASDASDSd");
}

 

It seems like the Event doesn't get called once a player places a block. But all the other Events in this class get fired... I have a KeyInputEvent who works fine

Link to comment
Share on other sites

Have you registered the handler on the right event bus? In 1.8 and earlier,

KeyInputEvent

is fired on the FML bus (

FMLCommonHandler#bus

) and

BlockEvent.PlaceEvent

is fired on the Forge bus (

MinecraftForge.EVENT_BUS

). In 1.8.8 and later, the FML bus has been merged with the Forge bus.

Please don't PM me to ask for help. Asking your question in a public thread preserves it for people who are having the same problem in the future.

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Unfortunately, your content contains terms that we do not allow. Please edit your content to remove the highlighted words below.
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Announcements



×
×
  • Create New...

Important Information

By using this site, you agree to our Terms of Use.