mirror of
				https://github.com/PlaceholderAPI/PlaceholderAPI
				synced 2025-10-31 06:12:28 +01:00 
			
		
		
		
	Added method to register expansion by specifying the file name
This commit is contained in:
		| @@ -20,6 +20,12 @@ | |||||||
|  */ |  */ | ||||||
| package me.clip.placeholderapi.expansion; | package me.clip.placeholderapi.expansion; | ||||||
|  |  | ||||||
|  | import java.io.File; | ||||||
|  | import java.lang.reflect.Constructor; | ||||||
|  | import java.util.HashMap; | ||||||
|  | import java.util.List; | ||||||
|  | import java.util.Map; | ||||||
|  | import java.util.Map.Entry; | ||||||
| import me.clip.placeholderapi.PlaceholderAPI; | import me.clip.placeholderapi.PlaceholderAPI; | ||||||
| import me.clip.placeholderapi.PlaceholderAPIPlugin; | import me.clip.placeholderapi.PlaceholderAPIPlugin; | ||||||
| import me.clip.placeholderapi.PlaceholderHook; | import me.clip.placeholderapi.PlaceholderHook; | ||||||
| @@ -29,171 +35,168 @@ import org.bukkit.Bukkit; | |||||||
| import org.bukkit.configuration.file.FileConfiguration; | import org.bukkit.configuration.file.FileConfiguration; | ||||||
| import org.bukkit.event.Listener; | import org.bukkit.event.Listener; | ||||||
|  |  | ||||||
| import java.lang.reflect.Constructor; |  | ||||||
| import java.util.HashMap; |  | ||||||
| import java.util.List; |  | ||||||
| import java.util.Map; |  | ||||||
| import java.util.Map.Entry; |  | ||||||
|  |  | ||||||
| public final class ExpansionManager { | public final class ExpansionManager { | ||||||
|  |  | ||||||
| 	private PlaceholderAPIPlugin plugin; |   private PlaceholderAPIPlugin plugin; | ||||||
|  |   private final Map<String, PlaceholderExpansion> cache = new HashMap<>(); | ||||||
|  |  | ||||||
| 	private final Map<String, PlaceholderExpansion> cache = new HashMap<>(); |   public ExpansionManager(PlaceholderAPIPlugin instance) { | ||||||
|  |     plugin = instance; | ||||||
|  |     File f = new File(PlaceholderAPIPlugin.getInstance().getDataFolder(), "expansions"); | ||||||
|  |     if (!f.exists()) { | ||||||
|  |       if (!f.mkdir()) { | ||||||
|  |         PlaceholderAPIPlugin.getInstance().getLogger() | ||||||
|  |             .severe("Failed to create expansions folder!"); | ||||||
|  |       } | ||||||
|  |     } | ||||||
|  |   } | ||||||
|  |  | ||||||
| 	public ExpansionManager(PlaceholderAPIPlugin instance) { |   public void clean() { | ||||||
| 		plugin = instance; |     cache.clear(); | ||||||
| 	} |   } | ||||||
|  |  | ||||||
| 	public void clean() { |   public PlaceholderExpansion getCachedExpansion(String plugin) { | ||||||
| 		cache.clear(); |     return cache.getOrDefault(plugin, null); | ||||||
| 	} |   } | ||||||
|  |  | ||||||
| 	public PlaceholderExpansion getCachedExpansion(String plugin) { |   public boolean removeCachedExpansion(String identifier) { | ||||||
| 		return cache.getOrDefault(plugin, null); |     return cache.remove(identifier) != null; | ||||||
| 	} |   } | ||||||
|  |  | ||||||
| 	public boolean removeCachedExpansion(String identifier) { |   public PlaceholderExpansion getRegisteredExpansion(String name) { | ||||||
| 		return cache.remove(identifier) != null; |     for (Entry<String, PlaceholderHook> hook : PlaceholderAPI.getPlaceholders().entrySet()) { | ||||||
| 	} |       if (hook.getValue() instanceof PlaceholderExpansion) { | ||||||
|  |         if (name.equalsIgnoreCase(hook.getKey())) { | ||||||
|  |           return (PlaceholderExpansion) hook.getValue(); | ||||||
|  |         } | ||||||
|  |       } | ||||||
|  |     } | ||||||
|  |     return null; | ||||||
|  |   } | ||||||
|  |  | ||||||
| 	public PlaceholderExpansion getRegisteredExpansion(String name) { |   public boolean registerExpansion(PlaceholderExpansion expansion) { | ||||||
| 		for (Entry<String, PlaceholderHook> hook : PlaceholderAPI.getPlaceholders().entrySet()) { |     if (expansion == null || expansion.getIdentifier() == null) { | ||||||
| 			if (hook.getValue() instanceof PlaceholderExpansion) { |       return false; | ||||||
| 				if (name.equalsIgnoreCase(hook.getKey())) { |     } | ||||||
| 					return (PlaceholderExpansion) hook.getValue(); |     if (expansion instanceof Configurable) { | ||||||
| 				} |       Map<String, Object> defaults = ((Configurable) expansion).getDefaults(); | ||||||
| 			} |       String pre = "expansions." + expansion.getIdentifier() + "."; | ||||||
| 		} |       FileConfiguration cfg = plugin.getConfig(); | ||||||
| 		return null; |       boolean save = false; | ||||||
| 	} |       for (Entry<String, Object> entries : defaults.entrySet()) { | ||||||
|  |         if (entries.getKey() == null || entries.getKey().isEmpty()) { | ||||||
|  |           continue; | ||||||
|  |         } | ||||||
|  |         if (entries.getValue() == null) { | ||||||
|  |           if (cfg.contains(pre + entries.getKey())) { | ||||||
|  |             save = true; | ||||||
|  |             cfg.set(pre + entries.getKey(), null); | ||||||
|  |           } | ||||||
|  |         } else { | ||||||
|  |           if (!cfg.contains(pre + entries.getKey())) { | ||||||
|  |             save = true; | ||||||
|  |             cfg.set(pre + entries.getKey(), entries.getValue()); | ||||||
|  |           } | ||||||
|  |         } | ||||||
|  |       } | ||||||
|  |       if (save) { | ||||||
|  |         plugin.saveConfig(); | ||||||
|  |         plugin.reloadConfig(); | ||||||
|  |       } | ||||||
|  |     } | ||||||
|  |     if (expansion instanceof VersionSpecific) { | ||||||
|  |       VersionSpecific nms = (VersionSpecific) expansion; | ||||||
|  |       if (!nms.isCompatibleWith(PlaceholderAPIPlugin.getServerVersion())) { | ||||||
|  |         plugin.getLogger() | ||||||
|  |             .info("Your server version is not compatible with expansion: " + expansion.getIdentifier() | ||||||
|  |                 + " version: " + expansion.getVersion()); | ||||||
|  |         return false; | ||||||
|  |       } | ||||||
|  |     } | ||||||
|  |     if (!expansion.canRegister()) { | ||||||
|  |       if (expansion.getRequiredPlugin() != null) { | ||||||
|  |         cache.put(expansion.getRequiredPlugin().toLowerCase(), expansion); | ||||||
|  |       } | ||||||
|  |       return false; | ||||||
|  |     } | ||||||
|  |     if (!expansion.register()) { | ||||||
|  |       return false; | ||||||
|  |     } | ||||||
|  |     if (expansion instanceof Listener) { | ||||||
|  |       Listener l = (Listener) expansion; | ||||||
|  |       Bukkit.getPluginManager().registerEvents(l, plugin); | ||||||
|  |     } | ||||||
|  |     plugin.getLogger().info("Successfully registered expansion: " + expansion.getIdentifier()); | ||||||
|  |     if (expansion instanceof Taskable) { | ||||||
|  |       ((Taskable) expansion).start(); | ||||||
|  |     } | ||||||
|  |     if (plugin.getExpansionCloud() != null) { | ||||||
|  |       CloudExpansion ce = plugin.getExpansionCloud().getCloudExpansion(expansion.getIdentifier()); | ||||||
|  |       if (ce != null) { | ||||||
|  |         ce.setHasExpansion(true); | ||||||
|  |         if (!ce.getLatestVersion().equals(expansion.getVersion())) { | ||||||
|  |           ce.setShouldUpdate(true); | ||||||
|  |         } | ||||||
|  |       } | ||||||
|  |     } | ||||||
|  |     return true; | ||||||
|  |   } | ||||||
|  |  | ||||||
| 	public boolean registerExpansion(PlaceholderExpansion c) { |  | ||||||
|  |  | ||||||
| 		if (c == null || c.getIdentifier() == null) { |   public PlaceholderExpansion registerExpansion(String fileName) { | ||||||
| 			return false; |     List<Class<?>> subs = FileUtil.getClasses("expansions", fileName, PlaceholderExpansion.class); | ||||||
| 		} |     if (subs == null || subs.isEmpty()) { | ||||||
|  |       return null; | ||||||
|  |     } | ||||||
|  |     // only register the first instance found as an expansion jar should only have 1 class | ||||||
|  |     // extending PlaceholderExpansion | ||||||
|  |     PlaceholderExpansion ex = createInstance(subs.get(0)); | ||||||
|  |     if (registerExpansion(ex)) { | ||||||
|  |       return ex; | ||||||
|  |     } | ||||||
|  |     return null; | ||||||
|  |   } | ||||||
|  |  | ||||||
| 		if (c instanceof Configurable) { |   public void registerAllExpansions() { | ||||||
|  |     if (plugin == null) { | ||||||
|  |       return; | ||||||
|  |     } | ||||||
|  |     List<Class<?>> subs = FileUtil.getClasses("expansions", null, PlaceholderExpansion.class); | ||||||
|  |     if (subs == null || subs.isEmpty()) { | ||||||
|  |       return; | ||||||
|  |     } | ||||||
|  |     for (Class<?> klass : subs) { | ||||||
|  |       PlaceholderExpansion ex = createInstance(klass); | ||||||
|  |       if (ex != null) { | ||||||
|  |         registerExpansion(ex); | ||||||
|  |       } | ||||||
|  |     } | ||||||
|  |   } | ||||||
|  |  | ||||||
| 			Map<String, Object> defaults = ((Configurable) c).getDefaults(); |   private PlaceholderExpansion createInstance(Class<?> klass) { | ||||||
| 			String pre = "expansions." + c.getIdentifier() + "."; |     if (klass == null) { | ||||||
| 			FileConfiguration cfg = plugin.getConfig(); |       return null; | ||||||
|  |     } | ||||||
| 			boolean save = false; |     PlaceholderExpansion ex = null; | ||||||
|  |     if (!klass.isAssignableFrom(PlaceholderExpansion.class)) { | ||||||
| 			for (Entry<String, Object> entries : defaults.entrySet()) { |       return null; | ||||||
| 				if (entries.getKey() == null || entries.getKey().isEmpty()) { |     } | ||||||
| 					continue; |     try { | ||||||
| 				} |       Constructor<?>[] c = klass.getConstructors(); | ||||||
|  |       if (c.length == 0) { | ||||||
| 				if (entries.getValue() == null) { |         ex = (PlaceholderExpansion) klass.newInstance(); | ||||||
| 					if (cfg.contains(pre + entries.getKey())) { |       } else { | ||||||
| 						save = true; |         for (Constructor<?> con : c) { | ||||||
| 						cfg.set(pre + entries.getKey(), null); |           if (con.getParameterTypes().length == 0) { | ||||||
| 					} |             ex = (PlaceholderExpansion) klass.newInstance(); | ||||||
| 				} else { |             break; | ||||||
| 					if (!cfg.contains(pre + entries.getKey())) { |           } | ||||||
| 						save = true; |         } | ||||||
| 						cfg.set(pre + entries.getKey(), entries.getValue()); |       } | ||||||
| 					} |     } catch (Throwable t) { | ||||||
| 				} |       plugin.getLogger().severe("Failed to init placeholder expansion from class: " + klass.getName()); | ||||||
| 			} |       plugin.getLogger().severe(t.getMessage()); | ||||||
|  |     } | ||||||
| 			if (save) { |     return ex; | ||||||
| 				plugin.saveConfig(); |   } | ||||||
| 				plugin.reloadConfig(); |  | ||||||
| 			} |  | ||||||
| 		} |  | ||||||
|  |  | ||||||
| 		if (c instanceof VersionSpecific) { |  | ||||||
| 			VersionSpecific nms = (VersionSpecific) c; |  | ||||||
| 			if (!nms.isCompatibleWith(PlaceholderAPIPlugin.getServerVersion())) { |  | ||||||
| 				plugin.getLogger().info("Your server version is not compatible with expansion: " + c.getIdentifier() |  | ||||||
| 						+ " version: " + c.getVersion()); |  | ||||||
| 				return false; |  | ||||||
| 			} |  | ||||||
| 		} |  | ||||||
|  |  | ||||||
| 		if (!c.canRegister()) { |  | ||||||
| 			if (c.getRequiredPlugin() != null) { |  | ||||||
| 				cache.put(c.getRequiredPlugin().toLowerCase(), c); |  | ||||||
| 			} |  | ||||||
| 			return false; |  | ||||||
| 		} |  | ||||||
|  |  | ||||||
| 		if (!c.register()) { |  | ||||||
| 			return false; |  | ||||||
| 		} |  | ||||||
|  |  | ||||||
| 		if (c instanceof Listener) { |  | ||||||
| 			Listener l = (Listener) c; |  | ||||||
| 			Bukkit.getPluginManager().registerEvents(l, plugin); |  | ||||||
| 		} |  | ||||||
|  |  | ||||||
| 		plugin.getLogger().info("Successfully registered expansion: " + c.getIdentifier()); |  | ||||||
|  |  | ||||||
| 		if (c instanceof Taskable) { |  | ||||||
| 			((Taskable) c).start(); |  | ||||||
| 		} |  | ||||||
| 		return true; |  | ||||||
| 	} |  | ||||||
|  |  | ||||||
| 	public void registerAllExpansions() { |  | ||||||
|  |  | ||||||
| 		if (plugin == null) { |  | ||||||
| 			return; |  | ||||||
| 		} |  | ||||||
|  |  | ||||||
| 		List<Class<?>> subs = FileUtil.getClasses("expansions", PlaceholderExpansion.class); |  | ||||||
|  |  | ||||||
| 		if (subs == null || subs.isEmpty()) { |  | ||||||
| 			return; |  | ||||||
| 		} |  | ||||||
|  |  | ||||||
| 		for (Class<?> klass : subs) { |  | ||||||
|  |  | ||||||
| 			if (klass == null) { |  | ||||||
| 				continue; |  | ||||||
| 			} |  | ||||||
|  |  | ||||||
| 			try { |  | ||||||
|  |  | ||||||
| 				PlaceholderExpansion ex = null; |  | ||||||
|  |  | ||||||
| 				Constructor<?>[] c = klass.getConstructors(); |  | ||||||
|  |  | ||||||
| 				if (c.length == 0) { |  | ||||||
| 					ex = (PlaceholderExpansion) klass.newInstance(); |  | ||||||
| 				} else { |  | ||||||
| 					for (Constructor<?> con : c) { |  | ||||||
| 						if (con.getParameterTypes().length == 0) { |  | ||||||
| 							ex = (PlaceholderExpansion) klass.newInstance(); |  | ||||||
| 							break; |  | ||||||
| 						} |  | ||||||
| 					} |  | ||||||
| 				} |  | ||||||
|  |  | ||||||
| 				if (ex == null) { |  | ||||||
| 					continue; |  | ||||||
| 				} |  | ||||||
|  |  | ||||||
| 				if (registerExpansion(ex)) { |  | ||||||
| 					if (plugin.getExpansionCloud() != null) { |  | ||||||
| 						CloudExpansion ce = plugin.getExpansionCloud().getCloudExpansion(ex.getIdentifier()); |  | ||||||
| 						if (ce != null) { |  | ||||||
| 							ce.setHasExpansion(true); |  | ||||||
| 							if (!ce.getVersion().equals(ex.getVersion())) { |  | ||||||
| 								ce.setShouldUpdate(true); |  | ||||||
| 							} |  | ||||||
| 						} |  | ||||||
| 					} |  | ||||||
| 				} |  | ||||||
|  |  | ||||||
| 			} catch (Throwable t) { |  | ||||||
| 				plugin.getLogger().severe("Failed to load placeholder expansion from class: " + klass.getName()); |  | ||||||
| 				plugin.getLogger().severe(t.getMessage()); |  | ||||||
| 			} |  | ||||||
| 		} |  | ||||||
| 	} |  | ||||||
| } | } | ||||||
|   | |||||||
		Reference in New Issue
	
	Block a user