#Class cast exception in networking for yoursalvation
1 messages · Page 1 of 1 (latest)
Perfect
hello
Could u resend your code real quick for me?
it seems as if whenever you're calling onReceive you're passing in an instance of ForwardedPacket instead of FindPlayerPacket
which parts?
^
FindPlayerPacket, ForwadedPacket and the Command @upbeat token
^ (again)
can you print what packet is when onReceive is called
okay
Replace Find player packet with ForwardedPacket
Ah no I know, when a packet is received you're passing in that packet to onReceive, but you're receiving ForwardedRedisPacket and you actually want the packet inside of that
you need to check if the received packet is an instance of ForwardedRedisPacket and if it is use the packet inside of that
i.e. before calling onReceive
The issue is that you call redis publish with a forwarded packet, which then in turn calls all onReceive's which in your case is only accepting FindPlayerPacket. The generics seem a bit messed up
Feels like typescript
LOL
fr
@upbeat token basically
you send a ForwardedRedisPacket
you receive a ForwardedRedisPacket and try to pass it to the original ForwardedReidsPacket, which is only accepting FindPlayerPacket
in the case you receive a ForwardedRedisPacket you want to pass to onReceive the packet inside the ForwardedRedisPacket
You can also just accept RedisPacket<?> If no something else doesn't work because of generics, would be the easiest fix. Then just cast to a ForwardedPacket if applicable
I mean either that or cast in the listener
+1
but that tightly couples the listener to the ForwardedRedisPacket
You can, instead, remove the generic from the onReceive and do as follows
It's a bit confusing when multiple people give multiple answers to your problem, so just listen to @tacit sundial and you're set
Good luck 🤞
okay thank you
public void onReceive(RedisPacket<?> packet) {
if(!(packet instanceof ForwardedRedisPacket<?>)) return;
ForwardedRedisPacket<?> forwarded = (ForwardedRedisPacket<?>) packet;
if(this.packet.getClass() != forwarded.packet.getClass()) return; // Wrong packet, probably handle the error
this.packet.onReceive((P) forwarded.packet);
this.processData((P) forwarded.packet);
}
Actually wait
Tight coupling should be avoided
@upbeat token
That code should fix your issue
im trying to find a solution that doesnt involve instanceof
There isn't
without you two i would have been stuck
You need that one instanceof there
uhhh
Yeah no if the received packet is not a ForwardedRedisPacket then that will crash
that is why I used the instanceof
It will crash
The compiler says it's fine, but it might not be at runtime
also why are you so against one instanceof
because then i would need to write this boilerplate code for every implementation