Jump to content

Keeping track of entity owners


Briggybros

Recommended Posts

Hello, I'm trying to make a mod which you own an entity, but you can only own one entity. I have created a list object which uses the Players EntityID as the index and then the instance of EntityHorse as the listed object. I have this code to decide whether to teleport the horse or spawn one;

    public ItemStack onItemRightClick(ItemStack par1ItemStack, World par2World, EntityPlayer par3EntityPlayer)
    {
        if (!par2World.isRemote)
        {
        	int playerID = par3EntityPlayer.entityId;
           	if(BattleFoxCox.horses.get(playerID) != null) // line 39
           	{
                EntityHorse entityHorse = new EntityHorse(par2World);
            	entityHorse.setLocationAndAngles(par3EntityPlayer.posX, par3EntityPlayer.posY, par3EntityPlayer.posZ, par2World.rand.nextFloat() * 360.0F, 0.0F);
               	par2World.spawnEntityInWorld(entityHorse);
               	entityHorse.setOwner(par3EntityPlayer.username);
                BattleFoxCox.horses.set(par3EntityPlayer.entityId, entityHorse);
            }
            else
            {
            	EntityHorse entityHorse = BattleFoxCox.horses.get(playerID);
            	entityHorse.setLocationAndAngles(par3EntityPlayer.posX, par3EntityPlayer.posY, par3EntityPlayer.posZ, par2World.rand.nextFloat() * 360.0F, 0.0F);
            }
            
        }

        return par1ItemStack;
    }

 

I know it's probably horribly wrong but I've never done anything with entities before. When I try to use the item this error is thrown.

2012-09-26 18:27:36 [sEVERE] [ForgeModLoader] A critical server error occured handling a packet, kicking net.minecraft.src.NetServerHandler@11038df
java.lang.NullPointerException
at uk.co.toomuchminecraft.battlefoxcox.ItemHorseShoe.onItemRightClick(ItemHorseShoe.java:39)
at net.minecraft.src.ItemStack.useItemRightClick(ItemStack.java:139)
at net.minecraft.src.ItemInWorldManager.tryUseItem(ItemInWorldManager.java:306)
at net.minecraft.src.NetServerHandler.handlePlace(NetServerHandler.java:478)
at net.minecraft.src.Packet15Place.processPacket(Packet15Place.java:78)
at net.minecraft.src.MemoryConnection.processReadPackets(MemoryConnection.java:75)
at net.minecraft.src.NetServerHandler.networkTick(NetServerHandler.java:72)
at net.minecraft.src.NetworkListenThread.networkTick(NetworkListenThread.java:55)
at net.minecraft.src.IntegratedServerListenThread.networkTick(IntegratedServerListenThread.java:111)
at net.minecraft.server.MinecraftServer.updateTimeLightAndEntities(MinecraftServer.java:630)
at net.minecraft.server.MinecraftServer.tick(MinecraftServer.java:547)
at net.minecraft.src.IntegratedServer.tick(IntegratedServer.java:105)
at net.minecraft.server.MinecraftServer.run(MinecraftServer.java:453)
at net.minecraft.src.ThreadServerApplication.run(ThreadServerApplication.java:17)

Link to comment
Share on other sites

My entity does save the owner's name. In that case how would I get the horse which has that player as its owner? The data which the horse currently saves is the owners username and the owners EntityPlayer instance. How could I get the instance of the Horse which has the owner as the user of the tool without editing base classes?

Link to comment
Share on other sites

I have done that, and that's what produced the error

 

EDIT: Found a way to do it, I'll reply after testing :)

 

EDIT: Now have the method;

    public ItemStack onItemRightClick(ItemStack par1ItemStack, World par2World, EntityPlayer par3EntityPlayer)
    {
        if (!par2World.isRemote)
        {
        	for(int x = 0; x < BattleFoxCox.horses.size(); x++)
        	{
        		EntityHorse horse = BattleFoxCox.horses.get(x);
        		if(horse.getOwnerName().equalsIgnoreCase(par3EntityPlayer.username))
        		{
        			horse.setLocationAndAngles(par3EntityPlayer.posX, par3EntityPlayer.posY, par3EntityPlayer.posZ, par2World.rand.nextFloat() * 360.0F, 0.0F);
        			break;
        		}
        		if(x == BattleFoxCox.horses.size())
        		{
        			EntityHorse entityHorse = new EntityHorse(par2World);
                	entityHorse.setLocationAndAngles(par3EntityPlayer.posX, par3EntityPlayer.posY, par3EntityPlayer.posZ, par2World.rand.nextFloat() * 360.0F, 0.0F);
                   	par2World.spawnEntityInWorld(entityHorse);
                   	entityHorse.setOwner(par3EntityPlayer.username);
                    BattleFoxCox.horses.add(entityHorse);
        		}
        	}
            
        }

        return par1ItemStack;
    }

not tested, I'll review it tomorrow and then test, too late now.

Link to comment
Share on other sites

Now I have this method;

	public void grantHorse(EntityPlayer par1EntityPlayer, World par2World)
{
	EntityHorse entityHorse = new EntityHorse(par2World);
    	entityHorse.setLocationAndAngles(par1EntityPlayer.posX, par1EntityPlayer.posY, par1EntityPlayer.posZ, par2World.rand.nextFloat() * 360.0F, 0.0F);
       	par2World.spawnEntityInWorld(entityHorse);
       	entityHorse.setOwner(par1EntityPlayer.username);
       	BattleFoxCox.addHorseToList(entityHorse);
}

public void moveHorse(EntityPlayer par1EntityPlayer, EntityHorse par2EntityHorse, World par3World)
{
	par2EntityHorse.setLocationAndAngles(par1EntityPlayer.posX, par1EntityPlayer.posY, par1EntityPlayer.posZ, par3World.rand.nextFloat() * 360.0F, 0.0F);
}

    /**
     * Called whenever this item is equipped and the right mouse button is pressed. Args: itemStack, world, entityPlayer
     */
    public ItemStack onItemRightClick(ItemStack par1ItemStack, World par2World, EntityPlayer par3EntityPlayer)
    {
        if (!par2World.isRemote)
        {
        	if(BattleFoxCox.horses != null)
        	{
        		for(EntityHorse horse: BattleFoxCox.horses)
        		{
        			if(horse.getOwnerName().equalsIgnoreCase(par3EntityPlayer.username))
        			{
        				this.moveHorse(par3EntityPlayer, horse, par2World);
        				break;
        			}
        		}
        	}
        	else
        	{
        		this.grantHorse(par3EntityPlayer, par2World);
        	}
            
        }

        return par1ItemStack;
    }

 

How would I get it so that it triggers the moveHorse if the for each loop is not broken?

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.



×
×
  • Create New...

Important Information

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