2018-03-21 09:15:51 -04:00
|
|
|
/*
|
|
|
|
*
|
|
|
|
* PlaceholderAPI
|
2019-06-21 12:30:12 -04:00
|
|
|
* Copyright (C) 2019 Ryan McCarthy
|
2018-03-21 09:15:51 -04:00
|
|
|
*
|
|
|
|
* 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 <http://www.gnu.org/licenses/>.
|
|
|
|
*
|
|
|
|
*
|
|
|
|
*/
|
2018-03-20 19:04:14 -04:00
|
|
|
package me.clip.placeholderapi.expansion;
|
|
|
|
|
|
|
|
import me.clip.placeholderapi.PlaceholderAPI;
|
|
|
|
import me.clip.placeholderapi.PlaceholderAPIPlugin;
|
2018-03-22 21:15:47 +02:00
|
|
|
import me.clip.placeholderapi.PlaceholderHook;
|
|
|
|
import me.clip.placeholderapi.expansion.cloud.CloudExpansion;
|
|
|
|
import me.clip.placeholderapi.util.FileUtil;
|
2018-03-20 19:04:14 -04:00
|
|
|
import org.bukkit.Bukkit;
|
|
|
|
import org.bukkit.configuration.file.FileConfiguration;
|
|
|
|
import org.bukkit.event.Listener;
|
|
|
|
|
2019-05-10 20:36:43 -04:00
|
|
|
import java.io.File;
|
|
|
|
import java.lang.reflect.Constructor;
|
|
|
|
import java.util.List;
|
|
|
|
import java.util.Map;
|
|
|
|
import java.util.Map.Entry;
|
|
|
|
|
2018-03-20 19:04:14 -04:00
|
|
|
public final class ExpansionManager {
|
2019-06-10 11:10:39 -07:00
|
|
|
private final PlaceholderAPIPlugin plugin;
|
2018-06-29 15:02:05 -04:00
|
|
|
|
|
|
|
public ExpansionManager(PlaceholderAPIPlugin instance) {
|
|
|
|
plugin = instance;
|
2019-06-10 11:03:48 -07:00
|
|
|
|
2018-06-29 15:02:05 -04:00
|
|
|
File f = new File(PlaceholderAPIPlugin.getInstance().getDataFolder(), "expansions");
|
|
|
|
if (!f.exists()) {
|
2018-08-27 00:05:51 -04:00
|
|
|
f.mkdirs();
|
2018-06-29 15:02:05 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public PlaceholderExpansion getRegisteredExpansion(String name) {
|
|
|
|
for (Entry<String, PlaceholderHook> hook : PlaceholderAPI.getPlaceholders().entrySet()) {
|
|
|
|
if (hook.getValue() instanceof PlaceholderExpansion) {
|
|
|
|
if (name.equalsIgnoreCase(hook.getKey())) {
|
|
|
|
return (PlaceholderExpansion) hook.getValue();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2019-06-10 11:03:48 -07:00
|
|
|
|
2018-06-29 15:02:05 -04:00
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
public boolean registerExpansion(PlaceholderExpansion expansion) {
|
|
|
|
if (expansion == null || expansion.getIdentifier() == null) {
|
|
|
|
return false;
|
|
|
|
}
|
2019-06-10 11:03:48 -07:00
|
|
|
|
2018-06-29 15:02:05 -04:00
|
|
|
if (expansion instanceof Configurable) {
|
|
|
|
Map<String, Object> defaults = ((Configurable) expansion).getDefaults();
|
|
|
|
String pre = "expansions." + expansion.getIdentifier() + ".";
|
|
|
|
FileConfiguration cfg = plugin.getConfig();
|
|
|
|
boolean save = false;
|
2019-06-10 11:03:48 -07:00
|
|
|
|
2019-05-10 20:36:43 -04:00
|
|
|
if (defaults != null) {
|
|
|
|
for (Entry<String, Object> entries : defaults.entrySet()) {
|
|
|
|
if (entries.getKey() == null || entries.getKey().isEmpty()) {
|
|
|
|
continue;
|
2018-06-29 15:02:05 -04:00
|
|
|
}
|
2019-06-10 11:03:48 -07:00
|
|
|
|
2019-05-10 20:36:43 -04:00
|
|
|
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());
|
|
|
|
}
|
2018-06-29 15:02:05 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2019-06-10 11:03:48 -07:00
|
|
|
|
2018-06-29 15:02:05 -04:00
|
|
|
if (save) {
|
|
|
|
plugin.saveConfig();
|
|
|
|
plugin.reloadConfig();
|
|
|
|
}
|
|
|
|
}
|
2019-06-10 11:03:48 -07:00
|
|
|
|
2018-06-29 15:02:05 -04:00
|
|
|
if (expansion instanceof VersionSpecific) {
|
|
|
|
VersionSpecific nms = (VersionSpecific) expansion;
|
|
|
|
if (!nms.isCompatibleWith(PlaceholderAPIPlugin.getServerVersion())) {
|
|
|
|
plugin.getLogger()
|
2018-07-16 17:21:33 +10:00
|
|
|
.info(
|
|
|
|
"Your server version is not compatible with expansion: " + expansion.getIdentifier()
|
|
|
|
+ " version: " + expansion.getVersion());
|
2018-06-29 15:02:05 -04:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
2019-06-10 11:03:48 -07:00
|
|
|
|
2018-06-29 15:02:05 -04:00
|
|
|
if (!expansion.canRegister()) {
|
|
|
|
return false;
|
|
|
|
}
|
2019-06-10 11:03:48 -07:00
|
|
|
|
2018-06-29 15:02:05 -04:00
|
|
|
if (!expansion.register()) {
|
|
|
|
return false;
|
|
|
|
}
|
2019-06-10 11:03:48 -07:00
|
|
|
|
2018-06-29 15:02:05 -04:00
|
|
|
if (expansion instanceof Listener) {
|
|
|
|
Listener l = (Listener) expansion;
|
|
|
|
Bukkit.getPluginManager().registerEvents(l, plugin);
|
|
|
|
}
|
2019-06-10 11:03:48 -07:00
|
|
|
|
2018-06-29 15:02:05 -04:00
|
|
|
plugin.getLogger().info("Successfully registered expansion: " + expansion.getIdentifier());
|
2019-06-10 11:03:48 -07:00
|
|
|
|
2018-06-29 15:02:05 -04:00
|
|
|
if (expansion instanceof Taskable) {
|
|
|
|
((Taskable) expansion).start();
|
|
|
|
}
|
2019-06-10 11:03:48 -07:00
|
|
|
|
2018-06-29 15:02:05 -04:00
|
|
|
if (plugin.getExpansionCloud() != null) {
|
|
|
|
CloudExpansion ce = plugin.getExpansionCloud().getCloudExpansion(expansion.getIdentifier());
|
2019-06-10 11:03:48 -07:00
|
|
|
|
2018-06-29 15:02:05 -04:00
|
|
|
if (ce != null) {
|
|
|
|
ce.setHasExpansion(true);
|
|
|
|
if (!ce.getLatestVersion().equals(expansion.getVersion())) {
|
|
|
|
ce.setShouldUpdate(true);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2019-06-10 11:03:48 -07:00
|
|
|
|
2018-06-29 15:02:05 -04:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public PlaceholderExpansion registerExpansion(String fileName) {
|
|
|
|
List<Class<?>> subs = FileUtil.getClasses("expansions", fileName, PlaceholderExpansion.class);
|
|
|
|
if (subs == null || subs.isEmpty()) {
|
|
|
|
return null;
|
|
|
|
}
|
2019-06-10 11:03:48 -07:00
|
|
|
|
2018-06-29 15:02:05 -04:00
|
|
|
// 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;
|
|
|
|
}
|
2019-06-10 11:03:48 -07:00
|
|
|
|
2018-06-29 15:02:05 -04:00
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
public void registerAllExpansions() {
|
|
|
|
if (plugin == null) {
|
|
|
|
return;
|
|
|
|
}
|
2019-06-10 11:03:48 -07:00
|
|
|
|
2018-06-29 15:02:05 -04:00
|
|
|
List<Class<?>> subs = FileUtil.getClasses("expansions", null, PlaceholderExpansion.class);
|
|
|
|
if (subs == null || subs.isEmpty()) {
|
|
|
|
return;
|
|
|
|
}
|
2019-06-10 11:03:48 -07:00
|
|
|
|
2018-06-29 15:02:05 -04:00
|
|
|
for (Class<?> klass : subs) {
|
|
|
|
PlaceholderExpansion ex = createInstance(klass);
|
|
|
|
if (ex != null) {
|
|
|
|
registerExpansion(ex);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private PlaceholderExpansion createInstance(Class<?> klass) {
|
|
|
|
if (klass == null) {
|
|
|
|
return null;
|
|
|
|
}
|
2019-06-10 11:03:48 -07:00
|
|
|
|
2018-06-29 15:02:05 -04:00
|
|
|
PlaceholderExpansion ex = null;
|
2018-07-16 17:21:33 +10:00
|
|
|
if (!PlaceholderExpansion.class.isAssignableFrom(klass)) {
|
2018-06-29 15:02:05 -04:00
|
|
|
return null;
|
|
|
|
}
|
2019-06-10 11:03:48 -07:00
|
|
|
|
2018-06-29 15:02:05 -04:00
|
|
|
try {
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} catch (Throwable t) {
|
2018-07-16 17:21:33 +10:00
|
|
|
plugin.getLogger()
|
|
|
|
.severe("Failed to init placeholder expansion from class: " + klass.getName());
|
2018-06-29 15:02:05 -04:00
|
|
|
plugin.getLogger().severe(t.getMessage());
|
|
|
|
}
|
2019-06-10 11:03:48 -07:00
|
|
|
|
2018-06-29 15:02:05 -04:00
|
|
|
return ex;
|
|
|
|
}
|
2018-03-20 19:04:14 -04:00
|
|
|
}
|