#Class cast exception in networking for yoursalvation

1 messages · Page 1 of 1 (latest)

tacit sundial
#

helpings

flat sleet
#

Perfect

upbeat token
#

hello

tacit sundial
#

Class cast exception in networking for yoursalvation

#

Okay so

flat sleet
#

Could u resend your code real quick for me?

tacit sundial
#

it seems as if whenever you're calling onReceive you're passing in an instance of ForwardedPacket instead of FindPlayerPacket

upbeat token
tacit sundial
#

FindPlayerPacket, ForwadedPacket and the Command @upbeat token

flat sleet
#

^ (again)

tacit sundial
#

can you print what packet is when onReceive is called

upbeat token
#

okay

tacit sundial
#

Actually

#

where are you calling onReceive

flat sleet
#

Replace Find player packet with ForwardedPacket

tacit sundial
#

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

flat sleet
#

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

tacit sundial
#

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

flat sleet
#

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

tacit sundial
#

I mean either that or cast in the listener

tacit sundial
#

but that tightly couples the listener to the ForwardedRedisPacket

#

You can, instead, remove the generic from the onReceive and do as follows

flat sleet
#

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 🤞

upbeat token
#

okay thank you

tacit sundial
#
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
#

yes?

#

i know understood the issue

tacit sundial
#

That code should fix your issue

upbeat token
#

im trying to find a solution that doesnt involve instanceof

tacit sundial
#

You need the instanceof only there

#

otherwise the rest of your code stays the same

upbeat token
#

im pretty sure there is a way without instanceof

#

but thank you :)

tacit sundial
#

There isn't

upbeat token
#

without you two i would have been stuck

tacit sundial
#

You need that one instanceof there

upbeat token
#

pretty sure taht will work

#

or am i wrong?

tacit sundial
#

uhhh

#

Yeah no if the received packet is not a ForwardedRedisPacket then that will crash

#

that is why I used the instanceof

upbeat token
#

it wont crash

#

the receiver takes everything

#

it doesnt care

tacit sundial
#

It will crash

#

The compiler says it's fine, but it might not be at runtime

#

also why are you so against one instanceof

upbeat token
#

because then i would need to write this boilerplate code for every implementation

tacit sundial
#

no

#

You wouldn't

#

You're just checking there when receiving the packet if it is a ForwardedRedisPacket, and ignoring all other received packets if the receiver is a ForwardedRedisPacket