Jump to content

Recommended Posts

Posted

Hey there!

 

So I'm in need of accessing a block's TE so I can get a couple of integers from it.. How would I do this? I have the itemblock already setup, however I do not know how to access that information from the TE.. I know I'll probably have to send a packet with the data in it, but I need to access it first.

Relatively new to modding.

Currently developing:

https://github.com/LambdaXV/DynamicGenerators

Posted

I have a custom ItemBlock

   public static class TheItemBlock extends ItemBlockBase {
        public TheItemBlock(Block block){
            super(block);
            this.setHasSubtypes(false);
            this.setMaxDamage(0);
        }


        @Override
        public String getUnlocalizedName(ItemStack stack){
            return this.getUnlocalizedName();
        }

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

        @Override
        public void addInformation(ItemStack stack, EntityPlayer player, List<String> list, boolean bool){
            
            list.add(StringUtil.localize(""));
        }
    }

which is a inner class to the block.

 

I want to put inside the ItemBlock tooltip some values from the TE.. The TE generates randomly, with randomly assigned values, when you pick it up, I want those values to display in the tooltip.

Relatively new to modding.

Currently developing:

https://github.com/LambdaXV/DynamicGenerators

Posted

Yes I know blocks and items blocks don't have tile entities, the block itself has one with 3 data numbers... So what I'm asking can I get information from the BLOCK TE and put it into the ITEMBLOCK an tooltip....

 

ex:

public class BlockVoidCrystal extends BlockContainerBase {

    public BlockVoidCrystal(String name) {
        super(Material.GLASS, name);
    }

    @Override
    public ItemBlockBase getItemBlock() {
        return new TheItemBlock(this);
    }

    @Nullable
    @Override
    public TileEntity createNewTileEntity(World worldIn, int meta) {
        return new TileEntityVoidCrystal(); 
    }

    @Override
    @SideOnly(Side.CLIENT)
    public boolean shouldSideBeRendered(IBlockState blockState, IBlockAccess worldIn, BlockPos pos, EnumFacing side) {
        return false;
    }

    @Override
    public boolean isBlockNormalCube(IBlockState blockState) {
        return false;
    }

    @Override
    public boolean isOpaqueCube(IBlockState blockState) {
        return false;
    }


    public static class TheItemBlock extends ItemBlockBase {
        public TheItemBlock(Block block){
            super(block);
            this.setHasSubtypes(false);
            this.setMaxDamage(0);
        }


        @Override
        public String getUnlocalizedName(ItemStack stack){
            return this.getUnlocalizedName();
        }

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

        @Override
        public void addInformation(ItemStack stack, EntityPlayer player, List<String> list, boolean bool){

            list.add(StringUtil.localize(""));
        }
    }
}

 

How would I access said arbitrary data?..

Relatively new to modding.

Currently developing:

https://github.com/LambdaXV/DynamicGenerators

Posted

I assume you want to add the integers from the

TileEntity

to the NBT of an

ItemStack

? You want to override

getDrops

to return a

List<ItemStack>

with the NBT set to the values of the

TileEntity

, which you can access using the

IBlockAccess

and

BlockPos

provided.

Don't PM me with questions. They will be ignored! Make a thread on the appropriate board for support.

 

1.12 -> 1.13 primer by williewillus.

 

1.7.10 and older versions of Minecraft are no longer supported due to it's age! Update to the latest version for support.

 

http://www.howoldisminecraft1710.today/

Posted

Note that by default, the

TileEntity

is removed from the world before

Block#getDrops

is called. You need to delay its removal like Forge's patch to

BlockFlowerPot

does.

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.

Posted

Here is what I have so far:


    @Override
    public ArrayList<ItemStack> getDrops(IBlockAccess world, BlockPos pos, IBlockState state, int fortune) {
        TileEntityVoidCrystal te = (TileEntityVoidCrystal)world.getTileEntity(pos);
        ArrayList<ItemStack> stack = new ArrayList<>();
        if(te != null) {
            NBTTagCompound compound = new NBTTagCompound();
            compound.setFloat("Decay", te.decay);
            compound.setFloat("Purity", te.purity);
            compound.setFloat("Charge", te.charge);

            stack.add(new ItemStack())
        }
    }

    @Override
    public boolean removedByPlayer(IBlockState state, World world, BlockPos pos, EntityPlayer player, boolean willHarvest) {
        if(willHarvest) return true;
        return super.removedByPlayer(state, world, pos, player, willHarvest);
    }

    @Override
    public void harvestBlock(World world, EntityPlayer player, BlockPos pos, IBlockState state, TileEntity te, ItemStack tool)
    {
        super.harvestBlock(world, player, pos, state, te, tool);
        world.setBlockToAir(pos);
    }

 

However, how do I write that NBT to the ItemStack?

Relatively new to modding.

Currently developing:

https://github.com/LambdaXV/DynamicGenerators

Posted

You want to make an

ItemStack

variable, then call

ItemStack#setTagCompound(NBTTagCompound)

on that variable. This is also the

ItemStack

you want to add to the

List<ItemStack>

.

Don't PM me with questions. They will be ignored! Make a thread on the appropriate board for support.

 

1.12 -> 1.13 primer by williewillus.

 

1.7.10 and older versions of Minecraft are no longer supported due to it's age! Update to the latest version for support.

 

http://www.howoldisminecraft1710.today/

Posted

Thanks for your help!

 

If anybody is searching for this here is the completed code:

 

 

    @Override
    public ArrayList<ItemStack> getDrops(IBlockAccess world, BlockPos pos, IBlockState state, int fortune) {
        TileEntityVoidCrystal te = (TileEntityVoidCrystal)world.getTileEntity(pos);
        ArrayList<ItemStack> stacks = new ArrayList<>();
        if(te != null) {
            ItemStack itemToReturn = new ItemStack(InitBlocks.blockVoidCrystal);
            NBTTagCompound compound = new NBTTagCompound();
            compound.setFloat("Decay", te.decay);
            compound.setFloat("Purity", te.purity);
            compound.setFloat("Charge", te.charge);

            itemToReturn.setTagCompound(compound);

            stacks.add(itemToReturn);
        }

        return stacks;
    }

    @Override
    public boolean removedByPlayer(IBlockState state, World world, BlockPos pos, EntityPlayer player, boolean willHarvest) {
        if(willHarvest) return true;
        return super.removedByPlayer(state, world, pos, player, willHarvest);
    }

    @Override
    public void harvestBlock(World world, EntityPlayer player, BlockPos pos, IBlockState state, TileEntity te, ItemStack tool)
    {
        super.harvestBlock(world, player, pos, state, te, tool);
        world.setBlockToAir(pos);
    }

    @Override
    public void onBlockPlacedBy(World world, BlockPos pos, IBlockState state, EntityLivingBase entity, ItemStack stack) {
        super.onBlockPlacedBy(world, pos, state, entity, stack);
        if(!world.isRemote) {
            TileEntityVoidCrystal te = (TileEntityVoidCrystal) world.getTileEntity(pos);
            if (te != null) {
                NBTTagCompound compound = stack.getTagCompound();
                if (compound != null) {
                    float tempDecay = compound.getFloat("Decay");
                    float tempPurity = compound.getFloat("Purity");
                    float tempCharge = compound.getFloat("Charge");

                    te.decay = tempDecay;
                    te.purity = tempPurity;
                    te.charge = tempCharge;
                }
            }
        }

    }

 

 

Relatively new to modding.

Currently developing:

https://github.com/LambdaXV/DynamicGenerators

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.