Jump to content

[1.7.2]Custom metadata sapling not return two different icons


JimiIT92

Recommended Posts

Hi guys :D I'm making two new saplings and i want to make it like the vanilla saplings (Same id with different meta).

I have copy the BlockSapling class and change id to returns 2 blocks, but it not returns two different icons but only the icon of the first sapling for both the blocks. It's only the icon because if i place them the texture is correct.

 

Here is the BlockSaplingMod file:

package mod.mineworld.blocks.mod_plants;

import cpw.mods.fml.relauncher.Side;
import cpw.mods.fml.relauncher.SideOnly;

import java.util.List;
import java.util.Random;

import mod.mineworld.mod_tabs;
import net.minecraft.block.Block;
import net.minecraft.block.BlockBush;
import net.minecraft.block.IGrowable;
import net.minecraft.client.renderer.texture.IIconRegister;
import net.minecraft.creativetab.CreativeTabs;
import net.minecraft.init.Blocks;
import net.minecraft.item.Item;
import net.minecraft.item.ItemStack;
import net.minecraft.util.IIcon;
import net.minecraft.util.MathHelper;
import net.minecraft.world.World;
import net.minecraft.world.gen.feature.WorldGenBigTree;
import net.minecraft.world.gen.feature.WorldGenCanopyTree;
import net.minecraft.world.gen.feature.WorldGenForest;
import net.minecraft.world.gen.feature.WorldGenMegaJungle;
import net.minecraft.world.gen.feature.WorldGenMegaPineTree;
import net.minecraft.world.gen.feature.WorldGenSavannaTree;
import net.minecraft.world.gen.feature.WorldGenTaiga2;
import net.minecraft.world.gen.feature.WorldGenTrees;
import net.minecraft.world.gen.feature.WorldGenerator;

public class BlockSaplings extends BlockBush implements IGrowable
{
    public static final String[] sapling_names = new String[] {"apple", "palm"};
    private static final IIcon[] icon = new IIcon[sapling_names.length];

    public BlockSaplings()
    {
        float f = 0.4F;
        this.setBlockBounds(0.5F - f, 0.0F, 0.5F - f, 0.5F + f, f * 2.0F, 0.5F + f);
        this.setCreativeTab(mod_tabs.tabPlantsDecorations);
    }

    /**
     * Ticks the block if it's been scheduled
     */
    public void updateTick(World par1, int par2, int par3, int par4, Random par5)
    {
        if (!par1.isRemote)
        {
            super.updateTick(par1, par2, par3, par4, par5);

            if (par1.getBlockLightValue(par2, par3 + 1, par4) >= 9 && par5.nextInt(7) == 0)
            {
                this.markOrGrowMarked(par1, par2, par3, par4, par5);
            }
        }
    }

    /**
     * Gets the block's texture. Args: side, meta
     */
    @SideOnly(Side.CLIENT)
    public IIcon getIcon(int par1, int par2)
    {
        par2 &= 7;
        return icon[MathHelper.clamp_int(par2, 0, 5)];
    }
    
    /**
     * Returns an item stack containing a single instance of the current block type. 'i' is the block's subtype/damage
     * and is ignored for blocks which do not support subtypes. Blocks which cannot be harvested should return null.
     */
    protected ItemStack createStackedBlock(int par1)
    {
        return new ItemStack(this, 1, par1 & 3);
    }
    
    public void markOrGrowMarked(World par1, int par2, int par3, int par4, Random par5)
    {
        int l = par1.getBlockMetadata(par2, par3, par4);

        if ((l &  == 0)
        {
            par1.setBlockMetadataWithNotify(par2, par3, par4, l | 8, 4);
        }
        else
        {
            this.growTree(par1, par2, par3, par4, par5);
        }
    }

    public void growTree(World par1, int par2, int par3, int par4, Random par5)
    {
        if (!net.minecraftforge.event.terraingen.TerrainGen.saplingGrowTree(par1, par5, par2, par3, par4)) return;
        int l = par1.getBlockMetadata(par2, par3, par4) & 7;
        Object object = par5.nextInt(10) == 0 ? new WorldGenBigTree(true) : new WorldGenTrees(true);
        int i1 = 0;
        int j1 = 0;
        boolean flag = false;

        switch (l)
        {
            case 0:
            default:
                break;
            case 1:
                label78:

                for (i1 = 0; i1 >= -1; --i1)
                {
                    for (j1 = 0; j1 >= -1; --j1)
                    {
                        if (this.isSameSapling(par1, par2 + i1, par3, par4 + j1, 1) && this.isSameSapling(par1, par2 + i1 + 1, par3, par4 + j1, 1) && this.isSameSapling(par1, par2 + i1, par3, par4 + j1 + 1, 1) && this.isSameSapling(par1, par2 + i1 + 1, par3, par4 + j1 + 1, 1))
                        {
                            object = new WorldGenMegaPineTree(false, par5.nextBoolean());
                            flag = true;
                            break label78;
                        }
                    }
                }

                if (!flag)
                {
                    j1 = 0;
                    i1 = 0;
                    object = new WorldGenTaiga2(true);
                }

                break;
            case 2:
                object = new WorldGenForest(true, false);
                break;
            case 3:
                label93:

                for (i1 = 0; i1 >= -1; --i1)
                {
                    for (j1 = 0; j1 >= -1; --j1)
                    {
                        if (this.isSameSapling(par1, par2 + i1, par3, par4 + j1, 3) && this.isSameSapling(par1, par2 + i1 + 1, par3, par4 + j1, 3) && this.isSameSapling(par1, par2 + i1, par3, par4 + j1 + 1, 3) && this.isSameSapling(par1, par2 + i1 + 1, par3, par4 + j1 + 1, 3))
                        {
                            object = new WorldGenMegaJungle(true, 10, 20, 3, 3);
                            flag = true;
                            break label93;
                        }
                    }
                }

                if (!flag)
                {
                    j1 = 0;
                    i1 = 0;
                    object = new WorldGenTrees(true, 4 + par5.nextInt(7), 3, 3, false);
                }

                break;
            case 4:
                object = new WorldGenSavannaTree(true);
                break;
            case 5:
                label108:

                for (i1 = 0; i1 >= -1; --i1)
                {
                    for (j1 = 0; j1 >= -1; --j1)
                    {
                        if (this.isSameSapling(par1, par2 + i1, par3, par4 + j1, 5) && this.isSameSapling(par1, par2 + i1 + 1, par3, par4 + j1, 5) && this.isSameSapling(par1, par2 + i1, par3, par4 + j1 + 1, 5) && this.isSameSapling(par1, par2 + i1 + 1, par3, par4 + j1 + 1, 5))
                        {
                            object = new WorldGenCanopyTree(true);
                            flag = true;
                            break label108;
                        }
                    }
                }

                if (!flag)
                {
                    return;
                }
        }

        Block block = Blocks.air;

        if (flag)
        {
            par1.setBlock(par2 + i1, par3, par4 + j1, block, 0, 4);
            par1.setBlock(par2 + i1 + 1, par3, par4 + j1, block, 0, 4);
            par1.setBlock(par2 + i1, par3, par4 + j1 + 1, block, 0, 4);
            par1.setBlock(par2 + i1 + 1, par3, par4 + j1 + 1, block, 0, 4);
        }
        else
        {
            par1.setBlock(par2, par3, par4, block, 0, 4);
        }

        if (!((WorldGenerator)object).generate(par1, par5, par2 + i1, par3, par4 + j1))
        {
            if (flag)
            {
                par1.setBlock(par2 + i1, par3, par4 + j1, this, l, 4);
                par1.setBlock(par2 + i1 + 1, par3, par4 + j1, this, l, 4);
                par1.setBlock(par2 + i1, par3, par4 + j1 + 1, this, l, 4);
                par1.setBlock(par2 + i1 + 1, par3, par4 + j1 + 1, this, l, 4);
            }
            else
            {
                par1.setBlock(par2, par3, par4, this, l, 4);
            }
        }
    }

    public boolean isSameSapling(World par1, int par2, int par3, int par4, int par5)
    {
        return par1.getBlock(par2, par3, par4) == this && (par1.getBlockMetadata(par2, par3, par4) & 7) == par5;
    }

    /**
     * Determines the damage on the item the block drops. Used in cloth and wood.
     */
    public int damageDropped(int par1)
    {
        return MathHelper.clamp_int(par1 & 7, 0, 5);
    }

    /**
     * returns a list of blocks with the same ID, but different meta (eg: wood returns 4 blocks)
     */
    @SideOnly(Side.CLIENT)
    public void getSubBlocks(Item par1, CreativeTabs par2, List par3)
    {
        par3.add(new ItemStack(par1, 1, 0));
        par3.add(new ItemStack(par1, 1, 1));
    }

    @SideOnly(Side.CLIENT)
    public void registerBlockIcons(IIconRegister par1)
    {
        for (int i = 0; i < icon.length; ++i)
        {
            icon[i] = par1.registerIcon(this.getTextureName() + "_" + sapling_names[i]);
        }
    }

    public boolean func_149851_a(World p_149851_1_, int p_149851_2_, int p_149851_3_, int p_149851_4_, boolean p_149851_5_)
    {
        return true;
    }

    public boolean func_149852_a(World par1, Random par2, int par3, int par4, int par5)
    {
        return (double)par1.rand.nextFloat() < 0.45D;
    }

    public void func_149853_b(World par1, Random par2, int par3, int par4, int par5)
    {
        this.markOrGrowMarked(par1, par3, par4, par5, par2);
    }
}

 

And here is a snapshot of the item Icons (wich is wrong):

255suja.png

and here a snapshot of the block texture when placed (wich is correct)

wuow2o.png

 

Thanks in advice for all who will help me :D

Don't blame me if i always ask for your help. I just want to learn to be better :)

Link to comment
Share on other sites

You need to register your block with a metadata sensitive version of ItemBlock.

When you use GameRegistry.registerBlock() for your sapling, use the version with the ItemBlock class parameter and use the ItemBlockWithMetadata class:

GameRegistry.registerBlock(myBlockInstance, ItemBlockWithMetadata.class, "myBlockRegisteredName");

Don't ask for support per PM! They'll get ignored! | If a post helped you, click the "Thank You" button at the top right corner of said post! |

mah twitter

This thread makes me sad because people just post copy-paste-ready code when it's obvious that the OP has little to no programming experience. This is not how learning works.

Link to comment
Share on other sites

Yes i have the ItemBlock file for the saplings and i already register it with

GameRegistry.registerBlock(saplings, ItemSaplings.class, "saplings");

but it doesn't work :/ I'm pretty sure the problem is not the ItemSapling.class beacuse i also have two metadata woods wich hase the same ItemBlock class and it all works fine, so i don't know why it's not working with the saplings :/

This is the ItemSaplings class

package mod.mineworld.blocks.mod_plants;

import net.minecraft.block.Block;
import net.minecraft.item.ItemBlock;
import net.minecraft.item.ItemStack;

public class ItemSaplings extends ItemBlock {

private final static String[] subNames = {
	"apple", "palm"
};

public ItemSaplings(Block id) {
	super(id);
	setHasSubtypes(true);
}

@Override
public int getMetadata (int damageValue) {
	return damageValue;
}

@Override
public String getUnlocalizedName(ItemStack itemstack) {
	return getUnlocalizedName() + "." + subNames[itemstack.getItemDamage()];
}

}

Don't blame me if i always ask for your help. I just want to learn to be better :)

Link to comment
Share on other sites

  • 1 month later...

[sOLVED]

I have the Solution!

 

in your ItemBlock file, change the extension to ItemBlockWithMetadata, and add and extra comma and "block" in super as seen below.

 

Here's my file, ItemEverySapling:

package net.davidjholland.everytrees;

import net.minecraft.block.Block;
import net.minecraft.item.ItemBlockWithMetadata;
import net.minecraft.item.ItemStack;

public class ItemEverySapling extends ItemBlockWithMetadata {

public ItemEverySapling(Block block) {
	super(block, block);
}

public String getUnlocalizedName(ItemStack itemStack){
	int i = itemStack.getItemDamage();
	if (i < 0 || i >= BlockEverySapling.field_149882_a.length){
		i = 0;
	}
	return super.getUnlocalizedName() + "." + BlockEverySapling.field_149882_a[i];
}

public int getMetadata(int meta){
	return meta;
}

}

 

You're Welcome :)

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



  • Recently Browsing

    • No registered users viewing this page.
  • Posts

    • https://pastebin.com/VwpAW6PX My game crashes upon launch when trying to implement the Oculus mod to this mod compilation, above is the crash report, I do not know where to begin to attempt to fix this issue and require assistance.
    • https://youtube.com/shorts/gqLTSMymgUg?si=5QOeSvA4TTs-bL46
    • CubeHaven is a SMP server with unique features that can't be found on the majority of other servers! Java: MC.CUBEHAVEN.NET Bedrock: MC.CUBEHAVEN.NET:19132 3 different stores: - CubeHaven Store: Our store to purchase using real money. - Bitcoin Store: Store for Bitcoin. Bitcoin can be earned from playing the server. Giving options for players if they want to spend real money or grind to obtain exclusive packages. - Black Market: A hidden store for trading that operates outside our traditional stores, like custom enchantments, exclusive items and more. Some of our features include: Rank Up: Progress through different ranks to unlock new privileges and perks. 📈 Skills: RPG-style skill system that enhances your gaming experience! 🎮 Leaderboards: Compete and shine! Top players are rewarded weekly! 🏆 Random Teleporter: Travel instantly across different worlds with a click! 🌐 Custom World Generation: Beautifully generated world. 🌍 Dungeons: Explore challenging and rewarding dungeons filled with treasures and monsters. 🏰 Kits: Unlock ranks and gain access to various kits. 🛠️ Fishing Tournament: Compete in a friendly fishing tournament! 🎣 Chat Games: Enjoy games right within the chat! 🎲 Minions: Get some help from your loyal minions. 👥 Piñata Party: Enjoy a festive party with Piñatas! 🎉 Quests: Over 1000 quests that you can complete! 📜 Bounty Hunter: Set a bounty on a player's head. 💰 Tags: Displayed on nametags, in the tab list, and in chat. 🏷️ Coinflip: Bet with other players on coin toss outcomes, victory, or defeat! 🟢 Invisible & Glowing Frames: Hide your frames for a cleaner look or apply a glow to it for a beautiful look. 🔲✨[ Player Warp: Set your own warp points for other players to teleport to. 🌟 Display Shop: Create your own shop and sell to other players! 🛒 Item Skins: Customize your items with unique skins. 🎨 Pets: Your cute loyal companion to follow you wherever you go! 🐾 Cosmetics: Enhance the look of your character with beautiful cosmetics! 💄 XP-Bottle: Store your exp safely in a bottle for later use! 🍶 Chest & Inventory Sorting: Keep your items neatly sorted in your inventory or chest! 📦 Glowing: Stand out from other players with a colorful glow! ✨ Player Particles: Over 100 unique particle effects to show off. 🎇 Portable Inventories: Over virtual inventories with ease. 🧳 And a lot more! Become part of our growing community today! Discord: https://cubehaven.net/discord Java: MC.CUBEHAVEN.NET Bedrock: MC.CUBEHAVEN.NET:19132
    • # Problematic frame: # C [libopenal.so+0x9fb4d] It is always the same issue - this refers to the Linux OS - so your system may prevent Java from working   I am not familiar with Linux - check for similar/related issues  
  • Topics

×
×
  • Create New...

Important Information

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