From 5c185476dd0eaad6bdfe7cd7f755d09ce523ea66 Mon Sep 17 00:00:00 2001 From: Jules Date: Sat, 30 Sep 2023 15:28:37 +0200 Subject: [PATCH] Enter/quit combat skill triggers --- .../java/net/Indyuce/mmocore/MMOCore.java | 2 ++ .../Indyuce/mmocore/player/CombatHandler.java | 10 ++++---- .../skill/trigger/MMOCoreTriggerType.java | 23 +++++++++++++++++++ .../net/Indyuce/mmocore/MMOCoreBukkit.java | 1 + .../listener/MMOCoreSkillTriggers.java | 18 +++++++++++++++ 5 files changed, 49 insertions(+), 5 deletions(-) create mode 100644 MMOCore-API/src/main/java/net/Indyuce/mmocore/skill/trigger/MMOCoreTriggerType.java create mode 100644 MMOCore-Dist/src/main/java/net/Indyuce/mmocore/listener/MMOCoreSkillTriggers.java diff --git a/MMOCore-API/src/main/java/net/Indyuce/mmocore/MMOCore.java b/MMOCore-API/src/main/java/net/Indyuce/mmocore/MMOCore.java index 1bf33cc0..ea236095 100644 --- a/MMOCore-API/src/main/java/net/Indyuce/mmocore/MMOCore.java +++ b/MMOCore-API/src/main/java/net/Indyuce/mmocore/MMOCore.java @@ -49,6 +49,7 @@ import net.Indyuce.mmocore.script.mechanic.ManaMechanic; import net.Indyuce.mmocore.script.mechanic.StaminaMechanic; import net.Indyuce.mmocore.script.mechanic.StelliumMechanic; import net.Indyuce.mmocore.skill.cast.SkillCastingMode; +import net.Indyuce.mmocore.skill.trigger.MMOCoreTriggerType; import org.apache.commons.lang.Validate; import org.bukkit.Bukkit; import org.bukkit.ChatColor; @@ -116,6 +117,7 @@ public class MMOCore extends JavaPlugin { MythicLib.plugin.getEntities().registerRelationHandler(new PartyRelationHandler()); MythicLib.plugin.getEntities().registerRelationHandler(new GuildRelationHandler()); MythicLib.plugin.getModifiers().registerModifierType("attribute", configObject -> new AttributeModifier(configObject)); + MMOCoreTriggerType.registerAll(); // Custom scripts MythicLib.plugin.getSkills().registerMechanic("mana", config -> new ManaMechanic(config)); diff --git a/MMOCore-API/src/main/java/net/Indyuce/mmocore/player/CombatHandler.java b/MMOCore-API/src/main/java/net/Indyuce/mmocore/player/CombatHandler.java index 5f4ce07b..bb0ab12b 100644 --- a/MMOCore-API/src/main/java/net/Indyuce/mmocore/player/CombatHandler.java +++ b/MMOCore-API/src/main/java/net/Indyuce/mmocore/player/CombatHandler.java @@ -8,6 +8,7 @@ import net.Indyuce.mmocore.command.PvpModeCommand; import org.apache.commons.lang.Validate; import org.bukkit.Bukkit; import org.bukkit.scheduler.BukkitTask; +import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; public class CombatHandler implements Closeable { @@ -30,7 +31,7 @@ public class CombatHandler implements Closeable { // Simply refreshing if (isInCombat()) { - Bukkit.getScheduler().cancelTask(task.getTaskId()); + task.cancel(); task = newTask(); // Entering combat @@ -42,6 +43,7 @@ public class CombatHandler implements Closeable { } } + @NotNull private BukkitTask newTask() { return Bukkit.getScheduler().runTaskLater(MMOCore.plugin, () -> quit(false), MMOCore.plugin.configManager.combatLogTimer / 50); } @@ -102,8 +104,7 @@ public class CombatHandler implements Closeable { */ private void quit(boolean cancelTask) { Validate.isTrue(isInCombat(), "Player not in combat"); - if (cancelTask) - Bukkit.getScheduler().cancelTask(task.getTaskId()); + if (cancelTask) task.cancel(); task = null; if (player.isOnline()) { @@ -114,8 +115,7 @@ public class CombatHandler implements Closeable { @Override public void close() { - if (isInCombat()) - quit(true); + if (isInCombat()) quit(true); // Necessary steps when entering a town. lastHit = 0; diff --git a/MMOCore-API/src/main/java/net/Indyuce/mmocore/skill/trigger/MMOCoreTriggerType.java b/MMOCore-API/src/main/java/net/Indyuce/mmocore/skill/trigger/MMOCoreTriggerType.java new file mode 100644 index 00000000..e229cb1e --- /dev/null +++ b/MMOCore-API/src/main/java/net/Indyuce/mmocore/skill/trigger/MMOCoreTriggerType.java @@ -0,0 +1,23 @@ +package net.Indyuce.mmocore.skill.trigger; + +import io.lumine.mythic.lib.skill.trigger.TriggerType; +import org.jetbrains.annotations.NotNull; + +public class MMOCoreTriggerType { + + /** + * Called when a player enters combat + */ + @NotNull + public static TriggerType ENTER_COMBAT = new TriggerType("ENTER_COMBAT"), + + /** + * Called when a player quits combat + */ + QUIT_COMBAT = new TriggerType("QUIT_COMBAT"); + + public static void registerAll() { + TriggerType.register(ENTER_COMBAT); + TriggerType.register(QUIT_COMBAT); + } +} diff --git a/MMOCore-Dist/src/main/java/net/Indyuce/mmocore/MMOCoreBukkit.java b/MMOCore-Dist/src/main/java/net/Indyuce/mmocore/MMOCoreBukkit.java index 3ceaf7d4..6c181f55 100644 --- a/MMOCore-Dist/src/main/java/net/Indyuce/mmocore/MMOCoreBukkit.java +++ b/MMOCore-Dist/src/main/java/net/Indyuce/mmocore/MMOCoreBukkit.java @@ -43,6 +43,7 @@ public class MMOCoreBukkit { Bukkit.getPluginManager().registerEvents(new FishingListener(), plugin); Bukkit.getPluginManager().registerEvents(new PlayerCollectStats(), plugin); Bukkit.getPluginManager().registerEvents(new PlayerPressKeyListener(), plugin); + Bukkit.getPluginManager().registerEvents(new MMOCoreSkillTriggers(), plugin); // Bukkit.getPluginManager().registerEvents(new ClassTriggers(), plugin); } } diff --git a/MMOCore-Dist/src/main/java/net/Indyuce/mmocore/listener/MMOCoreSkillTriggers.java b/MMOCore-Dist/src/main/java/net/Indyuce/mmocore/listener/MMOCoreSkillTriggers.java new file mode 100644 index 00000000..29bc3c36 --- /dev/null +++ b/MMOCore-Dist/src/main/java/net/Indyuce/mmocore/listener/MMOCoreSkillTriggers.java @@ -0,0 +1,18 @@ +package net.Indyuce.mmocore.listener; + +import io.lumine.mythic.lib.api.player.MMOPlayerData; +import io.lumine.mythic.lib.skill.trigger.TriggerMetadata; +import net.Indyuce.mmocore.api.event.PlayerCombatEvent; +import net.Indyuce.mmocore.skill.trigger.MMOCoreTriggerType; +import org.bukkit.event.EventHandler; +import org.bukkit.event.EventPriority; +import org.bukkit.event.Listener; + +public class MMOCoreSkillTriggers implements Listener { + + @EventHandler(priority = EventPriority.HIGH, ignoreCancelled = true) + public void a(PlayerCombatEvent event) { + final MMOPlayerData caster = event.getData().getMMOPlayerData(); + caster.triggerSkills(new TriggerMetadata(caster, event.entersCombat() ? MMOCoreTriggerType.ENTER_COMBAT : MMOCoreTriggerType.QUIT_COMBAT)); + } +}