Crypto Morin 54d5757d0a
Performance Improvements (#340)
* Performance Improvements

* More Optimizations

* Even More Optimizations & Cleanups

* Almost a recode I guess
2020-07-16 12:32:22 -04:00

188 lines
6.6 KiB
Java

/*
*
* PlaceholderAPI
* Copyright (C) 2019 Ryan McCarthy
*
* 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/>.
*
*
*/
package me.clip.placeholderapi.expansion;
import com.google.common.base.Strings;
import me.clip.placeholderapi.PlaceholderAPI;
import me.clip.placeholderapi.PlaceholderAPIPlugin;
import me.clip.placeholderapi.PlaceholderHook;
import me.clip.placeholderapi.expansion.cloud.CloudExpansion;
import me.clip.placeholderapi.util.FileUtil;
import org.bukkit.Bukkit;
import org.bukkit.configuration.file.FileConfiguration;
import org.bukkit.event.Listener;
import java.io.File;
import java.lang.reflect.Constructor;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
public final class ExpansionManager {
private final PlaceholderAPIPlugin plugin;
public ExpansionManager(PlaceholderAPIPlugin instance) {
plugin = instance;
File f = new File(plugin.getDataFolder(), "expansions");
if (!f.exists()) f.mkdirs();
}
public PlaceholderExpansion getRegisteredExpansion(String name) {
for (Entry<String, PlaceholderHook> hook : PlaceholderAPI.getPlaceholders().entrySet()) {
if (hook.getValue().isExpansion()) {
if (name.equalsIgnoreCase(hook.getKey())) {
return (PlaceholderExpansion) hook.getValue();
}
}
}
return null;
}
public boolean registerExpansion(PlaceholderExpansion expansion) {
if (expansion == null || expansion.getIdentifier() == null) return false;
if (expansion instanceof Configurable) {
Map<String, Object> defaults = ((Configurable) expansion).getDefaults();
String pre = expansion.getPathStarter();
FileConfiguration cfg = plugin.getConfig();
boolean save = false;
if (defaults != null) {
for (Entry<String, Object> entry : defaults.entrySet()) {
String key = entry.getKey();
if (Strings.isNullOrEmpty(key)) continue;
if (entry.getValue() == null) {
if (cfg.contains(pre + key)) {
save = true;
cfg.set(pre + key, null);
}
} else {
if (!cfg.contains(pre + key)) {
save = true;
cfg.set(pre + key, entry.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()) return false;
if (!expansion.register()) return false;
if (expansion instanceof Listener) {
Bukkit.getPluginManager().registerEvents((Listener) expansion, 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 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() {
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) {
try {
registerExpansion(ex);
} catch (Exception e) {
plugin.getLogger().info("Couldn't register " + ex.getIdentifier() + " expansion");
e.printStackTrace();
}
}
}
}
private PlaceholderExpansion createInstance(Class<?> clazz) {
if (clazz == null) return null;
if (!PlaceholderExpansion.class.isAssignableFrom(clazz)) return null;
PlaceholderExpansion expansion = null;
try {
Constructor<?>[] constructors = clazz.getConstructors();
if (constructors.length == 0) {
expansion = (PlaceholderExpansion) clazz.newInstance();
} else {
for (Constructor<?> ctor : constructors) {
if (ctor.getParameterTypes().length == 0) {
expansion = (PlaceholderExpansion) ctor.newInstance();
break;
}
}
}
} catch (Throwable t) {
plugin.getLogger()
.severe("Failed to init placeholder expansion from class: " + clazz.getName());
plugin.getLogger().severe(t.getMessage());
}
return expansion;
}
}