Jump to content

Minecraft gives error when using custom packets


FoxWare
 Share

Recommended Posts

Hi, I maked a custom packet to with the scope to use Minecraft.partciles.emitParticlesAtEntity(), but minecraft give me an error:
 

Internal Exception: io.netty.handler.codec.EncoderException: java.lang.RuntimeException: ConnectionProtocol unknown: me.foxware.potions.network.packets.SEmitParticlesPacket@389b00f

Here the packet code:

public class SEmitParticlesPacket implements IPacket<IClientPlayNetHandler> {
	private Minecraft client = Minecraft.getInstance();
	
	private int entityID;
	private IParticleData particle;
	private int lifeTime;
	
	public <T extends IParticleData> SEmitParticlesPacket(Entity entityIn, T particleIn, int lifeTimeIn) {
		this.entityID = entityIn.getEntityId();
		this.particle = particleIn;
		this.lifeTime = lifeTimeIn;
	}
	
	@Override
	public void readPacketData(PacketBuffer bufIn) throws IOException {
		ParticleType<?> particleType = Registry.PARTICLE_TYPE.getByValue(bufIn.readInt());
		if(particleType == null) {
			particleType = ParticleTypes.BARRIER;
		}
		
		this.entityID = bufIn.readInt();
		this.lifeTime = bufIn.readInt();
		this.particle = readParticle(bufIn, particleType);
	}

	@Override
	public void writePacketData(PacketBuffer bufIn) throws IOException {
		bufIn.writeInt(Registry.PARTICLE_TYPE.getId(this.particle.getType()));
		bufIn.writeInt(this.entityID);
		bufIn.writeInt(this.lifeTime);
		
		this.particle.write(bufIn);
	}
	private <T extends IParticleData> T readParticle(PacketBuffer bufIn, ParticleType<T> particleType) {
		return particleType.getDeserializer().read(particleType, bufIn);
	}
	
	public int getEntityID() {
		return this.entityID;
	}
	public IParticleData getParticle() {
		return this.particle;
	}
	public int getLifeTime() {
		return this.lifeTime;
	}
	
	@Override
	public void processPacket(IClientPlayNetHandler handlerIn) {
		this.client.particles.emitParticleAtEntity(this.client.world.getEntityByID(getEntityID()), getParticle(), getLifeTime());
	}
}

Here where is used:
 

@SubscribeEvent
public static void onEntityDeath(LivingDeathEvent eventIn) throws InterruptedException {
	LivingEntity entity = eventIn.getEntityLiving();
	if(entity.getActivePotionEffect(ModEffects.IMMORTALITY.get()) != null) {
		entity.setHealth(entity.getMaxHealth() * 25 / 100);
		entity.removePotionEffect(ModEffects.IMMORTALITY.get());
		
		entity.world.playSound(null, entity.getPosX(), entity.getPosY(), entity.getPosZ(), SoundEvents.ITEM_TOTEM_USE, entity.getSoundCategory(), 1.0F, 1.0F);
		for(PlayerEntity p : entity.world.getPlayers()) {
			((ServerPlayerEntity) p).connection.sendPacket(new SEmitParticlesPacket(entity, ParticleTypes.TOTEM_OF_UNDYING, 30));
		}
		
		eventIn.setCanceled(true);
	}
}

Thanks for everyone helps me!

Link to comment
Share on other sites

15 minutes ago, ChampionAsh5357 said:

Use Forge's network system.

Ok, i do something but are some issues...


Here Packet class:

public class EmitParticlesPacket {
	private int entityID;
	private IParticleData particle;
	private int lifeTime;
	
	public <T extends IParticleData> EmitParticlesPacket(Entity entityIn, T particleIn, int lifeTimeIn) {
		this.entityID = entityIn.getEntityId();
		this.particle = particleIn;
		this.lifeTime = lifeTimeIn;
	}
	
	public EmitParticlesPacket(PacketBuffer bufIn) throws IOException {
		ParticleType<?> particleType = Registry.PARTICLE_TYPE.getByValue(bufIn.readInt());
		if(particleType == null) {
			particleType = ParticleTypes.BARRIER;
		}
		
		this.entityID = bufIn.readInt();
		this.lifeTime = bufIn.readInt();
		this.particle = readParticle(bufIn, particleType);
	}

	public void encoder(EmitParticlesPacket msg, PacketBuffer bufIn) throws IOException {
		bufIn.writeInt(Registry.PARTICLE_TYPE.getId(this.particle.getType()));
		bufIn.writeInt(this.entityID);
		bufIn.writeInt(this.lifeTime);
		
		this.particle.write(bufIn);
	}
	
	private <T extends IParticleData> T readParticle(PacketBuffer bufIn, ParticleType<T> particleType) {
		return particleType.getDeserializer().read(particleType, bufIn);
	}
	
	public int getEntityID() {
		return this.entityID;
	}
	public IParticleData getParticle() {
		return this.particle;
	}
	public int getLifeTime() {
		return this.lifeTime;
	}
	
	public static void handle(EmitParticlesPacket msg, Supplier<NetworkEvent.Context> ctx) {
		ctx.get().enqueueWork(() -> {
			if(ctx.get().getDirection().getReceptionSide().equals(LogicalSide.CLIENT)) {
				Minecraft client = Minecraft.getInstance();
				client.particles.emitParticleAtEntity(client.world.getEntityByID(msg.getEntityID()), msg.getParticle(), msg.getLifeTime());
			}
		});
		ctx.get().setPacketHandled(true);
	}
}

 

Here the PacketHandler... here eclipse gives some errors at ISTANCE.registerMessage():
2 arg: The type EmitParticlesPacket does not define encoder(MSG, PacketBuffer) that is applicable here
3 arg: The constructed object of type EmitParticlesPacket is incompatible with the descriptor's return type: MSG
4 arg: The type EmitParticlesPacket does not define handle(MSG, Supplier<NetworkEvent.Context>) that is applicable here
Here the class of Packet Handler:

public class ModPacketHandler {
	private static final String PROTOCOL_VER = "1";
	public static final SimpleChannel INSTANCE = NetworkRegistry.newSimpleChannel(
		new ResourceLocation(FurtherPotionsMain.MODID, "main"),
		() -> PROTOCOL_VER,
		PROTOCOL_VER::equals,
		PROTOCOL_VER::equals
	);
	
	public static void register() {
		int ID = 0;
		INSTANCE.registerMessage(ID++, EmitParticlesPacket.class, EmitParticlesPacket::encoder, EmitParticlesPacket::new, EmitParticlesPacket::handle);
	}
}

 

Link to comment
Share on other sites

39 minutes ago, FoxWare said:

The type EmitParticlesPacket does not define encoder(MSG, PacketBuffer) that is applicable here

Learn a bit more about Functional Interfaces. The function is non-static, so why do you need to pass in an instance you already have?

40 minutes ago, FoxWare said:

The constructed object of type EmitParticlesPacket is incompatible with the descriptor's return type: MSG

The decoder requires a return value, something you are not providing.

41 minutes ago, FoxWare said:

The type EmitParticlesPacket does not define handle(MSG, Supplier<NetworkEvent.Context>) that is applicable here

Look at the parameter needed to supply the method.

Link to comment
Share on other sites

39 minutes ago, FoxWare said:

The type EmitParticlesPacket does not define encoder(MSG, PacketBuffer) that is applicable here

Learn a bit more about Functional Interfaces. The function is non-static, so why do you need to pass in an instance you already have?

40 minutes ago, FoxWare said:

The constructed object of type EmitParticlesPacket is incompatible with the descriptor's return type: MSG

The decoder requires a return value, something you are not providing.

41 minutes ago, FoxWare said:

The type EmitParticlesPacket does not define handle(MSG, Supplier<NetworkEvent.Context>) that is applicable here

Look at the parameter needed to supply the method.

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
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.

 Share



×
×
  • Create New...

Important Information

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