Jump to content

[SOLVED][1.10.2]How to keep TileEntityData with World#setBlockState ?


Mark136

Recommended Posts

hello all,

 

I have a TE Block that have stored the player who has placed the block,

and after something happened, i want to change that TE block to an other TE block and migrate the TE Data(the player) to that other block

 

is this possible?

and if it is possible, how can i do that?

 

 

my Mod: Extended RPG [W.I.P]

Link to comment
Share on other sites

I think he wants to let the TE be replaced, but he wants to transfer the data from old to new.

 

It has been a while since I walked the TE replacement code. I recommend using the debugger to do so. Make notes of when and where the switch occurs. There may be some overlap when both TEs exist (like inside the constructor of the replacement, or inside of your block's create TE). At that moment, you may be able to grab the data from the old TE and assign it to the new.

 

If that's not so easy, then you may see how to grab and hold a reference to the old TE and use it if needed. Just make sure to let it go so the TE will be garbage-collected when dead.

The debugger is a powerful and necessary tool in any IDE, so learn how to use it. You'll be able to tell us more and get better help here if you investigate your runtime problems in the debugger before posting.

Link to comment
Share on other sites

i found something that works:

public void onEntityCollidedWithBlock(World worldIn, BlockPos pos, IBlockState state, Entity entityIn)
    {
	 TileEntity tileentity = worldIn.getTileEntity(pos);
	 if (tileentity instanceof TETrap) {
	 TETrap TE = (TETrap) tileentity; 
   
    if(entityIn instanceof EntityRabbit){
    	ChangeBlock(pos, worldIn, entityIn, MarkBlocks.MarkTrap01, true, TE.getPlayer(), TE.getTrapLevel());


        }
	 }}

private void ChangeBlock(BlockPos pos, World world, Entity entity, Block newBlock, Boolean isCaught, EntityPlayer player, int Level){
	 world.setBlockState(pos, newBlock.getDefaultState());
	 if(isCaught){
     entity.attackEntityFrom(MarkDamageSrc.CapturedByTrap, 50000.0F);
	 }

	 if(world.getBlockState(pos) == newBlock.getDefaultState()){
		 if(world.getTileEntity(pos) != null){
			 if(player != null && Level != 0){
				 TileEntity tileentity = world.getTileEntity(pos);
				 if (tileentity instanceof TETrap) {
				 TETrap TE = (TETrap) tileentity;

				 TE.setPlayer(player);
				 TE.setTrapLevel(Level);
				 }
			 }
			 else{
				 System.err.println((player == null) + " / " + (Level == 0));
			 }
		 }
		 else{
			 System.err.println("TE = null!");
		 }
	 }
	 else{
		 System.err.println("Block = " + world.getBlockState(pos).getBlock().toString());

	 }
 }

sorry about the mess.

 

now an other problem with my tile entity what i can't figure out.

 

while reading the player from NBT, i need a worldObj to call world#getPlayerEntityByUUID. but the worldObj in Tile entity seems to be null when readFromNBT is called while loading the game.

 

here is code of my WritetoNBT and readfromNBT:

@Override
public NBTTagCompound writeToNBT(NBTTagCompound Compound)
{

	super.writeToNBT(Compound);
	String uuid = "null";




	Compound.setInteger("Hunter13695", this.HuntLvl);
	if(this.player != null){
	uuid = this.player.getUniqueID().toString();
	}
	Compound.setString("Player13695", uuid);
	String g = this.player == null ? "No Player" : this.player.getName();
	System.out.println("Tile Entity Saved, with hunter lvl: " + this.HuntLvl + ". Player: " + g);
	return Compound;
}
@Override
public void readFromNBT(NBTTagCompound Compound)
{


	if(this.worldObj != null){
	super.readFromNBT(Compound);


	int ReadLvl = 1;

	String a = "null";
	EntityPlayer ReadPlayer = null;



	if(Compound.hasKey("Hunter13695", 3)){
		ReadLvl = Compound.getInteger("Hunter13695");
	}
	if(Compound.hasKey("Player13695", ){
		a = Compound.getString("Player13695");
		if(!a.equals("null")){

		ReadPlayer = this.worldObj.getPlayerEntityByUUID(UUID.fromString(a));
			}

		}

	this.HuntLvl = ReadLvl;
	System.out.println("Has key lvl/Player : " + Compound.hasKey("Hunter13695", 3) + " : " + Compound.hasKey("Player13695",  + " : " + a);
	this.player = ReadPlayer;
	String g = this.player == null ? "No Player" : this.player.getName();
	System.out.println("Tile Entity Saved, with hunter lvl: " + this.HuntLvl + ". Player: " + g);
}
	else{
		System.err.println("World is null?");
	}
	}

 

and here is the code how i Registered my TE :

public class MarkTE {

public static void init(){	
	GameRegistry.registerTileEntity(TETrap.class, "mark13695");	
	// I know I gonna make more TEs later
}

}
@EventHandler
public void PreInit(FMLPreInitializationEvent Event){
..
..
	MarkTE.init();
..
..
}

my Mod: Extended RPG [W.I.P]

Link to comment
Share on other sites

Fixed it, i now storing the UUID to the block and i have a event wich checks if the UUIDs are the same

 

 

like: if(event.getPlayer().getUniqueID().equals(TE.getPlayerUUID)){ [do something] }

 

now i gonna do some update with saplings that gives xp when fully grown(pretty easy for me now)

and then i create an own set of crafting tables with custum gui.(this will be a bit harder part i think)

my Mod: Extended RPG [W.I.P]

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.