-
-
Notifications
You must be signed in to change notification settings - Fork 13
Fix: Resolve EntityMountEvent registration failure on modern Paper #400
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. Weβll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
vLuckyyy
merged 2 commits into
EternalCodeTeam:master
from
YoshiroMaximus:fix/entity-mount-event-relocation
Jun 21, 2026
+132
β22
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
129 changes: 129 additions & 0 deletions
129
...plugin/src/main/java/com/eternalcode/combat/fight/knockback/KnockbackMountController.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,129 @@ | ||
| package com.eternalcode.combat.fight.knockback; | ||
|
|
||
| import com.eternalcode.combat.fight.FightManager; | ||
| import com.eternalcode.combat.notification.NoticeService; | ||
| import com.eternalcode.combat.region.RegionProvider; | ||
| import java.lang.invoke.MethodHandle; | ||
| import java.lang.invoke.MethodHandles; | ||
| import java.lang.invoke.MethodType; | ||
| import java.lang.reflect.Method; | ||
| import java.util.concurrent.atomic.AtomicBoolean; | ||
| import java.util.logging.Level; | ||
| import org.bukkit.entity.Entity; | ||
| import org.bukkit.entity.Player; | ||
| import org.bukkit.event.Cancellable; | ||
| import org.bukkit.event.Event; | ||
| import org.bukkit.event.EventPriority; | ||
| import org.bukkit.event.Listener; | ||
| import org.bukkit.event.entity.EntityEvent; | ||
| import org.bukkit.plugin.Plugin; | ||
|
|
||
| // EntityMountEvent moved between org.spigotmc, io.papermc.paper and org.bukkit across versions, | ||
| // so we resolve it at runtime to stay compatible without bumping the compile API. | ||
| public final class KnockbackMountController implements Listener { | ||
|
|
||
| private static final String[] EVENT_CLASS_CANDIDATES = { | ||
| "org.bukkit.event.entity.EntityMountEvent", | ||
| "io.papermc.paper.event.entity.EntityMountEvent", | ||
| "org.spigotmc.event.entity.EntityMountEvent", | ||
| }; | ||
|
|
||
| private final NoticeService noticeService; | ||
| private final RegionProvider regionProvider; | ||
| private final FightManager fightManager; | ||
| private final AtomicBoolean mountFailureLogged = new AtomicBoolean(); | ||
|
|
||
| public KnockbackMountController(NoticeService noticeService, RegionProvider regionProvider, FightManager fightManager) { | ||
| this.noticeService = noticeService; | ||
| this.regionProvider = regionProvider; | ||
| this.fightManager = fightManager; | ||
| } | ||
|
|
||
| public void register(Plugin plugin) { | ||
| Class<? extends Event> eventClass = null; | ||
| for (String candidate : EVENT_CLASS_CANDIDATES) { | ||
| try { | ||
| eventClass = Class.forName(candidate).asSubclass(Event.class); | ||
| break; | ||
| } catch (ClassNotFoundException | ClassCastException ignored) { | ||
| } | ||
| } | ||
|
|
||
| if (eventClass == null) { | ||
| plugin.getLogger().warning("Could not find any EntityMountEvent class; mount protection in regions is disabled."); | ||
| return; | ||
| } | ||
|
|
||
| MethodHandle getMountHandle; | ||
| try { | ||
| getMountHandle = this.resolveGetMountHandle(eventClass); | ||
| } catch (NoSuchMethodException | IllegalAccessException exception) { | ||
| plugin.getLogger().log(Level.WARNING, "EntityMountEvent has incompatible getMount() method; mount protection in regions is disabled.", exception); | ||
| return; | ||
| } | ||
|
|
||
| plugin.getServer().getPluginManager().registerEvent( | ||
| eventClass, | ||
| this, | ||
| EventPriority.HIGHEST, | ||
| (listener, event) -> this.handle(event, getMountHandle, plugin), | ||
| plugin, | ||
| true | ||
| ); | ||
| } | ||
|
|
||
| private MethodHandle resolveGetMountHandle(Class<? extends Event> eventClass) throws NoSuchMethodException, IllegalAccessException { | ||
| if (!EntityEvent.class.isAssignableFrom(eventClass) || !Cancellable.class.isAssignableFrom(eventClass)) { | ||
| throw new NoSuchMethodException(eventClass.getName() + " is not a cancellable entity event"); | ||
| } | ||
|
|
||
| Method getMount = eventClass.getMethod("getMount"); | ||
| if (!Entity.class.isAssignableFrom(getMount.getReturnType())) { | ||
| throw new NoSuchMethodException(eventClass.getName() + "#getMount() does not return Entity"); | ||
| } | ||
|
|
||
| return MethodHandles.publicLookup() | ||
| .unreflect(getMount) | ||
| .asType(MethodType.methodType(Entity.class, Event.class)); | ||
| } | ||
|
|
||
| private void handle(Event event, MethodHandle getMountHandle, Plugin plugin) { | ||
| if (!(event instanceof EntityEvent entityEvent) || !(event instanceof Cancellable cancellable)) { | ||
| return; | ||
| } | ||
|
|
||
| if (!(entityEvent.getEntity() instanceof Player player)) { | ||
| return; | ||
| } | ||
|
|
||
| if (!this.fightManager.isInCombat(player.getUniqueId())) { | ||
| return; | ||
| } | ||
|
|
||
| Entity mount; | ||
| try { | ||
| mount = (Entity) getMountHandle.invokeExact(event); | ||
| } catch (Throwable exception) { | ||
| this.logMountFailure(plugin, exception); | ||
| return; | ||
| } | ||
|
|
||
| if (mount == null || !this.regionProvider.isInRegion(mount.getLocation())) { | ||
| return; | ||
| } | ||
|
|
||
| cancellable.setCancelled(true); | ||
| this.noticeService.create() | ||
| .player(player.getUniqueId()) | ||
| .notice(config -> config.messagesSettings.cantEnterOnRegion) | ||
| .send(); | ||
| } | ||
|
|
||
| private void logMountFailure(Plugin plugin, Throwable exception) { | ||
| if (!this.mountFailureLogged.compareAndSet(false, true)) { | ||
| return; | ||
| } | ||
|
|
||
| plugin.getLogger().log(Level.WARNING, "Could not read EntityMountEvent mount; mount protection in regions is skipped.", exception); | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.