Jump to content

[1.7.2] Block.getBlockFromName()


artman41

Recommended Posts

How does this function work? I'm trying to use it to get a block via an unlocalized name I'm getting from the nbt data of an item. Currently, however, it doesn't really do anything :/

 

public void addInformation(ItemStack itemstack, EntityPlayer player, List list, boolean par4)
{
	itemstack.stackTagCompound = new NBTTagCompound();
	itemstack.stackTagCompound.setString("Block", Blocks.stone.getUnlocalizedName());

	list.add("Magic Wand");
	list.add(itemstack.stackTagCompound.getString("Block").substring(5));
}

public ItemStack onItemRightClick(ItemStack par1ItemStack, World par2World, EntityPlayer par3EntityPlayer)
{
	if(!par2World.isRemote){

		int minWX = (int)par3EntityPlayer.posX & ~0x0f;  // throws away the bottom four bits, i.e. truncates to the next lowest multiple of 16, eg 32, 16, 0, -16, -32 etc
		int minWZ = (int)par3EntityPlayer.posZ & ~0x0f;
		int maxWX = minWX + 15;
		int maxWZ = minWZ + 15;

		Reference.fillChunk(minWX, minWZ, maxWX, maxWZ, par2World, par3EntityPlayer, Recipes.Stick2);
	}
	return par1ItemStack;
}

public boolean onItemUse(ItemStack par1ItemStack, EntityPlayer par2EntityPlayer, World par3World, int x, int y, int z, int side, float hitX, float hitY, float hitZ){
	if (Keyboard.isKeyDown(Keyboard.KEY_RSHIFT) || Keyboard.isKeyDown(Keyboard.KEY_LSHIFT))
	{
		if (!par3World.isRemote)
		{
			par1ItemStack.stackTagCompound.setString("Block", par3World.getBlock(x, y, z).getUnlocalizedName());
			return true;
		}
		return false;
	}

 

Then this is the fillChunk function:

 

public static void fillChunk(int minWX, int minWZ, int maxWX, int maxWZ, World par1World, EntityPlayer Player, ItemStack par1ItemStack){
        for (int wx = minWX; wx <= maxWX; ++wx) {
            for (int wz = minWZ; wz <= maxWZ; ++wz) {
               // setblock (wx, playerYpos - 1, wz)
            	par1World.setBlock(wx, (int)Player.posY -1, wz, Block.getBlockFromName(par1ItemStack.stackTagCompound.getString("Block").substring(5)));
            }
       }

Link to comment
Share on other sites

Use Block.blockRegistry.getNameForObject() to get the name from a Block and Block.blockRegistry.getObject(name) to get a Block from a name.

Then: Why do you modify the ItemStack inside addInformation? This method is called every frame on the CLIENT. That is not the right place to modify data.

Also: You should get the Block object before the loop, don't look it up again and again and again.

 

If I modify the NBT data inside the onCreated method, the game freezes since there is no data for the tooltip :/ So I don't know where else to put it .-.

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.