-
-
Notifications
You must be signed in to change notification settings - Fork 13
[codex] Fix combat runtime issues #393
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -5,34 +5,48 @@ | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| import com.eternalcode.combat.notification.NoticeService; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| import com.eternalcode.combat.region.Region; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| import com.eternalcode.combat.region.RegionProvider; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| import java.lang.reflect.InvocationTargetException; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| import java.lang.reflect.Method; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| import java.time.Duration; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| import java.util.Optional; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| import org.bukkit.Location; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| import org.bukkit.Server; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| import org.bukkit.entity.Entity; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| import org.bukkit.entity.Player; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| import org.bukkit.event.Cancellable; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| import org.bukkit.event.EventHandler; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| import org.bukkit.event.EventPriority; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| import org.bukkit.event.Listener; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| import org.bukkit.event.Event; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| import org.bukkit.event.entity.EntityEvent; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| import org.bukkit.event.player.PlayerMoveEvent; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| import org.bukkit.event.player.PlayerTeleportEvent; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| import org.bukkit.event.vehicle.VehicleMoveEvent; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| import org.spigotmc.event.entity.EntityMountEvent; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| import org.bukkit.plugin.EventExecutor; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| import org.bukkit.plugin.Plugin; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| public class KnockbackRegionController implements Listener { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| private static final String[] ENTITY_MOUNT_EVENT_CLASSES = { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| "org.bukkit.event.entity.EntityMountEvent", | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| "org.spigotmc.event.entity.EntityMountEvent" | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| }; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Comment on lines
+30
to
+33
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. To avoid performing expensive reflection lookups (
Suggested change
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| private final NoticeService noticeService; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| private final RegionProvider regionProvider; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| private final FightManager fightManager; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| private final KnockbackService knockbackService; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| private final Server server; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| private Method getMountMethod; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| public KnockbackRegionController(NoticeService noticeService, RegionProvider regionProvider, FightManager fightManager, KnockbackService knockbackService, Server server) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| public KnockbackRegionController(NoticeService noticeService, RegionProvider regionProvider, FightManager fightManager, KnockbackService knockbackService, Server server, Plugin plugin) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| this.noticeService = noticeService; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| this.regionProvider = regionProvider; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| this.fightManager = fightManager; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| this.knockbackService = knockbackService; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| this.server = server; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| this.registerEntityMountEvent(plugin); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| @EventHandler(ignoreCancelled = true, priority = EventPriority.HIGHEST) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
@@ -132,27 +146,82 @@ void onVehicleMove(VehicleMoveEvent event) { | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| @EventHandler(ignoreCancelled = true, priority = EventPriority.HIGHEST) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| void onEntityMount(EntityMountEvent event) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if (!(event.getEntity() instanceof Player player)) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| private void registerEntityMountEvent(Plugin plugin) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Optional<Class<? extends Event>> eventClass = this.findEntityMountEventClass(); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if (eventClass.isEmpty()) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| plugin.getLogger().fine("EntityMountEvent is not available. Mount region protection will be disabled."); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| return; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| try { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| this.getMountMethod = eventClass.get().getMethod("getMount"); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } catch (NoSuchMethodException exception) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| plugin.getLogger().warning("Failed to find getMount method on " + eventClass.get().getName()); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| return; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| EventExecutor executor = this::onEntityMount; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| plugin.getServer().getPluginManager().registerEvent(eventClass.get(), this, EventPriority.HIGHEST, executor, plugin, true); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Comment on lines
+149
to
+165
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Resolve and cache the private void registerEntityMountEvent(Plugin plugin) {
Optional<Class<? extends Event>> eventClass = this.findEntityMountEventClass();
if (eventClass.isEmpty()) {
plugin.getLogger().fine("EntityMountEvent is not available. Mount region protection will be disabled.");
return;
}
try {
this.getMountMethod = eventClass.get().getMethod("getMount");
} catch (NoSuchMethodException exception) {
plugin.getLogger().warning("Failed to find getMount method on " + eventClass.get().getName());
return;
}
EventExecutor executor = this::onEntityMount;
plugin.getServer().getPluginManager().registerEvent(eventClass.get(), this, EventPriority.HIGHEST, executor, plugin, true);
} |
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| private Optional<Class<? extends Event>> findEntityMountEventClass() { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| for (String eventClassName : ENTITY_MOUNT_EVENT_CLASSES) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| try { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| return Optional.of(Class.forName(eventClassName).asSubclass(Event.class)); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } catch (ClassNotFoundException ignored) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| // Try the next API package. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| return Optional.empty(); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| private void onEntityMount(Listener listener, Event event) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if (!(event instanceof EntityEvent entityEvent)) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| return; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if (!(entityEvent.getEntity() instanceof Player player)) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| return; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if (!this.fightManager.isInCombat(player.getUniqueId())) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| return; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if (!this.regionProvider.isInRegion(event.getMount().getLocation())) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Entity mount = this.getMount(event); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if (mount == null || !this.regionProvider.isInRegion(mount.getLocation())) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| return; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| event.setCancelled(true); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if (event instanceof Cancellable cancellable) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| cancellable.setCancelled(true); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| this.noticeService.create() | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| .player(player.getUniqueId()) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| .notice(config -> config.messagesSettings.cantEnterOnRegion) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| .send(); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| private Entity getMount(Event event) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if (this.getMountMethod == null) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| return null; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| try { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Object mount = this.getMountMethod.invoke(event); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if (mount instanceof Entity entity) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| return entity; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| return null; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } catch (IllegalAccessException | InvocationTargetException exception) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| return null; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Comment on lines
+207
to
+223
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Use the cached
Suggested change
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| @EventHandler(ignoreCancelled = true, priority = EventPriority.HIGHEST) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| void onTag(FightTagEvent event) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Player player = this.server.getPlayer(event.getPlayer()); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Using
player.getAttribute(Attribute.GENERIC_MAX_HEALTH).getBaseValue()retrieves the base maximum health (usually 20.0), which ignores any active modifiers such as health boost effects, absorption, or attribute modifiers from equipment. Sincelogout.health()represents the player's actual health at logout (which can exceed the base max health), this can result in incorrect percentage calculations (e.g., greater than 100%). UsegetValue()instead ofgetBaseValue(), and safely handle potential null values.