Jump to content

Packet Problem


loawkise

Recommended Posts

I have used CatDany's packet handling example and it works fine when I use it like this:

 

@Override
public void onLivingUpdate()
{
	super.onLivingUpdate();

	IMessage syncLevel = new SimplePacket.SimpleMessage(level);
	level++;
	System.out.println(level);
	PacketHandler.net.sendToServer(syncLevel);
}

 

However, when I use it in this method, which is called every time my entity attacks, it doesn't have an effect anymore:

 

public void checkLevel()
{
	if(xp >= 100)
	{
		level++;
		xp = 0;

		IMessage syncLevel = new SimplePacket.SimpleMessage(level);
		System.out.println(level);
		PacketHandler.net.sendToServer(syncLevel);
	}
}

 

If someone could explain what I am doing wrong so I can fix it, that would be great.

 

Entity:

 

 

package com.blocklings.entities;

import net.minecraft.entity.Entity;
import net.minecraft.entity.EntityAgeable;
import net.minecraft.entity.EntityLivingBase;
import net.minecraft.entity.SharedMonsterAttributes;
import net.minecraft.entity.ai.EntityAIAttackOnCollide;
import net.minecraft.entity.ai.EntityAIFollowOwner;
import net.minecraft.entity.ai.EntityAIHurtByTarget;
import net.minecraft.entity.ai.EntityAILookIdle;
import net.minecraft.entity.ai.EntityAIOwnerHurtByTarget;
import net.minecraft.entity.ai.EntityAIOwnerHurtTarget;
import net.minecraft.entity.ai.EntityAISwimming;
import net.minecraft.entity.ai.EntityAIWander;
import net.minecraft.entity.ai.EntityAIWatchClosest;
import net.minecraft.entity.passive.EntityTameable;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.pathfinding.PathEntity;
import net.minecraft.util.DamageSource;
import net.minecraft.world.World;

import com.blocklings.packets.PacketHandler;
import com.blocklings.packets.SimplePacket;

import cpw.mods.fml.common.network.simpleimpl.IMessage;


public class EntityBlockling extends EntityTameable 
{
private int level, xp;

public EntityBlockling(World world) 
{
	super(world);

	setSize(1.0F, 1.0F);

	this.getNavigator().setAvoidsWater(true);
	this.getNavigator().setCanSwim(false);

	this.tasks.addTask(1, new EntityAISwimming(this));
	this.tasks.addTask(2, this.aiSit);
	this.tasks.addTask(3, new EntityAIAttackOnCollide(this, 1.0D, false));
	this.tasks.addTask(4, new EntityAIFollowOwner(this, 1.0D, 6.0F, 3.0F));
	this.tasks.addTask(5, new EntityAIWander(this, 1.0D));
	this.tasks.addTask(6, new EntityAIWatchClosest(this, EntityPlayer.class, 8.0F));
	this.tasks.addTask(7, new EntityAILookIdle(this));

	this.targetTasks.addTask(1, new EntityAIHurtByTarget(this, true));
	this.targetTasks.addTask(2, new EntityAIOwnerHurtByTarget(this));
	this.targetTasks.addTask(3, new EntityAIOwnerHurtTarget(this));

	level = 1;
	xp = 0;
}

@Override
protected void applyEntityAttributes() 
{
	super.applyEntityAttributes();

	this.getEntityAttribute(SharedMonsterAttributes.maxHealth).setBaseValue(20.0D);
	this.getEntityAttribute(SharedMonsterAttributes.movementSpeed).setBaseValue(0.3D);
	this.getAttributeMap().registerAttribute(SharedMonsterAttributes.attackDamage);
	this.getEntityAttribute(SharedMonsterAttributes.attackDamage).setBaseValue(1.0D);
}

@Override
public void onLivingUpdate()
{
	super.onLivingUpdate();

	IMessage syncLevel = new SimplePacket.SimpleMessage(level);
	System.out.println(level);
	PacketHandler.net.sendToServer(syncLevel);
}

@Override
public boolean attackEntityAsMob(Entity entity) 
{
	xp+=10;
	checkLevel();

	return entity.attackEntityFrom(DamageSource.causeMobDamage(this), (float) getEntityAttribute(SharedMonsterAttributes.attackDamage).getAttributeValue());
}

@Override
public boolean interact(EntityPlayer player) 
{
	if(!worldObj.isRemote) 
	{
		this.setTamed(true);
		this.setPathToEntity((PathEntity) null);
		this.setAttackTarget((EntityLivingBase) null);
		this.func_152115_b(player.getUniqueID().toString());
		this.playTameEffect(true);
		this.worldObj.setEntityState(this, (byte) 7);
	}

	return false;
}

public void checkLevel()
{
	if(xp >= 100)
	{
		level++;
		xp = 0;

		IMessage syncLevel = new SimplePacket.SimpleMessage(level);
		System.out.println(level);
		PacketHandler.net.sendToServer(syncLevel);
	}
}

@Override
public void writeEntityToNBT(NBTTagCompound compound) 
{
	super.writeEntityToNBT(compound);

	compound.setInteger("level", this.level);
	compound.setInteger("xp", this.xp);
}

@Override
public void readEntityFromNBT(NBTTagCompound compound) 
{
	super.readEntityFromNBT(compound);

	this.level = compound.getInteger("level");
	this.xp = compound.getInteger("xp");
}

@Override
public boolean isAIEnabled() 
{
	return true;
}

@Override
public EntityAgeable createChild(EntityAgeable ageable) 
{
	return null;
}

public int getLevel()
{
	return level;
}

public int getXP()
{
	return xp;
}
}

 

 

Packet:

 

 

public class PacketHandler 
{
public static SimpleNetworkWrapper net;

public static void initPackets() 
{
	net = NetworkRegistry.INSTANCE.newSimpleChannel(References.MODID.toUpperCase());
	registerMessage(SimplePacket.class, SimplePacket.SimpleMessage.class);
}

private static int nextPacketId = 0;

private static void registerMessage(Class packet, Class message) 
{
	net.registerMessage(packet, message, nextPacketId, Side.CLIENT);
	net.registerMessage(packet, message, nextPacketId, Side.SERVER);
	nextPacketId++;
}
}

 

 

 

public class SimplePacket implements IMessageHandler<SimpleMessage, IMessage> 
{
@Override
public IMessage onMessage(SimpleMessage message, MessageContext ctx) 
{
	if (ctx.side.isClient()) 
	{
		int integer = message.simpleInt;
	}
	return null;
}

public static class SimpleMessage implements IMessage 
{
	private int simpleInt;

	public SimpleMessage() 
	{

	}

	public SimpleMessage(int simpleInt) 
	{
		this.simpleInt = simpleInt;
	}

	@Override
	public void fromBytes(ByteBuf buf) 
	{
		this.simpleInt = buf.readInt();
	}

	@Override
	public void toBytes(ByteBuf buf) 
	{
		buf.writeInt(simpleInt);
	}
}
}

 

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.