From 96cfcf78e90c80e77852eb9521263e6da86173b3 Mon Sep 17 00:00:00 2001 From: MachiganMC Date: Sat, 7 Oct 2023 02:27:59 +0200 Subject: [PATCH] refactor(vault): change way to hook providers Also the Permission provider has been added to get permission of an offline player. This has be done to easily add more providers in the future but also avoid code repetitions (like logging if Vault is enabled). --- .../com/gamingmesh/jobs/HookEconomyTask.java | 114 ++++++------------ .../gamingmesh/jobs/HookPermissionTask.java | 15 +++ .../java/com/gamingmesh/jobs/HookVault.java | 60 +++++++++ src/main/java/com/gamingmesh/jobs/Jobs.java | 102 +++++----------- 4 files changed, 141 insertions(+), 150 deletions(-) create mode 100644 src/main/java/com/gamingmesh/jobs/HookPermissionTask.java create mode 100644 src/main/java/com/gamingmesh/jobs/HookVault.java diff --git a/src/main/java/com/gamingmesh/jobs/HookEconomyTask.java b/src/main/java/com/gamingmesh/jobs/HookEconomyTask.java index ab51d1e7..eda9dd14 100644 --- a/src/main/java/com/gamingmesh/jobs/HookEconomyTask.java +++ b/src/main/java/com/gamingmesh/jobs/HookEconomyTask.java @@ -1,80 +1,34 @@ -/** - * Jobs Plugin for Bukkit - * Copyright (C) 2011 Zak Ford - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program. If not, see . - */ - -package com.gamingmesh.jobs; - -import org.bukkit.plugin.RegisteredServiceProvider; - -import com.gamingmesh.jobs.economy.BlackholeEconomy; -import com.gamingmesh.jobs.economy.VaultEconomy; - -import net.Zrips.CMILib.Messages.CMIMessages; -import net.milkbowl.vault.economy.Economy; - -public class HookEconomyTask implements Runnable { - - private Jobs plugin; - - public HookEconomyTask(Jobs plugin) { - this.plugin = plugin; - } - - enum hookResult { - novault, noeconomy, pass; - - } - - @Override - public void run() { - - hookResult result = setVault(); - - if (result.equals(hookResult.pass)) { - return; - } - - // no Economy found - Jobs.setEconomy(new BlackholeEconomy()); - Jobs.getPluginLogger().severe("==================== " + plugin.getDescription().getName() + " ===================="); - if (result.equals(hookResult.novault)) { - Jobs.getPluginLogger().severe("Vault is required by this plugin for economy support!"); - Jobs.getPluginLogger().severe("Please install them first!"); - Jobs.getPluginLogger().severe("You can find the latest versions here:"); - Jobs.getPluginLogger().severe("https://www.spigotmc.org/resources/34315/"); - } else { - Jobs.getPluginLogger().severe("Vault detected but economy plugin still missing!"); - Jobs.getPluginLogger().severe("Please install Vault supporting economy plugin!"); - } - Jobs.getPluginLogger().severe("=============================================="); - } - - private hookResult setVault() { - if (!plugin.getServer().getPluginManager().isPluginEnabled("Vault")) - return hookResult.novault; - - RegisteredServiceProvider provider = plugin.getServer().getServicesManager().getRegistration(Economy.class); - if (provider == null) { - return hookResult.noeconomy; - } - - Jobs.setEconomy(new VaultEconomy(provider.getProvider())); - CMIMessages.consoleMessage("&e[" + plugin.getDescription().getName() + "] Successfully linked with Vault. (" + provider.getProvider().getName() + ")"); - return hookResult.pass; - } - -} +/** + * Jobs Plugin for Bukkit + * Copyright (C) 2011 Zak Ford + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +package com.gamingmesh.jobs; + +import com.gamingmesh.jobs.economy.VaultEconomy; +import net.milkbowl.vault.economy.Economy; + +public class HookEconomyTask extends HookVault { + + public HookEconomyTask(Class providerClass) { + super(providerClass); + } + + @Override + void runIfProviderIsFound() { + Jobs.setEconomy(new VaultEconomy(provider.getProvider())); + } +} diff --git a/src/main/java/com/gamingmesh/jobs/HookPermissionTask.java b/src/main/java/com/gamingmesh/jobs/HookPermissionTask.java new file mode 100644 index 00000000..422cf00a --- /dev/null +++ b/src/main/java/com/gamingmesh/jobs/HookPermissionTask.java @@ -0,0 +1,15 @@ +package com.gamingmesh.jobs; + +import net.milkbowl.vault.permission.Permission; + +public class HookPermissionTask extends HookVault { + + public HookPermissionTask(Class providerClass) { + super(providerClass); + } + + @Override + void runIfProviderIsFound() { + Jobs.setVaultPermission(this.provider.getProvider()); + } +} diff --git a/src/main/java/com/gamingmesh/jobs/HookVault.java b/src/main/java/com/gamingmesh/jobs/HookVault.java new file mode 100644 index 00000000..5861795c --- /dev/null +++ b/src/main/java/com/gamingmesh/jobs/HookVault.java @@ -0,0 +1,60 @@ +package com.gamingmesh.jobs; + +import net.Zrips.CMILib.Messages.CMIMessages; +import org.bukkit.plugin.RegisteredServiceProvider; + +public abstract class HookVault { + private static Boolean vaultEnable = null; + protected Class providerClass; + protected RegisteredServiceProvider provider; + + protected HookVault(Class providerClass) { + this.providerClass = providerClass; + if (!isVaultEnable())return; + this.provider = Jobs.getInstance().getServer().getServicesManager().getRegistration(this.providerClass); + if (this.provider != null) { + logProviderConnected(); + runIfProviderIsFound(); + } else { + logProviderNotFound(); + } + } + + public static boolean isVaultEnable() { + if (vaultEnable == null) { + setIsVaultEnable(); + if (!vaultEnable) + logIfVaultIsNotEnable(); + } + return vaultEnable; + } + + private static void setIsVaultEnable() { + vaultEnable = Jobs.getInstance().getServer().getPluginManager().isPluginEnabled("Vault"); + } + + public static void logIfVaultIsNotEnable() { + if (vaultEnable)return; + Jobs.getPluginLogger().severe("==================== " + Jobs.getInstance().getName() + " ===================="); + Jobs.getPluginLogger().severe("Vault is required by this plugin for economy support!"); + Jobs.getPluginLogger().severe("Please install them first!"); + Jobs.getPluginLogger().severe("You can find the latest versions here:"); + Jobs.getPluginLogger().severe("https://www.spigotmc.org/resources/34315/"); + Jobs.getPluginLogger().severe("=============================================="); + } + + public void logProviderNotFound() { + if (this.provider == null && isVaultEnable()) { + Jobs.getPluginLogger().severe("==================== " + Jobs.getInstance().getDescription().getName() + " ===================="); + Jobs.getPluginLogger().severe("Vault detected but " + this.providerClass.getSimpleName() + " plugin still missing!"); + Jobs.getPluginLogger().severe("Please install Vault supporting " + this.providerClass.getSimpleName() + " plugin!"); + Jobs.getPluginLogger().severe("=============================================="); + } + } + + protected void logProviderConnected() { + CMIMessages.consoleMessage("&e[" + Jobs.getInstance().getName() + "] Successfully linked with Vault. (" + provider.getPlugin().getName() + ")"); + } + + abstract void runIfProviderIsFound(); +} diff --git a/src/main/java/com/gamingmesh/jobs/Jobs.java b/src/main/java/com/gamingmesh/jobs/Jobs.java index e6f55167..2867177f 100644 --- a/src/main/java/com/gamingmesh/jobs/Jobs.java +++ b/src/main/java/com/gamingmesh/jobs/Jobs.java @@ -18,28 +18,6 @@ package com.gamingmesh.jobs; -import java.io.File; -import java.sql.SQLException; -import java.util.ArrayList; -import java.util.Collections; -import java.util.HashMap; -import java.util.Iterator; -import java.util.LinkedHashMap; -import java.util.List; -import java.util.Map; -import java.util.Optional; -import java.util.UUID; -import java.util.WeakHashMap; -import java.util.logging.Logger; - -import org.bukkit.Bukkit; -import org.bukkit.block.Block; -import org.bukkit.entity.Entity; -import org.bukkit.entity.LivingEntity; -import org.bukkit.entity.Player; -import org.bukkit.event.HandlerList; -import org.bukkit.plugin.java.JavaPlugin; - import com.gamingmesh.jobs.Gui.GuiManager; import com.gamingmesh.jobs.Placeholders.Placeholder; import com.gamingmesh.jobs.Placeholders.PlaceholderAPIHook; @@ -47,40 +25,8 @@ import com.gamingmesh.jobs.Signs.SignUtil; import com.gamingmesh.jobs.api.JobsExpGainEvent; import com.gamingmesh.jobs.api.JobsPrePaymentEvent; import com.gamingmesh.jobs.commands.JobsCommands; -import com.gamingmesh.jobs.config.BlockProtectionManager; -import com.gamingmesh.jobs.config.BossBarManager; -import com.gamingmesh.jobs.config.ConfigManager; -import com.gamingmesh.jobs.config.ExploreManager; -import com.gamingmesh.jobs.config.GeneralConfigManager; -import com.gamingmesh.jobs.config.LanguageManager; -import com.gamingmesh.jobs.config.NameTranslatorManager; -import com.gamingmesh.jobs.config.RestrictedAreaManager; -import com.gamingmesh.jobs.config.RestrictedBlockManager; -import com.gamingmesh.jobs.config.ScheduleManager; -import com.gamingmesh.jobs.config.ShopManager; -import com.gamingmesh.jobs.config.TitleManager; -import com.gamingmesh.jobs.config.YmlMaker; -import com.gamingmesh.jobs.container.ActionInfo; -import com.gamingmesh.jobs.container.ActionType; -import com.gamingmesh.jobs.container.ArchivedJobs; -import com.gamingmesh.jobs.container.BlockProtection; -import com.gamingmesh.jobs.container.Boost; -import com.gamingmesh.jobs.container.Convert; -import com.gamingmesh.jobs.container.CurrencyLimit; -import com.gamingmesh.jobs.container.CurrencyType; -import com.gamingmesh.jobs.container.DBAction; -import com.gamingmesh.jobs.container.FastPayment; -import com.gamingmesh.jobs.container.Job; -import com.gamingmesh.jobs.container.JobInfo; -import com.gamingmesh.jobs.container.JobProgression; -import com.gamingmesh.jobs.container.JobsPlayer; -import com.gamingmesh.jobs.container.JobsWorld; -import com.gamingmesh.jobs.container.LoadStatus; -import com.gamingmesh.jobs.container.Log; -import com.gamingmesh.jobs.container.PlayerInfo; -import com.gamingmesh.jobs.container.PlayerPoints; -import com.gamingmesh.jobs.container.Quest; -import com.gamingmesh.jobs.container.QuestProgression; +import com.gamingmesh.jobs.config.*; +import com.gamingmesh.jobs.container.*; import com.gamingmesh.jobs.container.blockOwnerShip.BlockOwnerShip; import com.gamingmesh.jobs.container.blockOwnerShip.BlockTypes; import com.gamingmesh.jobs.dao.JobsClassLoader; @@ -93,32 +39,35 @@ import com.gamingmesh.jobs.economy.Economy; import com.gamingmesh.jobs.economy.PaymentData; import com.gamingmesh.jobs.hooks.HookManager; import com.gamingmesh.jobs.i18n.Language; -import com.gamingmesh.jobs.listeners.JobsListener; -import com.gamingmesh.jobs.listeners.JobsPayment1_14Listener; -import com.gamingmesh.jobs.listeners.JobsPaymentListener; -import com.gamingmesh.jobs.listeners.PistonProtectionListener; -import com.gamingmesh.jobs.listeners.JobsPayment1_16Listener; -import com.gamingmesh.jobs.listeners.PlayerSignEdit1_20Listeners; +import com.gamingmesh.jobs.listeners.*; import com.gamingmesh.jobs.selection.SelectionManager; -import com.gamingmesh.jobs.stuff.Loging; -import com.gamingmesh.jobs.stuff.TabComplete; -import com.gamingmesh.jobs.stuff.ToggleBarHandling; -import com.gamingmesh.jobs.stuff.Util; -import com.gamingmesh.jobs.stuff.VersionChecker; +import com.gamingmesh.jobs.stuff.*; import com.gamingmesh.jobs.stuff.complement.Complement; import com.gamingmesh.jobs.stuff.complement.Complement1; import com.gamingmesh.jobs.stuff.complement.Complement2; import com.gamingmesh.jobs.stuff.complement.JobsChatEvent; import com.gamingmesh.jobs.tasks.BufferedPaymentThread; import com.gamingmesh.jobs.tasks.DatabaseSaveThread; - import net.Zrips.CMILib.ActionBar.CMIActionBar; import net.Zrips.CMILib.Items.CMIMaterial; import net.Zrips.CMILib.Locale.LC; import net.Zrips.CMILib.Messages.CMIMessages; import net.Zrips.CMILib.RawMessages.RawMessage; -import net.Zrips.CMILib.Version.Version; import net.Zrips.CMILib.Version.Schedulers.CMIScheduler; +import net.Zrips.CMILib.Version.Version; +import net.milkbowl.vault.permission.Permission; +import org.bukkit.Bukkit; +import org.bukkit.block.Block; +import org.bukkit.entity.Entity; +import org.bukkit.entity.LivingEntity; +import org.bukkit.entity.Player; +import org.bukkit.event.HandlerList; +import org.bukkit.plugin.java.JavaPlugin; + +import java.io.File; +import java.sql.SQLException; +import java.util.*; +import java.util.logging.Logger; public final class Jobs extends JavaPlugin { @@ -142,6 +91,8 @@ public final class Jobs extends JavaPlugin { private static GeneralConfigManager gConfigManager; private static BufferedEconomy economy; + + private static Permission vaultPermission; private static PermissionHandler permissionHandler; private static PermissionManager permissionManager; @@ -691,6 +642,10 @@ public final class Jobs extends JavaPlugin { economy = new BufferedEconomy(getInstance(), eco); } + public static void setVaultPermission(Permission permission) { + vaultPermission = permission; + } + /** * Gets the economy handler * @return the economy handler @@ -699,6 +654,10 @@ public final class Jobs extends JavaPlugin { return economy; } + public static Permission getVaultPermission() { + return vaultPermission; + } + /** * Gets the version check manager * @return the version check manager @@ -762,7 +721,10 @@ public final class Jobs extends JavaPlugin { } // register economy - CMIScheduler.get().runTask(new HookEconomyTask(this)); + CMIScheduler.get().runTask(() -> new HookEconomyTask(net.milkbowl.vault.economy.Economy.class)); + + // register permission from vault + CMIScheduler.get().runTask(() -> new HookPermissionTask(Permission.class)); dao.loadBlockProtection(); getExploreManager().load();