Jump to content

Strange chunk load/unload issue


Flemmli97

Recommended Posts

 

I have an entity which keeps the chunk its inside loaded and when it dies it should unload the chunk. To test if my code is working i set up a repeating command block with "say hi", spawned in the entity at the command block and then moved away from it (far enough of course). The log printed "hi" so the chunk loading works. But now if i want to unload the chunk (kill the entity) something strange happens. 

 

I put an log message inside onLivingUpdate so i can see if the entity is still ticking. Then the entity upon "killing" it is not immediately removed from the world, but like the enderdragon stays a while.

The first thing i do in onDeathUpdate is unload the chunk the entity is in. The logger from the entity stops sending stuff so it seems the entity is unloaded. But the command block is continuing sending the message. 

How can the command block stay loaded, while the entity does not? the entity and command block is at the same spot and the entity is not moving from there either. Since the entity is immediately unloaded in onDeathUpdate and it cant get processed further so if i tp back the entity will of course still be there. Also tp to that spot and away fixes that issue.

 

Now here the said code:

 

The constructor of the entity (its private since its an abstract helper class)

Spoiler

	private EntityServant(World world) {
		super(world);
		this.experienceValue = 50;
	    this.tasks.addTask(0, this.follow);
        this.tasks.addTask(1, this.restrictArea);
	    this.tasks.addTask(2, this.wander);
	    this.tasks.addTask(3, new EntityAISwimming(this));
	    this.tasks.addTask(4, new EntityAILookIdle(this));
	    this.tasks.addTask(5, new EntityAIWatchClosest(this, EntityPlayer.class, 8.0F));
	    this.tasks.addTask(6, new EntityAIOpenDoor(this, true));
	    this.targetTasks.addTask(0, this.targetServant);
	    this.targetTasks.addTask(1, this.targetPlayer);
	    this.targetTasks.addTask(2, this.targetHurt);
		this.prop = ServantAttributes.attributes.get(this.getClass());
		if (this.prop == null) {
            throw new NullPointerException("Properties of " + this.getClass() + " is null. This is not allowed");
        }
		this.updateAttributes();
		this.ticket = ForgeChunkManager.requestTicket(Fate.instance, world, Type.ENTITY);
		this.ticket.bindEntity(this);
		this.ticket.setChunkListDepth(1);
	}

 

 

onLivingUpdate

 

Spoiler

	@Override
	public void onLivingUpdate() {
		super.onLivingUpdate();		
		this.attackTimer=Math.max(0, --this.attackTimer);
		if(!this.world.isRemote)
		{
			this.regenMana();
			this.combatTick=Math.max(0, --this.combatTick);
			//Decide it on server, wether attack has finished or not
			if(!this.world.isRemote && this.attackTimer==0 && State.isAttack(this.entityState()))
				this.setState(State.IDDLE);
			if(this.ticksExisted%10==0)
				System.out.println("chunk " + this.getPosition());
			if(this.ticket!=null && this.shouldLoadChunk())
				ForgeChunkManager.forceChunk(this.ticket, new ChunkPos(this.getPosition()));
		}
	}
	
	public boolean shouldLoadChunk()
	{
		return this.deathTicks==0;//this.isEntityAlive();
	}

 

 

onDeathUpdate:

 

Spoiler

	@Override
	protected void onDeathUpdate() {
		++this.deathTicks;
		if(!this.world.isRemote)
		{
			if(deathTicks == 1)
			{	
				this.dead=true;
				MinecraftServer minecraftserver = FMLCommonHandler.instance().getMinecraftServerInstance();
				minecraftserver.getPlayerList().sendMessage(new TextComponentString(TextFormatting.RED + "A servant has been killed"));			
				this.playSound(SoundEvents.ENTITY_WITHER_SPAWN, 1.0F, 1.0F);
				if(this.getOwner() != null)
				{
					IPlayer servantprop = this.getOwner().getCapability(PlayerCapProvider.PlayerCap, null);
					servantprop.setServant(this.getOwner(), null);
					GrailWarPlayerTracker track = GrailWarPlayerTracker.get(this.world);
					track.removePlayer(this.getOwner());
				}
				if(this.ticket!=null)
					ForgeChunkManager.unforceChunk(this.ticket, new ChunkPos(this.getPosition()));
			}
			int exp;
	        int splitExp;
	        if (this.deathTicks > 15 && this.deathTicks % 5 == 0 && (this.recentlyHit > 0 || this.isPlayer()) && this.canDropLoot() && this.world.getGameRules().getBoolean("doMobLoot"))
	        {
                exp = this.experienceValue;
                while (exp > 0)
                {
                    splitExp = EntityXPOrb.getXPSplit(exp);
                    exp -= splitExp;
                    this.world.spawnEntity(new EntityXPOrb(this.world, this.posX, this.posY, this.posZ, splitExp));
                }
	        }
			if (this.deathTicks == 200)
	        {
	            this.setDead();
	        }
		}
	}

 

 

and here the complete entity code:

https://github.com/Flemmli97/FateUBW/blob/master/src/main/java/com/flemmli97/fatemod/common/entity/servant/EntityServant.java

Edited by Flemmli97
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.