mirror of
https://github.com/PlaceholderAPI/PlaceholderAPI
synced 2025-09-08 15:36:14 +02:00
2.8.5
This commit is contained in:
@@ -0,0 +1,17 @@
|
||||
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
|
||||
*
|
||||
*/
|
||||
public interface Cacheable {
|
||||
|
||||
/**
|
||||
* Called when the implementing class is unregistered from PlaceholderAPI
|
||||
*/
|
||||
void clear();
|
||||
}
|
@@ -0,0 +1,19 @@
|
||||
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
|
||||
*
|
||||
*/
|
||||
public interface Cleanable {
|
||||
|
||||
/**
|
||||
* Called when a player leaves the server
|
||||
* @param p (@link Player} who left the server
|
||||
*/
|
||||
void cleanup(Player p);
|
||||
}
|
@@ -0,0 +1,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
|
||||
*
|
||||
*/
|
||||
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();
|
||||
}
|
@@ -0,0 +1,191 @@
|
||||
package me.clip.placeholderapi.expansion;
|
||||
|
||||
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.PlaceholderAPIPlugin;
|
||||
import org.bukkit.Bukkit;
|
||||
import org.bukkit.configuration.file.FileConfiguration;
|
||||
import org.bukkit.event.Listener;
|
||||
|
||||
import me.clip.placeholderapi.PlaceholderHook;
|
||||
import me.clip.placeholderapi.expansion.cloud.CloudExpansion;
|
||||
import me.clip.placeholderapi.util.FileUtil;
|
||||
|
||||
public final class ExpansionManager {
|
||||
|
||||
private PlaceholderAPIPlugin plugin;
|
||||
|
||||
private final Map<String, PlaceholderExpansion> cache = new HashMap<>();
|
||||
|
||||
public ExpansionManager(PlaceholderAPIPlugin instance) {
|
||||
plugin = instance;
|
||||
}
|
||||
|
||||
public void clean() {
|
||||
cache.clear();
|
||||
}
|
||||
|
||||
public PlaceholderExpansion getCachedExpansion(String plugin) {
|
||||
return cache.getOrDefault(plugin, null);
|
||||
}
|
||||
|
||||
public boolean removeCachedExpansion(String identifier) {
|
||||
return cache.remove(identifier) != null;
|
||||
}
|
||||
|
||||
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();
|
||||
}
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
public boolean registerExpansion(PlaceholderExpansion c) {
|
||||
|
||||
if (c == null || c.getIdentifier() == null) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if (c instanceof Configurable) {
|
||||
|
||||
Map<String, Object> defaults = ((Configurable) c).getDefaults();
|
||||
|
||||
String pre = "expansions." + c.getIdentifier() + ".";
|
||||
|
||||
if (defaults != null && !defaults.isEmpty()) {
|
||||
|
||||
FileConfiguration cfg = plugin.getConfig();
|
||||
|
||||
boolean save = false;
|
||||
|
||||
for (Entry<String, Object> entries : defaults.entrySet()) {
|
||||
if (entries.getKey() == null || entries.getKey().isEmpty()) {
|
||||
continue;
|
||||
}
|
||||
|
||||
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());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (save) {
|
||||
plugin.saveConfig();
|
||||
plugin.reloadConfig();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (c instanceof VersionSpecific) {
|
||||
VersionSpecific nms = (VersionSpecific) c;
|
||||
if (!nms.isCompatibleWith(PlaceholderAPIPlugin.getServerVersion())) {
|
||||
plugin.getLogger().info("Your server version is not compatible with expansion: " + c.getIdentifier()
|
||||
+ " version: " + c.getVersion());
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
if (!c.canRegister()) {
|
||||
if (c.getPlugin() != null) {
|
||||
cache.put(c.getPlugin().toLowerCase(), c);
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
if (!c.register()) {
|
||||
if (c.getPlugin() != null) {
|
||||
cache.put(c.getPlugin().toLowerCase(), c);
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
if (c instanceof Listener) {
|
||||
Listener l = (Listener) c;
|
||||
Bukkit.getPluginManager().registerEvents(l, plugin);
|
||||
plugin.getLogger().info("Registered event listener for expansion: " + c.getIdentifier());
|
||||
}
|
||||
|
||||
plugin.getLogger().info("Successfully registered expansion: " + c.getIdentifier());
|
||||
|
||||
if (c instanceof Taskable) {
|
||||
((Taskable) c).start();
|
||||
plugin.getLogger().info("Started scheduled task for expansion: " + c.getIdentifier());
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
public void registerAllExpansions() {
|
||||
|
||||
if (plugin == null) {
|
||||
return;
|
||||
}
|
||||
|
||||
List<Class<?>> subs = FileUtil.getClasses("expansions", PlaceholderExpansion.class);
|
||||
|
||||
if (subs == null || subs.isEmpty()) {
|
||||
return;
|
||||
}
|
||||
|
||||
for (Class<?> klass : subs) {
|
||||
|
||||
if (klass == null) {
|
||||
continue;
|
||||
}
|
||||
|
||||
try {
|
||||
|
||||
PlaceholderExpansion ex = null;
|
||||
|
||||
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;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
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) {
|
||||
plugin.getLogger().severe("Failed to load placeholder expansion from class: " + klass.getName());
|
||||
plugin.getLogger().severe(t.getMessage());
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
@@ -0,0 +1,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;
|
||||
}
|
||||
}
|
@@ -0,0 +1,145 @@
|
||||
package me.clip.placeholderapi.expansion;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import me.clip.placeholderapi.PlaceholderAPI;
|
||||
import me.clip.placeholderapi.PlaceholderAPIPlugin;
|
||||
import me.clip.placeholderapi.PlaceholderHook;
|
||||
|
||||
import org.apache.commons.lang.Validate;
|
||||
import org.bukkit.Bukkit;
|
||||
import org.bukkit.configuration.ConfigurationSection;
|
||||
|
||||
public abstract class PlaceholderExpansion extends PlaceholderHook {
|
||||
|
||||
/**
|
||||
* The name of this expansion
|
||||
* @return identifier used for expansion if no name present
|
||||
*/
|
||||
public String getName() {
|
||||
return getIdentifier();
|
||||
}
|
||||
/**
|
||||
* Get the identifier that this placeholder expansion uses to be passed placeholder requests
|
||||
* @return placeholder identifier that is associated with this class
|
||||
*/
|
||||
public abstract String getIdentifier();
|
||||
|
||||
/**
|
||||
* Get the plugin that this expansion hooks into.
|
||||
* This will ensure the expansion is added to a cache if canRegister() returns false
|
||||
* get.
|
||||
* The expansion will be removed from the cache
|
||||
* once a plugin loads with the name that is here and the expansion is registered
|
||||
* @return placeholder identifier that is associated with this class
|
||||
*/
|
||||
public abstract String getPlugin();
|
||||
|
||||
/**
|
||||
* Get the author of this PlaceholderExpansion
|
||||
* @return name of the author for this expansion
|
||||
*/
|
||||
public abstract String getAuthor();
|
||||
|
||||
/**
|
||||
* Get the version of this PlaceholderExpansion
|
||||
* @return current version of this expansion
|
||||
*/
|
||||
public abstract String getVersion();
|
||||
|
||||
/**
|
||||
* Check if a placeholder has already been registered with this identifier
|
||||
* @return true if the identifier for this expansion has already been registered
|
||||
*/
|
||||
public boolean isRegistered() {
|
||||
Validate.notNull(getIdentifier(), "Placeholder identifier can not be null!");
|
||||
return PlaceholderAPI.getRegisteredIdentifiers().contains(getIdentifier());
|
||||
}
|
||||
|
||||
/**
|
||||
* If any requirements are required to be checked before this hook can register, add them here
|
||||
* @return true if this hook meets all the requirements to register
|
||||
*/
|
||||
public boolean canRegister() {
|
||||
return getPlugin() == null || Bukkit.getPluginManager().getPlugin(getPlugin()) != null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Attempt to register this PlaceholderExpansion with PlaceholderAPI
|
||||
* @return true if this class and identifier have been successfully registered with PlaceholderAPI
|
||||
*/
|
||||
public boolean register() {
|
||||
Validate.notNull(getIdentifier(), "Placeholder identifier can not be null!");
|
||||
return PlaceholderAPI.registerPlaceholderHook(getIdentifier(), this);
|
||||
}
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* Quick getter for the {@link PlaceholderAPIPlugin} instance
|
||||
* @return {@link PlaceholderAPIPlugin} instance
|
||||
*/
|
||||
public PlaceholderAPIPlugin getPlaceholderAPI() {
|
||||
return PlaceholderAPIPlugin.getInstance();
|
||||
}
|
||||
|
||||
/**
|
||||
* A short description of this expansion
|
||||
* @return null if no description
|
||||
*/
|
||||
public String getDescription() {
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* The url link to this expansion page
|
||||
* @return null if no link
|
||||
*/
|
||||
public String getLink() {
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* A list of all valid placeholders
|
||||
* @return null if you dont care
|
||||
*/
|
||||
public List<String> getPlaceholders() {
|
||||
return null;
|
||||
}
|
||||
|
||||
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);
|
||||
}
|
||||
}
|
@@ -0,0 +1,7 @@
|
||||
package me.clip.placeholderapi.expansion;
|
||||
|
||||
import org.bukkit.entity.Player;
|
||||
|
||||
public interface Relational {
|
||||
String onPlaceholderRequest(Player one, Player two, String identifier);
|
||||
}
|
17
src/main/java/me/clip/placeholderapi/expansion/Taskable.java
Normal file
17
src/main/java/me/clip/placeholderapi/expansion/Taskable.java
Normal file
@@ -0,0 +1,17 @@
|
||||
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();
|
||||
}
|
21
src/main/java/me/clip/placeholderapi/expansion/Version.java
Normal file
21
src/main/java/me/clip/placeholderapi/expansion/Version.java
Normal file
@@ -0,0 +1,21 @@
|
||||
package me.clip.placeholderapi.expansion;
|
||||
|
||||
public class Version {
|
||||
|
||||
private boolean isSpigot;
|
||||
|
||||
private String version;
|
||||
|
||||
public Version(String version, boolean isSpigot) {
|
||||
this.version = version;
|
||||
this.isSpigot = isSpigot;
|
||||
}
|
||||
|
||||
public String getVersion() {
|
||||
return version == null ? "unknown" : version;
|
||||
}
|
||||
|
||||
public boolean isSpigot() {
|
||||
return isSpigot;
|
||||
}
|
||||
}
|
@@ -0,0 +1,19 @@
|
||||
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
|
||||
*
|
||||
*/
|
||||
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);
|
||||
}
|
@@ -0,0 +1,80 @@
|
||||
package me.clip.placeholderapi.expansion.cloud;
|
||||
|
||||
import java.util.concurrent.TimeUnit;
|
||||
|
||||
import me.clip.placeholderapi.util.TimeUtil;
|
||||
|
||||
|
||||
public class CloudExpansion {
|
||||
|
||||
private String name, author, version, description, link, releaseNotes;
|
||||
|
||||
private boolean hasExpansion, shouldUpdate;
|
||||
|
||||
private long lastUpdate;
|
||||
|
||||
public CloudExpansion(String name, String author, String version, String description, String link) {
|
||||
this.name = name;
|
||||
this.author = author;
|
||||
this.version = version;
|
||||
this.description = description;
|
||||
this.link = link;
|
||||
}
|
||||
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
public String getAuthor() {
|
||||
return author;
|
||||
}
|
||||
|
||||
public String getVersion() {
|
||||
return version;
|
||||
}
|
||||
|
||||
public String getDescription() {
|
||||
return description;
|
||||
}
|
||||
|
||||
public String getLink() {
|
||||
return link;
|
||||
}
|
||||
|
||||
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 long getLastUpdate() {
|
||||
return lastUpdate;
|
||||
}
|
||||
|
||||
public void setLastUpdate(long lastUpdate) {
|
||||
this.lastUpdate = lastUpdate;
|
||||
}
|
||||
|
||||
public String getReleaseNotes() {
|
||||
return releaseNotes;
|
||||
}
|
||||
|
||||
public void setReleaseNotes(String releaseNotes) {
|
||||
this.releaseNotes = releaseNotes;
|
||||
}
|
||||
|
||||
public String getTimeSinceLastUpdate() {
|
||||
int time = (int) TimeUnit.MILLISECONDS.toSeconds(System.currentTimeMillis() - getLastUpdate());
|
||||
return TimeUtil.getTime(time);
|
||||
}
|
||||
}
|
@@ -0,0 +1,422 @@
|
||||
package me.clip.placeholderapi.expansion.cloud;
|
||||
|
||||
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 me.clip.placeholderapi.PlaceholderAPIPlugin;
|
||||
import org.bukkit.Bukkit;
|
||||
import org.bukkit.entity.Player;
|
||||
import org.bukkit.scheduler.BukkitRunnable;
|
||||
import org.json.simple.JSONObject;
|
||||
import org.json.simple.parser.JSONParser;
|
||||
import org.json.simple.parser.ParseException;
|
||||
|
||||
import me.clip.placeholderapi.expansion.PlaceholderExpansion;
|
||||
import me.clip.placeholderapi.util.Msg;
|
||||
|
||||
public class ExpansionCloudManager {
|
||||
|
||||
private final File dir;
|
||||
|
||||
private PlaceholderAPIPlugin plugin;
|
||||
|
||||
private final TreeMap<Integer, CloudExpansion> remote = new TreeMap<>();
|
||||
|
||||
private final List<String> downloading = new ArrayList<>();
|
||||
|
||||
private int toUpdate = 0;
|
||||
|
||||
public ExpansionCloudManager(PlaceholderAPIPlugin instance) {
|
||||
|
||||
plugin = instance;
|
||||
dir = new File(instance.getDataFolder() + File.separator + "expansions");
|
||||
|
||||
if (!dir.exists()) {
|
||||
try {
|
||||
dir.mkdirs();
|
||||
} catch (Exception ex) {
|
||||
ex.printStackTrace();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public void clean() {
|
||||
remote.clear();
|
||||
toUpdate = 0;
|
||||
downloading.clear();
|
||||
}
|
||||
|
||||
public boolean isDownloading(String expansion) {
|
||||
return downloading.contains(expansion);
|
||||
}
|
||||
|
||||
public Map<Integer, CloudExpansion> getCloudExpansions() {
|
||||
return remote;
|
||||
}
|
||||
|
||||
public CloudExpansion getCloudExpansion(String name) {
|
||||
|
||||
for (CloudExpansion ex : remote.values()) {
|
||||
if (ex.getName().equalsIgnoreCase(name)) {
|
||||
return ex;
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
public int getCloudAuthorCount() {
|
||||
|
||||
if (remote == null) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
List<String> temp = new ArrayList<>();
|
||||
|
||||
for (CloudExpansion ex : remote.values()) {
|
||||
if (!temp.contains(ex.getAuthor())) {
|
||||
temp.add(ex.getAuthor());
|
||||
}
|
||||
}
|
||||
return temp.size();
|
||||
}
|
||||
|
||||
public int getToUpdateCount() {
|
||||
return toUpdate;
|
||||
}
|
||||
|
||||
public void decrementToUpdateCount() {
|
||||
if (toUpdate > 0) {
|
||||
toUpdate--;
|
||||
}
|
||||
}
|
||||
|
||||
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);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
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(0, ex);
|
||||
} else {
|
||||
has.put(has.lastKey()+1, ex);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
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 = pages+1;
|
||||
}
|
||||
}
|
||||
return pages;
|
||||
}
|
||||
|
||||
public Map<Integer, CloudExpansion> getPage(Map<Integer, CloudExpansion> map, int page) {
|
||||
|
||||
if (map == null || map.size() == 0) {
|
||||
return null;
|
||||
}
|
||||
|
||||
if (page > getPagesAvailable(map, 10)) {
|
||||
return null;
|
||||
}
|
||||
|
||||
int end = 10*page;
|
||||
|
||||
int start = end-10;
|
||||
|
||||
end = end-1;
|
||||
|
||||
int size = map.size();
|
||||
|
||||
if (end > size) {
|
||||
end = size-1;
|
||||
}
|
||||
|
||||
TreeMap<Integer, CloudExpansion> ex = new TreeMap<>();
|
||||
|
||||
for (int i = start ; i <= end ; i++) {
|
||||
ex.put(i, map.get(i));
|
||||
}
|
||||
return ex.isEmpty() ? null : ex;
|
||||
}
|
||||
|
||||
public void fetch() {
|
||||
|
||||
plugin.getLogger().info("Fetching available expansion list...");
|
||||
|
||||
toUpdate = 0;
|
||||
|
||||
new BukkitRunnable() {
|
||||
|
||||
@Override
|
||||
public void run() {
|
||||
|
||||
StringBuilder sb;
|
||||
|
||||
try {
|
||||
|
||||
URL api = new URL("http://api.extendedclip.com/");
|
||||
|
||||
HttpURLConnection connection = (HttpURLConnection) api.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);
|
||||
String name = o.toString();
|
||||
String author = (String) sub.get("author");
|
||||
String version = (String) sub.get("version");
|
||||
String link = (String) sub.get("link");
|
||||
String description = (String) sub.get("description");
|
||||
String notes = "";
|
||||
long update = -1;
|
||||
|
||||
if (sub.get("release_notes") != null) {
|
||||
notes = (String) sub.get("release_notes");
|
||||
}
|
||||
|
||||
if (sub.get("last_update") != null) {
|
||||
|
||||
Object u = sub.get("last_update");
|
||||
|
||||
if (u instanceof Long) {
|
||||
update = (long) sub.get("last_update");
|
||||
}
|
||||
}
|
||||
|
||||
CloudExpansion ce = new CloudExpansion(name, author, version, description, link);
|
||||
|
||||
ce.setReleaseNotes(notes);
|
||||
|
||||
ce.setLastUpdate(update);
|
||||
|
||||
PlaceholderExpansion ex = plugin.getExpansionManager().getRegisteredExpansion(name);
|
||||
|
||||
if (ex != null && ex.isRegistered()) {
|
||||
ce.setHasExpansion(true);
|
||||
if (!ex.getVersion().equals(version)) {
|
||||
ce.setShouldUpdate(true);
|
||||
toUpdate++;
|
||||
}
|
||||
}
|
||||
|
||||
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.");
|
||||
plugin.getLogger().info(toUpdate + " expansions you use have updates.");
|
||||
}
|
||||
}
|
||||
}.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[1024];
|
||||
|
||||
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) {
|
||||
|
||||
if (downloading.contains(ex.getName())) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (ex.getLink() == null) {
|
||||
return;
|
||||
}
|
||||
|
||||
downloading.add(ex.getName());
|
||||
|
||||
plugin.getLogger().info("Attempting download of expansion: " + ex.getName() + (player != null ? " by user: " + player : "") + " from url: " + ex.getLink());
|
||||
|
||||
Bukkit.getScheduler().runTaskAsynchronously(plugin, () -> {
|
||||
|
||||
try {
|
||||
|
||||
download(new URL(ex.getLink()), ex.getName());
|
||||
|
||||
plugin.getLogger().info("Download of expansion: " + ex.getName() + " complete!");
|
||||
|
||||
} catch (Exception e) {
|
||||
|
||||
plugin.getLogger().warning("Failed to download expansion: " + ex.getName() + " from: " + ex.getLink());
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
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!");
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
});
|
||||
}
|
||||
}
|
@@ -0,0 +1,28 @@
|
||||
package me.clip.placeholderapi.expansion.cloud;
|
||||
|
||||
import java.util.Comparator;
|
||||
|
||||
public class ExpansionComparator implements Comparator<CloudExpansion> {
|
||||
|
||||
private static final int AUTHOR = 0;
|
||||
private static final int LAST = 1;
|
||||
private static final int RATING = 2;
|
||||
private static final int TO_UPDATE = 3;
|
||||
private int compare_mode = LAST;
|
||||
|
||||
public ExpansionComparator() { }
|
||||
|
||||
public ExpansionComparator(int compare_mode) {
|
||||
this.compare_mode = compare_mode;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int compare(CloudExpansion o1, CloudExpansion o2) {
|
||||
switch (compare_mode) {
|
||||
case LAST:
|
||||
return (int) (o2.getLastUpdate() - o1.getLastUpdate());
|
||||
default:
|
||||
return o1.getAuthor().compareTo(o2.getAuthor());
|
||||
}
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user