Revert "Performance Improvements (#340)"

This reverts commit 54d5757d
This commit is contained in:
extendedclip
2020-07-20 16:57:16 -04:00
parent 8972f7cff4
commit 4ce0b03852
52 changed files with 873 additions and 874 deletions

View File

@@ -28,6 +28,7 @@ package me.clip.placeholderapi.expansion;
* @author Ryan McCarthy
*/
public interface Cacheable {
/**
* Called when the implementing class is unregistered from PlaceholderAPI
*/

View File

@@ -30,10 +30,11 @@ import org.bukkit.entity.Player;
* @author Ryan McCarthy
*/
public interface Cleanable {
/**
* Called when a player leaves the server
*
* @param player (@link Player} who left the server
* @param p (@link Player} who left the server
*/
void cleanup(Player player);
void cleanup(Player p);
}

View File

@@ -20,7 +20,6 @@
*/
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;
@@ -42,13 +41,15 @@ public final class ExpansionManager {
public ExpansionManager(PlaceholderAPIPlugin instance) {
plugin = instance;
File f = new File(plugin.getDataFolder(), "expansions");
if (!f.exists()) f.mkdirs();
File f = new File(PlaceholderAPIPlugin.getInstance().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 (hook.getValue() instanceof PlaceholderExpansion) {
if (name.equalsIgnoreCase(hook.getKey())) {
return (PlaceholderExpansion) hook.getValue();
}
@@ -59,28 +60,31 @@ public final class ExpansionManager {
}
public boolean registerExpansion(PlaceholderExpansion expansion) {
if (expansion == null || expansion.getIdentifier() == null) return false;
if (expansion == null || expansion.getIdentifier() == null) {
return false;
}
if (expansion instanceof Configurable) {
Map<String, Object> defaults = ((Configurable) expansion).getDefaults();
String pre = expansion.getPathStarter();
String pre = "expansions." + expansion.getIdentifier() + ".";
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;
for (Entry<String, Object> entries : defaults.entrySet()) {
if (entries.getKey() == null || entries.getKey().isEmpty()) {
continue;
}
if (entry.getValue() == null) {
if (cfg.contains(pre + key)) {
if (entries.getValue() == null) {
if (cfg.contains(pre + entries.getKey())) {
save = true;
cfg.set(pre + key, null);
cfg.set(pre + entries.getKey(), null);
}
} else {
if (!cfg.contains(pre + key)) {
if (!cfg.contains(pre + entries.getKey())) {
save = true;
cfg.set(pre + key, entry.getValue());
cfg.set(pre + entries.getKey(), entries.getValue());
}
}
}
@@ -103,11 +107,17 @@ public final class ExpansionManager {
}
}
if (!expansion.canRegister()) return false;
if (!expansion.register()) return false;
if (!expansion.canRegister()) {
return false;
}
if (!expansion.register()) {
return false;
}
if (expansion instanceof Listener) {
Bukkit.getPluginManager().registerEvents((Listener) expansion, plugin);
Listener l = (Listener) expansion;
Bukkit.getPluginManager().registerEvents(l, plugin);
}
plugin.getLogger().info("Successfully registered expansion: " + expansion.getIdentifier());
@@ -133,18 +143,29 @@ public final class ExpansionManager {
public PlaceholderExpansion registerExpansion(String fileName) {
List<Class<?>> subs = FileUtil.getClasses("expansions", fileName, PlaceholderExpansion.class);
if (subs == null || subs.isEmpty()) return null;
if (subs == null || subs.isEmpty()) {
return null;
}
// Only register the first instance found as an expansion JAR should only have 1 class
// 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;
if (registerExpansion(ex)) {
return ex;
}
return null;
}
public void registerAllExpansions() {
if (plugin == null) {
return;
}
List<Class<?>> subs = FileUtil.getClasses("expansions", null, PlaceholderExpansion.class);
if (subs == null || subs.isEmpty()) return;
if (subs == null || subs.isEmpty()) {
return;
}
for (Class<?> klass : subs) {
PlaceholderExpansion ex = createInstance(klass);
@@ -159,29 +180,34 @@ public final class ExpansionManager {
}
}
private PlaceholderExpansion createInstance(Class<?> clazz) {
if (clazz == null) return null;
if (!PlaceholderExpansion.class.isAssignableFrom(clazz)) return null;
private PlaceholderExpansion createInstance(Class<?> klass) {
if (klass == null) {
return null;
}
PlaceholderExpansion ex = null;
if (!PlaceholderExpansion.class.isAssignableFrom(klass)) {
return null;
}
PlaceholderExpansion expansion = null;
try {
Constructor<?>[] constructors = clazz.getConstructors();
if (constructors.length == 0) {
expansion = (PlaceholderExpansion) clazz.newInstance();
Constructor<?>[] c = klass.getConstructors();
if (c.length == 0) {
ex = (PlaceholderExpansion) klass.newInstance();
} else {
for (Constructor<?> ctor : constructors) {
if (ctor.getParameterTypes().length == 0) {
expansion = (PlaceholderExpansion) ctor.newInstance();
for (Constructor<?> con : c) {
if (con.getParameterTypes().length == 0) {
ex = (PlaceholderExpansion) klass.newInstance();
break;
}
}
}
} catch (Throwable t) {
plugin.getLogger()
.severe("Failed to init placeholder expansion from class: " + clazz.getName());
.severe("Failed to init placeholder expansion from class: " + klass.getName());
plugin.getLogger().severe(t.getMessage());
}
return expansion;
return ex;
}
}

View File

@@ -20,28 +20,26 @@
*/
package me.clip.placeholderapi.expansion;
import com.google.common.base.Enums;
import java.util.Optional;
public enum NMSVersion {
UNKNOWN("unknown"),
SPIGOT_1_7_R1("v1_7_R1"),
SPIGOT_1_7_R2("v1_7_R2"),
SPIGOT_1_7_R3("v1_7_R3"),
SPIGOT_1_7_R4("v1_7_R4"),
SPIGOT_1_8_R1("v1_8_R1"),
SPIGOT_1_8_R2("v1_8_R2"),
SPIGOT_1_8_R3("v1_8_R3"),
SPIGOT_1_9_R1("v1_9_R1"),
SPIGOT_1_9_R2("v1_9_R2"),
SPIGOT_1_10_R1("v1_10_R1"),
SPIGOT_1_11_R1("v1_11_R1"),
SPIGOT_1_12_R1("v1_12_R1"),
SPIGOT_1_13_R1("v1_13_R1"),
SPIGOT_1_13_R2("v1_13_R2"),
SPIGOT_1_14_R1("v1_14_R1"),
SPIGOT_1_15_R1("v1_15_R1");
UNKNOWN("unknown"),
SPIGOT_1_7_R1("v1_7_R1"),
SPIGOT_1_7_R2("v1_7_R2"),
SPIGOT_1_7_R3("v1_7_R3"),
SPIGOT_1_7_R4("v1_7_R4"),
SPIGOT_1_8_R1("v1_8_R1"),
SPIGOT_1_8_R2("v1_8_R2"),
SPIGOT_1_8_R3("v1_8_R3"),
SPIGOT_1_9_R1("v1_9_R1"),
SPIGOT_1_9_R2("v1_9_R2"),
SPIGOT_1_10_R1("v1_10_R1"),
SPIGOT_1_11_R1("v1_11_R1"),
SPIGOT_1_12_R1("v1_12_R1"),
SPIGOT_1_13_R1("v1_13_R1"),
SPIGOT_1_13_R2("v1_13_R2"),
SPIGOT_1_14_R1("v1_14_R1"),
SPIGOT_1_15_R1("v1_15_R1"),
SPIGOT_1_16_R1("v1_16_R1");
private final String version;
@@ -50,12 +48,17 @@ public enum NMSVersion {
}
public static NMSVersion getVersion(String version) {
// Guava caches values() as well.
Optional<NMSVersion> opt = Enums.getIfPresent(NMSVersion.class, version).toJavaUtil();
return opt.orElse(NMSVersion.UNKNOWN);
for (NMSVersion v : values()) {
if (v.getVersion().equalsIgnoreCase(version)) {
return v;
}
}
return NMSVersion.UNKNOWN;
}
public String getVersion() {
return version;
}
}

View File

@@ -26,11 +26,11 @@ import me.clip.placeholderapi.PlaceholderHook;
import org.apache.commons.lang.Validate;
import org.bukkit.Bukkit;
import org.bukkit.configuration.ConfigurationSection;
import org.bukkit.configuration.file.FileConfiguration;
import java.util.List;
public abstract class PlaceholderExpansion extends PlaceholderHook {
/**
* The name of this expansion
*
@@ -123,58 +123,60 @@ public abstract class PlaceholderExpansion extends PlaceholderHook {
}
/**
* Quick getter for the {@link PlaceholderAPIPlugin} config.
* Quick getter for the {@link PlaceholderAPIPlugin} instance
*
* @return {@link PlaceholderAPIPlugin} config instance.
* @return {@link PlaceholderAPIPlugin} instance
*/
public FileConfiguration getConfig() {
return PlaceholderAPIPlugin.getInstance().getConfig();
public PlaceholderAPIPlugin getPlaceholderAPI() {
return PlaceholderAPIPlugin.getInstance();
}
public String getString(String path, String def) {
return getConfig().getString(getPathStarter() + path, def);
return getPlaceholderAPI().getConfig()
.getString("expansions." + getIdentifier() + "." + path, def);
}
public int getInt(String path, int def) {
return getConfig().getInt(getPathStarter() + path, def);
return getPlaceholderAPI().getConfig()
.getInt("expansions." + getIdentifier() + "." + path, def);
}
public long getLong(String path, long def) {
return getConfig().getLong(getPathStarter() + path, def);
return getPlaceholderAPI().getConfig()
.getLong("expansions." + getIdentifier() + "." + path, def);
}
public double getDouble(String path, double def) {
return getConfig().getDouble(getPathStarter() + path, def);
return getPlaceholderAPI().getConfig()
.getDouble("expansions." + getIdentifier() + "." + path, def);
}
public List<String> getStringList(String path) {
return getConfig().getStringList(getPathStarter() + path);
return getPlaceholderAPI().getConfig()
.getStringList("expansions." + getIdentifier() + "." + path);
}
public Object get(String path, Object def) {
return getConfig().get(getPathStarter() + path, def);
return getPlaceholderAPI().getConfig().get("expansions." + getIdentifier() + "." + path, def);
}
public ConfigurationSection getConfigSection(String path) {
return getConfig().getConfigurationSection(getPathStarter() + path);
return getPlaceholderAPI().getConfig()
.getConfigurationSection("expansions." + getIdentifier() + "." + path);
}
public ConfigurationSection getConfigSection() {
return getConfig().getConfigurationSection("expansions." + getIdentifier());
return getPlaceholderAPI().getConfig().getConfigurationSection("expansions." + getIdentifier());
}
public boolean configurationContains(String path) {
return getConfig().contains(getPathStarter() + path);
return getPlaceholderAPI().getConfig().contains("expansions." + getIdentifier() + "." + path);
}
protected String getPathStarter() {
return "expansions." + getIdentifier() + '.';
}
/**
* @deprecated As of versions greater than 2.8.7, use {@link #getRequiredPlugin()}
*/
@SuppressWarnings("DeprecatedIsStillUsed")
@Deprecated
public String getPlugin() {
return null;

View File

@@ -23,5 +23,6 @@ package me.clip.placeholderapi.expansion;
import org.bukkit.entity.Player;
public interface Relational {
String onPlaceholderRequest(Player one, Player two, String identifier);
}

View File

@@ -20,7 +20,9 @@
*/
package me.clip.placeholderapi.expansion;
public interface Taskable {
/**
* Called when the implementing class has successfully been registered to the placeholder map
* Tasks that need to be performed when this expansion is registered should go here

View File

@@ -21,6 +21,7 @@
package me.clip.placeholderapi.expansion;
public final class Version {
private final boolean isSpigot;
private final String version;
@@ -40,4 +41,5 @@ public final class Version {
public boolean compareTo(String version) {
return getVersion().equalsIgnoreCase(version);
}
}

View File

@@ -36,5 +36,5 @@ public interface VersionSpecific {
*
* @return true if your expansion is compatible with the version the server is running.
*/
boolean isCompatibleWith(Version version);
boolean isCompatibleWith(Version v);
}

View File

@@ -26,8 +26,11 @@ import java.util.List;
import java.util.concurrent.TimeUnit;
import java.util.stream.Collectors;
public class CloudExpansion {
private String name, author,
private String name,
author,
latest_version,
description,
source_url,
@@ -71,12 +74,14 @@ public class CloudExpansion {
}
public Version getVersion() {
return latest_version == null ? null : getVersion(latest_version);
return getLatestVersion() == null ? null : getVersion(getLatestVersion());
}
public Version getVersion(String version) {
return versions == null ? null : versions.stream()
.filter(v -> v.getVersion().equals(version)).findFirst().orElse(null);
.filter(v -> v.getVersion().equals(version))
.findFirst()
.orElse(null);
}
public List<String> getAvailableVersions() {
@@ -135,10 +140,6 @@ public class CloudExpansion {
return verified;
}
public void setVerified(boolean verified) {
this.verified = verified;
}
public long getLastUpdate() {
return last_update;
}
@@ -155,10 +156,6 @@ public class CloudExpansion {
return average_rating;
}
public void setAverage_rating(double average_rating) {
this.average_rating = average_rating;
}
public List<String> getPlaceholders() {
return placeholders;
}
@@ -175,11 +172,7 @@ public class CloudExpansion {
this.versions = versions;
}
public void setRatings_count(long ratings_count) {
this.ratings_count = ratings_count;
}
public static class Version {
public class Version {
private String url, version, release_notes;
public String getUrl() {

View File

@@ -37,29 +37,37 @@ import java.util.stream.Collectors;
import java.util.stream.IntStream;
public class ExpansionCloudManager {
private static final String API_URL = "http://api.extendedclip.com/v2/";
private static final Gson GSON = new Gson();
private final PlaceholderAPIPlugin plugin;
private final File expansionsDir;
private final Set<String> downloading = new HashSet<>();
private final List<String> downloading = new ArrayList<>();
private final Map<Integer, CloudExpansion> remote = new TreeMap<>();
public ExpansionCloudManager(PlaceholderAPIPlugin plugin) {
this.plugin = plugin;
expansionsDir = new File(plugin.getDataFolder(), "expansions");
if (expansionsDir.mkdirs()) {
final boolean result = expansionsDir.mkdirs();
if (result) {
plugin.getLogger().info("Created Expansions Directory");
}
}
public void clean() {
remote.clear();
downloading.clear();
}
public Map<Integer, CloudExpansion> getCloudExpansions() {
return remote;
}
@@ -72,6 +80,7 @@ public class ExpansionCloudManager {
.orElse(null);
}
public int getCloudAuthorCount() {
return remote.values()
.stream()
@@ -117,10 +126,14 @@ public class ExpansionCloudManager {
public int getPagesAvailable(Map<Integer, CloudExpansion> map, int amount) {
if (map == null) return 0;
if (map == null) {
return 0;
}
int pages = map.size() > 0 ? 1 : 0;
if (pages == 0) return 0;
if (pages == 0) {
return pages;
}
if (map.size() > amount) {
pages = map.size() / amount;
@@ -146,11 +159,12 @@ public class ExpansionCloudManager {
return ex;
}
public void fetch(boolean allowUnverified) {
plugin.getLogger().info("Fetching available expansion information...");
plugin.getServer().getScheduler().runTaskAsynchronously(plugin, () -> {
Map<String, CloudExpansion> data = new HashMap<>();
final Map<String, CloudExpansion> data = new HashMap<>();
try (BufferedReader reader = new BufferedReader(new InputStreamReader(new URL(API_URL).openStream()))) {
data.putAll(GSON.fromJson(reader, new TypeToken<Map<String, CloudExpansion>>() {
@@ -159,12 +173,11 @@ public class ExpansionCloudManager {
if (plugin.getPlaceholderAPIConfig().isDebugMode()) {
ex.printStackTrace();
} else {
plugin.getLogger().warning("Unable to fetch expansions!\nThere was an error with the server host connecting to the PlaceholderAPI eCloud (https://api" +
".extendedclip.com/v2/)");
plugin.getLogger().warning("Unable to fetch expansions!\nThere was an error with the server host connecting to the PlaceholderAPI eCloud (https://api.extendedclip.com/v2/)");
}
}
List<CloudExpansion> unsorted = new ArrayList<>();
final List<CloudExpansion> unsorted = new ArrayList<>();
data.forEach((name, cexp) -> {
if ((allowUnverified || cexp.isVerified()) && cexp.getLatestVersion() != null && cexp.getVersion(cexp.getLatestVersion()) != null) {
@@ -191,6 +204,7 @@ public class ExpansionCloudManager {
}
plugin.getLogger().info(count + " placeholder expansions are available on the cloud.");
long updates = getToUpdateCount();
if (updates > 0) {
@@ -206,15 +220,19 @@ public class ExpansionCloudManager {
private void download(URL url, String name) throws IOException {
InputStream is = null;
FileOutputStream fos = null;
try {
URLConnection urlConn = url.openConnection();
is = urlConn.getInputStream();
fos = new FileOutputStream(
expansionsDir.getAbsolutePath() + File.separator + "Expansion-" + name + ".jar");
byte[] buffer = new byte[is.available()];
int l;
while ((l = is.read(buffer)) > 0) {
@@ -234,35 +252,42 @@ public class ExpansionCloudManager {
}
public void downloadExpansion(String player, CloudExpansion ex) {
public void downloadExpansion(final String player, final CloudExpansion ex) {
downloadExpansion(player, ex, ex.getLatestVersion());
}
public void downloadExpansion(String player, CloudExpansion ex, String version) {
public void downloadExpansion(final String player, final CloudExpansion ex, final String version) {
if (downloading.contains(ex.getName())) {
return;
}
CloudExpansion.Version ver = ex.getVersions()
final CloudExpansion.Version ver = ex.getVersions()
.stream()
.filter(v -> v.getVersion().equals(version))
.findFirst()
.orElse(null);
if (ver == null) return;
if (ver == null) {
return;
}
downloading.add(ex.getName());
plugin.getLogger().info("Attempting download of expansion: " + ex.getName() + (player != null ? " by user: " + player : "") + " from url: " + ver.getUrl());
Bukkit.getScheduler().runTaskAsynchronously(plugin, () -> {
try {
download(new URL(ver.getUrl()), ex.getName());
plugin.getLogger().info("Download of expansion: " + ex.getName() + " complete!");
} catch (Exception e) {
plugin.getLogger()
.warning("Failed to download expansion: " + ex.getName() + " from: " + ver.getUrl());
Bukkit.getScheduler().runTask(plugin, () -> {
downloading.remove(ex.getName());
if (player != null) {
@@ -289,6 +314,7 @@ public class ExpansionCloudManager {
}
}
});
});
}
}