Solved PlayerFishEvent Problem

Discussion in 'Spigot Plugin Development' started by qKing12, Mar 26, 2020.

  1. Hi! So I have a problem with the PlayerFishEvent. I use IntelliJ and I have spigot-1.8.8 and spigot-1.13.2 as my external libraries. The problem I have is that PlayerFishEvent is not the same for these versions. event.getHook() will return FishHook on spigot-1.13.2 and Fish on spigot-1.8.8 but the IDE doesn't let me choose the event from spigot-1.8.8, it automatically imports from 1.13.2 and doesn't even see 1.8.8 as a choice.
    How could I manage to get the event from 1.8.8 library WITHOUT removing the 1.13.2 one? Or any way to avoid "Caused by: java.lang.NoSuchMethodError: org.bukkit.event.player.PlayerFishEvent.getHook()Lorg/bukkit/entity/FishHook;"?
  2. Choco


    Well first of all, why are you depending on two versions years apart from one another? Second, you're not going to resolve this issue without reflection or abstraction of some sort. You're intentionally making your life more difficult by attempting to support more than one version and this is one of the more clear examples. Support the latest version and you wouldn't have this issue.
    • Agree Agree x 1
  3. Hi! Thanks for answering, I understand what you say but I really want compatibility with more versions. I thought about using reflection but I don't know how would I use it? The class name is the same ("org.bukkit.event.player.PlayerFishEvent") in both versions. And as for abstraction, even if I try to make it for 1.8.8 only it just doesn't detect the 1.8.8 class.
  4. Just use the latest or later versions and if you want to support 1.8 than you gotta recode the project.
  5. Choco


    The issue is that you shouldn't have to support any version below what makes it an inconvenience for you to support. This, to me, screams massive inconvenience. If you really want to use reflection for this, you'll have to fetch the method with PlayerFishEvent.class.getMethod("getHook");, invoke it with theMethod.invoke(theEventInstance); and assign it to an Object instead. Cast it depending on the version running on the server. Cache your reflection to avoid method lookups every time.
    • Agree Agree x 2
    • Useful Useful x 1
  6. Got it, thanks!