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



×
×
  • Create New...

Important Information

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