Jump to content

Recommended Posts

Posted (edited)

You need to save the TileEntity's data to the ItemStack's NBT (or capabilities). You can do this by overriding Block#getDrops to create an ItemStack with the appropriate data and then return a List<ItemStack> containing it.

 

By default, the TileEntity is removed from the world before Block#getDrops is called. You need to delay this like Forge's patch to BlockFlowerPot does.

Edited by Choonster

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
  On 2/9/2017 at 2:10 AM, abused_master said:

Do you by any chance have a simple example of some data being saved to athe ItemStack's NBT with getDrops? if not thats all right, but thanks for the info will see what happens.

Expand  

 

Look at BlockSkull or BlockBanner.

 

You can see an example of a TileEntity's IFluidHandler being saved to an ItemStack's IFluidHandlerItem here.

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 (edited)

I did this for my containers, because I wanted them to retain their inventory when broken. I created a method in my Tile Entity that looked like this:

 

public void dropContainerWithInventory(World world, BlockPos pos, IBlockState state, EntityPlayer player, TileEntity tileEntity)

{

    if (tileEntity != null && Item.getItemFromBlock(state.getBlock()) != null)

    {

        ItemStack stack = new ItemStack(this);

        NBTTagCompound inventoryTag = new NBTTagCompound();

        tileEntity.writeToNBT(inventoryTag);

        NBTTagCompound masterTag = new NBTTagCompound();

        masterTag.setTag("BlockEntityTag", inventoryTag);

        stack.setTagCompound(masterTag);

        spawnAsEntity(world, pos, stack);

    }

    world.setBlockToAir(pos);

}

 

If I remember right, you can delay the deletion of the tile entity by overriding removedByPlayer to return true if the willHarvest parameter is true.

Edited by Daeruin
  • 3 months later...
Posted (edited)

All right so when trying this on a fluid tank it would crash when breaking with

java.lang.NullPointerException: Unexpected error
	at abused_master.techexpansion.blocks.tank.BlockTank.saveFluidToStack(BlockTank.java:151)
	at abused_master.techexpansion.blocks.tank.BlockTank.removedByPlayer(BlockTank.java:165)
	at net.minecraft.client.multiplayer.PlayerControllerMP.onPlayerDestroyBlock(PlayerControllerMP.java:192)
	at net.minecraft.client.multiplayer.PlayerControllerMP.onPlayerDamageBlock(PlayerControllerMP.java:339)
	at net.minecraft.client.Minecraft.sendClickBlockToController(Minecraft.java:1512)
	at net.minecraft.client.Minecraft.processKeyBinds(Minecraft.java:2298)
	at net.minecraft.client.Minecraft.runTickKeyboard(Minecraft.java:2061)
	at net.minecraft.client.Minecraft.runTick(Minecraft.java:1849)
	at net.minecraft.client.Minecraft.runGameLoop(Minecraft.java:1127)
	at net.minecraft.client.Minecraft.run(Minecraft.java:407)
	at net.minecraft.client.main.Main.main(Main.java:118)
	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
	at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
	at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
	at java.lang.reflect.Method.invoke(Method.java:498)
	at net.minecraft.launchwrapper.Launch.launch(Launch.java:135)
	at net.minecraft.launchwrapper.Launch.main(Launch.java:28)
	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
	at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
	at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
	at java.lang.reflect.Method.invoke(Method.java:498)
	at net.minecraftforge.gradle.GradleStartCommon.launch(GradleStartCommon.java:97)
	at GradleStart.main(GradleStart.java:26)

this is how i'm saving my data:

 

public ItemStack saveFluidToStack(World world, BlockPos pos) {
        ItemStack stack = new ItemStack(Item.getItemFromBlock(this));

        TileEntityTank te = (TileEntityTank) world.getTileEntity(pos);
        if (te.tank.getFluid() != null && te.tank.getFluidAmount() > 0) {
            final NBTTagCompound tileTag = te.writeToNBT(new NBTTagCompound());
            stack.getTagCompound().setTag("TileData", tileTag);
        }
        return stack;
    }


    @Override
    public boolean removedByPlayer (IBlockState state, World world, BlockPos pos, EntityPlayer player, boolean willHarvest) {
        ItemStack dropStack = saveFluidToStack(world, pos);
        EntityItem item = new EntityItem(world, pos.getX(), pos.getY(), pos.getZ(), dropStack);
        world.spawnEntity(item);

        return world.setBlockToAir(pos);
    }

    @Override
    public int quantityDropped(Random random) {
        return 0;
    }

    @Override
    public void onBlockPlacedBy(World world, BlockPos pos, IBlockState state, EntityLivingBase placer, ItemStack stack) {
        world.setBlockState(pos, state.withProperty(TYPE, TankTier.get(stack.getMetadata())));
        final TileEntityTank tank = (TileEntityTank) world.getTileEntity(pos);
        if (stack.hasTagCompound()) {
            if (tank.tank != null) {
                tank.readFromNBT(stack.getTagCompound().getCompoundTag("TileData"));
            }
        }
    }

Line 151 is stack.getTagCompound().setTag("TileData", tileTag);

Edited by abused_master
Posted

ItemStacks don't have a stack compound tag by default, you need to create one and set it as the stack compound tag by calling ItemStack#setTagCompound.

 

Alternatively, use a method like ItemStack#setTagInfo that automatically creates the stack compound tag if it doesn't exist and then calls NBTTagCompound#setTag on it with the specified key and value.

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

All right i've used NBTTagCompound#setTag and it works fine, but when the block is broken it can be picked up but a ghost block remains on the floor, any idea what could be causing this? also when the block is broken then placed again with fluid inside it gives me this:

[09:29:15] [Client thread/ERROR]: Received invalid update packet for null tile entity at BlockPos{x=-137, y=64, z=70} with data: {FluidName:"water",Amount:2000,FluidData:{FluidName:"water",Amount:2000},x:-137,y:64,z:70,id:"minecraft:tile_tank"}

the fluid is also rendered at the old position instead of the new one

my TileEntity Code:

public class TileEntityTank extends TileEntityBase {
  
    public FluidTank tank;

    public TileEntityTank() {
        tank = new FluidTank(this.tier());
    }

    @Override
    public void readFromNBT(NBTTagCompound nbt) {
        super.readFromNBT(nbt);
        if (nbt.hasKey("FluidData")) {
            tank.setFluid(FluidStack.loadFluidStackFromNBT(nbt.getCompoundTag("FluidData")));
        }
      
        if(tank != null && tank.getFluid() != null) {
            tank.readFromNBT(nbt);
        }

        if (this.tank != null) {
            tank.setTileEntity(this);
        }
    }

    @Override
    public NBTTagCompound writeToNBT(NBTTagCompound nbt) {
        if (tank != null && tank.getFluid() != null) {
            final NBTTagCompound tankTag = new NBTTagCompound();
            tank.getFluid().writeToNBT(tankTag);
            nbt.setTag("FluidData", tankTag);
        }
        tank.writeToNBT(nbt);
        return super.writeToNBT(nbt);
    }

    public int tier() {
        return 60000;
    }

    @Override
    public void update() {
        if(!world.isRemote) {
            IBlockState state = world.getBlockState(pos);
            world.notifyBlockUpdate(pos, state, state, 8);
        }
    }
  
      @Override
    public boolean hasCapability(Capability<?> capability, @Nullable EnumFacing facing) {
        return this.getCapability(capability, facing) != null;
    }

    @Nullable
    @Override
    public <T> T getCapability(Capability<T> capability, @Nullable EnumFacing facing) {
        if(capability == CapabilityFluidHandler.FLUID_HANDLER_CAPABILITY) {
            return (T) this.tank;
        }
        return super.getCapability(capability, facing);
    }
}

 

Posted (edited)

Block#removedByPlayer is called on both sides, so you're spawning a real EntityItem on the server (which also spawns it on all nearby clients) and a "ghost" EntityItem on the client (that can't be interacted with because the server doesn't know about it). Entities should only be spawned on the server.

 

Don't manually spawn the EntityItem, override Block#getDrops like I told you to in my first reply.

 

If you store a compound tag in the "BlockEntityTag" key of an ItemStack's stack compound tag, Minecraft will automatically call TileEntity#readFromNBT with this tag when a player places the block. You don't need to do this yourself.

 

Is the update method being called every tick (e.g. because it implements ITickable#update)? If so, you shouldn't be calling World#notifyBlockUpdate every tick, only call it to send the TileEntity's update packet or re-render the chunk (it always does both). It's possible that this is causing the TileEntity's update packet to be sent before the client knows that there's now a TileEntity at that position.

 

The flags argument of World#notifyBlockUpdate is completely unused by the server and isn't sent to the client.

 

Why are you reading/writing the tank and its contents from/to NBT separately? Just read/write the tank (FluidTank#readFromNBT/FluidTank#writeToNBT) and it will read/write its contents.

Edited by Choonster

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

All right i've fixed those, but the problem still persists, also when the block is broken then replaced, and the world is reloaded the contents disappear, this however does not occur if the block is not moved on initial placement

Posted
  On 5/20/2017 at 3:53 PM, abused_master said:

All right i've fixed those, but the problem still persists, also when the block is broken then replaced, and the world is reloaded the contents disappear, this however does not occur if the block is not moved on initial placement

Expand  

 

Post your new Block and TileEntity classes.

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.

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

    • Looking to grab the Temu coupon code $100 off this month? You're in the right place for the biggest savings on your favorite products. Use the exclusive "acw696499" Temu coupon code for maximum benefits across the USA, Canada, and European nations. Whether you're a new customer or a long-time user, this code unlocks massive discounts and perks. With the Temu coupon $100 off and Temu 100 off coupon code, you’re not just saving money—you’re upgrading your shopping experience. What Is The Coupon Code For Temu $100 Off? Both new and existing customers can enjoy unbeatable savings with our exclusive coupon code. Use this Temu coupon $100 off and get a $100 off Temu coupon for your next order. acw696499 – Flat $100 off your order at checkout. acw696499 – $100 coupon pack you can use on multiple products. acw696499 – $100 flat discount exclusively for new customers. acw696499 – Extra $100 promo code for loyal, existing customers. acw696499 – $100 coupon available for shoppers in the USA and Canada. Temu Coupon Code $100 Off For New Users In 2025 If you're new to Temu, you're in for a treat. Use our code for the Temu coupon $100 off and enjoy unmatched discounts. acw696499 – Flat $100 discount for first-time buyers. acw696499 – Unlock a $100 coupon bundle specially for new customers. acw696499 – Redeem up to $100 coupon value across multiple purchases. acw696499 – Get free shipping to over 68 countries. acw696499 – Enjoy an extra 30% off any purchase as a new user. How To Redeem The Temu Coupon $100 Off For New Customers? To use the Temu $100 coupon and claim your Temu $100 off coupon code for new users, follow these steps: Download the Temu app or visit the official website. Register for a new account using your email or phone number. Add your favorite products to the cart. Enter the coupon code acw696499 at checkout. Enjoy instant savings and free shipping benefits. Temu Coupon $100 Off For Existing Customers Returning customers can still enjoy exceptional deals by applying our exclusive code. Use the Temu $100 coupon codes for existing users and unlock Temu coupon $100 off for existing customers free shipping perks. acw696499 – Receive an additional $100 discount as a returning user. acw696499 – Use the $100 coupon bundle for multiple purchases. acw696499 – Get a free gift with express shipping across the USA and Canada. acw696499 – Enjoy an extra 30% off on top of your current discounts. acw696499 – Free shipping available to 68 countries worldwide. How To Use The Temu Coupon Code $100 Off For Existing Customers? To activate the Temu coupon code $100 off and enjoy your savings as a returning buyer, follow these simple steps: Log into your existing Temu account. Add your chosen items to the shopping cart. Head to the checkout page. Apply the code acw696499 in the promo code box. Watch your total drop instantly with the Temu coupon $100 off code. Latest Temu Coupon $100 Off First Order Enjoy your first shopping experience on Temu with massive savings! Apply the Temu coupon code $100 off first order, Temu coupon code first order, or Temu coupon code $100 off first time user to save more. acw696499 – Flat $100 discount for your first order. acw696499 – Special $100 Temu coupon code for first orders. acw696499 – Enjoy up to $100 coupon bundle across different purchases. acw696499 – Free shipping to more than 68 countries. acw696499 – Extra 30% off on your initial order. How To Find The Temu Coupon Code $100 Off? Searching for the best Temu coupon $100 off deals? Check out Temu coupon $100 off Reddit threads for user-shared experiences and updated codes. You can also sign up for Temu’s newsletter for personalized offers. Visit their official social media pages or rely on trusted coupon-sharing websites to grab the most recent working promo codes. Is Temu $100 Off Coupon Legit? Yes, the Temu $100 Off Coupon Legit claim is absolutely true. We guarantee our Temu 100 off coupon legit code "acw696499" is tested and verified for accuracy. Anyone can safely use this code to receive $100 off their first or repeat orders. It’s valid internationally and doesn’t expire, so use it anytime for instant savings. How Does Temu $100 Off Coupon Work? The Temu coupon code $100 off first-time user and Temu coupon codes 100 off give users a direct discount during checkout. When you enter the coupon code during payment, the system automatically deducts $100 from your total bill. Whether you're a first-time buyer or a loyal customer, our code ensures unbeatable savings. How To Earn Temu $100 Coupons As A New Customer? To earn the Temu coupon code $100 off and get access to the 100 off Temu coupon code, sign up as a new customer on the Temu platform. Once registered, apply the promo code "acw696499" during checkout. You’ll instantly receive a $100 coupon bundle, free shipping, and extra discounts exclusive to new users. What Are The Advantages Of Using The Temu Coupon $100 Off? Using the Temu coupon code 100 off and Temu coupon code $100 off unlocks the following perks: $100 discount on your first order. $100 coupon bundle for multiple uses. 70% discount on popular items. Extra 30% off for returning customers. Up to 90% off on selected products. Free gift for new users. Free shipping to over 68 countries. Temu $100 Discount Code And Free Gift For New And Existing Customers Want more than discounts? The Temu $100 off coupon code and $100 off Temu coupon code offer added bonuses for everyone. acw696499 – $100 discount for your first order. acw696499 – Extra 30% off on any product. acw696499 – Free gift for first-time buyers. acw696499 – Up to 70% discount sitewide. acw696499 – Free shipping and gift in 68 countries including USA & UK. Pros And Cons Of Using The Temu Coupon Code $100 Off This Month Using the Temu coupon $100 off code and Temu 100 off coupon offers major pros and a couple of cons: Pros: Huge $100 discount on first and repeat orders. Free shipping globally. Free gift for new users. Up to 90% off on exclusive deals. Extra 30% discount for all users. Cons: Cannot be combined with certain flash sale items. Limited-time availability for some regional users. Terms And Conditions Of Using The Temu Coupon $100 Off In 2025 Before using the Temu coupon code $100 off free shipping and latest Temu coupon code $100 off, keep these in mind: Valid for both new and returning users. Code "acw696499" works across 68 countries worldwide. No minimum purchase required. No expiration date—use it anytime. Free shipping and gifts depend on regional availability. Final Note: Use The Latest Temu Coupon Code $100 Off Save big on every order with our Temu coupon code $100 off—it’s the smartest way to shop. Apply your Temu coupon $100 off today and make your online shopping budget-friendly and exciting.
    • Update your drivers: https://www.amd.com/en/support/downloads/previous-drivers.html/processors/ryzen/ryzen-3000-series/amd-ryzen-7-3700u.html
    • mclo only shows 25000 lines - add the rest with another link
    • Make a test without Create Big Cannons More Shells
    • Add this mod and test it again https://www.curseforge.com/minecraft/mc-mods/night-config-fixes/files/4614068
  • Topics

×
×
  • Create New...

Important Information

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