Jump to content

Recommended Posts

Posted

Hey forum,

 

so another newbie question from me: How does one get the metadata from a block object? I've read the tutorial and it says

 

If you know the coordinates of the block and have the world it is in, you can get its metadata with the following method in the World class.

 

public int getBlockMetadata(int x, int y, int z);

 

Many methods in the Block class also provide the metadata as one of their arguments.

 

The problem is, I don't have the world, so I can't use getBlockMetadata. And I can't find a single method of the "many methods" that would return the metadata value. I get the ID by using Block's static method Block.getIdFromBlock(b), but there's no Block.getMetadataFromBlock() equivalent.

 

If you could help me out, that'd be awesome.

 

Thanks a lot!

Cheers

Henrik

Posted

you can't, Minecraft use one instance of Block class for all blocks of the same type, so it is almost static, metadata is not stored in Block, it is stored in World.

 

There is usually one way or another to get the world, what are you trying to do?

Posted

Aaaaah okay, I was so stuck within searching for a method of the Block class because it said there were methods in the docs.

 

WIth your hint I looked in the Chunk class (of which I extract my block) and sure enough, there is Chunk.getBlockMetadata(x, y, z) which I think does what I want! Thank you very much for the pointer, I was completely blind :-)

 

I would also like to know what the daylight light value of that block is and Chunk seems to offer that, too: Chunk.getBlockLightValue() - but it takes 4 parameters which are undocumented (they're only named p_76629_1_ through p_76629_4_). Is there any way to find out more documentation of this method? If I could find out where the docs to stuff like this is I'd have to ask fewer dumb questions :-)

 

Thank you again so much!

Cheers,

Henrik

Posted

You are most probably doing it wrong, I insist on using World. If you can give us where exactly you want the metadata, show us some code.

Some methods have metadata parameter, but it is not well documented, so the metadata might be in your code without you noticing.

If you have an entity or a player or a tile entity, all have a worldObj instance that can be used in getting metadata (and light too).

Posted

Ok, I first thought that passing a Chunk instance to my handler class would be sufficient. I get the Chunk from the OnChunLoad() event. I retrieve it from chunk using event.getChunk(). Not sure where I get the world from (chunk has a chunk.worldObj instance, is that what you meant?).

 

Cheers,

Henrik

Posted

Hi

 

You might find these links interesting.  They were for 1.6.4 but are still mostly accurate.

 

http://greyminecraftcoder.blogspot.com.au/2013/10/client-side-class-linkage-map.html

http://greyminecraftcoder.blogspot.com.au/2013/10/server-side-class-linkage-map.html

 

Most places you need a world, the method will give it to you as a parameter.

 

If not, you can get world from

Server side you can access the worlds from

MinecraftServer.getServer().worldServerForDimension or DimensionManager.getWorld()

 

Client side you can access the world from

Minecraft.theworld

 

This page might also be helpful for background info

http://greyminecraftcoder.blogspot.com.au/2013/07/blocks.html

 

-TGG

 

 

  • 3 months later...
Posted

I do it like this

 

public class EventGetBlockThePlayerLooksAt
{
private Block	previousBlock;

@SubscribeEvent
public void onPlayerTick(PlayerTickEvent event)
{
	if (event.side == Side.CLIENT)
	{
		EntityPlayer player = event.player;
		World world = player.worldObj;
//Gets the ray trace vector from the player using the arguments  distance, partialTickTime
		MovingObjectPosition movingObjectPosition = player.rayTrace(5, 1.0F);

		//if the ray trace isnt null, and if the block isnt an air block and if the ray trace hits a block then do stuff so that you dont always get an air block
		if (movingObjectPosition != null && !world.isAirBlock(movingObjectPosition.blockX, movingObjectPosition.blockY, movingObjectPosition.blockZ) && movingObjectPosition.typeOfHit == MovingObjectPosition.MovingObjectType.BLOCK)
		{
			Block selectedBlock = world.getBlock(movingObjectPosition.blockX, movingObjectPosition.blockY, movingObjectPosition.blockZ);
//if the last block you looked at isnt the new block your looking at (this is done so that you dont spam the block you are looking at
			if (previousBlock != selectedBlock)
			{
				// gets the blocks MetaData specifically the x,y,z coordinates of the block that is ray traced against
				int blockMetaData = world.getBlockMetadata(movingObjectPosition.blockX, movingObjectPosition.blockY, movingObjectPosition.blockZ);
				//gets the item from the block
				Item item = Item.getItemFromBlock(selectedBlock);
				//gets the unlocalized name for the blocks itemstack
				String unlocalizedName = item.getUnlocalizedName(new ItemStack(selectedBlock, 1, blockMetaData));
				// translates the unlocalized name stat to the name
				String selectedBlocksName = StatCollector.translateToLocal(unlocalizedName + ".name");
				event.player.addChatMessage(new ChatComponentText(EnumChatFormatting.WHITE + "This is a " + selectedBlocksName));
				System.out.println("You looked at a " + selectedBlocksName);
				previousBlock = selectedBlock;
			}
		}
	}
}
}

[code]

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.