Jump to content

Recommended Posts

Posted

My Entity keeps disappearing when I close and re-open the world. I spawn it in, save and exit the world, then when I re-open it, nothing is there. What's happening?

 

 

Code:

 

package com.apmods.creeps.entity;

import com.google.common.base.Predicate;

import net.minecraft.entity.Entity;
import net.minecraft.entity.ai.EntityAIAttackOnCollide;
import net.minecraft.entity.ai.EntityAIAvoidEntity;
import net.minecraft.entity.ai.EntityAIHurtByTarget;
import net.minecraft.entity.ai.EntityAILookIdle;
import net.minecraft.entity.ai.EntityAINearestAttackableTarget;
import net.minecraft.entity.ai.EntityAISwimming;
import net.minecraft.entity.ai.EntityAIWander;
import net.minecraft.entity.ai.EntityAIWatchClosest;
import net.minecraft.entity.monster.EntityMob;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.init.Items;
import net.minecraft.inventory.IInventory;
import net.minecraft.item.ItemStack;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.nbt.NBTTagList;
import net.minecraft.world.World;

public class EntityThief extends EntityMob implements IInventory{

private ItemStack[] invItems = new ItemStack[2];

private EntityAIAvoidEntity aiAvoid = new EntityAIAvoidEntity(this, new Predicate(){
	private static final String __OBFID = "CL_00002243";
        public boolean func_179874_a(Entity p_179874_1_)
        {
            return p_179874_1_ instanceof EntityPlayer;
        }
        public boolean apply(Object p_apply_1_)
        {
            return this.func_179874_a((Entity)p_apply_1_);
        }
}, 8f, 0.3, 0.;

private EntityAINearestAttackableTarget aiNearest = new EntityAINearestAttackableTarget(this, EntityPlayer.class, true);
private EntityAIAttackOnCollide aiAttack = new EntityAIAttackOnCollide(this, EntityPlayer.class, 0.5D, false);

public EntityThief(World worldIn) {
	super(worldIn);
	this.tasks.addTask(1, new EntityAISwimming(this));
	this.tasks.addTask(3, aiAttack);
	this.tasks.addTask(4, new EntityAIWander(this, 1.0D));
	this.tasks.addTask(5, new EntityAIWatchClosest(this, EntityPlayer.class, 8.0F));
	this.tasks.addTask(6, new EntityAILookIdle(this));

	this.targetTasks.addTask(1, new EntityAIHurtByTarget(this, false));
	this.targetTasks.addTask(2, aiNearest);
}

public boolean attackEntityAsMob(Entity p_70652_1_)
    {
	if(p_70652_1_ instanceof EntityPlayer){
		EntityPlayer player = (EntityPlayer) p_70652_1_;
		int i = rand.nextInt(player.inventory.getSizeInventory());
		if(player.inventory.getStackInSlot(i) != null){
			if(this.invItems[0] == null){
				this.invItems[0] = player.inventory.getStackInSlot(i).copy();
				this.getStackInSlot(0).setTagCompound(player.inventory.getStackInSlot(i).getTagCompound());
			}
			else{
				this.invItems[1] = player.inventory.getStackInSlot(i).copy();
				this.getStackInSlot(1).setTagCompound(player.inventory.getStackInSlot(i).getTagCompound());
			}
			player.inventory.setInventorySlotContents(i, (ItemStack)null);
		}
	}
        return super.attackEntityAsMob(p_70652_1_);
    }

protected void updateAITasks() {
	if(this.invItems[0] != null || this.invItems[1] != null){
		this.tasks.addTask(3, aiAvoid);
		this.targetTasks.removeTask(aiNearest);
	}
	else{
		this.tasks.addTask(3, aiAttack);
		this.targetTasks.addTask(2, aiNearest);
	}
}



@Override
protected void dropEquipment(boolean p_82160_1_, int p_82160_2_) {
	if(invItems[0] != null){
		this.dropItem(invItems[0].getItem(), invItems[0].stackSize);
	}
	if(invItems[1] != null){
		this.dropItem(invItems[1].getItem(), invItems[1].stackSize);
	}
}

@Override
public void writeToNBT(NBTTagCompound nbt) {
	NBTTagList nbttaglist = new NBTTagList();

	for (int i = 0; i < this.invItems.length; ++i)
	{
		if (this.invItems[i] != null)
		{
			NBTTagCompound slotTag = new NBTTagCompound();
			slotTag.setByte("Slot", (byte)i);
			this.invItems[i].writeToNBT(slotTag);
			nbttaglist.appendTag(slotTag);
		}
	}
	nbt.setTag("Items", nbttaglist);
}

@Override
public void readFromNBT(NBTTagCompound nbt) {

	NBTTagList nbttaglist = nbt.getTagList("Items", 10);
	this.invItems = new ItemStack[this.getSizeInventory()];

	for (int i = 0; i < nbttaglist.tagCount(); ++i)
	{
		NBTTagCompound slotTag = nbttaglist.getCompoundTagAt(i);
		int j = slotTag.getByte("Slot") & 255;

		if (j >= 0 && j < this.invItems.length)
		{
			this.invItems[j] = ItemStack.loadItemStackFromNBT(slotTag);
		}
	}
}

@Override
public int getSizeInventory() {
	// TODO Auto-generated method stub
	return invItems.length;
}

@Override
public ItemStack getStackInSlot(int index) {
	// TODO Auto-generated method stub
	return invItems[index];
}

public ItemStack decrStackSize(int slot, int amount)
{
	if (this.invItems[slot] != null)
	{
		ItemStack itemstack;

		if (this.invItems[slot].stackSize <= amount)
		{
			itemstack = this.invItems[slot];
			this.invItems[slot] = null;
			return itemstack;
		}
		else
		{
			itemstack = this.invItems[slot].splitStack(amount);

			if (this.invItems[slot].stackSize == 0)
			{
				this.invItems[slot] = null;
			}

			return itemstack;
		}
	}
	else
	{
		return null;
	}
}

/**
 * When some containers are closed they call this on each slot, then drop whatever it returns as an EntityItem -
 * like when you close a workbench GUI.
 */
public ItemStack getStackInSlotOnClosing(int slot)
{
	if (this.invItems[slot] != null)
	{
		ItemStack itemstack = this.invItems[slot];
		this.invItems[slot] = null;
		return itemstack;
	}
	else
	{
		return null;
	}
}

/**
 * Sets the given item stack to the specified slot in the inventory (can be crafting or armor sections).
 */
public void setInventorySlotContents(int slot, ItemStack stack)
{
	this.invItems[slot] = stack;

	if (stack != null && stack.stackSize > this.getInventoryStackLimit())
	{
		stack.stackSize = this.getInventoryStackLimit();
	}
}

/**
 * For tile entities, ensures the chunk containing the tile entity is saved to disk later - the game won't think it
 * hasn't changed and skip it.
 */
public void markDirty() {}

/**
 * Do not make give this method the name canInteractWith because it clashes with Container
 */
public boolean isUseableByPlayer(EntityPlayer player)
{
	return false;
}

public void openInventory(EntityPlayer p) {}

public void closeInventory(EntityPlayer p) {}

/**
 * Returns true if automation is allowed to insert the given stack (ignoring stack size) into the given slot.
 */
public boolean isItemValidForSlot(int slot, ItemStack stack)
{	
	return true;
}

/**
 * Returns the name of the inventory
 */
public String getName()
{
	return "Theif";
}

/**
 * Returns the maximum stack size for a inventory slot.
 */
public int getInventoryStackLimit()
{
	return 64;
}

@Override
public int getField(int id) {
	// TODO Auto-generated method stub
	return 0;
}

@Override
public void setField(int id, int value) {
	// TODO Auto-generated method stub

}

@Override
public int getFieldCount() {
	// TODO Auto-generated method stub
	return 0;
}

@Override
public void clear() {
	// TODO Auto-generated method stub

}


}

 

Creator of the MyFit, MagiCraft, Tesseract gun, and Papa's Wingeria mod.

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.