And when it's not cannot cast its null error.
Hi,
So I have this packet, it activates when the player uses mouse over and clicks on a living entity to send that Entitys UUID as two longs (the most and least important bits.) find the entity by comparing the remade UUID from the entitys on the getLoadedEntityList, then make that entity float aronud the player, basically the same as mounting.
Here's the code:
public class CAPacket implements cpw.mods.fml.common.network.simpleimpl.IMessage {
public static class CAHandler implements IMessageHandler<CAPacket, IMessage> {
@Override public IMessage onMessage(CAPacket msg, MessageContext ctx) {
// This is the player the packet was sent to the server from
EntityPlayerMP serverPlayer = ctx.getServerHandler().playerEntity;
// The value that was sent
long M = msg.M;
long L = msg.L;
UUID U = new UUID(M, L);
EntityLivingBase Q = null;
List allPlayers = serverPlayer.worldObj.getLoadedEntityList();
Iterator itr = allPlayers.iterator();
while(itr.hasNext()) {
if(itr.next() instanceof EntityLivingBase){
EntityLivingBase G = (EntityLivingBase) itr.next();
if (G.getUniqueID().compareTo(U) == 0)
{
Q = G;
}}
}
DeistanceHelper D = DeistanceHelper.GetEffect(serverPlayer, serverPlayer, serverPlayer.rotationYaw, -3, + 9);
Q.setVelocity(serverPlayer.posX, serverPlayer.posY, serverPlayer.posZ);
Q.motionX = serverPlayer.motionX;
Q.motionY = serverPlayer.motionY;
Q.motionZ = serverPlayer.motionZ;
Q.prevPosX = D.prevPosX();
Q.lastTickPosX = D.lastTickPosX();
Q.posX = D.posX();
Q.prevPosY = serverPlayer.prevPosY + 11;
Q.lastTickPosY = serverPlayer.lastTickPosY + 11;
Q.posY = serverPlayer.posY + 11;
Q.prevPosZ = D.prevPosZ();
Q.lastTickPosZ = D.lastTickPosZ();
Q.posZ = D.posZ();
// No response packet
return null;
}
}
}
the cannot cast error happens on EntityLivingBase G = (EntityLivingBase) itr.next(); and I don't even understand why since i put in that instaceof checker there.
When it's null all the Qs in the part of the code that makes the entity float aronud the player are null, so I guess that means their was no UUID match found on the getLoadedEntityList list. witch I don't understand either.
Thanks in advance, if I need to provide any more info I can, I'm not to sure what I should say.
Use Entity#getEntityId() and World#getEntityByID(int) instead of looking them up by UUID. For one, UUIDs may not even be synced between the server and client, but the entity ID is. Jabelar has an example/tutorial here.
Second, assign itr.next() to a value before using or checking it at all. Calling it twice does NOT return the same value each time! You check if itr.next() is instanceof EntityLivingBase, then cast the NEXT entity in the list to EntityLivingBase. See the problem?
Rollback Post to RevisionRollBack
Click this banner for a list of illegal mod distributors -- only download from legal sites!
Alright! it stopped crashing, but now nothing happens? XD I'm to tired to think this through.
Here's the updated code if it'll help. I'll edit this if my brain wakes up enough to come up with something. again if more info is needed just ask ask XD
public class CAPacket implements cpw.mods.fml.common.network.simpleimpl.IMessage {
private int M;
@Override
public void fromBytes(ByteBuf buf) {
this.M = buf.readInt();
}
@Override
public void toBytes(ByteBuf buf) {
buf.writeInt(this.M);
}
public static class CAHandler implements IMessageHandler<CAPacket, IMessage> {
@Override public IMessage onMessage(CAPacket msg, MessageContext ctx) {
// This is the player the packet was sent to the server from
EntityPlayerMP serverPlayer = ctx.getServerHandler().playerEntity;
// The value that was sent
int M = msg.M;
Entity Q = serverPlayer.worldObj.getEntityByID(M);
Motion (and possibly position?) is actually mostly a client-side thing. It's possible that the values ARE being set correctly, but only server-side, and so your client doesn't show any difference.
I suggest adding printouts after you send that message (both on client and server) so you know more about what's going on.
Rollback Post to RevisionRollBack
Click this banner for a list of illegal mod distributors -- only download from legal sites!
Motion (and possibly position?) is actually mostly a client-side thing.
Yeah I can confirm this. I made something not long ago which was an item that created an entity which looked like a "snapshot" of the player. I found that almost all of the positioning of the entity's body and limbs were only handled on the client side, so I had to create a packet which sent all the data to the client from the server.
You can have a look at it at the classes below: Item Packets
As you can see, I send a packet from the client to the server (since the server doesn't have the player texture). The entity is then spawned on the server, and a packet is sent to clients to update that entity's position and everything, since it's only client side.
weird. I was handling it with the PlayerTickEvent event but it was jittering back and forth so I assumed I had to set it on the server. maybe I'll try doing both? If that doesn't work I'll try what bright spark said.
I mean, I've seen entites do that when like lastTickPos isn't set right but I think I got it all in the right spot....
OH MY GOOOOOD DX
FIGURED IT OUT. Sigghhhhhhhhh.
apparently PlayerTickEvent evnt is pretty un reliable. I just moved the code to RenderTickEvent event and it worked, didn't even need the packet it just works now. ARGHHHHHHHHh I spent to much time on that. But I did learn alot about packets an entity Ids and I will eventually need those so thanks
Case in point, while I have you bright spark, thanks for the info about Most important and Least importnt UUID bits, they ended up coming in handy
OH MY GOOOOOD DX
FIGURED IT OUT. Sigghhhhhhhhh.
apparently PlayerTickEvent evnt is pretty un reliable. I just moved the code to RenderTickEvent event and it worked, didn't even need the packet it just works now.
Hang on, isn't RenderTickEvent client side only? And if you're not sending any packets, that means all you're doing is moving the client side entity around... Not the server side one. This won't work properly.
Yes, it's best to test things on a server environment too. That's the best way to know for sure that your client/server sided stuff is all working as expected. If you're moving an entity only on the client side, and you're on a server, the entity shouldn't actually move because the client syncs down data from the server. So changes need to happen on the server.
And when it's not cannot cast its null error.
Hi,
So I have this packet, it activates when the player uses mouse over and clicks on a living entity to send that Entitys UUID as two longs (the most and least important bits.) find the entity by comparing the remade UUID from the entitys on the getLoadedEntityList, then make that entity float aronud the player, basically the same as mounting.
Here's the code:
public class CAPacket implements cpw.mods.fml.common.network.simpleimpl.IMessage {
private long M;
private Long L;
@Override
public void fromBytes(ByteBuf buf) {
this.M = buf.readLong();
this.L = buf.readLong();
}
@Override
public void toBytes(ByteBuf buf) {
buf.writeLong(this.M);
buf.writeLong(this.L);
}
public CAPacket() {}
public CAPacket(long M, Long L) {
this.M = M;
this.L = L;
}
// =========================================================================
public static class CAHandler implements IMessageHandler<CAPacket, IMessage> {
@Override public IMessage onMessage(CAPacket msg, MessageContext ctx) {
// This is the player the packet was sent to the server from
EntityPlayerMP serverPlayer = ctx.getServerHandler().playerEntity;
// The value that was sent
long M = msg.M;
long L = msg.L;
UUID U = new UUID(M, L);
EntityLivingBase Q = null;
List allPlayers = serverPlayer.worldObj.getLoadedEntityList();
Iterator itr = allPlayers.iterator();
while(itr.hasNext()) {
if(itr.next() instanceof EntityLivingBase){
EntityLivingBase G = (EntityLivingBase) itr.next();
if (G.getUniqueID().compareTo(U) == 0)
{
Q = G;
}}
}
DeistanceHelper D = DeistanceHelper.GetEffect(serverPlayer, serverPlayer, serverPlayer.rotationYaw, -3, + 9);
Q.setVelocity(serverPlayer.posX, serverPlayer.posY, serverPlayer.posZ);
Q.motionX = serverPlayer.motionX;
Q.motionY = serverPlayer.motionY;
Q.motionZ = serverPlayer.motionZ;
Q.prevPosX = D.prevPosX();
Q.lastTickPosX = D.lastTickPosX();
Q.posX = D.posX();
Q.prevPosY = serverPlayer.prevPosY + 11;
Q.lastTickPosY = serverPlayer.lastTickPosY + 11;
Q.posY = serverPlayer.posY + 11;
Q.prevPosZ = D.prevPosZ();
Q.lastTickPosZ = D.lastTickPosZ();
Q.posZ = D.posZ();
// No response packet
return null;
}
}
}
the cannot cast error happens on EntityLivingBase G = (EntityLivingBase) itr.next(); and I don't even understand why since i put in that instaceof checker there.
When it's null all the Qs in the part of the code that makes the entity float aronud the player are null, so I guess that means their was no UUID match found on the getLoadedEntityList list. witch I don't understand either.
Thanks in advance, if I need to provide any more info I can, I'm not to sure what I should say.
Use Entity#getEntityId() and World#getEntityByID(int) instead of looking them up by UUID. For one, UUIDs may not even be synced between the server and client, but the entity ID is. Jabelar has an example/tutorial here.
Second, assign itr.next() to a value before using or checking it at all. Calling it twice does NOT return the same value each time! You check if itr.next() is instanceof EntityLivingBase, then cast the NEXT entity in the list to EntityLivingBase. See the problem?
Yeah I see the problem XD Ok I'll go try that.
Alright! it stopped crashing, but now nothing happens? XD I'm to tired to think this through.
Here's the updated code if it'll help. I'll edit this if my brain wakes up enough to come up with something. again if more info is needed just ask ask XD
public class CAPacket implements cpw.mods.fml.common.network.simpleimpl.IMessage {
private int M;
@Override
public void fromBytes(ByteBuf buf) {
this.M = buf.readInt();
}
@Override
public void toBytes(ByteBuf buf) {
buf.writeInt(this.M);
}
public CAPacket() {}
public CAPacket(int M) {
this.M = M;
}
// =========================================================================
public static class CAHandler implements IMessageHandler<CAPacket, IMessage> {
@Override public IMessage onMessage(CAPacket msg, MessageContext ctx) {
// This is the player the packet was sent to the server from
EntityPlayerMP serverPlayer = ctx.getServerHandler().playerEntity;
// The value that was sent
int M = msg.M;
Entity Q = serverPlayer.worldObj.getEntityByID(M);
DeistanceHelper D = DeistanceHelper.GetEffect(serverPlayer, serverPlayer, serverPlayer.rotationYaw, -3, + 9);
Q.setVelocity(serverPlayer.posX, serverPlayer.posY, serverPlayer.posZ);
Q.motionX = serverPlayer.motionX;
Q.motionY = serverPlayer.motionY;
Q.motionZ = serverPlayer.motionZ;
Q.prevPosX = D.prevPosX();
Q.lastTickPosX = D.lastTickPosX();
Q.posX = D.posX();
Q.prevPosY = serverPlayer.prevPosY + 11;
Q.lastTickPosY = serverPlayer.lastTickPosY + 11;
Q.posY = serverPlayer.posY + 11;
Q.prevPosZ = D.prevPosZ();
Q.lastTickPosZ = D.lastTickPosZ();
Q.posZ = D.posZ();
// No response packet
return null;
}
}
}
Motion (and possibly position?) is actually mostly a client-side thing. It's possible that the values ARE being set correctly, but only server-side, and so your client doesn't show any difference.
I suggest adding printouts after you send that message (both on client and server) so you know more about what's going on.
Yeah I can confirm this. I made something not long ago which was an item that created an entity which looked like a "snapshot" of the player. I found that almost all of the positioning of the entity's body and limbs were only handled on the client side, so I had to create a packet which sent all the data to the client from the server.
You can have a look at it at the classes below:
Item
Packets
As you can see, I send a packet from the client to the server (since the server doesn't have the player texture). The entity is then spawned on the server, and a packet is sent to clients to update that entity's position and everything, since it's only client side.
weird. I was handling it with the PlayerTickEvent event but it was jittering back and forth so I assumed I had to set it on the server. maybe I'll try doing both? If that doesn't work I'll try what bright spark said.
I mean, I've seen entites do that when like lastTickPos isn't set right but I think I got it all in the right spot....
OH MY GOOOOOD DX
FIGURED IT OUT. Sigghhhhhhhhh.
apparently PlayerTickEvent evnt is pretty un reliable. I just moved the code to RenderTickEvent event and it worked, didn't even need the packet it just works now. ARGHHHHHHHHh I spent to much time on that. But I did learn alot about packets an entity Ids and I will eventually need those so thanks
Case in point, while I have you bright spark, thanks for the info about Most important and Least importnt UUID bits, they ended up coming in handy
Hang on, isn't RenderTickEvent client side only? And if you're not sending any packets, that means all you're doing is moving the client side entity around... Not the server side one. This won't work properly.
But that's okay, glad I'm helping
If that's right I will need the packet XD
Glad I didn't dlelete it
Yes, it's best to test things on a server environment too. That's the best way to know for sure that your client/server sided stuff is all working as expected. If you're moving an entity only on the client side, and you're on a server, the entity shouldn't actually move because the client syncs down data from the server. So changes need to happen on the server.
makes sense