Jump to content
Search In
  • More options...
Find results that contain...
Find results in...

Saving TileEntity NBT data even when world is closed


FireTamer81
 Share

Recommended Posts

My goal with this is to have a block with a "Health Value" to both determine whether it should break and it's current model. I currently have that value being set to 3000 when the block is placed down.

I don't understand why, but after mimicking both vanilla and other mod examples of saving NBT data in a TileEntity, mine just won't save. 

I have gotten the actual value/data to work since I can use the "/data get block" command on it, but it always resets to 0 when I close and reload the world.

 

 

This is my Block Class:

public class WarenaiBlock extends Block
{
    public static final IntegerProperty CRACKED_DIRTY_CLEAN_POLISHED = CustomBlockstateProperties.CRACKED_DIRTY_CLEAN_POLISHED;

    public WarenaiBlock(Properties properties) {
        super(properties);
        this.registerDefaultState(this.stateDefinition.any()
                .setValue(CRACKED_DIRTY_CLEAN_POLISHED, Integer.valueOf(3)));
    }

    protected void createBlockStateDefinition(StateContainer.Builder<Block, BlockState> stateBuilder) {
        stateBuilder.add(CRACKED_DIRTY_CLEAN_POLISHED);
    }

    public void onPlace(BlockState p_220082_1_, World p_220082_2_, BlockPos p_220082_3_, BlockState p_220082_4_, boolean p_220082_5_) {
        StrongBlockTileEntity.setInitialBlockHealth();
    }

    
    @Override
    public boolean hasTileEntity(BlockState state) {
        return true;
    }

    @Override
    public TileEntity createTileEntity(BlockState state, IBlockReader world) {
        return new StrongBlockTileEntity();
    }
    

 

 

This is my TileEntity class:

public class StrongBlockTileEntity extends TileEntity
{
    private static int strongBlockHealth;


    public StrongBlockTileEntity(TileEntityType<?> tileEntityType) {
        super(tileEntityType);
    }

    public StrongBlockTileEntity() { this(TileEntityTypesInit.WARENAI_BLOCK_BLACK_STRONGBLOCK_TILE.get()); }


    public static void damageStrongBlock(int damageAmount) {
        int currentBlockHealth = strongBlockHealth;
        strongBlockHealth = currentBlockHealth - damageAmount;

    }

    public void repairStrongBlock(int repairAmount) {
        int currentBlockHealth = strongBlockHealth;
        strongBlockHealth = currentBlockHealth + repairAmount;
    }



    public static int getStrongBlockHealth() { return strongBlockHealth; }

    public static void showBlockHealth() {
        System.out.println("Block Health is: " + getStrongBlockHealth());
    }

    public static void setInitialBlockHealth() { strongBlockHealth = 3000; }




    /**
     * Overrides
     */

    @Override
    public CompoundNBT save(CompoundNBT tags) {
        super.save(tags);
        tags.putInt("BlockHealth", this.strongBlockHealth);
        return tags;
    }

    @Override
    public void load(BlockState state, CompoundNBT tags) {
        super.load(state, tags);
        strongBlockHealth = tags.getInt("BlockHealth");
    }


}

 

 

I also have two "Development Tools" that I made to manipulate the BlockHealth Value which do work:

public class DamageItem extends Item
{
    public DamageItem(Properties properties) {
        super(properties);
    }


    @Override
    public ActionResultType useOn(ItemUseContext context) {
        World worldIn = context.getLevel();
        BlockPos blockPos = context.getClickedPos();
        BlockState blockState = worldIn.getBlockState(blockPos);

        //Block warenaiClassBlock = blockState.getBlock();
        boolean warenaiClassBlock = blockState.getBlock() instanceof WarenaiBlock;

        if (blockState.hasTileEntity() && warenaiClassBlock) {
            StrongBlockTileEntity strongBlockTile = new StrongBlockTileEntity();

            strongBlockTile.damageStrongBlock(50);
            //strongBlockTile.showBlockHealth();
            return ActionResultType.SUCCESS;

        } else {
            return ActionResultType.PASS;
        }
    }
}

 

 

Link to comment
Share on other sites

I am currently taking Java Classes with my highschool, so I still a little weak on remembering those things.

However, I had it static because the "setInitialBlockHealth()" method cannot be referenced when it isn't static by my "onPlace" method in my block class, so then I have to change all the other methods and the "strongBlockHealth" field to static to match. But that then causes the problem above.

The value also doesn't change at all when the field and methods are not static. 

Link to comment
Share on other sites

You can't use static methods and fields like this. Those should all be instance methods.

You want to set a default value? Supply it in the constructor.

This is also wrong

2 hours ago, FireTamer81 said:
StrongBlockTileEntity strongBlockTile = new StrongBlockTileEntity();
Edited by Draco18s

Apparently I'm a complete and utter jerk and come to this forum just like to make fun of people, be confrontational, and make your personal life miserable.  If you think this is the case, JUST REPORT ME.  Otherwise you're just going to get reported when you reply to my posts and point it out, because odds are, I was trying to be nice.

 

Exception: If you do not understand Java, I WILL NOT HELP YOU and your thread will get locked.

 

DO NOT PM ME WITH PROBLEMS. No help will be given.

Link to comment
Share on other sites

Okay, non of the methods or fields are static now.

I put the default value in the constructor:

public StrongBlockTileEntity(TileEntityType<?> tileEntityType) {
        super(tileEntityType);
        this.strongBlockHealth = 3000;
    }

 

But now it doesn't change when I use the Dev Tool on it.

 

I also changed this part maybe an hour ago because I realized it was stupid. (I think the reason it was stupid was because I was supplying the class extending TileEntity instead of the TileEntityType RegistryObject, but not sure)

StrongBlockTileEntity strongBlockTile = new StrongBlockTileEntity();

 

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
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.

 Share



  • Recently Browsing

    No registered users viewing this page.

  • Posts

    • Minecraft Version-1.16.5 Forge Version-36.2.0   Crash Log-  https://gist.github.com/keepinitreal247/05669330de1d11fba00d1cbe565f35b1 Debug Log-https://gist.github.com/keepinitreal247/af4304598bd4192be840e8c111af2c81
    • When I try to open forge with java it says unable to install java but I can open java itself just fine. I've tried deleting and redownloading both forge and java but nothing changed. I also tried opening it from my files and from my desktop but it still gave me the same message. I don't think I'm doing anything wrong, I just click on forge and pressed open with java and java opens but only to give me the message saying " unable to install java there are errors in the following switches".   (this is just a picture of what i got from google not my exact message)
    • This is the capability inject and provider for one of my capabilities. All of them are coded the exact same way. public class CapabilityDamageTypes { @CapabilityInject(IDamageTypes.class) public static Capability<IDamageTypes> DAMAGE_TYPES_CAPABILITY = null; public static void register() { CapabilityManager.INSTANCE.register(IDamageTypes.class, new Storage(), new Factory()); } private static class Storage implements Capability.IStorage<IDamageTypes> { @Nullable @Override public INBT writeNBT(Capability<IDamageTypes> capability, IDamageTypes instance, Direction side) { CompoundNBT tag = new CompoundNBT(); tag.putFloat("slash", instance.getSlashDamage()); tag.putFloat("impact", instance.getImpactDamage()); tag.putFloat("puncture", instance.getPunctureDamage()); tag.putFloat("fire", instance.getFireDamage()); tag.putFloat("lightning", instance.getLightningDamage()); tag.putFloat("ice", instance.getIceDamage()); tag.putFloat("dark", instance.getDarkDamage()); tag.putFloat("holy", instance.getHolyDamage()); tag.putFloat("magic", instance.getMagicDamage()); tag.putString("mobDamageType", instance.getMobDamageType()); return tag; } @Override public void readNBT(Capability<IDamageTypes> capability, IDamageTypes instance, Direction side, INBT nbt) { float slash = ((CompoundNBT) nbt).getFloat("slash"); float impact = ((CompoundNBT) nbt).getFloat("impact"); float puncture = ((CompoundNBT) nbt).getFloat("puncture"); float fire = ((CompoundNBT) nbt).getFloat("fire"); float lightning = ((CompoundNBT) nbt).getFloat("lightning"); float ice = ((CompoundNBT) nbt).getFloat("ice"); float dark = ((CompoundNBT) nbt).getFloat("dark"); float holy = ((CompoundNBT) nbt).getFloat("holy"); float magic = ((CompoundNBT) nbt).getFloat("magic"); String mobDamageType = ((CompoundNBT) nbt).getString("mobDamageType"); instance.setSlashDamage(slash); instance.setImpactDamage(impact); instance.setPunctureDamage(puncture); instance.setFireDamage(fire); instance.setLightningDamage(lightning); instance.setIceDamage(ice); instance.setDarkDamage(dark); instance.setHolyDamage(holy); instance.setMagicDamage(magic); instance.setMobDamageType(mobDamageType); } } private static class Factory implements Callable<IDamageTypes> { @Override public IDamageTypes call() throws Exception { return new DamageTypes(0, 0, 0, 0, 0, 0, 0, 0, 0, "IMPACT"); } } } package com.cwjn.hardstuckintegration.Capabilities.DamageTypes; import net.minecraft.nbt.CompoundNBT; import net.minecraft.util.Direction; import net.minecraftforge.common.capabilities.Capability; import net.minecraftforge.common.capabilities.ICapabilitySerializable; import net.minecraftforge.common.util.LazyOptional; import javax.annotation.Nonnull; import javax.annotation.Nullable; public class DamageTypesProvider implements ICapabilitySerializable<CompoundNBT> { //private final DamageTypes damage = new DamageTypes(); //private final LazyOptional<IDamageTypes> damageOptional = LazyOptional.of(() -> damage); private DamageTypes damage; private LazyOptional<IDamageTypes> damageOptional; public void invalidate() { damageOptional.invalidate(); } public DamageTypesProvider(float I, float P, float S, float F, float L, float IC, float D, float H, float M, String mobDamageType) { damage = new DamageTypes(I, P, S, F, L, IC, D, H, M, mobDamageType); damageOptional = LazyOptional.of(() -> damage); } @Nonnull @Override public <T> LazyOptional<T> getCapability(@Nonnull Capability<T> cap, @Nullable Direction side) { return cap == CapabilityDamageTypes.DAMAGE_TYPES_CAPABILITY ? damageOptional.cast() : LazyOptional.empty(); } @Override public CompoundNBT serializeNBT() { if (CapabilityDamageTypes.DAMAGE_TYPES_CAPABILITY == null) { return new CompoundNBT(); } else { return (CompoundNBT) CapabilityDamageTypes.DAMAGE_TYPES_CAPABILITY.writeNBT(damage, null); } } @Override public void deserializeNBT(CompoundNBT nbt) { if (CapabilityDamageTypes.DAMAGE_TYPES_CAPABILITY != null) { CapabilityDamageTypes.DAMAGE_TYPES_CAPABILITY.readNBT(damage, null, nbt); } } }  
    • I got it now I installed the java 16 jdk instead of 8. Now it works thanks!  
    • net.minecraft.ResourceLocationException: Non [a-z0-9/._-] character in path of location: minecraft:textures/models/armor/gold_layer_١.png     at net.minecraft.resources.ResourceLocation.<init>(ResourceLocation.java:37) ~[client-1.17.1-20210706.113038-srg.jar%2353!:?] {re:classloading}     at net.minecraft.resources.ResourceLocation.<init>(ResourceLocation.java:42) ~[client-1.17.1-20210706.113038-srg.jar%2353!:?] {re:classloading}     at net.minecraft.client.renderer.entity.layers.HumanoidArmorLayer.getArmorResource(HumanoidArmorLayer.java:146) ~[client-1.17.1-20210706.113038-srg.jar%2353!:?] {re:classloading,pl:runtimedistcleaner:A}     at net.minecraft.client.renderer.entity.layers.HumanoidArmorLayer.m_117118_(HumanoidArmorLayer.java:60) ~[client-1.17.1-20210706.113038-srg.jar%2353!:?] {re:classloading,pl:runtimedistcleaner:A}     at net.minecraft.client.renderer.entity.layers.HumanoidArmorLayer.m_6494_(HumanoidArmorLayer.java:36) ~[client-1.17.1-20210706.113038-srg.jar%2353!:?] {re:classloading,pl:runtimedistcleaner:A}     at net.minecraft.client.renderer.entity.layers.HumanoidArmorLayer.m_6494_(HumanoidArmorLayer.java:23) ~[client-1.17.1-20210706.113038-srg.jar%2353!:?] {re:classloading,pl:runtimedistcleaner:A}     at net.minecraft.client.renderer.entity.LivingEntityRenderer.m_7392_(LivingEntityRenderer.java:126) ~[client-1.17.1-20210706.113038-srg.jar%2353!:?] {re:classloading,pl:accesstransformer:B,pl:runtimedistcleaner:A}     at net.minecraft.client.renderer.entity.MobRenderer.m_7392_(SourceFile:45) ~[client-1.17.1-20210706.113038-srg.jar%2353!:?] {re:classloading}     at net.minecraft.client.renderer.entity.MobRenderer.m_7392_(SourceFile:18) ~[client-1.17.1-20210706.113038-srg.jar%2353!:?] {re:classloading}     at net.minecraft.client.renderer.entity.EntityRenderDispatcher.m_114384_(EntityRenderDispatcher.java:129) ~[client-1.17.1-20210706.113038-srg.jar%2353!:?] {re:classloading,pl:accesstransformer:B,pl:runtimedistcleaner:A}     at net.minecraft.client.renderer.LevelRenderer.m_109517_(LevelRenderer.java:1280) ~[client-1.17.1-20210706.113038-srg.jar%2353!:?] {re:classloading,pl:runtimedistcleaner:A}     at net.minecraft.client.renderer.LevelRenderer.m_109599_(LevelRenderer.java:1076) ~[client-1.17.1-20210706.113038-srg.jar%2353!:?] {re:classloading,pl:runtimedistcleaner:A}     at net.minecraft.client.renderer.GameRenderer.m_109089_(GameRenderer.java:1050) ~[client-1.17.1-20210706.113038-srg.jar%2353!:?] {re:classloading,pl:accesstransformer:B,pl:runtimedistcleaner:A}     at net.minecraft.client.renderer.GameRenderer.m_109093_(GameRenderer.java:830) ~[client-1.17.1-20210706.113038-srg.jar%2353!:?] {re:classloading,pl:accesstransformer:B,pl:runtimedistcleaner:A}     at net.minecraft.client.Minecraft.m_91383_(Minecraft.java:1039) ~[client-1.17.1-20210706.113038-srg.jar%2353!:?] {re:classloading,pl:accesstransformer:B,pl:runtimedistcleaner:A}     at net.minecraft.client.Minecraft.m_91374_(Minecraft.java:659) ~[client-1.17.1-20210706.113038-srg.jar%2353!:?] {re:classloading,pl:accesstransformer:B,pl:runtimedistcleaner:A}     at net.minecraft.client.main.Main.main(Main.java:186) ~[client-1.17.1-20210706.113038-srg.jar%2353!:?] {re:classloading,pl:runtimedistcleaner:A}     at jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[?:?] {}     at jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:78) ~[?:?] {}     at jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[?:?] {}     at java.lang.reflect.Method.invoke(Method.java:567) ~[?:?] {}     at net.minecraftforge.fml.loading.targets.CommonClientLaunchHandler.lambda$launchService$0(CommonClientLaunchHandler.java:45) ~[fmlloader-1.17.1-37.0.126.jar%2322!:?] {}     at cpw.mods.modlauncher.LaunchServiceHandlerDecorator.launch(LaunchServiceHandlerDecorator.java:37) [modlauncher-9.0.7.jar%235!:?] {}     at cpw.mods.modlauncher.LaunchServiceHandler.launch(LaunchServiceHandler.java:53) [modlauncher-9.0.7.jar%235!:?] {}     at cpw.mods.modlauncher.LaunchServiceHandler.launch(LaunchServiceHandler.java:71) [modlauncher-9.0.7.jar%235!:?] {}     at cpw.mods.modlauncher.Launcher.run(Launcher.java:106) [modlauncher-9.0.7.jar%235!:?] {}     at cpw.mods.modlauncher.Launcher.main(Launcher.java:77) [modlauncher-9.0.7.jar%235!:?] {}     at cpw.mods.modlauncher.BootstrapLaunchConsumer.accept(BootstrapLaunchConsumer.java:26) [modlauncher-9.0.7.jar%235!:?] {}     at cpw.mods.modlauncher.BootstrapLaunchConsumer.accept(BootstrapLaunchConsumer.java:23) [modlauncher-9.0.7.jar%235!:?] {}     at cpw.mods.bootstraplauncher.BootstrapLauncher.main(BootstrapLauncher.java:90) [bootstraplauncher-0.1.17.jar:?] {}   (This the crash report(
  • Topics

  • Who's Online (See full list)

×
×
  • Create New...

Important Information

By using this site, you agree to our Privacy Policy.