Fix issues when enabling hooks with external classes

This commit is contained in:
Fernando Pettinelli 2023-07-02 15:03:50 -04:00
parent 975eaa9cb6
commit 49c03c85d3
11 changed files with 37 additions and 9 deletions

View File

@ -3,6 +3,7 @@ package com.craftaro.core.hooks;
import org.bukkit.Bukkit;
import org.bukkit.plugin.Plugin;
import java.lang.reflect.InvocationTargetException;
import java.util.Collection;
import java.util.HashMap;
import java.util.Map;
@ -29,15 +30,22 @@ public abstract class HookManager<T extends PluginHook> {
return registeredHooks.values();
}
public void registerHook(T hook) {
public void registerHook(Class<? extends T> hook) {
registerHook(null, hook);
}
public void registerHook(String requiredPlugin, T hook) {
public void registerHook(String requiredPlugin, Class<? extends T> hookClazz) {
if (requiredPlugin != null && !Bukkit.getPluginManager().isPluginEnabled(requiredPlugin)) {
return;
}
T hook = null;
try {
hook = hookClazz.getConstructor(Plugin.class).newInstance(plugin);
} catch (InstantiationException | InvocationTargetException | NoSuchMethodException | IllegalAccessException e) {
e.printStackTrace();
}
if (!hook.enableHook()) {
return;
}

View File

@ -13,7 +13,7 @@ public class EconomyManager extends HookManager<IEconomy> {
@Override
protected void registerDefaultHooks() {
registerHook("Vault", new VaultImplementation());
registerHook("Vault", VaultImplementation.class);
}
@Override

View File

@ -4,10 +4,13 @@ import com.craftaro.core.hooks.economy.IEconomy;
import net.milkbowl.vault.economy.Economy;
import org.bukkit.Bukkit;
import org.bukkit.OfflinePlayer;
import org.bukkit.plugin.Plugin;
import org.bukkit.plugin.RegisteredServiceProvider;
public class VaultImplementation implements IEconomy {
public VaultImplementation(Plugin plugin) {}
private Economy economy;
@Override

View File

@ -14,8 +14,8 @@ public class HologramManager extends HookManager<AbstractHologram> {
@Override
protected void registerDefaultHooks() {
registerHook("HolographicDisplays", new HolographicDisplaysImplementation(plugin));
registerHook("DecentHolograms", new DecentHologramsImplementation());
registerHook("HolographicDisplays", HolographicDisplaysImplementation.class);
registerHook("DecentHolograms", DecentHologramsImplementation.class);
}
@Override

View File

@ -5,12 +5,15 @@ import eu.decentsoftware.holograms.api.DHAPI;
import eu.decentsoftware.holograms.api.DecentHologramsAPI;
import eu.decentsoftware.holograms.api.holograms.Hologram;
import org.bukkit.Location;
import org.bukkit.plugin.Plugin;
import java.util.*;
public class DecentHologramsImplementation extends AbstractHologram {
private final Map<String, Hologram> holograms = new HashMap<>();
public DecentHologramsImplementation(Plugin plugin) {}
@Override
public String getHookName() {
return "DecentHolograms";

View File

@ -2,11 +2,13 @@ package com.craftaro.core.hooks.holograms.impl;
import com.craftaro.core.hooks.holograms.AbstractHologram;
import org.bukkit.Location;
import org.bukkit.plugin.Plugin;
import java.util.List;
import java.util.Map;
public class DummyHologramImplementation extends AbstractHologram {
@Override
public String getHookName() {
return "None";

View File

@ -17,15 +17,16 @@ public class ProtectionManager extends HookManager<IProtection> {
@Override
protected void registerDefaultHooks() {
registerHook("BentoBox", new BentoBoxImplementation());
registerHook("GriefPrevention", new GriefPreventionImplementation());
registerHook("WorldGuard", new WorldGuardImplementation());
registerHook("BentoBox", BentoBoxImplementation.class);
registerHook("GriefPrevention", GriefPreventionImplementation.class);
registerHook("WorldGuard", WorldGuardImplementation.class);
}
@Override
protected IProtection getDummyHook() {
return new DummyProtectionImplementation();
}
public ProtectionSet getHooksByName(List<String> names) {
ProtectionSet hooks = new ProtectionSet();
for (String name : names) {

View File

@ -3,6 +3,7 @@ package com.craftaro.core.hooks.protection.impl;
import com.craftaro.core.hooks.protection.IProtection;
import org.bukkit.Location;
import org.bukkit.entity.Player;
import org.bukkit.plugin.Plugin;
import world.bentobox.bentobox.BentoBox;
import world.bentobox.bentobox.api.flags.Flag;
import world.bentobox.bentobox.api.user.User;
@ -14,6 +15,8 @@ import java.util.Optional;
public class BentoBoxImplementation implements IProtection {
public BentoBoxImplementation(Plugin plugin) {}
private IslandsManager islandsManager;
@Override

View File

@ -3,8 +3,10 @@ package com.craftaro.core.hooks.protection.impl;
import com.craftaro.core.hooks.protection.IProtection;
import org.bukkit.Location;
import org.bukkit.entity.Player;
import org.bukkit.plugin.Plugin;
public class DummyProtectionImplementation implements IProtection {
@Override
public String getHookName() {
return "None";

View File

@ -6,10 +6,13 @@ import me.ryanhamshire.GriefPrevention.GriefPrevention;
import me.ryanhamshire.GriefPrevention.DataStore;
import org.bukkit.Location;
import org.bukkit.entity.Player;
import org.bukkit.plugin.Plugin;
public class GriefPreventionImplementation implements IProtection {
private DataStore dataStore;
private DataStore dataStore;
public GriefPreventionImplementation(Plugin plugin) {}
@Override
public String getHookName() {

View File

@ -10,6 +10,7 @@ import com.sk89q.worldguard.protection.managers.RegionManager;
import com.sk89q.worldguard.protection.regions.RegionContainer;
import org.bukkit.Location;
import org.bukkit.entity.Player;
import org.bukkit.plugin.Plugin;
import java.util.Objects;
@ -17,6 +18,8 @@ public class WorldGuardImplementation implements IProtection {
private RegionContainer container;
public WorldGuardImplementation(Plugin plugin) {}
@Override
public String getHookName() {
return "WorldGuard";