Reformatted project to conform to google style guide.

This commit is contained in:
Mitchell Cook
2018-07-16 17:28:56 +10:00
parent bc915af13e
commit 9cc5a9678f
25 changed files with 1833 additions and 1741 deletions

View File

@@ -21,17 +21,16 @@
package me.clip.placeholderapi.expansion;
/**
* This interface allows a class which extends a {@link PlaceholderExpansion}
* to have the clear method called when the implementing expansion is unregistered
* from PlaceholderAPI.
* This is useful if we want to do things when the implementing hook is unregistered
* @author Ryan McCarthy
* This interface allows a class which extends a {@link PlaceholderExpansion} to have the clear
* method called when the implementing expansion is unregistered from PlaceholderAPI. This is useful
* if we want to do things when the implementing hook is unregistered
*
* @author Ryan McCarthy
*/
public interface Cacheable {
/**
* Called when the implementing class is unregistered from PlaceholderAPI
*/
void clear();
/**
* Called when the implementing class is unregistered from PlaceholderAPI
*/
void clear();
}

View File

@@ -23,17 +23,18 @@ package me.clip.placeholderapi.expansion;
import org.bukkit.entity.Player;
/**
* This interface allows a class which extends a {@link PlaceholderExpansion}
* to have the cleanup method called every time a player leaves the server.
* This is useful if we want to clean up after the player
* @author Ryan McCarthy
* This interface allows a class which extends a {@link PlaceholderExpansion} to have the cleanup
* method called every time a player leaves the server. This is useful if we want to clean up after
* the player
*
* @author Ryan McCarthy
*/
public interface Cleanable {
/**
* Called when a player leaves the server
* @param p (@link Player} who left the server
*/
void cleanup(Player p);
/**
* Called when a player leaves the server
*
* @param p (@link Player} who left the server
*/
void cleanup(Player p);
}

View File

@@ -23,19 +23,21 @@ package me.clip.placeholderapi.expansion;
import java.util.Map;
/**
* Any {@link PlaceholderExpansion} class which implements configurable will
* have any options listed in the getDefaults map automatically added to the PlaceholderAPI config.yml file
* @author Ryan McCarthy
* Any {@link PlaceholderExpansion} class which implements configurable will have any options listed
* in the getDefaults map automatically added to the PlaceholderAPI config.yml file
*
* @author Ryan McCarthy
*/
public interface Configurable {
/**
* This method will be called before the implementing class is registered
* to obtain a map of configuration options that the implementing class needs
* These paths and values will be added to the PlaceholderAPI config.yml in the configuration section
* expansions.(placeholder identifier).(your key): (your value)
* @return Map of config path / values which need to be added / removed from the PlaceholderAPI config.yml file
*/
Map<String, Object> getDefaults();
/**
* This method will be called before the implementing class is registered to obtain a map of
* configuration options that the implementing class needs These paths and values will be added to
* the PlaceholderAPI config.yml in the configuration section expansions.(placeholder
* identifier).(your key): (your value)
*
* @return Map of config path / values which need to be added / removed from the PlaceholderAPI
* config.yml file
*/
Map<String, Object> getDefaults();
}

View File

@@ -22,36 +22,37 @@ package me.clip.placeholderapi.expansion;
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");
private String version;
NMSVersion(String version) {
this.version = version;
}
public String getVersion() {
return version;
}
public static NMSVersion getVersion(String version) {
for (NMSVersion v : values()) {
if (v.getVersion().equalsIgnoreCase(version)) {
return v;
}
}
return NMSVersion.UNKNOWN;
}
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");
private String version;
NMSVersion(String version) {
this.version = version;
}
public static NMSVersion getVersion(String version) {
for (NMSVersion v : values()) {
if (v.getVersion().equalsIgnoreCase(version)) {
return v;
}
}
return NMSVersion.UNKNOWN;
}
public String getVersion() {
return version;
}
}

View File

@@ -20,6 +20,7 @@
*/
package me.clip.placeholderapi.expansion;
import java.util.List;
import me.clip.placeholderapi.PlaceholderAPI;
import me.clip.placeholderapi.PlaceholderAPIPlugin;
import me.clip.placeholderapi.PlaceholderHook;
@@ -27,39 +28,42 @@ import org.apache.commons.lang.Validate;
import org.bukkit.Bukkit;
import org.bukkit.configuration.ConfigurationSection;
import java.util.List;
public abstract class PlaceholderExpansion extends PlaceholderHook {
/**
* The name of this expansion
* @return {@link #getIdentifier()} by default, name of this expansion if specified
*/
public String getName() {
return getIdentifier();
}
/**
* The placeholder identifier of this expanion
* @return placeholder identifier that is associated with this expansion
*/
public abstract String getIdentifier();
/**
* The author of this expansion
* @return name of the author for this expansion
*/
public abstract String getAuthor();
/**
* The version of this expansion
* @return current version of this expansion
*/
public abstract String getVersion();
/**
* The name of the plugin that this expansion hooks into.
* by default will return the deprecated {@link #getPlugin()} method
* The name of this expansion
*
* @return {@link #getIdentifier()} by default, name of this expansion if specified
*/
public String getName() {
return getIdentifier();
}
/**
* The placeholder identifier of this expanion
*
* @return placeholder identifier that is associated with this expansion
*/
public abstract String getIdentifier();
/**
* The author of this expansion
*
* @return name of the author for this expansion
*/
public abstract String getAuthor();
/**
* The version of this expansion
*
* @return current version of this expansion
*/
public abstract String getVersion();
/**
* The name of the plugin that this expansion hooks into. by default will return the deprecated
* {@link #getPlugin()} method
*
* @return plugin name that this expansion requires to function
*/
public String getRequiredPlugin() {
@@ -68,95 +72,109 @@ public abstract class PlaceholderExpansion extends PlaceholderHook {
/**
* The placeholders associated with this expansion
*
* @return placeholder list that this expansion provides
*/
public List<String> getPlaceholders() {
return null;
}
/**
* Expansions that do not use the ecloud and instead register from the dependency should set this to true
* to ensure that your placeholder expansion is not unregistered when the papi reload command is used
* @return if this expansion should persist through placeholder reloads
*/
public boolean persist() {
return false;
}
/**
* Check if this placeholder identfier has already been registered
* @return true if the identifier for this expansion is already registered
*/
public boolean isRegistered() {
Validate.notNull(getIdentifier(), "Placeholder identifier can not be null!");
return PlaceholderAPI.getRegisteredIdentifiers().contains(getIdentifier());
}
/**
* If any requirements need to be checked before this expansion should register,
* you can check them here
* @return true if this hook meets all the requirements to register
*/
public boolean canRegister() {
return getRequiredPlugin() == null || Bukkit.getPluginManager().getPlugin(getRequiredPlugin()) != null;
}
/**
* Attempt to register this PlaceholderExpansion
* @return true if this expansion is now registered with PlaceholderAPI
*/
public boolean register() {
Validate.notNull(getIdentifier(), "Placeholder identifier can not be null!");
return PlaceholderAPI.registerExpansion(this);
}
/**
* Quick getter for the {@link PlaceholderAPIPlugin} instance
* @return {@link PlaceholderAPIPlugin} instance
*/
public PlaceholderAPIPlugin getPlaceholderAPI() {
return PlaceholderAPIPlugin.getInstance();
}
public String getString(String path, String def) {
return getPlaceholderAPI().getConfig().getString("expansions." + getIdentifier() + "." + path, def);
}
public int getInt(String path, int def) {
return getPlaceholderAPI().getConfig().getInt("expansions." + getIdentifier() + "." + path, def);
}
public long getLong(String path, long def) {
return getPlaceholderAPI().getConfig().getLong("expansions." + getIdentifier() + "." + path, def);
}
public double getDouble(String path, double def) {
return getPlaceholderAPI().getConfig().getDouble("expansions." + getIdentifier() + "." + path, def);
}
public List<String> getStringList(String path) {
return getPlaceholderAPI().getConfig().getStringList("expansions." + getIdentifier() + "." + path);
}
public Object get(String path, Object def) {
return getPlaceholderAPI().getConfig().get("expansions." + getIdentifier() + "." + path, def);
}
public ConfigurationSection getConfigSection(String path) {
return getPlaceholderAPI().getConfig().getConfigurationSection("expansions." + getIdentifier() + "." + path);
}
public ConfigurationSection getConfigSection() {
return getPlaceholderAPI().getConfig().getConfigurationSection("expansions." + getIdentifier());
}
public boolean configurationContains(String path) {
return getPlaceholderAPI().getConfig().contains("expansions." + getIdentifier() + "." + path);
}
/**
* Expansions that do not use the ecloud and instead register from the dependency should set this
* to true to ensure that your placeholder expansion is not unregistered when the papi reload
* command is used
*
* @return if this expansion should persist through placeholder reloads
*/
public boolean persist() {
return false;
}
/**
* Check if this placeholder identfier has already been registered
*
* @return true if the identifier for this expansion is already registered
*/
public boolean isRegistered() {
Validate.notNull(getIdentifier(), "Placeholder identifier can not be null!");
return PlaceholderAPI.getRegisteredIdentifiers().contains(getIdentifier());
}
/**
* If any requirements need to be checked before this expansion should register, you can check
* them here
*
* @return true if this hook meets all the requirements to register
*/
public boolean canRegister() {
return getRequiredPlugin() == null
|| Bukkit.getPluginManager().getPlugin(getRequiredPlugin()) != null;
}
/**
* Attempt to register this PlaceholderExpansion
*
* @return true if this expansion is now registered with PlaceholderAPI
*/
public boolean register() {
Validate.notNull(getIdentifier(), "Placeholder identifier can not be null!");
return PlaceholderAPI.registerExpansion(this);
}
/**
* Quick getter for the {@link PlaceholderAPIPlugin} instance
*
* @return {@link PlaceholderAPIPlugin} instance
*/
public PlaceholderAPIPlugin getPlaceholderAPI() {
return PlaceholderAPIPlugin.getInstance();
}
public String getString(String path, String def) {
return getPlaceholderAPI().getConfig()
.getString("expansions." + getIdentifier() + "." + path, def);
}
public int getInt(String path, int def) {
return getPlaceholderAPI().getConfig()
.getInt("expansions." + getIdentifier() + "." + path, def);
}
public long getLong(String path, long def) {
return getPlaceholderAPI().getConfig()
.getLong("expansions." + getIdentifier() + "." + path, def);
}
public double getDouble(String path, double def) {
return getPlaceholderAPI().getConfig()
.getDouble("expansions." + getIdentifier() + "." + path, def);
}
public List<String> getStringList(String path) {
return getPlaceholderAPI().getConfig()
.getStringList("expansions." + getIdentifier() + "." + path);
}
public Object get(String path, Object def) {
return getPlaceholderAPI().getConfig().get("expansions." + getIdentifier() + "." + path, def);
}
public ConfigurationSection getConfigSection(String path) {
return getPlaceholderAPI().getConfig()
.getConfigurationSection("expansions." + getIdentifier() + "." + path);
}
public ConfigurationSection getConfigSection() {
return getPlaceholderAPI().getConfig().getConfigurationSection("expansions." + getIdentifier());
}
public boolean configurationContains(String path) {
return getPlaceholderAPI().getConfig().contains("expansions." + getIdentifier() + "." + path);
}
/**
* @deprecated As of versions greater than 2.8.7, use {@link #getRequiredPlugin()}
* @deprecated As of versions greater than 2.8.7, use {@link #getRequiredPlugin()}
*/
@Deprecated
public String getPlugin() {
@@ -164,14 +182,18 @@ public abstract class PlaceholderExpansion extends PlaceholderHook {
}
/**
* @deprecated As of versions greater than 2.8.7, use the expansion cloud to show a description
* @deprecated As of versions greater than 2.8.7, use the expansion cloud to show a description
*/
@Deprecated
public String getDescription() { return null; }
public String getDescription() {
return null;
}
/**
* @deprecated As of versions greater than 2.8.7, use the expansion cloud to display a link
* @deprecated As of versions greater than 2.8.7, use the expansion cloud to display a link
*/
@Deprecated
public String getLink() { return null; }
public String getLink() {
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);
String onPlaceholderRequest(Player one, Player two, String identifier);
}

View File

@@ -22,16 +22,16 @@ 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
*/
void start();
/**
* Called when the implementing class has been unregistered from PlaceholderAPI
* Tasks that need to be performed when this expansion has unregistered should go here
*/
void stop();
/**
* 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
*/
void start();
/**
* Called when the implementing class has been unregistered from PlaceholderAPI Tasks that need to
* be performed when this expansion has unregistered should go here
*/
void stop();
}

View File

@@ -21,19 +21,20 @@
package me.clip.placeholderapi.expansion;
/**
* Placeholder expansions which use NMS code should be version specific.
* Implementing this class allows you to perform checks based on the version the server is running.
* The isCompatibleWith method will be passed the server version and allow you to return if your expansion is compatible with that version.
* @author Ryan McCarthy
* Placeholder expansions which use NMS code should be version specific. Implementing this class
* allows you to perform checks based on the version the server is running. The isCompatibleWith
* method will be passed the server version and allow you to return if your expansion is compatible
* with that version.
*
* @author Ryan McCarthy
*/
public interface VersionSpecific {
/**
* This method is called before the expansion is attempted to be registered
* The server version will be passed to this method so you know what version the server is currently running.
*
* @return true if your expansion is compatible with the version the server is running.
*/
boolean isCompatibleWith(Version v);
/**
* This method is called before the expansion is attempted to be registered The server version
* will be passed to this method so you know what version the server is currently running.
*
* @return true if your expansion is compatible with the version the server is running.
*/
boolean isCompatibleWith(Version v);
}

View File

@@ -20,183 +20,183 @@
*/
package me.clip.placeholderapi.expansion.cloud;
import me.clip.placeholderapi.util.TimeUtil;
import java.util.List;
import java.util.concurrent.TimeUnit;
import java.util.stream.Collectors;
import me.clip.placeholderapi.util.TimeUtil;
public class CloudExpansion {
private String name,
author,
latest_version,
description,
source_url,
dependency_url;
private String name,
author,
latest_version,
description,
source_url,
dependency_url;
private boolean hasExpansion,
shouldUpdate,
verified;
private boolean hasExpansion,
shouldUpdate,
verified;
private long last_update,
ratings_count;
private long last_update,
ratings_count;
private double average_rating;
private double average_rating;
private List<String> placeholders;
private List<String> placeholders;
private List<Version> versions;
private List<Version> versions;
public CloudExpansion() {
}
public CloudExpansion() {
}
public String getTimeSinceLastUpdate() {
int time = (int) TimeUnit.MILLISECONDS.toSeconds(System.currentTimeMillis() - getLastUpdate());
return TimeUtil.getTime(time);
}
public String getTimeSinceLastUpdate() {
int time = (int) TimeUnit.MILLISECONDS.toSeconds(System.currentTimeMillis() - getLastUpdate());
return TimeUtil.getTime(time);
}
public String getName() {
return name;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getAuthor() {
return author;
}
public void setAuthor(String author) {
this.author = author;
}
public Version getVersion() {
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);
}
public List<String> getAvailableVersions() {
return versions.stream().map(Version::getVersion).collect(Collectors.toList());
}
public String getLatestVersion() {
return latest_version;
}
public void setLatestVersion(String latest_version) {
this.latest_version = latest_version;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
public String getSourceUrl() {
return source_url;
}
public void setSourceUrl(String source_url) {
this.source_url = source_url;
}
public String getDependencyUrl() {
return dependency_url;
}
public void setDependencyUrl(String dependency_url) {
this.dependency_url = dependency_url;
}
public boolean hasExpansion() {
return hasExpansion;
}
public void setHasExpansion(boolean hasExpansion) {
this.hasExpansion = hasExpansion;
}
public boolean shouldUpdate() {
return shouldUpdate;
}
public void setShouldUpdate(boolean shouldUpdate) {
this.shouldUpdate = shouldUpdate;
}
public boolean isVerified() {
return verified;
}
public long getLastUpdate() {
return last_update;
}
public void setLastUpdate(long last_update) {
this.last_update = last_update;
}
public long getRatingsCount() {
return ratings_count;
}
public double getAverage_rating() {
return average_rating;
}
public List<String> getPlaceholders() {
return placeholders;
}
public void setPlaceholders(List<String> placeholders) {
this.placeholders = placeholders;
}
public List<Version> getVersions() {
return versions;
}
public void setVersions(List<Version> versions) {
this.versions = versions;
}
public class Version {
private String url, version, release_notes;
public String getUrl() {
return url;
}
public void setName(String name) {
this.name = name;
public void setUrl(String url) {
this.url = url;
}
public String getAuthor() {
return author;
public String getVersion() {
return version;
}
public void setAuthor(String author) {
this.author = author;
public void setVersion(String version) {
this.version = version;
}
public Version getVersion() {
return getLatestVersion() == null ? null : getVersion(getLatestVersion());
public String getReleaseNotes() {
return release_notes;
}
public Version getVersion(String version) {
return versions == null ? null : versions.stream()
.filter(v -> v.getVersion().equals(version))
.findFirst()
.orElse(null);
}
public List<String> getAvailableVersions() {
return versions.stream().map(Version::getVersion).collect(Collectors.toList());
}
public String getLatestVersion() {
return latest_version;
}
public void setLatestVersion(String latest_version) {
this.latest_version = latest_version;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
public String getSourceUrl() {
return source_url;
}
public void setSourceUrl(String source_url) {
this.source_url = source_url;
}
public String getDependencyUrl() {
return dependency_url;
}
public void setDependencyUrl(String dependency_url) {
this.dependency_url = dependency_url;
}
public boolean hasExpansion() {
return hasExpansion;
}
public void setHasExpansion(boolean hasExpansion) {
this.hasExpansion = hasExpansion;
}
public boolean shouldUpdate() {
return shouldUpdate;
}
public void setShouldUpdate(boolean shouldUpdate) {
this.shouldUpdate = shouldUpdate;
}
public boolean isVerified() {
return verified;
}
public long getLastUpdate() {
return last_update;
}
public void setLastUpdate(long last_update) {
this.last_update = last_update;
}
public long getRatingsCount() {
return ratings_count;
}
public double getAverage_rating() {
return average_rating;
}
public List<String> getPlaceholders() {
return placeholders;
}
public void setPlaceholders(List<String> placeholders) {
this.placeholders = placeholders;
}
public List<Version> getVersions() {
return versions;
}
public void setVersions(List<Version> versions) {
this.versions = versions;
}
public class Version {
private String url, version, release_notes;
public String getUrl() {
return url;
}
public void setUrl(String url) {
this.url = url;
}
public String getVersion() {
return version;
}
public void setVersion(String version) {
this.version = version;
}
public String getReleaseNotes() {
return release_notes;
}
public void setReleaseNotes(String release_notes) {
this.release_notes = release_notes;
}
public void setReleaseNotes(String release_notes) {
this.release_notes = release_notes;
}
}
}

View File

@@ -21,6 +21,22 @@
package me.clip.placeholderapi.expansion.cloud;
import com.google.gson.Gson;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLConnection;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import java.util.Map;
import java.util.TreeMap;
import java.util.stream.Collectors;
import java.util.stream.IntStream;
import me.clip.placeholderapi.PlaceholderAPI;
import me.clip.placeholderapi.PlaceholderAPIPlugin;
import me.clip.placeholderapi.expansion.PlaceholderExpansion;
@@ -32,342 +48,346 @@ import org.json.simple.JSONObject;
import org.json.simple.parser.JSONParser;
import org.json.simple.parser.ParseException;
import java.io.*;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLConnection;
import java.util.*;
import java.util.stream.Collectors;
import java.util.stream.IntStream;
public class ExpansionCloudManager {
private PlaceholderAPIPlugin plugin;
private final String API = "http://api.extendedclip.com/v2/";
private final File dir;
private final TreeMap<Integer, CloudExpansion> remote = new TreeMap<>();
private final List<String> downloading = new ArrayList<>();
private Gson gson;
private final String API = "http://api.extendedclip.com/v2/";
private final File dir;
private final TreeMap<Integer, CloudExpansion> remote = new TreeMap<>();
private final List<String> downloading = new ArrayList<>();
private PlaceholderAPIPlugin plugin;
private Gson gson;
public ExpansionCloudManager(PlaceholderAPIPlugin instance) {
plugin = instance;
gson = new Gson();
dir = new File(instance.getDataFolder() + File.separator + "expansions");
if (!dir.exists()) {
try {
dir.mkdirs();
} catch (Exception ex) {
ex.printStackTrace();
}
public ExpansionCloudManager(PlaceholderAPIPlugin instance) {
plugin = instance;
gson = new Gson();
dir = new File(instance.getDataFolder() + File.separator + "expansions");
if (!dir.exists()) {
try {
dir.mkdirs();
} catch (Exception ex) {
ex.printStackTrace();
}
}
}
public void clean() {
remote.clear();
downloading.clear();
}
public boolean isDownloading(String expansion) {
return downloading.contains(expansion);
}
public Map<Integer, CloudExpansion> getCloudExpansions() {
return remote;
}
public CloudExpansion getCloudExpansion(String name) {
return remote.values().stream().filter(ex -> ex.getName().equalsIgnoreCase(name)).findFirst()
.orElse(null);
}
public int getCloudAuthorCount() {
return remote.values().stream()
.collect(Collectors.groupingBy(CloudExpansion::getAuthor, Collectors.counting())).size();
}
public long getToUpdateCount() {
return PlaceholderAPI.getExpansions().stream().filter(
ex -> getCloudExpansion(ex.getName()) != null && getCloudExpansion(ex.getName())
.shouldUpdate()).count();
}
public Map<Integer, CloudExpansion> getAllByAuthor(String author) {
if (remote.isEmpty()) {
return null;
}
TreeMap<Integer, CloudExpansion> byAuthor = new TreeMap<>();
boolean first = true;
for (CloudExpansion ex : remote.values()) {
if (ex.getAuthor().equalsIgnoreCase(author)) {
if (first) {
first = false;
byAuthor.put(0, ex);
} else {
byAuthor.put(byAuthor.lastKey() + 1, ex);
}
}
}
public void clean() {
remote.clear();
downloading.clear();
if (byAuthor.isEmpty()) {
return null;
}
return byAuthor;
}
public boolean isDownloading(String expansion) {
return downloading.contains(expansion);
public Map<Integer, CloudExpansion> getAllInstalled() {
if (remote.isEmpty()) {
return null;
}
public Map<Integer, CloudExpansion> getCloudExpansions() {
return remote;
}
public CloudExpansion getCloudExpansion(String name) {
return remote.values().stream().filter(ex -> ex.getName().equalsIgnoreCase(name)).findFirst().orElse(null);
}
public int getCloudAuthorCount() {
return remote.values().stream().collect(Collectors.groupingBy(CloudExpansion::getAuthor, Collectors.counting())).size();
}
public long getToUpdateCount() {
return PlaceholderAPI.getExpansions().stream().filter(ex -> getCloudExpansion(ex.getName()) != null && getCloudExpansion(ex.getName()).shouldUpdate()).count();
}
public Map<Integer, CloudExpansion> getAllByAuthor(String author) {
if (remote.isEmpty()) {
return null;
TreeMap<Integer, CloudExpansion> has = new TreeMap<>();
boolean first = true;
for (CloudExpansion ex : remote.values()) {
if (ex.hasExpansion()) {
if (first) {
first = false;
has.put(1, ex);
} else {
has.put(has.lastKey() + 1, ex);
}
TreeMap<Integer, CloudExpansion> byAuthor = new TreeMap<>();
boolean first = true;
for (CloudExpansion ex : remote.values()) {
if (ex.getAuthor().equalsIgnoreCase(author)) {
if (first) {
first = false;
byAuthor.put(0, ex);
} else {
byAuthor.put(byAuthor.lastKey() + 1, ex);
}
}
}
if (byAuthor.isEmpty()) {
return null;
}
return byAuthor;
}
}
public Map<Integer, CloudExpansion> getAllInstalled() {
if (remote.isEmpty()) {
return null;
}
TreeMap<Integer, CloudExpansion> has = new TreeMap<>();
boolean first = true;
for (CloudExpansion ex : remote.values()) {
if (ex.hasExpansion()) {
if (first) {
first = false;
has.put(1, ex);
} else {
has.put(has.lastKey() + 1, ex);
}
}
}
if (has.isEmpty()) {
return null;
}
return has;
if (has.isEmpty()) {
return null;
}
return has;
}
public int getPagesAvailable(Map<Integer, CloudExpansion> map, int amount) {
if (map == null) {
return 0;
}
int pages = map.size() > 0 ? 1 : 0;
if (pages == 0) {
return pages;
}
if (map.size() > amount) {
pages = map.size() / amount;
if (map.size() % amount > 0) {
pages++;
}
}
return pages;
public int getPagesAvailable(Map<Integer, CloudExpansion> map, int amount) {
if (map == null) {
return 0;
}
public Map<Integer, CloudExpansion> getPage(Map<Integer, CloudExpansion> map, int page, int size) {
if (map == null || map.size() == 0 || page > getPagesAvailable(map, size)) {
return null;
}
int end = size * page;
int start = end - size;
TreeMap<Integer, CloudExpansion> ex = new TreeMap<>();
IntStream.range(start, end).forEach(n -> ex.put(n, map.get(n)));
return ex;
int pages = map.size() > 0 ? 1 : 0;
if (pages == 0) {
return pages;
}
public void fetch(boolean allowUnverified) {
plugin.getLogger().info("Fetching available expansion information...");
new BukkitRunnable() {
@Override
public void run() {
StringBuilder sb;
try {
URL site = new URL(API);
HttpURLConnection connection = (HttpURLConnection) site.openConnection();
connection.setRequestMethod("GET");
connection.connect();
BufferedReader br = new BufferedReader(new InputStreamReader(connection.getInputStream()));
sb = new StringBuilder();
String line;
while ((line = br.readLine()) != null) {
sb.append(line);
}
br.close();
connection.disconnect();
} catch (Exception e) {
return;
}
String json = sb.toString();
JSONParser parser = new JSONParser();
Object obj = null;
try {
obj = parser.parse(json);
} catch (ParseException e) {
e.printStackTrace();
}
if (obj == null) {
return;
}
List<CloudExpansion> unsorted = new ArrayList<>();
if (obj instanceof JSONObject) {
JSONObject jo = (JSONObject) obj;
for (Object o : jo.keySet()) {
JSONObject sub = (JSONObject) jo.get(o);
CloudExpansion ce = gson.fromJson(sub.toJSONString(), CloudExpansion.class);
if (!allowUnverified && !ce.isVerified()) {
continue;
}
if (ce.getLatestVersion() == null || ce.getVersion(ce.getLatestVersion()) == null) {
continue;
}
ce.setName(o.toString());
PlaceholderExpansion ex = plugin.getExpansionManager().getRegisteredExpansion(ce.getName());
if (ex != null && ex.isRegistered()) {
ce.setHasExpansion(true);
if (!ex.getVersion().equals(ce.getLatestVersion())) {
ce.setShouldUpdate(true);
}
}
unsorted.add(ce);
}
int count = 0;
unsorted.sort(Comparator.comparing(CloudExpansion::getLastUpdate).reversed());
for (CloudExpansion e : unsorted) {
remote.put(count, e);
count++;
}
plugin.getLogger().info(count + " placeholder expansions are available on the cloud.");
long updates = getToUpdateCount();
if (updates > 0) {
plugin.getLogger().info(updates + " installed expansions have updates available.");
}
}
}
}.runTaskAsynchronously(plugin);
if (map.size() > amount) {
pages = map.size() / amount;
if (map.size() % amount > 0) {
pages++;
}
}
return pages;
}
private void download(URL url, String name) throws IOException {
public Map<Integer, CloudExpansion> getPage(Map<Integer, CloudExpansion> map, int page,
int size) {
if (map == null || map.size() == 0 || page > getPagesAvailable(map, size)) {
return null;
}
int end = size * page;
int start = end - size;
TreeMap<Integer, CloudExpansion> ex = new TreeMap<>();
IntStream.range(start, end).forEach(n -> ex.put(n, map.get(n)));
return ex;
}
InputStream is = null;
public void fetch(boolean allowUnverified) {
FileOutputStream fos = null;
plugin.getLogger().info("Fetching available expansion information...");
new BukkitRunnable() {
@Override
public void run() {
StringBuilder sb;
try {
URLConnection urlConn = url.openConnection();
URL site = new URL(API);
is = urlConn.getInputStream();
HttpURLConnection connection = (HttpURLConnection) site.openConnection();
fos = new FileOutputStream(dir.getAbsolutePath() + File.separator + "Expansion-" + name + ".jar");
connection.setRequestMethod("GET");
byte[] buffer = new byte[is.available()];
connection.connect();
int l;
BufferedReader br = new BufferedReader(
new InputStreamReader(connection.getInputStream()));
while ((l = is.read(buffer)) > 0) {
fos.write(buffer, 0, l);
}
} finally {
try {
if (is != null) {
is.close();
}
} finally {
if (fos != null) {
fos.close();
}
}
sb = new StringBuilder();
String line;
while ((line = br.readLine()) != null) {
sb.append(line);
}
br.close();
connection.disconnect();
} catch (Exception e) {
return;
}
String json = sb.toString();
JSONParser parser = new JSONParser();
Object obj = null;
try {
obj = parser.parse(json);
} catch (ParseException e) {
e.printStackTrace();
}
if (obj == null) {
return;
}
List<CloudExpansion> unsorted = new ArrayList<>();
if (obj instanceof JSONObject) {
JSONObject jo = (JSONObject) obj;
for (Object o : jo.keySet()) {
JSONObject sub = (JSONObject) jo.get(o);
CloudExpansion ce = gson.fromJson(sub.toJSONString(), CloudExpansion.class);
if (!allowUnverified && !ce.isVerified()) {
continue;
}
if (ce.getLatestVersion() == null || ce.getVersion(ce.getLatestVersion()) == null) {
continue;
}
ce.setName(o.toString());
PlaceholderExpansion ex = plugin.getExpansionManager()
.getRegisteredExpansion(ce.getName());
if (ex != null && ex.isRegistered()) {
ce.setHasExpansion(true);
if (!ex.getVersion().equals(ce.getLatestVersion())) {
ce.setShouldUpdate(true);
}
}
unsorted.add(ce);
}
int count = 0;
unsorted.sort(Comparator.comparing(CloudExpansion::getLastUpdate).reversed());
for (CloudExpansion e : unsorted) {
remote.put(count, e);
count++;
}
plugin.getLogger().info(count + " placeholder expansions are available on the cloud.");
long updates = getToUpdateCount();
if (updates > 0) {
plugin.getLogger().info(updates + " installed expansions have updates available.");
}
}
}
}.runTaskAsynchronously(plugin);
}
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(
dir.getAbsolutePath() + File.separator + "Expansion-" + name + ".jar");
byte[] buffer = new byte[is.available()];
int l;
while ((l = is.read(buffer)) > 0) {
fos.write(buffer, 0, l);
}
} finally {
try {
if (is != null) {
is.close();
}
} finally {
if (fos != null) {
fos.close();
}
}
}
}
public void downloadExpansion(final String player, final CloudExpansion ex) {
downloadExpansion(player, ex, ex.getLatestVersion());
}
public void downloadExpansion(final String player, final CloudExpansion ex,
final String version) {
if (downloading.contains(ex.getName())) {
return;
}
public void downloadExpansion(final String player, final CloudExpansion ex) {
downloadExpansion(player, ex, ex.getLatestVersion());
final CloudExpansion.Version ver = ex.getVersions()
.stream()
.filter(v -> v.getVersion().equals(version))
.findFirst()
.orElse(null);
if (ver == null) {
return;
}
public void downloadExpansion(final String player, final CloudExpansion ex, final String version) {
downloading.add(ex.getName());
if (downloading.contains(ex.getName())) {
return;
}
plugin.getLogger().info(
"Attempting download of expansion: " + ex.getName() + (player != null ? " by user: "
+ player : "") + " from url: " + ver.getUrl());
final CloudExpansion.Version ver = ex.getVersions()
.stream()
.filter(v -> v.getVersion().equals(version))
.findFirst()
.orElse(null);
Bukkit.getScheduler().runTaskAsynchronously(plugin, () -> {
if (ver == null) {
return;
}
try {
downloading.add(ex.getName());
download(new URL(ver.getUrl()), ex.getName());
plugin.getLogger().info("Attempting download of expansion: " + ex.getName() + (player != null ? " by user: " + player : "") + " from url: " + ver.getUrl());
plugin.getLogger().info("Download of expansion: " + ex.getName() + " complete!");
Bukkit.getScheduler().runTaskAsynchronously(plugin, () -> {
} catch (Exception e) {
try {
plugin.getLogger()
.warning("Failed to download expansion: " + ex.getName() + " from: " + ver.getUrl());
download(new URL(ver.getUrl()), ex.getName());
Bukkit.getScheduler().runTask(plugin, () -> {
plugin.getLogger().info("Download of expansion: " + ex.getName() + " complete!");
downloading.remove(ex.getName());
} catch (Exception e) {
if (player != null) {
Player p = Bukkit.getPlayer(player);
plugin.getLogger().warning("Failed to download expansion: " + ex.getName() + " from: " + ver.getUrl());
Bukkit.getScheduler().runTask(plugin, () -> {
downloading.remove(ex.getName());
if (player != null) {
Player p = Bukkit.getPlayer(player);
if (p != null) {
Msg.msg(p, "&cThere was a problem downloading expansion: &f" + ex.getName());
}
}
});
return;
if (p != null) {
Msg.msg(p, "&cThere was a problem downloading expansion: &f" + ex.getName());
}
Bukkit.getScheduler().runTask(plugin, () -> {
downloading.remove(ex.getName());
if (player != null) {
Player p = Bukkit.getPlayer(player);
if (p != null) {
Msg.msg(p, "&aExpansion &f" + ex.getName() + " &adownload complete!");
}
}
});
}
});
}
return;
}
Bukkit.getScheduler().runTask(plugin, () -> {
downloading.remove(ex.getName());
if (player != null) {
Player p = Bukkit.getPlayer(player);
if (p != null) {
Msg.msg(p, "&aExpansion &f" + ex.getName() + " &adownload complete!");
}
}
});
});
}
}