Skip to content

Commit

Permalink
Bring MythicMobs 4 support back
Browse files Browse the repository at this point in the history
Fixes #318
  • Loading branch information
Krakenied authored and LMBishop committed May 5, 2022
1 parent ec20c79 commit 759575c
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 14 deletions.
6 changes: 4 additions & 2 deletions bukkit/build.gradle
Expand Up @@ -68,8 +68,10 @@ dependencies {
// download from spigot and add to /libs/ yourself
// uSkyBlock
compileOnly 'com.github.rlf:uSkyBlock-API:2.6.4'
// MythicMobs
compileOnly 'io.lumine:Mythic-Dist:5.0.2'
// MythicMobs 4
compileOnly 'io.lumine.xikage:MythicMobs:4.12.0'
// MythicMobs 5
compileOnly 'io.lumine:Mythic-Dist:5.0.3'
// Citizens
compileOnly ('net.citizensnpcs:citizens:2.0.27-SNAPSHOT') {
exclude group: 'net.citizensnpcs', module: 'citizens-main'
Expand Down
Expand Up @@ -342,8 +342,9 @@ public void onEnable() {
taskTypeManager.registerTaskType(new CitizensInteractTaskType(this));
}
if (Bukkit.getPluginManager().isPluginEnabled("MythicMobs")) {
if (Bukkit.getPluginManager().getPlugin("MythicMobs").getDescription().getVersion().startsWith("5")) {
taskTypeManager.registerTaskType(new MythicMobsKillingType(this));
String mythicMobsVersion = Bukkit.getPluginManager().getPlugin("MythicMobs").getDescription().getVersion();
if (mythicMobsVersion.startsWith("4") || mythicMobsVersion.startsWith("5")) {
taskTypeManager.registerTaskType(new MythicMobsKillingType(this, mythicMobsVersion));
}
}
if (Bukkit.getPluginManager().isPluginEnabled("PlaceholderAPI")) {
Expand Down
Expand Up @@ -9,13 +9,15 @@
import com.leonardobishop.quests.common.player.questprogressfile.TaskProgress;
import com.leonardobishop.quests.common.quest.Quest;
import com.leonardobishop.quests.common.quest.Task;
import io.lumine.mythic.bukkit.events.MythicMobDeathEvent;
import org.bukkit.entity.Entity;
import org.bukkit.entity.LivingEntity;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.EventPriority;
import org.bukkit.event.Listener;
import org.jetbrains.annotations.NotNull;

import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
Expand All @@ -24,9 +26,26 @@ public final class MythicMobsKillingType extends BukkitTaskType {

private final BukkitQuestsPlugin plugin;

public MythicMobsKillingType(BukkitQuestsPlugin plugin) {
public MythicMobsKillingType(BukkitQuestsPlugin plugin, String mythicMobsVersion) {
super("mythicmobs_killing", TaskUtils.TASK_ATTRIBUTION_STRING, "Kill a set amount of a MythicMobs entity.");
this.plugin = plugin;

// MythicMobs 4
try {
Class.forName("io.lumine.xikage.mythicmobs.api.bukkit.events.MythicMobDeathEvent");
plugin.getServer().getPluginManager().registerEvents(new MythicMobs4Listener(), plugin);
return;
} catch (ClassNotFoundException | NoSuchFieldException ignored) { } // MythicMobs version cannot support task type

// MythicMobs 5
try {
Class.forName("io.lumine.mythic.bukkit.events.MythicMobDeathEvent");
plugin.getServer().getPluginManager().registerEvents(new MythicMobs5Listener(), plugin);
return;
} catch (ClassNotFoundException ignored) { } // MythicMobs version cannot support task type

plugin.getLogger().severe("Failed to register event handler for MythicMobs task type!");
plugin.getLogger().severe("MythicMobs version detected: " + mythicMobsVersion);
}

@Override
Expand All @@ -40,21 +59,46 @@ public MythicMobsKillingType(BukkitQuestsPlugin plugin) {
return problems;
}

@EventHandler(priority = EventPriority.MONITOR, ignoreCancelled = true)
public void onMobKill(MythicMobDeathEvent event) {
Entity killer = event.getKiller();
Entity mob = event.getEntity();
private final class MythicMobs4Listener implements Listener {

if (mob == null || mob instanceof Player) {
return;
private final Field levelField;

public MythicMobs4Listener() throws ClassNotFoundException, NoSuchFieldException {
// Fixes https://github.com/LMBishop/Quests/issues/318
levelField = Class.forName("io.lumine.xikage.mythicmobs.mobs.ActiveMob").getDeclaredField("level");
levelField.setAccessible(true);
}

@EventHandler(priority = EventPriority.MONITOR, ignoreCancelled = true)
public void onMythicMobs4MobDeath(io.lumine.xikage.mythicmobs.api.bukkit.events.MythicMobDeathEvent event) {
// Fixes https://github.com/LMBishop/Quests/issues/318
double level;
try {
level = ((Number) levelField.get(event.getMob())).doubleValue();
} catch (Exception ignored) {
// It should never happen
return;
}

handle(event.getKiller(), event.getEntity(), event.getMobType().getInternalName(), level);
}
}

private final class MythicMobs5Listener implements Listener {
@EventHandler(priority = EventPriority.MONITOR, ignoreCancelled = true)
public void onMythicMobs5MobDeath(io.lumine.mythic.bukkit.events.MythicMobDeathEvent event) {
handle(event.getKiller(), event.getEntity(), event.getMobType().getInternalName(), event.getMobLevel());
}
}

private void handle(LivingEntity killer, Entity mob, String mobName, double level) {
if (killer == null) {
return;
}

String mobName = event.getMobType().getInternalName();
double level = event.getMobLevel();
if (mob == null || mob instanceof Player) {
return;
}

QPlayer qPlayer = plugin.getPlayerManager().getPlayer(killer.getUniqueId());
if (qPlayer == null) {
Expand Down

0 comments on commit 759575c

Please sign in to comment.