Jump to content

[Help]client side updating of Entity


Recommended Posts

In my mod i have a single EntityGuard.class that does everything related to that Entity. Part of the class is picking the  weapon, team and texture of the Entity to be displayed to the player. The problem is that on smp every guard show up the same. Too fix this i'm trying to use Forge network packet system but nothing seem to be changing. Could someone help me with the code needed to send that data required for the guard entities to update correctly.

 

here the what i'm using to send the packet

public void SendType(int i, int j, Entity entity)
    {
            ByteArrayOutputStream bytes = new ByteArrayOutputStream();
            DataOutputStream data = new DataOutputStream(bytes);
            try
            {
            	
                    int[] coords = {i, j};
                    for(int a = 0; a < 2; a++)
                    {
                            data.writeInt(coords[a]);
                    }
            }
            catch(IOException e)
            {
                    e.printStackTrace();
            }

            Packet250CustomPayload packet = new Packet250CustomPayload();
            packet.channel = "mod_Guardsman";
            packet.data = bytes.toByteArray();
            packet.length = packet.data.length;

            ModLoader.getMinecraftServerInstance().configManager.sendPacketToAllPlayers(packet);
    }

and here is what is reciving it

public void onPacketData(NetworkManager network, String channel, byte[] bytes)
    {
            DataInputStream dataStream = new DataInputStream(new ByteArrayInputStream(bytes));
            
            try
            {
                    for(int i = 0; i < 3; i++)
                    {
                            coords[i] = dataStream.readInt();
                    }
            }
            catch(IOException e)
            {
                    e.printStackTrace();
            }
            
            
    }

Link to comment
Share on other sites

I'm still working on figuring out how to use the packet system myself, but I do see a problem with the code you posted, and solving it might help.

 

Mainly, you're trying to read three ints, but only sending two, so you're probably getting a stack trace every time - and may be failing to record any of the packet input. I'm assuming you have defined 'coords' somewhere in the class containing onPacketData(), but I'm not sure what the exact results would be of this situation. Make sure what you're sending and receiving matches exactly.

 

Also, I don't see anything that appears to be transferring information about the properties you described (weapon, team, and texture). Unless "coords" actually contains that information, in which case it's a poor choice of variable naming.

 

Finally, in SendType, why create an array and loop like that? You could simply write:

 

data.writeInt(i);

data.writeInt(j);

 

For information that doesn't come in the form of an array to begin with, there's really no need to try to reformat it that way. As long as the sequence and data types are the same on the sending and receiving ends, you should be fine.

 

Hopefully this will be of some use to you!

Link to comment
Share on other sites

thank you for the responce

My code is based off a tuturial i read up on how to move from Modloadermp to Forge network. So some of my code is about a complete match at the moment. When i do code clean up when the mod works i'll go threw and rename/recode everything cleaner. Later on i'll build on the send data to have more than 2 int so thats why i keep the loop. Makes things easier when i start to send data that will be used in GUIs used to interface with the guard. Also i think the data has to be stored as a byte array inorder to be sent without overriding the first number.

 

The properties are set as ints to make it possible to send and store as an array. Int (i) is the team id and Int(j) is the weapon type. Both are set when SendType is called during updating of the entity. Texture is done client side by picking first the team color then the profession texture to go with the weapon.

Link to comment
Share on other sites

ok i took a look at both client & server code for the Wolf. I found i'm using datawatchers already in the same way wolf.class does. So is there a special way to use datawatcher to get them to link from server to client.

 

here is client side EntityGuard class

package net.minecraft.src.GSM;
import java.util.*;

import net.minecraft.src.*;

public class EntityGuardBase extends EntityCreature {


/** How much damage this mob's attacks deal */
    protected static int attackStrength;
    public static int team;
    public static int type;
    public EntityGuardBase(World par1World)
    {
        this(par1World,1, 1);
    }
    public EntityGuardBase(World par1World, int Team, int Type)
    {
    	super(par1World);
    	attackStrength = 5;
        experienceValue = 5;
        this.moveSpeed = 0.3F;       
        team = Team;
        type = Type;
        this.setProfession(Type);
        this.setTeam(Team);
    	this.setTextureTeamType(); 
    	//tasks
        this.tasks.addTask(1, new EntityAISwimming(this));  
        this.tasks.addTask(2, new EntityAIAvoidEntity(this, EntityCreeper.class, 2.0F, 0.3F, 0.35F));
        if(Type == 1){this.tasks.addTask(3, new EntityAIArrowAttack(this, this.moveSpeed, 1, 50));}
        this.tasks.addTask(9, new EntityAIWander(this, this.moveSpeed));
        this.tasks.addTask(10, new EntityAIWatchClosest(this, EntityPlayer.class, 8.0F));
        this.targetTasks.addTask(1, new EntityAIHurtByTarget(this, false));        
        this.targetTasks.addTask(3, new EntityAINearestAttackableTarget(this, EntityMob.class, 16.0F, 0, true));
        this.targetTasks.addTask(3, new EntityAINearestAttackableTarget(this, EntityPlayer.class, 16.0F, 0, false));
        
    }
   

public void setTextureTeamType() {
    	texture = "/mob/char.png";
    	if(this.getTeam() ==1)
    	{
    	texture = "/GSM/NPC/Red" + this.getProfession() + ".png";
    	}
    	if(this.getTeam() == 2)
    	{
    	texture = "/GSM/NPC/Blue" + this.getProfession() + ".png";
    	}
}

    protected boolean canDespawn()
    {
        return false;
    }
    public void onLivingUpdate(){super.onLivingUpdate();}
    public void onUpdate(){
    	super.onUpdate();
    	if(worldObj.isRemote)
    	{
    		updateFromSmp();
    	}
    }
    public boolean getCanSpawnHere(){return true;}
    public int getMaxHealth(){return 50;} 
    public boolean isAIEnabled(){return true;}
    protected Entity findPlayerToAttack()
    {
        EntityPlayer entityplayer = worldObj.getClosestVulnerablePlayerToEntity(this, 16D);

        if (entityplayer != null && canEntityBeSeen(entityplayer))
        {
            return entityplayer;
        }
        else
        {
            return null;
        }
    }
    public boolean attackEntityFrom(DamageSource par1DamageSource, int par2)
    {
    	
        if (super.attackEntityFrom(par1DamageSource, par2))
        {
            Entity entity = par1DamageSource.getEntity();            
            if (riddenByEntity == entity || ridingEntity == entity)
            {
                return true;
            }

            if (entity != this || entity.entityId != this.entityId )
            {
                entityToAttack = entity;
            }

            return true;
        }
        else
        {
            return false;
        }
    }
    public boolean attackEntityAsMob(Entity par1Entity)
    {
        int i = attackStrength;

        if (isPotionActive(Potion.damageBoost))
        {
            i += 3 << getActivePotionEffect(Potion.damageBoost).getAmplifier();
        }

        if (isPotionActive(Potion.weakness))
        {
            i -= 2 << getActivePotionEffect(Potion.weakness).getAmplifier();
        }

        return par1Entity.attackEntityFrom(DamageSource.causeMobDamage(this), i);
    }
    protected void attackEntity(Entity par1Entity, float par2)
    {
        if (attackTime <= 0 && par2 < 2.0F && par1Entity.boundingBox.maxY > boundingBox.minY && par1Entity.boundingBox.minY < boundingBox.maxY)
        {
            attackTime = 20;
            attackEntityAsMob(par1Entity);
        }
    }
    public float getBlockPathWeight(int par1, int par2, int par3)
    {
        return 0.5F + worldObj.getLightBrightness(par1, par2, par3);
    }   
    //Write
    public void writeEntityToNBT(NBTTagCompound par1NBTTagCompound)
    {
        super.writeEntityToNBT(par1NBTTagCompound);
        par1NBTTagCompound.setInteger("Profession", this.getProfession());
        par1NBTTagCompound.setInteger("Team", this.getTeam());
        
    }    
   //Read
    public void readEntityFromNBT(NBTTagCompound par1NBTTagCompound)
    {
        super.readEntityFromNBT(par1NBTTagCompound);
        this.setProfession(par1NBTTagCompound.getInteger("Profession"));
        this.setTeam(par1NBTTagCompound.getInteger("Team"));
        this.setTextureTeamType();
        this.getHeldItem();
    }
    public void updateFromSmp()
    { 
    	setTeam(net.minecraft.src.mod_Guardsman.coords[0]);
    	setProfession(net.minecraft.src.mod_Guardsman.coords[1]);
    	
    }
      
    protected void entityInit()
    {
        super.entityInit();
        this.dataWatcher.addObject(20, Integer.valueOf(0));
        this.dataWatcher.addObject(21, Integer.valueOf(0));
    }
    public void setProfession(int par1)
    {
        this.dataWatcher.updateObject(20, Integer.valueOf(par1));
    }

    public int getProfession()
    {
        return this.dataWatcher.getWatchableObjectInt(20);
    }
    public void setTeam(int par1)
    {
        this.dataWatcher.updateObject(21, Integer.valueOf(par1));
    }

    public int getTeam()
    {
        return this.dataWatcher.getWatchableObjectInt(21);
    }
              
      @Override
    public ItemStack getHeldItem()
    {
    	
    	if(this.getProfession() == 1)
	  {
	      
		   return new ItemStack(Item.bow, 1);

	  }
    	else
    	{
    		return null;
        }
    

    }
}

 

And here is the server side

package net.minecraft.src.GSM;
import java.io.*;
import java.util.*;

import net.minecraft.src.*;

public class EntityGuardBase extends EntityCreature implements IAnimals {


/** How much damage this mob's attacks deal */
    protected static int attackStrength;
    public static int team;
    public static int type;
    public EntityGuardBase(World par1World)
    {
        this(par1World,1, 1);
    }
    public EntityGuardBase(World par1World, int Team, int Type)
    {
    	super(par1World);
    	attackStrength = 5;
        experienceValue = 5;
        this.moveSpeed = 0.3F;       
        team = Team;
        type = Type;
        this.setProfession(Type);
        this.setTeam(Team); 
    	//tasks
        this.tasks.addTask(2, new EntityAISwimming(this));  
        this.tasks.addTask(2, new EntityAIAvoidEntity(this, EntityCreeper.class, 2.0F, 0.3F, 0.35F));
        if(Type == 1){this.tasks.addTask(3, new EntityAIArrowAttack(this, this.moveSpeed, 1, 50));}
        this.tasks.addTask(9, new EntityAIWander(this, this.moveSpeed));
        this.tasks.addTask(10, new EntityAIWatchClosest(this, EntityPlayer.class, 8.0F));
        this.targetTasks.addTask(1, new EntityAIHurtByTarget(this, false));
        this.targetTasks.addTask(2, new EntityAINearestAttackableTarget(this, IMob.class, 16.0F, 0, true));
        this.targetTasks.addTask(1, new EntityAINearestAttackableTarget(this, EntityPlayer.class, 16.0F, 0, false));
        
    }
    private String[] getTeamList() {
      if(getTeam() == 1)
      {
    	  return net.minecraft.src.GSM.ConfigMaker.red;
      }
      if(getTeam() == 2)
      {
    	  return net.minecraft.src.GSM.ConfigMaker.blue;
      }
return null;
}
    public void SendType(int i, int j, Entity entity)
    {
    	int k = entity.entityId;
            ByteArrayOutputStream bytes = new ByteArrayOutputStream();
            DataOutputStream data = new DataOutputStream(bytes);
            try
            {
            	
                    int[] coords = {i, j, k};
                    for(int a = 0; a < 3; a++)
                    {
                            data.writeInt(coords[a]);
                    }
            }
            catch(IOException e)
            {
                    e.printStackTrace();
            }

            Packet250CustomPayload packet = new Packet250CustomPayload();           
            packet.channel = "mod_Guardsman"; // CHANNEL MAX 16 CHARS            
            packet.data = bytes.toByteArray();            
            packet.length = packet.data.length;

            ModLoader.getMinecraftServerInstance().configManager.sendPacketToAllPlayers(packet);
    }

    protected boolean canDespawn()
    {
        return false;
    }
    public void onLivingUpdate(){super.onLivingUpdate();}
    public void onUpdate(){
    	super.onUpdate();
    	findPlayerToAttack();
    	SendType(getTeam(), getProfession(), this);
    	}
    public boolean getCanSpawnHere(){return true;}
    public int getMaxHealth(){return 50;} 
    public boolean isAIEnabled(){return true;}
    protected Entity findPlayerToAttack()
    {
        EntityPlayer entityplayer = worldObj.getClosestVulnerablePlayerToEntity(this, 16D);

        if (entityplayer != null && canEntityBeSeen(entityplayer))
        {
        	if(getTeam() == 1)
        	{
		  for(int j = 0; j > 21; j++)
		  {
			  if (entityplayer.username.equals(net.minecraft.src.GSM.ConfigMaker.red[j]))
				  {
				  return null;
				  }			 

		  }
        	}
		  if(getTeam() == 2)
		  {
			  for(int j = 0; j > 21; j++)
			  {
				  if (entityplayer.username.equals(net.minecraft.src.GSM.ConfigMaker.blue[j]))
				      {
					  return null;
					  }			 

			  }
		  }
		  
        }
	return entityplayer;
    }
        
        
    

    private boolean compareNameToTeam (String name, String[] Team)
  {	
	  if (Team != null && name != null )
	  {		
       }
	  
	return true;
}
    public boolean attackEntityFrom(DamageSource par1DamageSource, int par2)
    {
    	
        if (super.attackEntityFrom(par1DamageSource, par2))
        {
            Entity entity = par1DamageSource.getEntity();            
            if (riddenByEntity == entity || ridingEntity == entity)
            {
                return true;
            }

            if (entity != this || entity.entityId != this.entityId )
            {
                entityToAttack = entity;
            }

            return true;
        }
        else
        {
            return false;
        }
    }
    
    public boolean attackEntityAsMob(Entity par1Entity)
    {
        int i = attackStrength;

        if (isPotionActive(Potion.damageBoost))
        {
            i += 3 << getActivePotionEffect(Potion.damageBoost).getAmplifier();
        }

        if (isPotionActive(Potion.weakness))
        {
            i -= 2 << getActivePotionEffect(Potion.weakness).getAmplifier();
        }

        return par1Entity.attackEntityFrom(DamageSource.causeMobDamage(this), i);
    }
    protected void attackEntity(Entity par1Entity, float par2)
    {
        if (attackTime <= 0 && par2 < 2.0F && par1Entity.boundingBox.maxY > boundingBox.minY && par1Entity.boundingBox.minY < boundingBox.maxY)
        {
            attackTime = 20;
            attackEntityAsMob(par1Entity);
        }
    }
    public float getBlockPathWeight(int par1, int par2, int par3)
    {
        return 0.5F + worldObj.getLightBrightness(par1, par2, par3);
    }   
    //Write
    public void writeEntityToNBT(NBTTagCompound par1NBTTagCompound)
    {
        super.writeEntityToNBT(par1NBTTagCompound);
        par1NBTTagCompound.setInteger("Profession", this.getProfession());
        par1NBTTagCompound.setInteger("Team", this.getTeam());
        
    }    
   //Read
    public void readEntityFromNBT(NBTTagCompound par1NBTTagCompound)
    {
        super.readEntityFromNBT(par1NBTTagCompound);
        this.setProfession(par1NBTTagCompound.getInteger("Profession"));
        this.setTeam(par1NBTTagCompound.getInteger("Team"));
        this.getHeldItem();
    }
    
      
    protected void entityInit()
    {
        super.entityInit();
        this.dataWatcher.addObject(20, Integer.valueOf(0));
        this.dataWatcher.addObject(21, Integer.valueOf(0));
    }
    public void setProfession(int par1)
    {
        this.dataWatcher.updateObject(20, Integer.valueOf(par1));
    }

    public int getProfession()
    {
        return this.dataWatcher.getWatchableObjectInt(20);
    }
    public void setTeam(int par1)
    {
        this.dataWatcher.updateObject(21, Integer.valueOf(par1));
    }

    public int getTeam()
    {
        return this.dataWatcher.getWatchableObjectInt(21);
    }
              
      public ItemStack getHeldItem()
    {
    	
    	if(this.getProfession() == 1)
	  {
	      
		   return new ItemStack(Item.bow, 1);

	  }
    	else
    	{
    		return null;
        }
    

    }
}

Link to comment
Share on other sites

I thought they were linked and were synchronized automatically, have been for me anyway.

I think they do from what i've read out of the code and on forums. If they are that means i'm doing something wrong. Either something is overriding the data or server side is not spawning the guard right. Think you can show me how you do it in your code? then i can compare and find my mistake.

Link to comment
Share on other sites

I thought they were linked and were synchronized automatically, have been for me anyway.

I think they do from what i've read out of the code and on forums. If they are that means i'm doing something wrong. Either something is overriding the data or server side is not spawning the guard right. Think you can show me how you do it in your code? then i can compare and find my mistake.

I just copied how EntityWolf did it was all actually.  :)

Link to comment
Share on other sites

I thought they were linked and were synchronized automatically, have been for me anyway.

I think they do from what i've read out of the code and on forums. If they are that means i'm doing something wrong. Either something is overriding the data or server side is not spawning the guard right. Think you can show me how you do it in your code? then i can compare and find my mistake.

I just copied how EntityWolf did it was all actually.  :)

:/ k guess i'll recode my EntityClass again off the wolf class. I'm getting packet handler errors when spawning them Smp anywyas. So its best i restart fresh, get it working and then add in my own code

 

Edit: i recoded my code. descided not use the wolf.class as my base but villager.class. I'm still getting this error:

 

java.lang.NullPointerException

at net.minecraft.src.NetClientHandler.handleMobSpawn(NetClientHandler.java:758)

at net.minecraft.src.Packet24MobSpawn.processPacket(Packet24MobSpawn.java:88)

at net.minecraft.src.NetworkManager.processReadPackets(NetworkManager.java:343)

at net.minecraft.src.NetClientHandler.processReadPackets(NetClientHandler.java:68)

at net.minecraft.src.WorldClient.tick(WorldClient.java:62)

at net.minecraft.client.Minecraft.runTick(Minecraft.java:1867)

at net.minecraft.client.Minecraft.runGameLoop(Minecraft.java:819)

at net.minecraft.client.Minecraft.run(Minecraft.java:747)

at java.lang.Thread.run(Unknown Source)

--- END ERROR REPORT c6232b2f ----------

Edit: fixed that error turns out you need something like this

public EntityGuardBase(World par1World)
    {
	 this(par1World, 0,0);
    }

at the being on the class. Though i still can't get the texture updated for smp yet.

Link to comment
Share on other sites

Ok i got it working. What i needed to do was get the entity to reset its texture, type and team on EntityUpdate. the only problem i have now is that Guard Entities are not rendering tile the chunk  is updated. eg i click. place, destroy or cause an update of somethings. Also i seem not too be able to set the item in the Entities hand. I'm using the same approach that other mobs use but nothing seems too work.

Link to comment
Share on other sites

Guest
This topic is now closed to further replies.

Announcements



×
×
  • Create New...

Important Information

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