Jump to content

[1.7.10] Custom tile entity data not saving


eliteznightmare

Recommended Posts

So i am making a power system where everything extends a base tile entity class and i cant get the energy parameter to save properly after i exit the world and reload it.

 

Here is the base tile entity class

package libraCraft.blocks.tileEntity;

import java.io.ByteArrayOutputStream;
import java.io.DataOutputStream;
import java.util.Random;

import cpw.mods.fml.relauncher.Side;
import cpw.mods.fml.relauncher.SideOnly;
import libraCraft.blocks.LCBlockEnergy;
import net.minecraft.entity.player.EntityPlayerMP;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.network.NetworkManager;
import net.minecraft.network.Packet;
import net.minecraft.network.PacketBuffer;
import net.minecraft.network.play.server.S35PacketUpdateTileEntity;
import net.minecraft.tileentity.TileEntity;
import net.minecraft.world.World;
import net.minecraft.world.chunk.Chunk;

public class LCBlockEnergyTE extends TileEntity {
public int energy;
public int packetAmount;
public int maxEnergy;
public int fuel;
public boolean canSend = false;
public boolean canReceive = false;

@Override
public void writeToNBT(NBTTagCompound nbt) {
	System.out.println("WriteToNBT");
	nbt.setInteger("energy", energy);
	nbt.setInteger("packetAmount", packetAmount);
	nbt.setInteger("maxEnergy", maxEnergy);
	nbt.setInteger("fuel", fuel);
	nbt.setBoolean("canSend", canSend);
	nbt.setBoolean("canReceive", canReceive);
}

@Override
public void readFromNBT(NBTTagCompound nbt) {
	System.out.println("ReadFromNBT");
	energy = nbt.getInteger("energy");
	packetAmount = nbt.getInteger("packetAmount");
	maxEnergy = nbt.getInteger("maxEnergy");
	fuel = nbt.getInteger("fuel");
	canSend = nbt.getBoolean("canSend");
	canReceive = nbt.getBoolean("canReceive");
}

@Override
public void updateEntity() {
	BalanceEnergy();
}

public void BalanceEnergy() {
	int x = xCoord;
	int y = yCoord;
	int z = zCoord;
	Random random = new Random();
	World world = worldObj;
	int range = 5;
	for (int  = -range;  <= range; ++) {
		for (int yD = -range; yD <= range; yD++) {
			for (int zD = -range; zD <= range; zD++) {
				if ( != 0 || yD != 0 || zD != 0) {
					if (world.getBlock(x + , y + yD, z + zD) instanceof LCBlockEnergy) {
						if ((LCBlockEnergyTE) world.getTileEntity(x + , y
								+ yD, z + zD) instanceof LCBlockEnergyTE) {
							LCBlockEnergyTE tile2 = (LCBlockEnergyTE) world
									.getTileEntity(x + , y + yD, z + zD);
							LCBlockEnergyTE tile1 = (LCBlockEnergyTE) world
									.getTileEntity(x, y, z);
							if (tile1.canSend == true
									&& tile2.canReceive == true) {
								if (tile1.energy >= tile1.packetAmount
										&& tile2.energy <= tile2.maxEnergy
										&& tile1.energy > tile2.energy
										&& tile1.energy - tile2.energy > tile1.packetAmount) {
									tile1.energy = tile1.energy
											- tile1.packetAmount;
									tile2.energy = tile2.energy
											+ tile1.packetAmount;
									world.markBlockForUpdate(x, y, z);
									SpawnParticles(world, tile2.xCoord,
											tile2.yCoord, tile2.zCoord, ,
											yD, zD);

								}
							}
						}
					}
				}
			}
		}
	}
}

private void SpawnParticles(World world, int xCoord, int yCoord,
		int zCoord, int , int yD, int zD) {
	Random random = new Random();
	for (int lk = 0; lk < 40; lk++) {
		world.spawnParticle("portal", (double) xCoord + 0.5D,
				(double) yCoord + 1.0D, (double) zCoord + 0.5D,
				(double) ((float) (-) + random.nextFloat()) - 0.5D,
				(double) ((float) (-yD) - random.nextFloat() - 0.5F),
				(double) ((float) (-zD) + random.nextFloat()) - 0.5D);
	}
}

@Override
public Packet getDescriptionPacket() {
	NBTTagCompound tag = new NBTTagCompound();
	System.out.println("GetDescriptionPacket");
	writeToNBT(tag);
	return new S35PacketUpdateTileEntity(xCoord, yCoord, zCoord, -999, tag);
}

@Override
public void onDataPacket(NetworkManager net, S35PacketUpdateTileEntity pkt) {
	System.out.println("OnDataPacket");
	super.onDataPacket(net, pkt);
	readFromNBT(pkt.func_148857_g());
}
}

 

one of the blocks extending this tile entity

package libraCraft.blocks.tileEntity;

import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.tileentity.TileEntity;
import net.minecraft.world.World;

public class TEEnergyCube extends LCBlockEnergyTE {
public TEEnergyCube() {
		this.maxEnergy = 3000;
		this.packetAmount = 1;
		this.canReceive = true;
		this.canSend = true;
	}
}

 

main Class

package libraCraft;

import libraCraft.blocks.BlockInit;
import libraCraft.blocks.tileEntity.LCBlockEnergyTE;
import libraCraft.blocks.tileEntity.TEEnergyCube;
import libraCraft.blocks.tileEntity.TEEnergyGen;
import libraCraft.blocks.tileEntity.TEEnergyGrowth;
import libraCraft.handler.ConfigHandler;
import libraCraft.items.ItemInit;
import libraCraft.proxy.IProxy;
import cpw.mods.fml.common.FMLCommonHandler;
import cpw.mods.fml.common.Mod;
import cpw.mods.fml.common.Mod.Instance;
import cpw.mods.fml.common.SidedProxy;
import cpw.mods.fml.common.event.FMLInitializationEvent;
import cpw.mods.fml.common.event.FMLPostInitializationEvent;
import cpw.mods.fml.common.event.FMLPreInitializationEvent;
import cpw.mods.fml.common.registry.GameRegistry;

@Mod(modid = Ref.MOD_ID, name = Ref.MOD_NAME, version= Ref.MOD_VERSION, guiFactory = Ref.GUI_FACTORY_CLASS)
public class LibraCraft {
@Instance("CulinaryCraft")
public static LibraCraft instance;

@SidedProxy(clientSide = Ref.CLIENT_PROXY, serverSide = Ref.SERVER_PROXY)
public static IProxy proxy;

@Mod.EventHandler
public void preInit(FMLPreInitializationEvent event){
	ConfigHandler.init(event.getSuggestedConfigurationFile());
	FMLCommonHandler.instance().bus().register(new ConfigHandler());
	ItemInit.init();
	BlockInit.init();

}
@Mod.EventHandler
public void Init(FMLInitializationEvent event){
        proxy.registerRenderThings();
        GameRegistry.registerTileEntity(LCBlockEnergyTE.class, "EnergyBase");
        GameRegistry.registerTileEntity(TEEnergyCube.class, "EnergyCube");
	GameRegistry.registerTileEntity(TEEnergyGen.class, "EnergyGen");
	GameRegistry.registerTileEntity(TEEnergyGrowth.class, "EnergyGrowth");

}
@Mod.EventHandler
public void postInit(FMLPostInitializationEvent event){

}
}

 

I add some energy to the to the tile entity with a debug item and I have it print the energy amount, lets say it has 50 energy after clicking it. after exiting and reloading the world it is back at 0. anyone have any ideas? the rest of the source is available here: https://github.com/eliteznightmare/LibraCraft

 

 

Link to comment
Share on other sites

in readFromNBT and writeToNBT you must call the super method. It saves the most important information i.e. the id and x, y, z coords

Link to comment
Share on other sites

Hi

 

I am a newbie and I just started to play around with the whole TE system. I'm either not sure what you're line there exactely does:

world.markBlockForUpdate(x, y, z);

 

If I want to get my TE's nbt stored to disk, I use

markDirty()

From the (vanilla) TE class.

 

May this helps and you're able to set your issue to null;)

 

Sincerely -pick

Since English is not my mother tongue, my sentences may are confusing.

 

I'm coding java for a long time now - just MC and forge stop me sometimes.

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

    • I'm using Modrinth as a launcher for a forge modpack on 1.20.1, and can't diagnose the issue on the crash log myself. Have tried repairing the Minecraft instillation as well as removing a few mods that have been problematic for me in the past to no avail. Crash log is below, if any further information is necessary let me know. Thank you! https://paste.ee/p/k6xnS
    • Hey folks. I am working on a custom "Mecha" entity (extended from LivingEntity) that the player builds up from blocks that should get modular stats depending on the used blocks. e.g. depending on what will be used for the legs, the entity will have a different jump strength. However, something unexpected is happening when trying to override a few of LivingEntity's functions and using my new own "Mecha" specific fields: instead of their actual instance-specific value, the default value is used (0f for a float, null for an object...) This is especially strange as when executing with the same entity from a point in the code specific to the mecha entity, the correct value is used. Here are some code snippets to better illustrate what I mean: /* The main Mecha class, cut down for brevity */ public class Mecha extends LivingEntity { protected float jumpMultiplier; //somewhere later during the code when spawning the entity, jumpMultiplier is set to something like 1.5f //changing the access to public didn't help @Override //Overridden from LivingEntity, this function is only used in the jumpFromGround() function, used in the aiStep() function, used in the LivingEntity tick() function protected float getJumpPower() { //something is wrong with this function //for some reason I can't correctly access the fields and methods from the instanciated entity when I am in one of those overridden protected functions. this is very annoying LogUtils.getLogger().info(String.valueOf(this.jumpMultiplier))) //will print 0f return this.jumpMultiplier * super.getJumpPower(); } //The code above does not operate properly. Written as is, the entity will not jump, and adding debug logs shows that when executing the code, the value of this.jumpMultiplier is 0f //in contrast, it will be the correct value when done here: @Override public void tick() { super.tick(); //inherited LivingEntity logic //Custom logic LogUtils.getLogger().info(String.valueOf(this.jumpMultiplier))) //will print 1.5f } } My actual code is slightly different, as the jumpMuliplier is stored in another object (so I am calling "this.legModule.getJumpPower()" instead of the float), but even using a simple float exactly like in the code above didn't help. When running my usual code, the object I try to use is found to be null instead, leading to a crash from a nullPointerException. Here is the stacktrace of said crash: The full code can be viewed here. I have found a workaround in the case of jump strength, but have already found the same problem for another parameter I want to do, and I do not understand why the code is behaving as such, and I would very much like to be able to override those methods as intended - they seemed to work just fine like that for vanilla mobs... Any clues as to what may be happening here?
    • Please delete post. Had not noticed the newest edition for 1.20.6 which resolves the issue.
    • https://paste.ee/p/GTgAV Here's my debug log, I'm on 1.18.2 with forge 40.2.4 and I just want to get it to work!! I cant find any mod names in the error part and I would like some help from the pros!! I have 203 mods at the moment.
  • Topics

×
×
  • Create New...

Important Information

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