Exception handling when hooking onto dependency

This commit is contained in:
Jules 2024-11-03 22:33:06 +01:00
parent 3cb01e5bc1
commit 1de6d16e12
6 changed files with 70 additions and 61 deletions

View File

@ -124,11 +124,11 @@ public class MMOItems extends MMOPlugin {
PluginUtils.isDependencyPresent("MMOCore", u -> new MMOCoreMMOLoader());
PluginUtils.isDependencyPresent("mcMMO", u -> statManager.register(McMMOHook.disableMcMMORepair));
PluginUtils.hookDependencyIfPresent("AdvancedEnchantments", u -> {
PluginUtils.hookDependencyIfPresent("AdvancedEnchantments", true, u -> {
statManager.register(AdvancedEnchantmentsHook.ADVANCED_ENCHANTMENTS);
statManager.register(AdvancedEnchantmentsHook.DISABLE_ADVANCED_ENCHANTMENTS);
});
PluginUtils.hookDependencyIfPresent("MythicEnchants", u -> enchantPlugins.add(new MythicEnchantsSupport()));
PluginUtils.hookDependencyIfPresent("MythicEnchants", true, plugin -> enchantPlugins.add(new MythicEnchantsSupport(plugin)));
PluginUtils.isDependencyPresent("Heroes", u -> {
statManager.register(HeroesHook.MAX_STAMINA);
statManager.register(HeroesHook.REQUIRED_SECONDARY_HERO_LEVEL);
@ -155,12 +155,12 @@ public class MMOItems extends MMOPlugin {
}
// registering here so the stats will load with the templates
PluginUtils.hookDependencyIfPresent("MythicMobs", unused -> {
PluginUtils.hookDependencyIfPresent("MythicMobs", true, unused -> {
new MythicMobsCompatibility();
if (getConfig().getBoolean("lootsplosion.enabled"))
Bukkit.getPluginManager().registerEvents(new LootsplosionListener(), this);
});
PluginUtils.hookDependencyIfPresent("MMOInventory", unused -> new MMOInventorySupport());
PluginUtils.hookDependencyIfPresent("MMOInventory", true, unused -> new MMOInventorySupport());
// This needs to be before modifier registration (MMOCore)
findRpgPlugins();
@ -181,7 +181,7 @@ public class MMOItems extends MMOPlugin {
blockManager = new BlockManager();
statManager.reload(false);
PluginUtils.hookDependencyIfPresent("Vault", u -> vaultSupport = new VaultSupport());
PluginUtils.hookDependencyIfPresent("Vault", true, u -> vaultSupport = new VaultSupport());
getLogger().log(Level.INFO, "Loading crafting stations, please wait..");
layoutManager.reload();
@ -216,27 +216,24 @@ public class MMOItems extends MMOPlugin {
* of items to search for when doing inventory updates.
*/
getInventory().register(new DefaultPlayerInventory());
PluginUtils.hookDependencyIfPresent("RPGInventory", unused -> getInventory().register(new RPGInventoryHook()));
PluginUtils.hookDependencyIfPresent("RPGInventory", true, unused -> getInventory().register(new RPGInventoryHook()));
if (MMOItems.plugin.getConfig().getBoolean("iterate-whole-inventory"))
getInventory().register(new OrnamentPlayerInventory());
PluginUtils.hookDependencyIfPresent("CrazyEnchantments", unused -> getStats().register(new CrazyEnchantsStat()));
PluginUtils.hookDependencyIfPresent("AdvancedEnchantments", unused -> Bukkit.getPluginManager().registerEvents(new AdvancedEnchantmentsHook(), this));
PluginUtils.hookDependencyIfPresent("PlaceholderAPI", unused -> new MMOItemsPlaceholders().register());
PluginUtils.hookDependencyIfPresent("CrazyEnchantments", true, unused -> getStats().register(new CrazyEnchantsStat()));
PluginUtils.hookDependencyIfPresent("AdvancedEnchantments", true, plugin -> Bukkit.getPluginManager().registerEvents(new AdvancedEnchantmentsHook(), this));
PluginUtils.hookDependencyIfPresent("PlaceholderAPI", true, unused -> new MMOItemsPlaceholders().register());
if (Bukkit.getPluginManager().getPlugin("BossShopPro") != null) {
getLogger().log(Level.INFO, "Hooked onto BossShopPro");
(new BukkitRunnable() {
public void run() {
//noinspection ProhibitedExceptionCaught
try {
new MMOItemsRewardTypes().register();
} catch (NullPointerException ignored) {
getLogger().log(Level.SEVERE, "Could not Hook onto BossShopPro");
}
PluginUtils.hookDependencyIfPresent("BossShopPro", true, plugin -> new BukkitRunnable() {
public void run() {
//noinspection ProhibitedExceptionCaught
try {
new MMOItemsRewardTypes().register();
} catch (NullPointerException ignored) {
getLogger().log(Level.SEVERE, "Could not Hook onto BossShopPro");
}
}).runTaskLater(this, 1L);
}
}
}.runTaskLater(this, 1L));
/*if (Bukkit.getPluginManager().getPlugin("Denizen") != null) {
new DenizenHook();
@ -411,10 +408,10 @@ public class MMOItems extends MMOPlugin {
*
* @param value The player inventory subclass
* @deprecated Rather than setting this to the only inventory MMOItems will
* search equipment within, you must add your inventory to the
* handler with <code>getInventory().register()</code>. This method
* will clear all other PlayerInventories for now, as to keep
* backwards compatibility.
* search equipment within, you must add your inventory to the
* handler with <code>getInventory().register()</code>. This method
* will clear all other PlayerInventories for now, as to keep
* backwards compatibility.
*/
@Deprecated
public void setPlayerInventory(PlayerInventory value) {
@ -524,9 +521,9 @@ public class MMOItems extends MMOPlugin {
/**
* @return Generates an item given an item template. The item level will
* scale according to the player RPG level if the template has the
* 'level-item' option. The item will pick a random tier if the
* template has the 'tiered' option
* scale according to the player RPG level if the template has the
* 'level-item' option. The item will pick a random tier if the
* template has the 'tiered' option
*/
@Nullable
public MMOItem getMMOItem(@Nullable Type type, @Nullable String id, @Nullable PlayerData player) {
@ -544,9 +541,9 @@ public class MMOItems extends MMOPlugin {
/**
* @return Generates an item given an item template. The item level will
* scale according to the player RPG level if the template has the
* 'level-item' option. The item will pick a random tier if the
* template has the 'tiered' option
* scale according to the player RPG level if the template has the
* 'level-item' option. The item will pick a random tier if the
* template has the 'tiered' option
*/
@Nullable
public ItemStack getItem(@Nullable Type type, @Nullable String id, @NotNull PlayerData player) {
@ -563,7 +560,7 @@ public class MMOItems extends MMOPlugin {
* @param itemLevel The desired item level
* @param itemTier The desired item tier, can be null
* @return Generates an item given an item template with a
* specific item level and item tier
* specific item level and item tier
*/
@Nullable
public MMOItem getMMOItem(@Nullable Type type, @Nullable String id, int itemLevel, @Nullable ItemTier itemTier) {
@ -583,7 +580,7 @@ public class MMOItems extends MMOPlugin {
* @param itemLevel The desired item level
* @param itemTier The desired item tier, can be null
* @return Generates an item given an item template with a
* specific item level and item tier
* specific item level and item tier
*/
@Nullable
public ItemStack getItem(@Nullable Type type, @Nullable String id, int itemLevel, @Nullable ItemTier itemTier) {
@ -598,10 +595,10 @@ public class MMOItems extends MMOPlugin {
/**
* @return Generates an item given an item template. The item level will be
* 0 and the item will have no item tier unless one is specified in
* the base item data.
* <p></p>
* Will return <code>null</code> if such MMOItem does not exist.
* 0 and the item will have no item tier unless one is specified in
* the base item data.
* <p></p>
* Will return <code>null</code> if such MMOItem does not exist.
*/
@Nullable
public MMOItem getMMOItem(@Nullable Type type, @Nullable String id) {
@ -610,10 +607,10 @@ public class MMOItems extends MMOPlugin {
/**
* @return Generates an item given an item template. The item level will be
* 0 and the item will have no item tier unless one is specified in
* the base item data.
* <p></p>
* Will return <code>null</code> if such MMOItem does not exist.
* 0 and the item will have no item tier unless one is specified in
* the base item data.
* <p></p>
* Will return <code>null</code> if such MMOItem does not exist.
*/
@Nullable
@ -626,10 +623,10 @@ public class MMOItems extends MMOPlugin {
/**
* @return Generates an item given an item template. The item level will be
* 0 and the item will have no item tier unless one is specified in
* the base item data.
* <p></p>
* Will return <code>null</code> if such MMOItem does not exist.
* 0 and the item will have no item tier unless one is specified in
* the base item data.
* <p></p>
* Will return <code>null</code> if such MMOItem does not exist.
*/
@Nullable
public ItemStack getItem(@Nullable Type type, @Nullable String id) {

View File

@ -10,6 +10,7 @@ import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.meta.ItemMeta;
import org.bukkit.plugin.Plugin;
/**
* Its absolute <b><i><u>pain</u></i></b> that PhatLoots keeps giving outdated
@ -18,6 +19,9 @@ import org.bukkit.inventory.meta.ItemMeta;
*/
public class PhatLootsHook implements Listener {
public PhatLootsHook(Plugin pl) {
}
@EventHandler
public void OnLootBeLooted(MobDropLootEvent event) { handle(event);}

View File

@ -4,14 +4,18 @@ import io.lumine.mythicenchants.MythicEnchants;
import io.lumine.mythicenchants.enchants.MythicEnchant;
import io.lumine.mythicenchants.util.LoreParser;
import net.Indyuce.mmoitems.api.item.build.ItemStackBuilder;
import org.bukkit.Bukkit;
import org.bukkit.enchantments.Enchantment;
import org.bukkit.inventory.ItemFlag;
import org.bukkit.plugin.Plugin;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
public class MythicEnchantsSupport implements EnchantPlugin<MythicEnchant> {
private final MythicEnchants manager = (MythicEnchants) Bukkit.getPluginManager().getPlugin("MythicEnchants");
private final MythicEnchants manager;
public MythicEnchantsSupport(Plugin plugin) {
manager = (MythicEnchants) plugin;
}
@NotNull
@Override

View File

@ -18,11 +18,10 @@ import net.Indyuce.mmoitems.api.crafting.ingredient.inventory.PlayerIngredient;
import net.Indyuce.mmoitems.api.crafting.ingredient.inventory.VanillaPlayerIngredient;
import net.Indyuce.mmoitems.api.crafting.trigger.*;
import net.Indyuce.mmoitems.comp.mythicmobs.crafting.MythicMobsSkillTrigger;
import org.bukkit.Bukkit;
import net.Indyuce.mmoitems.util.PluginUtils;
import org.bukkit.configuration.file.YamlConfiguration;
import org.jetbrains.annotations.NotNull;
import javax.annotation.Nullable;
import java.io.File;
import java.util.*;
import java.util.function.Function;
@ -64,12 +63,12 @@ public class CraftingManager implements Reloadable {
registerIngredient("mmoitem", MMOItemIngredient::new, new ConditionalDisplay("&8" + AltChar.check + " &7#amount# #level##item#", "&c" + AltChar.cross + " &7#amount# #level##item#"), NBTItem::hasType, MMOItemPlayerIngredient::new);
// MythicMobs native compatibility
if (Bukkit.getPluginManager().getPlugin("MythicMobs") != null) {
PluginUtils.isDependencyPresent("MythicMobs", unused -> {
/*registerIngredient("mythicitem", MythicItemIngredient::new,
new ConditionalDisplay("&8" + AltChar.check + " &7#amount# #item#", "&c" + AltChar.cross + " &7#amount# #item#"),
nbt -> nbt.hasTag("MYTHIC_TYPE"), MythicItemPlayerIngredient::new);*/
registerTrigger("mmskill", MythicMobsSkillTrigger::new);
}
});
}
public void reload() {

View File

@ -2,6 +2,7 @@ package net.Indyuce.mmoitems.util;
import net.Indyuce.mmoitems.MMOItems;
import org.bukkit.Bukkit;
import org.bukkit.plugin.Plugin;
import org.jetbrains.annotations.NotNull;
import java.util.function.Consumer;
@ -19,15 +20,19 @@ public class PluginUtils {
throw new IllegalStateException("Utility class");
}
public static void isDependencyPresent(@NotNull String name, @NotNull Consumer<Void> callback) {
if (Bukkit.getPluginManager().getPlugin(name) != null)
callback.accept(null);
public static void isDependencyPresent(@NotNull String name, @NotNull Consumer<Plugin> callback) {
hookDependencyIfPresent(name, false, callback);
}
public static void hookDependencyIfPresent(@NotNull String name, @NotNull Consumer<Void> callback) {
if (Bukkit.getPluginManager().getPlugin(name) == null)
return;
callback.accept(null);
MMOItems.plugin.getLogger().log(Level.INFO, String.format("Hooked onto %s", name));
public static void hookDependencyIfPresent(@NotNull String name, boolean verbose, @NotNull Consumer<Plugin> callback) {
Plugin plugin = Bukkit.getPluginManager().getPlugin(name);
if (plugin == null) return;
try {
callback.accept(plugin);
if (verbose) MMOItems.plugin.getLogger().log(Level.INFO, String.format("Hooked onto %s", name));
} catch (Exception exception) {
MMOItems.plugin.getLogger().log(Level.INFO, String.format("Could not hook onto %s (plugin is out-of-date?): %s", name, exception.getMessage()));
}
}
}

View File

@ -7,6 +7,7 @@ import net.Indyuce.mmoitems.gui.listener.GuiListener;
import net.Indyuce.mmoitems.listener.*;
import net.Indyuce.mmoitems.listener.option.DroppedItems;
import net.Indyuce.mmoitems.listener.option.SoulboundNoDrop;
import net.Indyuce.mmoitems.util.PluginUtils;
import org.bukkit.Bukkit;
public class MMOItemsBukkit {
@ -26,8 +27,7 @@ public class MMOItemsBukkit {
Bukkit.getPluginManager().registerEvents(new GuiListener(), plugin);
Bukkit.getPluginManager().registerEvents(new BiomeChangeListener(), plugin);
Bukkit.getPluginManager().registerEvents(new CustomBlockListener(), plugin);
if (Bukkit.getPluginManager().getPlugin("PhatLoots") != null)
Bukkit.getPluginManager().registerEvents(new PhatLootsHook(), plugin);
PluginUtils.hookDependencyIfPresent("PhatLoots", true, pl -> Bukkit.getPluginManager().registerEvents(new PhatLootsHook(pl), plugin));
if (plugin.getConfig().getBoolean("dropped-items.tier-glow") || plugin.getConfig().getBoolean("dropped-items.hints"))
Bukkit.getPluginManager().registerEvents(new DroppedItems(plugin.getConfig().getConfigurationSection("dropped-items")), plugin);