mirror of
https://github.com/PlaceholderAPI/PlaceholderAPI
synced 2025-02-05 15:55:28 +01:00
Added method to register expansion by specifying the file name
This commit is contained in:
parent
adca215ea7
commit
43b8013c29
@ -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,20 +35,20 @@ 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) {
|
public ExpansionManager(PlaceholderAPIPlugin instance) {
|
||||||
plugin = 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 void clean() {
|
public void clean() {
|
||||||
@ -68,25 +74,19 @@ public final class ExpansionManager {
|
|||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
public boolean registerExpansion(PlaceholderExpansion c) {
|
public boolean registerExpansion(PlaceholderExpansion expansion) {
|
||||||
|
if (expansion == null || expansion.getIdentifier() == null) {
|
||||||
if (c == null || c.getIdentifier() == null) {
|
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
if (expansion instanceof Configurable) {
|
||||||
if (c instanceof Configurable) {
|
Map<String, Object> defaults = ((Configurable) expansion).getDefaults();
|
||||||
|
String pre = "expansions." + expansion.getIdentifier() + ".";
|
||||||
Map<String, Object> defaults = ((Configurable) c).getDefaults();
|
|
||||||
String pre = "expansions." + c.getIdentifier() + ".";
|
|
||||||
FileConfiguration cfg = plugin.getConfig();
|
FileConfiguration cfg = plugin.getConfig();
|
||||||
|
|
||||||
boolean save = false;
|
boolean save = false;
|
||||||
|
|
||||||
for (Entry<String, Object> entries : defaults.entrySet()) {
|
for (Entry<String, Object> entries : defaults.entrySet()) {
|
||||||
if (entries.getKey() == null || entries.getKey().isEmpty()) {
|
if (entries.getKey() == null || entries.getKey().isEmpty()) {
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (entries.getValue() == null) {
|
if (entries.getValue() == null) {
|
||||||
if (cfg.contains(pre + entries.getKey())) {
|
if (cfg.contains(pre + entries.getKey())) {
|
||||||
save = true;
|
save = true;
|
||||||
@ -99,70 +99,90 @@ public final class ExpansionManager {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (save) {
|
if (save) {
|
||||||
plugin.saveConfig();
|
plugin.saveConfig();
|
||||||
plugin.reloadConfig();
|
plugin.reloadConfig();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
if (expansion instanceof VersionSpecific) {
|
||||||
if (c instanceof VersionSpecific) {
|
VersionSpecific nms = (VersionSpecific) expansion;
|
||||||
VersionSpecific nms = (VersionSpecific) c;
|
|
||||||
if (!nms.isCompatibleWith(PlaceholderAPIPlugin.getServerVersion())) {
|
if (!nms.isCompatibleWith(PlaceholderAPIPlugin.getServerVersion())) {
|
||||||
plugin.getLogger().info("Your server version is not compatible with expansion: " + c.getIdentifier()
|
plugin.getLogger()
|
||||||
+ " version: " + c.getVersion());
|
.info("Your server version is not compatible with expansion: " + expansion.getIdentifier()
|
||||||
|
+ " version: " + expansion.getVersion());
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
if (!expansion.canRegister()) {
|
||||||
if (!c.canRegister()) {
|
if (expansion.getRequiredPlugin() != null) {
|
||||||
if (c.getRequiredPlugin() != null) {
|
cache.put(expansion.getRequiredPlugin().toLowerCase(), expansion);
|
||||||
cache.put(c.getRequiredPlugin().toLowerCase(), c);
|
|
||||||
}
|
}
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
if (!expansion.register()) {
|
||||||
if (!c.register()) {
|
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
if (expansion instanceof Listener) {
|
||||||
if (c instanceof Listener) {
|
Listener l = (Listener) expansion;
|
||||||
Listener l = (Listener) c;
|
|
||||||
Bukkit.getPluginManager().registerEvents(l, plugin);
|
Bukkit.getPluginManager().registerEvents(l, plugin);
|
||||||
}
|
}
|
||||||
|
plugin.getLogger().info("Successfully registered expansion: " + expansion.getIdentifier());
|
||||||
plugin.getLogger().info("Successfully registered expansion: " + c.getIdentifier());
|
if (expansion instanceof Taskable) {
|
||||||
|
((Taskable) expansion).start();
|
||||||
if (c instanceof Taskable) {
|
}
|
||||||
((Taskable) c).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;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void registerAllExpansions() {
|
|
||||||
|
|
||||||
|
public PlaceholderExpansion registerExpansion(String fileName) {
|
||||||
|
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;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void registerAllExpansions() {
|
||||||
if (plugin == null) {
|
if (plugin == null) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
List<Class<?>> subs = FileUtil.getClasses("expansions", null, PlaceholderExpansion.class);
|
||||||
List<Class<?>> subs = FileUtil.getClasses("expansions", PlaceholderExpansion.class);
|
|
||||||
|
|
||||||
if (subs == null || subs.isEmpty()) {
|
if (subs == null || subs.isEmpty()) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
for (Class<?> klass : subs) {
|
for (Class<?> klass : subs) {
|
||||||
|
PlaceholderExpansion ex = createInstance(klass);
|
||||||
if (klass == null) {
|
if (ex != null) {
|
||||||
continue;
|
registerExpansion(ex);
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
try {
|
private PlaceholderExpansion createInstance(Class<?> klass) {
|
||||||
|
if (klass == null) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
PlaceholderExpansion ex = null;
|
PlaceholderExpansion ex = null;
|
||||||
|
if (!klass.isAssignableFrom(PlaceholderExpansion.class)) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
try {
|
||||||
Constructor<?>[] c = klass.getConstructors();
|
Constructor<?>[] c = klass.getConstructors();
|
||||||
|
|
||||||
if (c.length == 0) {
|
if (c.length == 0) {
|
||||||
ex = (PlaceholderExpansion) klass.newInstance();
|
ex = (PlaceholderExpansion) klass.newInstance();
|
||||||
} else {
|
} else {
|
||||||
@ -173,27 +193,10 @@ public final class ExpansionManager {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
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) {
|
} catch (Throwable t) {
|
||||||
plugin.getLogger().severe("Failed to load placeholder expansion from class: " + klass.getName());
|
plugin.getLogger().severe("Failed to init placeholder expansion from class: " + klass.getName());
|
||||||
plugin.getLogger().severe(t.getMessage());
|
plugin.getLogger().severe(t.getMessage());
|
||||||
}
|
}
|
||||||
}
|
return ex;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user