Posted September 3, 201411 yr 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
September 3, 201411 yr 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?
September 3, 201411 yr Author 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
September 3, 201411 yr 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).
September 3, 201411 yr Author 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
September 3, 201411 yr 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
September 4, 201411 yr Author Thank you GreyGhost, this is most helpful! Now I also understand block directions (I found some examples where integers were just hardcoded and wondered what meant what). Thank you!
December 9, 201410 yr 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.