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 trying to start up a 1.16.5 server with a kitchensink modpack that I made, but it won't work and the server keeps crashing. https://paste.ee/p/yBk8L  
    • BITCOIN RECOVERY; The Expertise of Captain WebGenesis in Retrieving scammed Cryptocurrency. I was involved in a bitcoin trading scam, I came across a company website that promised a big return on investment. I was completely sold. The website was excellent, and after much coaxing, I opted to invest 278,000 USD, which unfortunately ended up in the wrong hands. I was frustrated until I sought the counsel of a Crypto Expert  CAPTAIN WEBGENESIS. I had no notion there were techniques for reclaiming stolen funds. I emailed the highly rated expert. I explained my case to Expert and provided all the required information and to my amazement, CAPTAIN WEBGENESIS refunded 90% of my BITCOIN to my wallet within a few working days, which I didn't think was possible. I was startled as well as relieved. With folks like CAPTAIN WEBGENESIS on your side, Crypto recovery is possible
    • I can’t see what the problem is since there seem to be no errors. Are you sure that was everything? The last log seems to be about the graphics library. Maybe that is the issue.  [20:26:39] [main/INFO]: Incorrect key [earlyWindowSkipGLVersions] was corrected from null to [] …… [20:26:41] [main/INFO]: Trying GL version 4.6 looks sus, but it might not be the issue
    • setDeltaMovement only works on the client. this makes the animations smoother. The reason sendSystemMessage works is because you don’t have to constantly send messages from the server to the client, and a little bit of lag is ok. This is a core minecraft mechanism, but you can send custom packets over to the client to set its deltaMovement. The client has to have a way to receive the packets too.
    • The full log is this What do I do? [20:26:39] [main/INFO]: ModLauncher running: args [--username, xYumixks, --version, forge-47.2.32, --gameDir, C:\Users\danil\curseforge\minecraft\Instances\mine guerra 2, --assetsDir, C:\Users\danil\curseforge\minecraft\Install\assets, --assetIndex, 5, --uuid, 979909db94104f33a94abdc77f4cc574, --accessToken, ????????, --clientId, NzI5M2NmYmUtZWI3Ni00MWVlLWFhZTgtODUzMTFjZTVkYTM0, --xuid, 2535414120491579, --userType, msa, --versionType, release, --width, 1024, --height, 768, --quickPlayPath, C:\Users\danil\curseforge\minecraft\Install\quickPlay\java\1715815597218.json, --launchTarget, forgeclient, --fml.forgeVersion, 47.2.32, --fml.mcVersion, 1.20.1, --fml.forgeGroup, net.minecraftforge, --fml.mcpVersion, 20230612.114412] [20:26:39] [main/INFO]: ModLauncher 10.0.9+10.0.9+main.dcd20f30 starting: java version 17.0.8 by Microsoft; OS Windows 11 arch amd64 version 10.0 [20:26:39] [main/WARN]: Configuration file C:\Users\danil\curseforge\minecraft\Instances\mine guerra 2\config\fml.toml is not correct. Correcting [20:26:39] [main/INFO]: Incorrect key [earlyWindowSkipGLVersions] was corrected from null to [] [20:26:39] [main/INFO]: Incorrect key [earlyWindowSquir] was corrected from null to false [20:26:39] [main/INFO]: Incorrect key [earlyWindowShowCPU] was corrected from null to false [20:26:41] [main/INFO]: Loading ImmediateWindowProvider fmlearlywindow [20:26:41] [main/INFO]: Trying GL version 4.6
  • Topics

×
×
  • Create New...

Important Information

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