@Override void toBytes(ByteBuf buf) { super.toBytes(buf); ByteBufUtils.writeUTF8String(buf, modId); buf.writeInt(modEntityTypeId); // posX, posY, posZ buf.writeInt(MathHelper.func_76128_c(entity.field_70165_t * 32D)); buf.writeInt(MathHelper.func_76128_c(entity.field_70163_u * 32D)); buf.writeInt(MathHelper.func_76128_c(entity.field_70161_v * 32D)); // yaw, pitch buf.writeByte((byte)(entity.field_70177_z * 256.0F / 360.0F)); buf.writeByte((byte) (entity.field_70125_A * 256.0F / 360.0F)); // head yaw if (entity instanceof EntityLivingBase) { buf.writeByte((byte) (((EntityLivingBase)entity).field_70759_as * 256.0F / 360.0F)); } else { buf.writeByte(0); } ByteBuf tmpBuf = Unpooled.buffer(); PacketBuffer pb = new PacketBuffer(tmpBuf); try { entity.func_70096_w().func_151509_a(pb); } catch (IOException e) { FMLLog.log(Level.FATAL,e,"Encountered fatal exception trying to send entity spawn data watchers"); throw Throwables.propagate(e); } buf.writeBytes(tmpBuf); if (entity instanceof IThrowableEntity) { Entity owner = ((IThrowableEntity)entity).getThrower(); buf.writeInt(owner == null ? entity.func_145782_y() : owner.func_145782_y()); double maxVel = 3.9D; double mX = entity.field_70159_w; double mY = entity.field_70181_x; double mZ = entity.field_70179_y; if (mX < -maxVel) mX = -maxVel; if (mY < -maxVel) mY = -maxVel; if (mZ < -maxVel) mZ = -maxVel; if (mX > maxVel) mX = maxVel; if (mY > maxVel) mY = maxVel; if (mZ > maxVel) mZ = maxVel; buf.writeInt((int)(mX * 8000D)); buf.writeInt((int)(mY * 8000D)); buf.writeInt((int)(mZ * 8000D)); } else { buf.writeInt(0); } if (entity instanceof IEntityAdditionalSpawnData) { tmpBuf = Unpooled.buffer(); ((IEntityAdditionalSpawnData)entity).writeSpawnData(tmpBuf); buf.writeBytes(tmpBuf); } }
private void spawnEntity(FMLMessage.EntitySpawnMessage spawnMsg) { ModContainer mc = Loader.instance().getIndexedModList().get(spawnMsg.modId); EntityRegistration er = EntityRegistry.instance().lookupModSpawn(mc, spawnMsg.modEntityTypeId); if (er == null) { throw new RuntimeException( "Could not spawn mod entity ModID: " + spawnMsg.modId + " EntityID: " + spawnMsg.modEntityTypeId + " at ( " + spawnMsg.scaledX + "," + spawnMsg.scaledY + ", " + spawnMsg.scaledZ + ") Please contact mod author or server admin."); } WorldClient wc = FMLClientHandler.instance().getWorldClient(); Class<? extends Entity> cls = er.getEntityClass(); try { Entity entity; if (er.hasCustomSpawning()) { entity = er.doCustomSpawning(spawnMsg); } else { entity = (Entity) (cls.getConstructor(World.class).newInstance(wc)); int offset = spawnMsg.entityId - entity.func_145782_y(); entity.func_145769_d(spawnMsg.entityId); entity.func_70012_b(spawnMsg.scaledX, spawnMsg.scaledY, spawnMsg.scaledZ, spawnMsg.scaledYaw, spawnMsg.scaledPitch); if (entity instanceof EntityLiving) { ((EntityLiving) entity).field_70759_as = spawnMsg.scaledHeadYaw; } Entity parts[] = entity.func_70021_al(); if (parts != null) { for (int j = 0; j < parts.length; j++) { parts[j].func_145769_d(parts[j].func_145782_y() + offset); } } } entity.field_70118_ct = spawnMsg.rawX; entity.field_70117_cu = spawnMsg.rawY; entity.field_70116_cv = spawnMsg.rawZ; EntityClientPlayerMP clientPlayer = FMLClientHandler.instance().getClientPlayerEntity(); if (entity instanceof IThrowableEntity) { Entity thrower = clientPlayer.func_145782_y() == spawnMsg.throwerId ? clientPlayer : wc.func_73045_a(spawnMsg.throwerId); ((IThrowableEntity) entity).setThrower(thrower); } if (spawnMsg.dataWatcherList != null) { entity.func_70096_w().func_75687_a((List<?>) spawnMsg.dataWatcherList); } if (spawnMsg.throwerId > 0) { entity.func_70016_h(spawnMsg.speedScaledX, spawnMsg.speedScaledY, spawnMsg.speedScaledZ); } if (entity instanceof IEntityAdditionalSpawnData) { ((IEntityAdditionalSpawnData) entity).readSpawnData(spawnMsg.dataStream); } wc.func_73027_a(spawnMsg.entityId, entity); } catch (Exception e) { FMLLog.log(Level.ERROR, e, "A severe problem occurred during the spawning of an entity at ( " + spawnMsg.scaledX + "," + spawnMsg.scaledY + ", " + spawnMsg.scaledZ +")"); throw Throwables.propagate(e); } }
@Override void toBytes(ByteBuf buf) { super.toBytes(buf); ByteBufUtils.writeUTF8String(buf, modId); buf.writeInt(modEntityTypeId); // posX, posY, posZ buf.writeInt(MathHelper.floor_double(entity.posX * 32D)); buf.writeInt(MathHelper.floor_double(entity.posY * 32D)); buf.writeInt(MathHelper.floor_double(entity.posZ * 32D)); // yaw, pitch buf.writeByte((byte)(entity.rotationYaw * 256.0F / 360.0F)); buf.writeByte((byte) (entity.rotationPitch * 256.0F / 360.0F)); // head yaw if (entity instanceof EntityLivingBase) { buf.writeByte((byte) (((EntityLivingBase)entity).rotationYawHead * 256.0F / 360.0F)); } ByteBuf tmpBuf = Unpooled.buffer(); PacketBuffer pb = new PacketBuffer(tmpBuf); try { entity.getDataWatcher().func_151509_a(pb); } catch (IOException e) { // Sigh } buf.writeBytes(tmpBuf); if (entity instanceof IThrowableEntity) { Entity owner = ((IThrowableEntity)entity).getThrower(); buf.writeInt(owner == null ? entity.getEntityId() : owner.getEntityId()); double maxVel = 3.9D; double mX = entity.motionX; double mY = entity.motionY; double mZ = entity.motionZ; if (mX < -maxVel) mX = -maxVel; if (mY < -maxVel) mY = -maxVel; if (mZ < -maxVel) mZ = -maxVel; if (mX > maxVel) mX = maxVel; if (mY > maxVel) mY = maxVel; if (mZ > maxVel) mZ = maxVel; buf.writeInt((int)(mX * 8000D)); buf.writeInt((int)(mY * 8000D)); buf.writeInt((int)(mZ * 8000D)); } else { buf.writeInt(0); } if (entity instanceof IEntityAdditionalSpawnData) { tmpBuf = Unpooled.buffer(); ((IEntityAdditionalSpawnData)entity).writeSpawnData(tmpBuf); buf.writeBytes(tmpBuf); } }
private void spawnEntity(FMLMessage.EntitySpawnMessage spawnMsg) { ModContainer mc = Loader.instance().getIndexedModList().get(spawnMsg.modId); EntityRegistration er = EntityRegistry.instance().lookupModSpawn(mc, spawnMsg.modEntityTypeId); WorldClient wc = FMLClientHandler.instance().getWorldClient(); Class<? extends Entity> cls = er.getEntityClass(); try { Entity entity; if (er.hasCustomSpawning()) { entity = er.doCustomSpawning(spawnMsg); } else { entity = (Entity) (cls.getConstructor(World.class).newInstance(wc)); int offset = spawnMsg.entityId - entity.getEntityId(); entity.setEntityId(spawnMsg.entityId); entity.setLocationAndAngles(spawnMsg.scaledX, spawnMsg.scaledY, spawnMsg.scaledZ, spawnMsg.scaledYaw, spawnMsg.scaledPitch); if (entity instanceof EntityLiving) { ((EntityLiving) entity).rotationYawHead = spawnMsg.scaledHeadYaw; } Entity parts[] = entity.getParts(); if (parts != null) { for (int j = 0; j < parts.length; j++) { parts[j].setEntityId(parts[j].getEntityId() + offset); } } } entity.serverPosX = spawnMsg.rawX; entity.serverPosY = spawnMsg.rawY; entity.serverPosZ = spawnMsg.rawZ; EntityClientPlayerMP clientPlayer = FMLClientHandler.instance().getClientPlayerEntity(); if (entity instanceof IThrowableEntity) { Entity thrower = clientPlayer.getEntityId() == spawnMsg.throwerId ? clientPlayer : wc.getEntityByID(spawnMsg.throwerId); ((IThrowableEntity) entity).setThrower(thrower); } if (spawnMsg.dataWatcherList != null) { entity.getDataWatcher().updateWatchedObjectsFromList((List<?>) spawnMsg.dataWatcherList); } if (spawnMsg.throwerId > 0) { entity.setVelocity(spawnMsg.speedScaledX, spawnMsg.speedScaledY, spawnMsg.speedScaledZ); } if (entity instanceof IEntityAdditionalSpawnData) { ((IEntityAdditionalSpawnData) entity).readSpawnData(spawnMsg.dataStream); } wc.addEntityToWorld(spawnMsg.entityId, entity); } catch (Exception e) { FMLLog.log(Level.ERROR, e, "A severe problem occurred during the spawning of an entity"); throw Throwables.propagate(e); } }
@Override public Entity spawnEntityIntoClientWorld(EntityRegistration er, EntitySpawnPacket packet) { WorldClient wc = client.field_71441_e; Class<? extends Entity> cls = er.getEntityClass(); try { Entity entity; if (er.hasCustomSpawning()) { entity = er.doCustomSpawning(packet); } else { entity = (Entity)(cls.getConstructor(World.class).newInstance(wc)); int offset = packet.entityId - entity.field_70157_k; entity.field_70157_k = packet.entityId; entity.func_70012_b(packet.scaledX, packet.scaledY, packet.scaledZ, packet.scaledYaw, packet.scaledPitch); if (entity instanceof EntityLiving) { ((EntityLiving)entity).field_70759_as = packet.scaledHeadYaw; } Entity parts[] = entity.func_70021_al(); if (parts != null) { for (int j = 0; j < parts.length; j++) { parts[j].field_70157_k += offset; } } } entity.field_70118_ct = packet.rawX; entity.field_70117_cu = packet.rawY; entity.field_70116_cv = packet.rawZ; if (entity instanceof IThrowableEntity) { Entity thrower = client.field_71439_g.field_70157_k == packet.throwerId ? client.field_71439_g : wc.func_73045_a(packet.throwerId); ((IThrowableEntity)entity).setThrower(thrower); } if (packet.metadata != null) { entity.func_70096_w().func_75687_a((List)packet.metadata); } if (packet.throwerId > 0) { entity.func_70016_h(packet.speedScaledX, packet.speedScaledY, packet.speedScaledZ); } if (entity instanceof IEntityAdditionalSpawnData) { ((IEntityAdditionalSpawnData)entity).readSpawnData(packet.dataStream); } wc.func_73027_a(packet.entityId, entity); return entity; } catch (Exception e) { FMLLog.log(Level.SEVERE, e, "A severe problem occurred during the spawning of an entity"); throw Throwables.propagate(e); } }
@Override public byte[] generatePacket(Object... data) { EntityRegistration er = (EntityRegistration) data[0]; Entity ent = (Entity) data[1]; NetworkModHandler handler = (NetworkModHandler) data[2]; ByteArrayDataOutput dat = ByteStreams.newDataOutput(); dat.writeInt(handler.getNetworkId()); dat.writeInt(er.getModEntityId()); // entity id dat.writeInt(ent.field_70157_k); // entity pos x,y,z dat.writeInt(MathHelper.func_76128_c(ent.field_70165_t * 32D)); dat.writeInt(MathHelper.func_76128_c(ent.field_70163_u * 32D)); dat.writeInt(MathHelper.func_76128_c(ent.field_70161_v * 32D)); // yaw, pitch dat.writeByte((byte) (ent.field_70177_z * 256.0F / 360.0F)); dat.writeByte((byte) (ent.field_70125_A * 256.0F / 360.0F)); // head yaw if (ent instanceof EntityLiving) { dat.writeByte((byte) (((EntityLiving)ent).field_70759_as * 256.0F / 360.0F)); } else { dat.writeByte(0); } ByteArrayOutputStream bos = new ByteArrayOutputStream(); DataOutputStream dos = new DataOutputStream(bos); try { ent.func_70096_w().func_75689_a(dos); } catch (IOException e) { // unpossible } dat.write(bos.toByteArray()); if (ent instanceof IThrowableEntity) { Entity owner = ((IThrowableEntity)ent).getThrower(); dat.writeInt(owner == null ? ent.field_70157_k : owner.field_70157_k); double maxVel = 3.9D; double mX = ent.field_70159_w; double mY = ent.field_70181_x; double mZ = ent.field_70179_y; if (mX < -maxVel) mX = -maxVel; if (mY < -maxVel) mY = -maxVel; if (mZ < -maxVel) mZ = -maxVel; if (mX > maxVel) mX = maxVel; if (mY > maxVel) mY = maxVel; if (mZ > maxVel) mZ = maxVel; dat.writeInt((int)(mX * 8000D)); dat.writeInt((int)(mY * 8000D)); dat.writeInt((int)(mZ * 8000D)); } else { dat.writeInt(0); } if (ent instanceof IEntityAdditionalSpawnData) { ((IEntityAdditionalSpawnData)ent).writeSpawnData(dat); } return dat.toByteArray(); }
@Override public Entity spawnEntityIntoClientWorld(EntityRegistration er, EntitySpawnPacket packet) { WorldClient wc = client.theWorld; Class<? extends Entity> cls = er.getEntityClass(); try { Entity entity; if (er.hasCustomSpawning()) { entity = er.doCustomSpawning(packet); } else { entity = (Entity)(cls.getConstructor(World.class).newInstance(wc)); int offset = packet.entityId - entity.entityId; entity.entityId = packet.entityId; entity.setLocationAndAngles(packet.scaledX, packet.scaledY, packet.scaledZ, packet.scaledYaw, packet.scaledPitch); if (entity instanceof EntityLiving) { ((EntityLiving)entity).rotationYawHead = packet.scaledHeadYaw; } Entity parts[] = entity.getParts(); if (parts != null) { for (int j = 0; j < parts.length; j++) { parts[j].entityId += offset; } } } entity.serverPosX = packet.rawX; entity.serverPosY = packet.rawY; entity.serverPosZ = packet.rawZ; if (entity instanceof IThrowableEntity) { Entity thrower = client.thePlayer.entityId == packet.throwerId ? client.thePlayer : wc.getEntityByID(packet.throwerId); ((IThrowableEntity)entity).setThrower(thrower); } if (packet.metadata != null) { entity.getDataWatcher().updateWatchedObjectsFromList((List)packet.metadata); } if (packet.throwerId > 0) { entity.setVelocity(packet.speedScaledX, packet.speedScaledY, packet.speedScaledZ); } if (entity instanceof IEntityAdditionalSpawnData) { ((IEntityAdditionalSpawnData)entity).readSpawnData(packet.dataStream); } wc.addEntityToWorld(packet.entityId, entity); return entity; } catch (Exception e) { FMLLog.log(Level.SEVERE, e, "A severe problem occurred during the spawning of an entity"); throw Throwables.propagate(e); } }
@Override public byte[] generatePacket(Object... data) { EntityRegistration er = (EntityRegistration) data[0]; Entity ent = (Entity) data[1]; NetworkModHandler handler = (NetworkModHandler) data[2]; ByteArrayDataOutput dat = ByteStreams.newDataOutput(); dat.writeInt(handler.getNetworkId()); dat.writeInt(er.getModEntityId()); // entity id dat.writeInt(ent.entityId); // entity pos x,y,z dat.writeInt(MathHelper.floor_double(ent.posX * 32D)); dat.writeInt(MathHelper.floor_double(ent.posY * 32D)); dat.writeInt(MathHelper.floor_double(ent.posZ * 32D)); // yaw, pitch dat.writeByte((byte) (ent.rotationYaw * 256.0F / 360.0F)); dat.writeByte((byte) (ent.rotationPitch * 256.0F / 360.0F)); // head yaw if (ent instanceof EntityLiving) { dat.writeByte((byte) (((EntityLiving)ent).rotationYawHead * 256.0F / 360.0F)); } else { dat.writeByte(0); } ByteArrayOutputStream bos = new ByteArrayOutputStream(); DataOutputStream dos = new DataOutputStream(bos); try { ent.getDataWatcher().writeWatchableObjects(dos); } catch (IOException e) { // unpossible } dat.write(bos.toByteArray()); if (ent instanceof IThrowableEntity) { Entity owner = ((IThrowableEntity)ent).getThrower(); dat.writeInt(owner == null ? ent.entityId : owner.entityId); double maxVel = 3.9D; double mX = ent.motionX; double mY = ent.motionY; double mZ = ent.motionZ; if (mX < -maxVel) mX = -maxVel; if (mY < -maxVel) mY = -maxVel; if (mZ < -maxVel) mZ = -maxVel; if (mX > maxVel) mX = maxVel; if (mY > maxVel) mY = maxVel; if (mZ > maxVel) mZ = maxVel; dat.writeInt((int)(mX * 8000D)); dat.writeInt((int)(mY * 8000D)); dat.writeInt((int)(mZ * 8000D)); } else { dat.writeInt(0); } if (ent instanceof IEntityAdditionalSpawnData) { ((IEntityAdditionalSpawnData)ent).writeSpawnData(dat); } return dat.toByteArray(); }