Refactor Integration, prepare for hot loading feature

Former-commit-id: 2ee199100447b6299a1a95fea53298ac0a503e06
This commit is contained in:
sandtechnology committed 2020-11-22 00:24:49 +08:00
1 parent d58d4c1c9f
commit 9c1922c27d
13 files changed
+164 -102

No files matched your search

@@ -45,12 +45,7 @@ import org.maxgamer.quickshop.database.*;
import org.maxgamer.quickshop.economy.*;
import org.maxgamer.quickshop.event.QSReloadEvent;
import org.maxgamer.quickshop.integration.IntegrateStage;
import org.maxgamer.quickshop.integration.factionsuuid.FactionsUUIDIntegration;
import org.maxgamer.quickshop.integration.griefprevention.GriefPreventionIntegration;
import org.maxgamer.quickshop.integration.lands.LandsIntegration;
import org.maxgamer.quickshop.integration.plotsquared.PlotSquaredIntegrationHolder;
import org.maxgamer.quickshop.integration.residence.ResidenceIntegration;
import org.maxgamer.quickshop.integration.towny.TownyIntegration;
import org.maxgamer.quickshop.integration.IntegrationHelper;
import org.maxgamer.quickshop.integration.worldguard.WorldGuardIntegration;
import org.maxgamer.quickshop.listener.*;
import org.maxgamer.quickshop.permission.PermissionManager;
@@ -500,7 +495,7 @@ public class QuickShop extends JavaPlugin {
this.bootError = null;
getLogger().info("Loading up integration modules.");
this.integrationHelper = new IntegrationHelper();
this.integrationHelper = new IntegrationHelper(this);
this.integrationHelper.callIntegrationsLoad(IntegrateStage.onLoadBegin);
if (getConfig().getBoolean("integration.worldguard.enable")) {
Plugin wg = Bukkit.getPluginManager().getPlugin("WorldGuard");
@@ -556,7 +551,7 @@ public class QuickShop extends JavaPlugin {
Util.debugLog("Calling integrations...");
this.integrationHelper.callIntegrationsUnload(IntegrateStage.onUnloadAfter);
this.compatibilityTool.clear();
new HashSet<>(this.integrationHelper.getIntegrations()).forEach(integratedPlugin -> this.integrationHelper.unregister(integratedPlugin));
integrationHelper.unregisterAll();
Util.debugLog("Unregistering tasks...");
// if (itemWatcherTask != null)
@@ -807,7 +802,7 @@ public class QuickShop extends JavaPlugin {
getLogger().info("Ongoing fee feature is enabled.");
ongoingFeeWatcher.runTaskTimerAsynchronously(this, 0, getConfig().getInt("shop.ongoing-fee.ticks"));
}
registerIntegrations();
integrationHelper.searchAndRegisterPlugins();
this.integrationHelper.callIntegrationsLoad(IntegrateStage.onEnableAfter);
new BukkitRunnable() {
@Override
@@ -823,45 +818,6 @@ public class QuickShop extends JavaPlugin {
}
}
private void registerIntegrations() {
if (getConfig().getBoolean("integration.towny.enable")) {
Plugin towny = Bukkit.getPluginManager().getPlugin("Towny");
if (towny != null && towny.isEnabled()) {
this.integrationHelper.register(new TownyIntegration(this));
}
}
if (getConfig().getBoolean("integration.plotsquared.enable")) {
Plugin plotSquared = Bukkit.getPluginManager().getPlugin("PlotSquared");
if (plotSquared != null && plotSquared.isEnabled()) {
this.integrationHelper.register(PlotSquaredIntegrationHolder.getPlotSquaredIntegration(this));
}
}
if (getConfig().getBoolean("integration.residence.enable")) {
Plugin residence = Bukkit.getPluginManager().getPlugin("Residence");
if (residence != null && residence.isEnabled()) {
this.integrationHelper.register(new ResidenceIntegration(this));
}
}
if (getConfig().getBoolean("integration.factions.enable")) {
Plugin factions = Bukkit.getPluginManager().getPlugin("Factions");
if (factions != null && factions.isEnabled()) {
this.integrationHelper.register(new FactionsUUIDIntegration(this));
}
}
if (getConfig().getBoolean("integration.lands.enable")) {
Plugin lands = Bukkit.getPluginManager().getPlugin("Lands");
if (lands != null && lands.isEnabled()) {
this.integrationHelper.register(new LandsIntegration(this));
}
}
if (getConfig().getBoolean("integration.griefprevention.enable")) {
Plugin griefPrevention = Bukkit.getPluginManager().getPlugin("GriefPrevention");
if (griefPrevention != null && griefPrevention.isEnabled()) {
this.integrationHelper.register(new GriefPreventionIntegration(this));
}
}
}
/**
* Setup the database
*
@@ -23,10 +23,16 @@ package org.maxgamer.quickshop.integration;
import org.bukkit.Location;
import org.bukkit.entity.Player;
import org.jetbrains.annotations.NotNull;
import org.maxgamer.quickshop.QuickShop;
import org.maxgamer.quickshop.util.holder.QuickShopInstanceHolder;
@IntegrationStage
public interface IntegratedPlugin {
public abstract class IntegratedPlugin extends QuickShopInstanceHolder {
public IntegratedPlugin(QuickShop plugin) {
super(plugin);
}
/**
* Return the integrated plugin name.
@@ -35,7 +41,7 @@ public interface IntegratedPlugin {
* @return integrated plugin
*/
@NotNull
String getName();
public abstract String getName();
/**
* Check if a player can create shop here
@@ -44,7 +50,7 @@ public interface IntegratedPlugin {
* @param location shop location
* @return If you can create shop here
*/
boolean canCreateShopHere(@NotNull Player player, @NotNull Location location);
public abstract boolean canCreateShopHere(@NotNull Player player, @NotNull Location location);
/**
* Check if a player can trade with shop here
@@ -53,7 +59,7 @@ public interface IntegratedPlugin {
* @param location shop location
* @return If you can trade with shop here
*/
boolean canTradeShopHere(@NotNull Player player, @NotNull Location location);
public abstract boolean canTradeShopHere(@NotNull Player player, @NotNull Location location);
/**
* Check if a player can delete a shop here
@@ -62,7 +68,7 @@ public interface IntegratedPlugin {
* @param location shop location
* @return If you can delete the shop here
*/
default boolean canDeleteShopHere(@NotNull Player player, @NotNull Location location) {
public boolean canDeleteShopHere(@NotNull Player player, @NotNull Location location) {
return false;
}
@@ -70,11 +76,11 @@ public interface IntegratedPlugin {
* Loading logic
* Execute Stage defined by IntegrationStage
*/
void load();
public abstract void load();
/**
* Unloding logic
* Will execute when Quickshop unloading
*/
void unload();
public abstract void unload();
}
@@ -18,31 +18,78 @@
*
*/
package org.maxgamer.quickshop.util;
package org.maxgamer.quickshop.integration;
import lombok.Getter;
import org.bukkit.Location;
import org.bukkit.entity.Player;
import org.bukkit.plugin.PluginManager;
import org.jetbrains.annotations.NotNull;
import org.maxgamer.quickshop.integration.IntegrateStage;
import org.maxgamer.quickshop.integration.IntegratedPlugin;
import org.maxgamer.quickshop.integration.IntegrationStage;
import org.maxgamer.quickshop.QuickShop;
import org.maxgamer.quickshop.integration.factionsuuid.FactionsUUIDIntegration;
import org.maxgamer.quickshop.integration.griefprevention.GriefPreventionIntegration;
import org.maxgamer.quickshop.integration.lands.LandsIntegration;
import org.maxgamer.quickshop.integration.plotsquared.PlotSquaredIntegrationProxy;
import org.maxgamer.quickshop.integration.residence.ResidenceIntegration;
import org.maxgamer.quickshop.integration.towny.TownyIntegration;
import org.maxgamer.quickshop.integration.worldguard.WorldGuardIntegration;
import org.maxgamer.quickshop.util.Util;
import org.maxgamer.quickshop.util.holder.QuickShopInstanceHolder;
import org.maxgamer.quickshop.util.holder.Result;
import java.util.HashSet;
import java.util.Set;
import java.lang.reflect.InvocationTargetException;
import java.util.*;
@Getter
public class IntegrationHelper {
private final Set<IntegratedPlugin> integrations = new HashSet<>(5);
public void register(@NotNull IntegratedPlugin clazz) {
if (!isIntegrationClass(clazz.getClass())) {
throw new InvaildIntegratedPluginClass("Invaild Integration module: " + clazz.getName());
public class IntegrationHelper extends QuickShopInstanceHolder {
private static final Map<String, Class<? extends IntegratedPlugin>> integratedPluginNameMap = new HashMap<>(7);
static {
integratedPluginNameMap.put("Factions", FactionsUUIDIntegration.class);
integratedPluginNameMap.put("GriefPrevention", GriefPreventionIntegration.class);
integratedPluginNameMap.put("Lands", LandsIntegration.class);
integratedPluginNameMap.put("PlotSquared", PlotSquaredIntegrationProxy.class);
integratedPluginNameMap.put("Residence", ResidenceIntegration.class);
integratedPluginNameMap.put("Towny", TownyIntegration.class);
integratedPluginNameMap.put("WorldGuard", WorldGuardIntegration.class);
}
private final Map<String, IntegratedPlugin> integrations = new HashMap<>(7);
public IntegrationHelper(QuickShop plugin) {
super(plugin);
}
public List<IntegratedPlugin> getIntegrations() {
return Collections.unmodifiableList(new ArrayList<>(integrations.values()));
}
public void searchAndRegisterPlugins() {
PluginManager pluginManager = plugin.getServer().getPluginManager();
for (String pluginName : integratedPluginNameMap.keySet()) {
if (plugin.getConfig().getBoolean("integration." + pluginName.toLowerCase() + ".enable")) {
if (pluginManager.isPluginEnabled(pluginName)) {
register(pluginName);
}
}
}
Util.debugLog("Registering " + clazz.getName());
integrations.add(clazz);
}
public void register(@NotNull IntegratedPlugin integratedPlugin) {
if (!isIntegrationClass(integratedPlugin.getClass())) {
throw new InvaildIntegratedPluginClass("Invaild Integration module: " + integratedPlugin.getName());
}
Util.debugLog("Registering " + integratedPlugin.getName());
integrations.put(integratedPlugin.getName(), integratedPlugin);
}
public void register(@NotNull String integratedPluginName) {
IntegratedPlugin integratedPlugin;
try {
integratedPlugin = integratedPluginNameMap.get(integratedPluginName).getConstructor(plugin.getClass()).newInstance(plugin);
} catch (NullPointerException | NoSuchMethodException | InvocationTargetException | InstantiationException | IllegalAccessException e) {
throw new InvaildIntegratedPluginClass("Invaild Integration module name: " + integratedPluginName);
}
register(integratedPlugin);
}
@SuppressWarnings("BooleanMethodIsAlwaysInverted")
@@ -50,22 +97,35 @@ public class IntegrationHelper {
return clazz.getDeclaredAnnotation(IntegrationStage.class) != null;
}
public void unregister(@NotNull IntegratedPlugin clazz) {
if (!isIntegrationClass(clazz.getClass())) {
public void unregister(@NotNull String integratedPluginName) {
IntegratedPlugin integratedPlugin = integrations.get(integratedPluginName);
if (integratedPlugin != null) {
unregister(integratedPlugin);
}
}
public void unregisterAll() {
for (IntegratedPlugin integratedPlugin : integrations.values()) {
unregister(integratedPlugin);
}
}
public void unregister(@NotNull IntegratedPlugin integratedPlugin) {
if (!isIntegrationClass(integratedPlugin.getClass())) {
throw new InvaildIntegratedPluginClass();
}
//Prevent it being removed
//WorldGuardIntegration will load in onload()V
//so it won't be registered again when reload
if (clazz instanceof WorldGuardIntegration) {
if (integratedPlugin instanceof WorldGuardIntegration) {
return;
}
Util.debugLog("Unregistering " + clazz.getName());
integrations.remove(clazz);
Util.debugLog("Unregistering " + integratedPlugin.getName());
integrations.remove(integratedPlugin.getName());
}
public void callIntegrationsLoad(@NotNull IntegrateStage stage) {
integrations.forEach(
integrations.values().forEach(
integratedPlugin -> {
if (integratedPlugin.getClass().getDeclaredAnnotation(IntegrationStage.class).loadStage()
== stage) {
@@ -85,7 +145,7 @@ public class IntegrationHelper {
}
public void callIntegrationsUnload(@NotNull IntegrateStage stage) {
integrations.forEach(
integrations.values().forEach(
integratedPlugin -> {
if (integratedPlugin.getClass().getDeclaredAnnotation(IntegrationStage.class).unloadStage()
== stage) {
@@ -105,7 +165,7 @@ public class IntegrationHelper {
}
public Result callIntegrationsCanCreate(@NotNull Player player, @NotNull Location location) {
for (IntegratedPlugin plugin : integrations) {
for (IntegratedPlugin plugin : integrations.values()) {
if (!plugin.canCreateShopHere(player, location)) {
Util.debugLog("Cancelled by " + plugin.getName());
return new Result(plugin.getName());
@@ -115,7 +175,7 @@ public class IntegrationHelper {
}
public Result callIntegrationsCanTrade(@NotNull Player player, @NotNull Location location) {
for (IntegratedPlugin plugin : integrations) {
for (IntegratedPlugin plugin : integrations.values()) {
if (!plugin.canTradeShopHere(player, location)) {
Util.debugLog("Cancelled by " + plugin.getName());
return new Result(plugin.getName());
@@ -36,7 +36,7 @@ import org.maxgamer.quickshop.integration.IntegrationStage;
import java.util.List;
@IntegrationStage(loadStage = IntegrateStage.onEnableAfter)
public class FactionsUUIDIntegration implements IntegratedPlugin {
public class FactionsUUIDIntegration extends IntegratedPlugin {
private final List<String> createFlags;
private final List<String> tradeFlags;
@@ -76,6 +76,7 @@ public class FactionsUUIDIntegration implements IntegratedPlugin {
private final boolean whiteList;
public FactionsUUIDIntegration(QuickShop plugin) {
super(plugin);
this.createFlags = plugin.getConfig().getStringList("integration.factions.create.flags");
this.tradeFlags = plugin.getConfig().getStringList("integration.factions.trade.flags");
@@ -16,13 +16,14 @@ import java.util.ArrayList;
import java.util.List;
@IntegrationStage(loadStage = IntegrateStage.onEnableAfter)
public class GriefPreventionIntegration implements IntegratedPlugin {
public class GriefPreventionIntegration extends IntegratedPlugin {
private final List<Flag> createLimits = new ArrayList<>(3);
private final List<Flag> tradeLimits = new ArrayList<>(3);
private final boolean whiteList;
final GriefPrevention griefPrevention = GriefPrevention.instance;
public GriefPreventionIntegration(QuickShop plugin) {
super(plugin);
ConfigurationSection configurationSection = plugin.getConfig();
this.whiteList = configurationSection.getBoolean("integration.griefprevention.whitelist-mode");
createLimits.addAll(toFlags(configurationSection.getStringList("integration.griefprevention.create")));
@@ -41,16 +41,15 @@ import java.util.Map;
import java.util.UUID;
@IntegrationStage
public class LandsIntegration implements IntegratedPlugin, Listener {
public class LandsIntegration extends IntegratedPlugin implements Listener {
private final boolean ignoreDisabledWorlds;
private final boolean whitelist;
private final me.angeschossen.lands.api.integration.LandsIntegration landsIntegration;
private final boolean deleteWhenLosePermission;
private final QuickShop plugin;
public LandsIntegration(QuickShop plugin) {
this.plugin = plugin;
super(plugin);
landsIntegration = new me.angeschossen.lands.api.integration.LandsIntegration(plugin);
ignoreDisabledWorlds = plugin.getConfig().getBoolean("integration.lands.ignore-disabled-worlds");
whitelist = plugin.getConfig().getBoolean("integration.lands.whitelist-mode");
@@ -21,13 +21,17 @@
package org.maxgamer.quickshop.integration.plotsquared;
import org.bukkit.Bukkit;
import org.bukkit.Location;
import org.bukkit.entity.Player;
import org.jetbrains.annotations.NotNull;
import org.maxgamer.quickshop.QuickShop;
import org.maxgamer.quickshop.integration.IntegratedPlugin;
public class PlotSquaredIntegrationHolder {
public class PlotSquaredIntegrationProxy extends IntegratedPlugin {
private static IntegratedPlugin plotSquared;
public static IntegratedPlugin getPlotSquaredIntegration(QuickShop instance) {
public PlotSquaredIntegrationProxy(QuickShop instance) {
super(instance);
if (plotSquared == null) {
if (Bukkit.getPluginManager().getPlugin("PlotSquared").getClass().getPackage().getName().contains("intellectualsite")) {
plotSquared = new PlotSquaredIntegrationV4(instance);
@@ -35,6 +39,35 @@ public class PlotSquaredIntegrationHolder {
plotSquared = new PlotSquaredIntegrationV5(instance);
}
}
return plotSquared;
}
@Override
public @NotNull String getName() {
return plotSquared.getName();
}
@Override
public boolean canCreateShopHere(@NotNull Player player, @NotNull Location location) {
return plotSquared.canCreateShopHere(player, location);
}
@Override
public boolean canTradeShopHere(@NotNull Player player, @NotNull Location location) {
return plotSquared.canCreateShopHere(player, location);
}
@Override
public boolean canDeleteShopHere(@NotNull Player player, @NotNull Location location) {
return plotSquared.canDeleteShopHere(player, location);
}
@Override
public void load() {
plotSquared.load();
}
@Override
public void unload() {
plotSquared.unload();
}
}
@@ -35,16 +35,14 @@ import org.maxgamer.quickshop.util.Util;
@SuppressWarnings("DuplicatedCode")
@IntegrationStage(loadStage = IntegrateStage.onEnableAfter)
public class PlotSquaredIntegrationV4 implements IntegratedPlugin {
private final QuickShop plugin;
public class PlotSquaredIntegrationV4 extends IntegratedPlugin {
private final boolean whiteList;
private BooleanFlag createFlag;
private BooleanFlag tradeFlag;
public PlotSquaredIntegrationV4(QuickShop plugin) {
this.plugin = plugin;
super(plugin);
this.whiteList = plugin.getConfig().getBoolean("integration.plotsquared.whitelist-mode");
// PlotAPI plotAPI = new PlotAPI();
}
@Override
@@ -39,14 +39,13 @@ import java.util.Arrays;
@SuppressWarnings("DuplicatedCode")
@IntegrationStage(loadStage = IntegrateStage.onEnableAfter)
public class PlotSquaredIntegrationV5 implements IntegratedPlugin {
private final QuickShop plugin;
public class PlotSquaredIntegrationV5 extends IntegratedPlugin {
private final boolean whiteList;
private QuickshopCreateFlag createFlag;
private QuickshopTradeFlag tradeFlag;
public PlotSquaredIntegrationV5(QuickShop plugin) {
this.plugin = plugin;
super(plugin);
this.whiteList = plugin.getConfig().getBoolean("integration.plotsquared.whitelist-mode");
}
@@ -37,7 +37,7 @@ import java.util.Map;
@SuppressWarnings("DuplicatedCode")
@IntegrationStage(loadStage = IntegrateStage.onEnableAfter)
public class ResidenceIntegration implements IntegratedPlugin {
public class ResidenceIntegration extends IntegratedPlugin {
private static final String createFlag = "quickshop-create";
private static final String tradeFlag = "quickshop-trade";
private final List<String> createLimits;
@@ -45,6 +45,7 @@ public class ResidenceIntegration implements IntegratedPlugin {
private final boolean whiteList;
public ResidenceIntegration(QuickShop plugin) {
super(plugin);
this.whiteList = plugin.getConfig().getBoolean("integration.residence.whitelist-mode");
this.createLimits = plugin.getConfig().getStringList("integration.residence.create");
this.tradeLimits = plugin.getConfig().getStringList("integration.residence.trade");
@@ -48,17 +48,16 @@ import java.util.UUID;
@SuppressWarnings("DuplicatedCode")
@IntegrationStage(loadStage = IntegrateStage.onEnableAfter)
public class TownyIntegration implements IntegratedPlugin, Listener {
public class TownyIntegration extends IntegratedPlugin implements Listener {
private final List<TownyFlags> createFlags;
private final List<TownyFlags> tradeFlags;
private final boolean ignoreDisabledWorlds;
private final QuickShop plugin;
private final boolean deleteShopOnLeave;
public TownyIntegration(QuickShop plugin) {
this.plugin = plugin;
super(plugin);
createFlags =
TownyFlags.deserialize(plugin.getConfig().getStringList("integration.towny.create"));
tradeFlags =
@@ -44,7 +44,7 @@ import java.util.List;
@SuppressWarnings("DuplicatedCode")
@IntegrationStage(loadStage = IntegrateStage.onLoadAfter)
public class WorldGuardIntegration implements IntegratedPlugin {
public class WorldGuardIntegration extends IntegratedPlugin {
private List<WorldGuardFlags> createFlags;
private List<WorldGuardFlags> tradeFlags;
@@ -54,13 +54,11 @@ public class WorldGuardIntegration implements IntegratedPlugin {
private final StateFlag tradeFlag = new StateFlag("quickshop-trade", true);
private boolean anyOwner;
private final QuickShop plugin;
private boolean whiteList;
private boolean load = false;
public WorldGuardIntegration(QuickShop plugin) {
this.plugin = plugin;
super(plugin);
}
@Override
@@ -0,0 +1,11 @@
package org.maxgamer.quickshop.util.holder;
import org.maxgamer.quickshop.QuickShop;
public class QuickShopInstanceHolder {
protected final QuickShop plugin;
public QuickShopInstanceHolder(QuickShop plugin) {
this.plugin = plugin;
}
}