mirror of
https://github.com/PlaceholderAPI/PlaceholderAPI
synced 2025-09-05 17:07:06 +02:00
Compare commits
35 Commits
Author | SHA1 | Date | |
---|---|---|---|
|
46839c4bf2 | ||
|
2894d525f2 | ||
|
53fb678035 | ||
|
86a1fe862f | ||
|
eba181d04d | ||
|
25758ee653 | ||
|
a50212e395 | ||
|
cfccc69839 | ||
|
659af4833e | ||
|
e1ba07b7bd | ||
|
d5146ea3e5 | ||
|
3a5e3ed950 | ||
|
763c0aabc1 | ||
|
6eaad829cf | ||
|
dd45bc16d0 | ||
|
7900ee71e0 | ||
|
6825c9afc1 | ||
|
1a905f88a9 | ||
|
796136982a | ||
|
3ada9d988b | ||
|
1504972e51 | ||
|
1789c3b183 | ||
|
e336eccd9a | ||
|
d256286a02 | ||
|
81aaef2319 | ||
|
b7003c5142 | ||
|
1e9811507f | ||
|
06b1599f66 | ||
|
7011a8d8c5 | ||
|
92d30ea4cb | ||
|
906bf0a338 | ||
|
134d18c71c | ||
|
59539d591c | ||
|
a5200fd5c1 | ||
|
2856889bbe |
20
pom.xml
20
pom.xml
@@ -4,7 +4,7 @@
|
||||
<groupId>me.clip</groupId>
|
||||
<artifactId>placeholderapi</artifactId>
|
||||
|
||||
<version>2.8.5</version>
|
||||
<version>2.9.1</version>
|
||||
<name>PlaceholderAPI</name>
|
||||
<description>An awesome placeholder provider!</description>
|
||||
<url>http://extendedclip.com</url>
|
||||
@@ -22,6 +22,10 @@
|
||||
<id>bstats-repo</id>
|
||||
<url>http://repo.bstats.org/content/repositories/releases/</url>
|
||||
</repository>
|
||||
<repository>
|
||||
<id>rayzr-repo</id>
|
||||
<url>https://cdn.rawgit.com/Rayzr522/maven-repo/master/</url>
|
||||
</repository>
|
||||
</repositories>
|
||||
|
||||
<dependencies>
|
||||
@@ -37,6 +41,12 @@
|
||||
<version>1.2</version>
|
||||
<scope>compile</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>me.rayzr522</groupId>
|
||||
<artifactId>jsonmessage</artifactId>
|
||||
<version>1.0.0</version>
|
||||
<scope>compile</scope>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
<build>
|
||||
@@ -71,13 +81,17 @@
|
||||
<goal>shade</goal>
|
||||
</goals>
|
||||
<configuration>
|
||||
<minimizeJar>true</minimizeJar>
|
||||
<minimizeJar>false</minimizeJar>
|
||||
<createDependencyReducedPom>false</createDependencyReducedPom>
|
||||
<relocations>
|
||||
<relocation>
|
||||
<pattern>org.bstats</pattern>
|
||||
<shadedPattern>me.clip.placeholderapi.metrics</shadedPattern>
|
||||
</relocation>
|
||||
<relocation>
|
||||
<pattern>me.rayzr522</pattern>
|
||||
<shadedPattern>me.clip.placeholderapi.util</shadedPattern>
|
||||
</relocation>
|
||||
</relocations>
|
||||
</configuration>
|
||||
</execution>
|
||||
@@ -91,4 +105,4 @@
|
||||
</resource>
|
||||
</resources>
|
||||
</build>
|
||||
</project>
|
||||
</project>
|
||||
|
@@ -20,304 +20,375 @@
|
||||
*/
|
||||
package me.clip.placeholderapi;
|
||||
|
||||
import static me.clip.placeholderapi.util.Msg.color;
|
||||
|
||||
import com.google.common.collect.ImmutableMap;
|
||||
import com.google.common.collect.ImmutableSet;
|
||||
import java.util.HashMap;
|
||||
import java.util.HashSet;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Set;
|
||||
import java.util.regex.Matcher;
|
||||
import java.util.regex.Pattern;
|
||||
import java.util.stream.Collectors;
|
||||
import me.clip.placeholderapi.events.ExpansionRegisterEvent;
|
||||
import me.clip.placeholderapi.events.ExpansionUnregisterEvent;
|
||||
import me.clip.placeholderapi.events.PlaceholderHookUnloadEvent;
|
||||
import me.clip.placeholderapi.expansion.PlaceholderExpansion;
|
||||
import me.clip.placeholderapi.expansion.Relational;
|
||||
import org.apache.commons.lang.Validate;
|
||||
import org.bukkit.Bukkit;
|
||||
import org.bukkit.ChatColor;
|
||||
import org.bukkit.OfflinePlayer;
|
||||
import org.bukkit.entity.Player;
|
||||
import org.bukkit.plugin.Plugin;
|
||||
|
||||
import java.util.*;
|
||||
import java.util.regex.Matcher;
|
||||
import java.util.regex.Pattern;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
public class PlaceholderAPI {
|
||||
|
||||
private PlaceholderAPI() {
|
||||
}
|
||||
|
||||
private final static Pattern PLACEHOLDER_PATTERN = Pattern.compile("[%]([^%]+)[%]");
|
||||
private final static Pattern BRACKET_PLACEHOLDER_PATTERN = Pattern.compile("[{]([^{}]+)[}]");
|
||||
private final static Pattern RELATIONAL_PLACEHOLDER_PATTERN = Pattern.compile("[%](rel_)([^%]+)[%]");
|
||||
private final static Map<String, PlaceholderHook> placeholders = new HashMap<>();
|
||||
|
||||
/**
|
||||
* check if a specific placeholder identifier is currently registered
|
||||
* @param identifier to check
|
||||
* @return true if identifier is already registered
|
||||
*/
|
||||
public static boolean isRegistered(String identifier) {
|
||||
return !placeholders.isEmpty() && getRegisteredIdentifiers().stream().filter(id -> id.equalsIgnoreCase(identifier)).findFirst().orElse(null) != null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Register a new placeholder hook
|
||||
* @param identifier Identifier of the placeholder -> "%(identifier)_(args...)%
|
||||
*
|
||||
* @param placeholderHook implementing class that contains the onPlaceholderRequest method which is called when a value is needed for the specific placeholder
|
||||
* @return true if the hook was successfully registered, false if there is already a hook registered for the specified identifier
|
||||
*/
|
||||
public static boolean registerPlaceholderHook(String identifier, PlaceholderHook placeholderHook) {
|
||||
Validate.notNull(identifier, "Identifier can not be null");
|
||||
Validate.notNull(placeholderHook, "Placeholderhook can not be null");
|
||||
if (isRegistered(identifier)) return false;
|
||||
placeholders.put(identifier.toLowerCase(), placeholderHook);
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* unregister a placeholder hook by identifier
|
||||
* @param identifier the identifier for the placeholder hook to unregister
|
||||
* @return true if the placeholder hook was successfully unregistered, false if there was no placeholder hook registered for the identifier specified
|
||||
*/
|
||||
public static boolean unregisterPlaceholderHook(String identifier) {
|
||||
Validate.notNull(identifier, "Identifier can not be null");
|
||||
return placeholders.remove(identifier.toLowerCase()) != null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get all registered placeholder identifiers
|
||||
* @return all registered placeholder identifiers
|
||||
*/
|
||||
public static Set<String> getRegisteredIdentifiers() {
|
||||
if (placeholders.isEmpty()) return new HashSet<>();
|
||||
return new HashSet<>(placeholders.keySet());
|
||||
}
|
||||
|
||||
/**
|
||||
* Get map of registered placeholders
|
||||
* @return copy of the internal placeholder map
|
||||
*/
|
||||
public static Map<String, PlaceholderHook> getPlaceholders() {
|
||||
return new HashMap<>(placeholders);
|
||||
}
|
||||
private static final Pattern PLACEHOLDER_PATTERN = Pattern.compile("[%]([^%]+)[%]");
|
||||
private static final Pattern BRACKET_PLACEHOLDER_PATTERN = Pattern.compile("[{]([^{}]+)[}]");
|
||||
private static final Pattern RELATIONAL_PLACEHOLDER_PATTERN = Pattern.compile("[%](rel_)([^%]+)[%]");
|
||||
private static final Map<String, PlaceholderHook> placeholders = new HashMap<>();
|
||||
private PlaceholderAPI() {
|
||||
}
|
||||
|
||||
public static Set<PlaceholderExpansion> getExpansions() {
|
||||
return getPlaceholders().values().stream().filter(PlaceholderExpansion.class::isInstance).map(PlaceholderExpansion.class::cast).collect(Collectors.toCollection(HashSet::new));
|
||||
/**
|
||||
* check if a specific placeholder identifier is currently registered
|
||||
*
|
||||
* @param identifier to check
|
||||
* @return true if identifier is already registered
|
||||
*/
|
||||
public static boolean isRegistered(String identifier) {
|
||||
return getRegisteredIdentifiers().stream().filter(id -> id.equalsIgnoreCase(identifier))
|
||||
.findFirst().orElse(null) != null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Register a new placeholder hook
|
||||
*
|
||||
* @param identifier Identifier of the placeholder -> "%(identifier)_(args...)%
|
||||
* @param placeholderHook implementing class that contains the onPlaceholderRequest method which
|
||||
* is called when a value is needed for the specific placeholder
|
||||
* @return true if the hook was successfully registered, false if there is already a hook
|
||||
* registered for the specified identifier
|
||||
*/
|
||||
public static boolean registerPlaceholderHook(String identifier,
|
||||
PlaceholderHook placeholderHook) {
|
||||
Validate.notNull(identifier, "Identifier can not be null");
|
||||
Validate.notNull(placeholderHook, "Placeholderhook can not be null");
|
||||
if (isRegistered(identifier)) {
|
||||
return false;
|
||||
}
|
||||
placeholders.put(identifier.toLowerCase(), placeholderHook);
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* check if a String contains any PlaceholderAPI placeholders
|
||||
* @param text String to check
|
||||
* @return true if String contains any registered placeholder identifiers, false otherwise
|
||||
*/
|
||||
public static boolean containsPlaceholders(String text) {
|
||||
return text != null && PLACEHOLDER_PATTERN.matcher(text).find();
|
||||
}
|
||||
|
||||
/**
|
||||
* check if a String contains any PlaceholderAPI bracket placeholders
|
||||
* @param text String to check
|
||||
* @return true if String contains any registered placeholder identifiers, false otherwise
|
||||
*/
|
||||
public static boolean containsBracketPlaceholders(String text) {
|
||||
return text != null && BRACKET_PLACEHOLDER_PATTERN.matcher(text).find();
|
||||
}
|
||||
|
||||
/**
|
||||
* set placeholders in the list<String> text provided
|
||||
* placeholders are matched with the pattern {<placeholder>} when set with this method
|
||||
* @param p Player to parse the placeholders for
|
||||
* @param text text to set the placeholder values in
|
||||
* @return modified list with all placeholders set to the corresponding values
|
||||
*/
|
||||
public static List<String> setBracketPlaceholders(Player p, List<String> text) {
|
||||
return setPlaceholders(p, text, BRACKET_PLACEHOLDER_PATTERN);
|
||||
}
|
||||
/**
|
||||
* unregister a placeholder hook by identifier
|
||||
*
|
||||
* @param identifier the identifier for the placeholder hook to unregister
|
||||
* @return true if the placeholder hook was successfully unregistered, false if there was no
|
||||
* placeholder hook registered for the identifier specified
|
||||
*/
|
||||
public static boolean unregisterPlaceholderHook(String identifier) {
|
||||
Validate.notNull(identifier, "Identifier can not be null");
|
||||
return placeholders.remove(identifier.toLowerCase()) != null;
|
||||
}
|
||||
|
||||
/**
|
||||
* set placeholders in the list<String> text provided
|
||||
* placeholders are matched with the pattern %(identifier)_(params)>% when set with this method
|
||||
* @param p Player to parse the placeholders for
|
||||
* @param text text to parse the placeholder values in
|
||||
* @return modified list with all placeholders set to the corresponding values
|
||||
*/
|
||||
public static List<String> setPlaceholders(Player p, List<String> text) {
|
||||
return setPlaceholders(p, text, PLACEHOLDER_PATTERN);
|
||||
}
|
||||
/**
|
||||
* Get all registered placeholder identifiers
|
||||
*
|
||||
* @return all registered placeholder identifiers
|
||||
*/
|
||||
public static Set<String> getRegisteredIdentifiers() {
|
||||
return ImmutableSet.copyOf(placeholders.keySet());
|
||||
}
|
||||
|
||||
/**
|
||||
* set placeholders in the list<String> text provided
|
||||
* placeholders are matched with the pattern %(identifier)_(params)>% when set with this method
|
||||
* @param p Player to parse the placeholders for
|
||||
* @param text text to parse the placeholder values in
|
||||
* @return modified list with all placeholders set to the corresponding values
|
||||
*/
|
||||
public static List<String> setPlaceholders(Player p, List<String> text, Pattern pattern) {
|
||||
if (text == null) return null;
|
||||
List<String> temp = new ArrayList<>();
|
||||
text.forEach(line -> {
|
||||
temp.add(setPlaceholders(p, line, pattern));
|
||||
});
|
||||
return temp;
|
||||
/**
|
||||
* Get map of registered placeholders
|
||||
*
|
||||
* @return copy of the internal placeholder map
|
||||
*/
|
||||
public static Map<String, PlaceholderHook> getPlaceholders() {
|
||||
return ImmutableMap.copyOf(placeholders);
|
||||
}
|
||||
|
||||
public static Set<PlaceholderExpansion> getExpansions() {
|
||||
Set<PlaceholderExpansion> set = getPlaceholders().values().stream()
|
||||
.filter(PlaceholderExpansion.class::isInstance).map(PlaceholderExpansion.class::cast)
|
||||
.collect(Collectors.toCollection(HashSet::new));
|
||||
return ImmutableSet.copyOf(set);
|
||||
}
|
||||
|
||||
/**
|
||||
* check if a String contains any PlaceholderAPI placeholders
|
||||
*
|
||||
* @param text String to check
|
||||
* @return true if String contains any registered placeholder identifiers, false otherwise
|
||||
*/
|
||||
public static boolean containsPlaceholders(String text) {
|
||||
return text != null && PLACEHOLDER_PATTERN.matcher(text).find();
|
||||
}
|
||||
|
||||
/**
|
||||
* check if a String contains any PlaceholderAPI bracket placeholders
|
||||
*
|
||||
* @param text String to check
|
||||
* @return true if String contains any registered placeholder identifiers, false otherwise
|
||||
*/
|
||||
public static boolean containsBracketPlaceholders(String text) {
|
||||
return text != null && BRACKET_PLACEHOLDER_PATTERN.matcher(text).find();
|
||||
}
|
||||
|
||||
/**
|
||||
* set placeholders in the list<String> text provided placeholders are matched with the pattern
|
||||
* {<placeholder>} when set with this method
|
||||
*
|
||||
* @param p Player to parse the placeholders for
|
||||
* @param text text to set the placeholder values in
|
||||
* @return modified list with all placeholders set to the corresponding values
|
||||
*/
|
||||
public static List<String> setBracketPlaceholders(OfflinePlayer p, List<String> text) {
|
||||
return setPlaceholders(p, text, BRACKET_PLACEHOLDER_PATTERN);
|
||||
}
|
||||
|
||||
/**
|
||||
* set placeholders in the list<String> text provided placeholders are matched with the pattern
|
||||
* %(identifier)_(params)>% when set with this method
|
||||
*
|
||||
* @param p Player to parse the placeholders for
|
||||
* @param text text to parse the placeholder values in
|
||||
* @return modified list with all placeholders set to the corresponding values
|
||||
*/
|
||||
public static List<String> setPlaceholders(OfflinePlayer p, List<String> text) {
|
||||
return setPlaceholders(p, text, PLACEHOLDER_PATTERN);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* set placeholders in the list<String> text provided placeholders are matched with the pattern
|
||||
* %(identifier)_(params)>% when set with this method
|
||||
*
|
||||
* @param p Player to parse the placeholders for
|
||||
* @param text text to parse the placeholder values in
|
||||
* @return modified list with all placeholders set to the corresponding values
|
||||
*/
|
||||
public static List<String> setPlaceholders(OfflinePlayer p, List<String> text, Pattern pattern) {
|
||||
if (text == null) {
|
||||
return null;
|
||||
}
|
||||
return text.stream().map(line -> setPlaceholders(p, line, pattern))
|
||||
.collect(Collectors.toList());
|
||||
}
|
||||
|
||||
/**
|
||||
* set placeholders in the text specified
|
||||
* placeholders are matched with the pattern {<placeholder>} when set with this method
|
||||
* @param player Player to parse the placeholders for
|
||||
* @param text text to parse the placeholder values to
|
||||
* @return modified text with all placeholders set to the corresponding values
|
||||
*/
|
||||
public static String setBracketPlaceholders(Player player, String text) {
|
||||
return setPlaceholders(player, text, BRACKET_PLACEHOLDER_PATTERN);
|
||||
/**
|
||||
* set placeholders in the text specified placeholders are matched with the pattern
|
||||
* {<placeholder>} when set with this method
|
||||
*
|
||||
* @param player Player to parse the placeholders for
|
||||
* @param text text to parse the placeholder values to
|
||||
* @return modified text with all placeholders set to the corresponding values
|
||||
*/
|
||||
public static String setBracketPlaceholders(OfflinePlayer player, String text) {
|
||||
return setPlaceholders(player, text, BRACKET_PLACEHOLDER_PATTERN);
|
||||
}
|
||||
|
||||
/**
|
||||
* set placeholders in the text specified placeholders are matched with the pattern
|
||||
* %<(identifier)_(params)>% when set with this method
|
||||
*
|
||||
* @param player Player to parse the placeholders for
|
||||
* @param text text to parse the placeholder values to
|
||||
* @return text with all placeholders set to the corresponding values
|
||||
*/
|
||||
public static String setPlaceholders(OfflinePlayer player, String text) {
|
||||
return setPlaceholders(player, text, PLACEHOLDER_PATTERN);
|
||||
}
|
||||
|
||||
/**
|
||||
* set placeholders in the text specified placeholders are matched with the pattern
|
||||
* %<(identifier)_(params)>% when set with this method
|
||||
*
|
||||
* @param player Player to parse the placeholders for
|
||||
* @param text text to parse the placeholder values to
|
||||
* @param placeholderPattern the pattern to match placeholders to. Capture group 1 must contain an
|
||||
* underscore separating the identifier from the params
|
||||
* @return text with all placeholders set to the corresponding values
|
||||
*/
|
||||
public static String setPlaceholders(OfflinePlayer player, String text,
|
||||
Pattern placeholderPattern) {
|
||||
if (text == null) {
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* set placeholders in the text specified
|
||||
* placeholders are matched with the pattern %<(identifier)_(params)>% when set with this method
|
||||
* @param player Player to parse the placeholders for
|
||||
* @param text text to parse the placeholder values to
|
||||
* @return text with all placeholders set to the corresponding values
|
||||
*/
|
||||
public static String setPlaceholders(Player player, String text) {
|
||||
return setPlaceholders(player, text, PLACEHOLDER_PATTERN);
|
||||
}
|
||||
|
||||
/**
|
||||
* set placeholders in the text specified
|
||||
* placeholders are matched with the pattern %<(identifier)_(params)>% when set with this method
|
||||
* @param player Player to parse the placeholders for
|
||||
* @param text text to parse the placeholder values to
|
||||
* @param placeholderPattern the pattern to match placeholders to. Capture group 1 must contain an underscore separating the identifier from the params
|
||||
* @return text with all placeholders set to the corresponding values
|
||||
*/
|
||||
public static String setPlaceholders(Player player, String text, Pattern placeholderPattern) {
|
||||
if (text == null) return null;
|
||||
if (placeholders.isEmpty()) return colorize(text);
|
||||
Matcher m = placeholderPattern.matcher(text);
|
||||
Map<String, PlaceholderHook> hooks = getPlaceholders();
|
||||
while (m.find()) {
|
||||
String format = m.group(1);
|
||||
int index = format.indexOf("_");
|
||||
if (index <= 0 || index >= format.length()) continue;
|
||||
String identifier = format.substring(0, index).toLowerCase();
|
||||
String params = format.substring(index+1);
|
||||
if (hooks.containsKey(identifier)) {
|
||||
String value = hooks.get(identifier).onPlaceholderRequest(player, params);
|
||||
if (value != null) {
|
||||
text = text.replaceAll(m.group(), Matcher.quoteReplacement(value));
|
||||
}
|
||||
}
|
||||
}
|
||||
return colorize(text);
|
||||
}
|
||||
|
||||
/**
|
||||
* set relational placeholders in the text specified
|
||||
* placeholders are matched with the pattern %<rel_(identifier)_(params)>% when set with this method
|
||||
* @param one Player to compare
|
||||
* @param two Player to compare
|
||||
* @param text text to parse the placeholder values to
|
||||
* @return text with all relational placeholders set to the corresponding values
|
||||
*/
|
||||
public static List<String> setRelationalPlaceholders(Player one, Player two, List<String> text) {
|
||||
if (text == null) return null;
|
||||
List<String> temp = new ArrayList<>();
|
||||
text.forEach(line -> {
|
||||
temp.add(setRelationalPlaceholders(one, two, line));
|
||||
});
|
||||
return temp;
|
||||
}
|
||||
|
||||
/**
|
||||
* set relational placeholders in the text specified
|
||||
* placeholders are matched with the pattern %<rel_(identifier)_(params)>% when set with this method
|
||||
* @param one Player to compare
|
||||
* @param two Player to compare
|
||||
* @param text text to parse the placeholder values to
|
||||
* @return text with all relational placeholders set to the corresponding values
|
||||
*/
|
||||
public static String setRelationalPlaceholders(Player one, Player two, String text) {
|
||||
if (text == null) return null;
|
||||
if (placeholders.isEmpty()) return colorize(text);
|
||||
Matcher m = RELATIONAL_PLACEHOLDER_PATTERN.matcher(text);
|
||||
Map<String, PlaceholderHook> hooks = getPlaceholders();
|
||||
while (m.find()) {
|
||||
String format = m.group(2);
|
||||
int index = format.indexOf("_");
|
||||
if (index <= 0 || index >= format.length()) continue;
|
||||
String identifier = format.substring(0, index).toLowerCase();
|
||||
String params = format.substring(index+1);
|
||||
if (hooks.containsKey(identifier)) {
|
||||
if (!(hooks.get(identifier) instanceof Relational)) {
|
||||
continue;
|
||||
}
|
||||
Relational rel = (Relational) hooks.get(identifier);
|
||||
String value = rel.onPlaceholderRequest(one, two, params);
|
||||
if (value != null) {
|
||||
text = text.replaceAll(m.group(), Matcher.quoteReplacement(value));
|
||||
}
|
||||
}
|
||||
}
|
||||
return colorize(text);
|
||||
}
|
||||
|
||||
/**
|
||||
* unregister ALL placeholder hooks that are currently registered
|
||||
*/
|
||||
protected static void unregisterAll() {
|
||||
unregisterAllProvidedExpansions();
|
||||
placeholders.clear();
|
||||
if (placeholders.isEmpty()) {
|
||||
return color(text);
|
||||
}
|
||||
|
||||
/**
|
||||
* unregister all expansions provided by PlaceholderAPI
|
||||
*/
|
||||
public static void unregisterAllProvidedExpansions() {
|
||||
if (placeholders.isEmpty()) return;
|
||||
getPlaceholders().forEach((key, value) -> {
|
||||
if (value instanceof PlaceholderExpansion) {
|
||||
PlaceholderExpansion ex = (PlaceholderExpansion) value;
|
||||
if (!ex.persist()) {
|
||||
unregisterExpansion(ex);
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
public static boolean unregisterExpansion(PlaceholderExpansion ex) {
|
||||
if (unregisterPlaceholderHook(ex.getIdentifier())) {
|
||||
Bukkit.getPluginManager().callEvent(new ExpansionUnregisterEvent(ex));
|
||||
return true;
|
||||
Matcher m = placeholderPattern.matcher(text);
|
||||
Map<String, PlaceholderHook> hooks = getPlaceholders();
|
||||
while (m.find()) {
|
||||
String format = m.group(1);
|
||||
int index = format.indexOf("_");
|
||||
if (index <= 0 || index >= format.length()) {
|
||||
continue;
|
||||
}
|
||||
String identifier = format.substring(0, index).toLowerCase();
|
||||
String params = format.substring(index + 1);
|
||||
if (hooks.containsKey(identifier)) {
|
||||
String value = hooks.get(identifier).onRequest(player, params);
|
||||
if (value != null) {
|
||||
text = text.replaceAll(Pattern.quote(m.group()), Matcher.quoteReplacement(value));
|
||||
}
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
public static Pattern getPlaceholderPattern() {
|
||||
return PLACEHOLDER_PATTERN;
|
||||
}
|
||||
|
||||
public static Pattern getBracketPlaceholderPattern() {
|
||||
return BRACKET_PLACEHOLDER_PATTERN;
|
||||
}
|
||||
|
||||
public static Pattern getRelationalPlaceholderPattern() {
|
||||
return RELATIONAL_PLACEHOLDER_PATTERN;
|
||||
}
|
||||
|
||||
public static String colorize(String text) {
|
||||
return ChatColor.translateAlternateColorCodes('&', text);
|
||||
}
|
||||
|
||||
@Deprecated
|
||||
public static Set<String> getRegisteredPlaceholderPlugins() {
|
||||
return getRegisteredIdentifiers();
|
||||
}
|
||||
@Deprecated
|
||||
public static Set<String> getExternalPlaceholderPlugins() {
|
||||
return null;
|
||||
}
|
||||
@Deprecated
|
||||
public static boolean registerPlaceholderHook(Plugin plugin, PlaceholderHook placeholderHook) {
|
||||
return plugin != null && registerPlaceholderHook(plugin.getName(), placeholderHook);
|
||||
}
|
||||
@Deprecated
|
||||
public static boolean unregisterPlaceholderHook(Plugin plugin) {
|
||||
return plugin != null && unregisterPlaceholderHook(plugin.getName());
|
||||
}
|
||||
return color(text);
|
||||
}
|
||||
|
||||
/**
|
||||
* set relational placeholders in the text specified placeholders are matched with the pattern
|
||||
* %<rel_(identifier)_(params)>% when set with this method
|
||||
*
|
||||
* @param one Player to compare
|
||||
* @param two Player to compare
|
||||
* @param text text to parse the placeholder values to
|
||||
* @return text with all relational placeholders set to the corresponding values
|
||||
*/
|
||||
public static List<String> setRelationalPlaceholders(Player one, Player two, List<String> text) {
|
||||
if (text == null) {
|
||||
return null;
|
||||
}
|
||||
return text.stream().map(line -> setRelationalPlaceholders(one, two, line))
|
||||
.collect(Collectors.toList());
|
||||
}
|
||||
|
||||
/**
|
||||
* set relational placeholders in the text specified placeholders are matched with the pattern
|
||||
* %<rel_(identifier)_(params)>% when set with this method
|
||||
*
|
||||
* @param one Player to compare
|
||||
* @param two Player to compare
|
||||
* @param text text to parse the placeholder values to
|
||||
* @return text with all relational placeholders set to the corresponding values
|
||||
*/
|
||||
public static String setRelationalPlaceholders(Player one, Player two, String text) {
|
||||
if (text == null) {
|
||||
return null;
|
||||
}
|
||||
if (placeholders.isEmpty()) {
|
||||
return color(text);
|
||||
}
|
||||
Matcher m = RELATIONAL_PLACEHOLDER_PATTERN.matcher(text);
|
||||
Map<String, PlaceholderHook> hooks = getPlaceholders();
|
||||
while (m.find()) {
|
||||
String format = m.group(2);
|
||||
int index = format.indexOf("_");
|
||||
if (index <= 0 || index >= format.length()) {
|
||||
continue;
|
||||
}
|
||||
String identifier = format.substring(0, index).toLowerCase();
|
||||
String params = format.substring(index + 1);
|
||||
if (hooks.containsKey(identifier)) {
|
||||
if (!(hooks.get(identifier) instanceof Relational)) {
|
||||
continue;
|
||||
}
|
||||
Relational rel = (Relational) hooks.get(identifier);
|
||||
String value = rel.onPlaceholderRequest(one, two, params);
|
||||
if (value != null) {
|
||||
text = text.replaceAll(Pattern.quote(m.group()), Matcher.quoteReplacement(value));
|
||||
}
|
||||
}
|
||||
}
|
||||
return color(text);
|
||||
}
|
||||
|
||||
/**
|
||||
* unregister ALL placeholder hooks that are currently registered
|
||||
*/
|
||||
protected static void unregisterAll() {
|
||||
unregisterAllProvidedExpansions();
|
||||
placeholders.clear();
|
||||
}
|
||||
|
||||
/**
|
||||
* unregister all expansions provided by PlaceholderAPI
|
||||
*/
|
||||
public static void unregisterAllProvidedExpansions() {
|
||||
if (placeholders.isEmpty()) {
|
||||
return;
|
||||
}
|
||||
getPlaceholders().forEach((key, value) -> {
|
||||
if (value instanceof PlaceholderExpansion) {
|
||||
PlaceholderExpansion ex = (PlaceholderExpansion) value;
|
||||
if (!ex.persist()) {
|
||||
unregisterExpansion(ex);
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
public static boolean registerExpansion(PlaceholderExpansion ex) {
|
||||
if (registerPlaceholderHook(ex.getIdentifier(), ex)) {
|
||||
Bukkit.getPluginManager().callEvent(new ExpansionRegisterEvent(ex));
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
public static boolean unregisterExpansion(PlaceholderExpansion ex) {
|
||||
if (unregisterPlaceholderHook(ex.getIdentifier())) {
|
||||
Bukkit.getPluginManager().callEvent(new ExpansionUnregisterEvent(ex));
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
public static Pattern getPlaceholderPattern() {
|
||||
return PLACEHOLDER_PATTERN;
|
||||
}
|
||||
|
||||
public static Pattern getBracketPlaceholderPattern() {
|
||||
return BRACKET_PLACEHOLDER_PATTERN;
|
||||
}
|
||||
|
||||
public static Pattern getRelationalPlaceholderPattern() {
|
||||
return RELATIONAL_PLACEHOLDER_PATTERN;
|
||||
}
|
||||
|
||||
@Deprecated
|
||||
public static Set<String> getRegisteredPlaceholderPlugins() {
|
||||
return getRegisteredIdentifiers();
|
||||
}
|
||||
|
||||
@Deprecated
|
||||
public static Set<String> getExternalPlaceholderPlugins() {
|
||||
return null;
|
||||
}
|
||||
|
||||
@Deprecated
|
||||
public static boolean registerPlaceholderHook(Plugin plugin, PlaceholderHook placeholderHook) {
|
||||
return plugin != null && registerPlaceholderHook(plugin.getName(), placeholderHook);
|
||||
}
|
||||
|
||||
@Deprecated
|
||||
public static boolean unregisterPlaceholderHook(Plugin plugin) {
|
||||
return plugin != null && unregisterPlaceholderHook(plugin.getName());
|
||||
}
|
||||
|
||||
@Deprecated
|
||||
public static String setPlaceholders(Player p, String text) {
|
||||
return setPlaceholders((OfflinePlayer)p, text, PLACEHOLDER_PATTERN);
|
||||
}
|
||||
|
||||
@Deprecated
|
||||
public static List<String> setPlaceholders(Player p, List<String> text) {
|
||||
return setPlaceholders((OfflinePlayer)p, text, PLACEHOLDER_PATTERN);
|
||||
}
|
||||
|
||||
@Deprecated
|
||||
public static String setBracketPlaceholders(Player p, String text) {
|
||||
return setPlaceholders((OfflinePlayer)p, text, BRACKET_PLACEHOLDER_PATTERN);
|
||||
}
|
||||
|
||||
@Deprecated
|
||||
public static List<String> setBracketPlaceholders(Player p, List<String> text) {
|
||||
return setPlaceholders((OfflinePlayer)p, text, BRACKET_PLACEHOLDER_PATTERN);
|
||||
}
|
||||
}
|
||||
|
@@ -79,12 +79,12 @@ public class PlaceholderAPIPlugin extends JavaPlugin {
|
||||
public void onEnable() {
|
||||
config.loadDefConfig();
|
||||
setupOptions();
|
||||
setupCommands();
|
||||
getCommand("placeholderapi").setExecutor(new PlaceholderAPICommands(this));
|
||||
new PlaceholderListener(this);
|
||||
getLogger().info("Placeholder expansion registration initializing...");
|
||||
expansionManager.registerAllExpansions();
|
||||
if (config.checkUpdates()) {
|
||||
new UpdateChecker(this);
|
||||
new UpdateChecker(this).fetch();
|
||||
}
|
||||
if (config.isCloudEnabled()) {
|
||||
enableCloud();
|
||||
@@ -118,10 +118,6 @@ public class PlaceholderAPIPlugin extends JavaPlugin {
|
||||
s.sendMessage(ChatColor.translateAlternateColorCodes('&', PlaceholderAPI.getRegisteredIdentifiers().size() + " &aplaceholder hooks successfully registered!"));
|
||||
}
|
||||
|
||||
private void setupCommands() {
|
||||
getCommand("placeholderapi").setExecutor(new PlaceholderAPICommands(this, (serverVersion != null && serverVersion.isSpigot())));
|
||||
}
|
||||
|
||||
private void setupOptions() {
|
||||
booleanTrue = config.booleanTrue();
|
||||
if (booleanTrue == null) {
|
||||
@@ -153,8 +149,8 @@ public class PlaceholderAPIPlugin extends JavaPlugin {
|
||||
for (PlaceholderHook hook : p.values()) {
|
||||
if (hook instanceof PlaceholderExpansion) {
|
||||
PlaceholderExpansion ex = (PlaceholderExpansion) hook;
|
||||
map.put(ex.getPlugin() == null ? ex.getIdentifier()
|
||||
: ex.getPlugin(), 1);
|
||||
map.put(ex.getRequiredPlugin() == null ? ex.getIdentifier()
|
||||
: ex.getRequiredPlugin(), 1);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -184,10 +180,10 @@ public class PlaceholderAPIPlugin extends JavaPlugin {
|
||||
public void enableCloud() {
|
||||
if (expansionCloud == null) {
|
||||
expansionCloud = new ExpansionCloudManager(this);
|
||||
expansionCloud.fetch();
|
||||
expansionCloud.fetch(config.cloudAllowUnverifiedExpansions());
|
||||
} else {
|
||||
expansionCloud.clean();
|
||||
expansionCloud.fetch();
|
||||
expansionCloud.fetch(config.cloudAllowUnverifiedExpansions());
|
||||
}
|
||||
}
|
||||
|
||||
|
@@ -20,15 +20,31 @@
|
||||
*/
|
||||
package me.clip.placeholderapi;
|
||||
|
||||
import org.bukkit.OfflinePlayer;
|
||||
import org.bukkit.entity.Player;
|
||||
|
||||
public abstract class PlaceholderHook {
|
||||
|
||||
/**
|
||||
* called when a placeholder value is requested from this hook
|
||||
* @param p {@link OfflinePlayer} to request the placeholder value for, null if not needed for a player
|
||||
* @param params String passed to the hook to determine what value to return
|
||||
* @return value for the requested player and params
|
||||
*/
|
||||
public String onRequest(OfflinePlayer p, String params) {
|
||||
if (p != null && p.isOnline()) {
|
||||
return onPlaceholderRequest((Player) p, params);
|
||||
}
|
||||
return onPlaceholderRequest(null, params);
|
||||
}
|
||||
|
||||
/**
|
||||
* called when a placeholder is requested from this PlaceholderHook
|
||||
* @param p Player requesting the placeholder value for, null if not needed for a player
|
||||
* @param params String passed for the placeholder hook to determine what value to return
|
||||
* called when a placeholder is requested from this hook
|
||||
* @param p {@link Player} to request the placeholder value for, null if not needed for a player
|
||||
* @param params String passed to the hook to determine what value to return
|
||||
* @return value for the requested player and params
|
||||
*/
|
||||
public abstract String onPlaceholderRequest(Player p, String params);
|
||||
public String onPlaceholderRequest(Player p, String params) {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
@@ -80,7 +80,7 @@ public class PlaceholderListener implements Listener {
|
||||
PlaceholderExpansion e = m.getCachedExpansion(event.getPlugin().getName().toLowerCase());
|
||||
if (e != null && e.canRegister()) {
|
||||
if (e.isRegistered() || m.registerExpansion(e)) {
|
||||
m.removeCachedExpansion(e.getPlugin());
|
||||
m.removeCachedExpansion(e.getRequiredPlugin());
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -122,11 +122,11 @@ public class PlaceholderListener implements Listener {
|
||||
|
||||
PlaceholderExpansion ex = (PlaceholderExpansion) i;
|
||||
|
||||
if (ex.getPlugin() == null) {
|
||||
if (ex.getRequiredPlugin() == null) {
|
||||
continue;
|
||||
}
|
||||
|
||||
if (ex.getPlugin().equalsIgnoreCase(n)) {
|
||||
if (ex.getRequiredPlugin().equalsIgnoreCase(n)) {
|
||||
if (PlaceholderAPI.unregisterExpansion(ex)) {
|
||||
plugin.getLogger().info("Unregistered placeholder expansion: " + ex.getIdentifier());
|
||||
}
|
||||
|
@@ -0,0 +1,396 @@
|
||||
/*
|
||||
*
|
||||
* PlaceholderAPI
|
||||
* Copyright (C) 2018 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.commands;
|
||||
|
||||
import me.clip.placeholderapi.PlaceholderAPI;
|
||||
import me.clip.placeholderapi.PlaceholderAPIPlugin;
|
||||
import me.clip.placeholderapi.expansion.PlaceholderExpansion;
|
||||
import me.clip.placeholderapi.expansion.cloud.CloudExpansion;
|
||||
import me.rayzr522.jsonmessage.JSONMessage;
|
||||
import org.bukkit.command.Command;
|
||||
import org.bukkit.command.CommandExecutor;
|
||||
import org.bukkit.command.CommandSender;
|
||||
import org.bukkit.entity.Player;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Map.Entry;
|
||||
|
||||
import static me.clip.placeholderapi.util.Msg.color;
|
||||
import static me.clip.placeholderapi.util.Msg.msg;
|
||||
|
||||
public class ExpansionCloudCommands implements CommandExecutor {
|
||||
|
||||
private PlaceholderAPIPlugin plugin;
|
||||
|
||||
public ExpansionCloudCommands(PlaceholderAPIPlugin instance) {
|
||||
plugin = instance;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean onCommand(CommandSender s, Command c, String label, String[] args) {
|
||||
|
||||
if (args.length == 1) {
|
||||
msg(s, "&bExpansion cloud commands",
|
||||
" ",
|
||||
"&b/papi ecloud status",
|
||||
"&fView status of the ecloud",
|
||||
"&b/papi ecloud list <all/author> (page)",
|
||||
"&fList all/author specific available expansions",
|
||||
"&b/papi ecloud info <expansion name>",
|
||||
"&fView information about a specific expansion available on the cloud",
|
||||
"&b/papi ecloud versioninfo <expansion name> <version>",
|
||||
"&fView information about a specific version of an expansion",
|
||||
"&b/papi ecloud placeholders <expansion name>",
|
||||
"&fView placeholders for an expansion",
|
||||
"&b/papi ecloud download <expansion name> (version)",
|
||||
"&fDownload an expansion from the ecloud",
|
||||
"&b/papi ecloud refresh",
|
||||
"&fFetch the most up to date list of expansions available.",
|
||||
"&b/papi ecloud clear",
|
||||
"&fClear the expansion cloud cache.");
|
||||
return true;
|
||||
}
|
||||
|
||||
if (args[1].equalsIgnoreCase("refresh") || args[1].equalsIgnoreCase("update") || args[1].equalsIgnoreCase("fetch")) {
|
||||
msg(s, "&aRefresh task started. Use &f/papi ecloud list all &ain a few!!");
|
||||
plugin.getExpansionCloud().clean();
|
||||
plugin.getExpansionCloud().fetch(plugin.getPlaceholderAPIConfig().cloudAllowUnverifiedExpansions());
|
||||
return true;
|
||||
}
|
||||
|
||||
if (plugin.getExpansionCloud().getCloudExpansions().isEmpty()) {
|
||||
msg(s, "&7No cloud expansions are available at this time.");
|
||||
return true;
|
||||
}
|
||||
|
||||
if (args[1].equalsIgnoreCase("clear")) {
|
||||
plugin.getExpansionCloud().clean();
|
||||
msg(s, "&aThe cache has been cleared!!");
|
||||
return true;
|
||||
}
|
||||
|
||||
if (args[1].equalsIgnoreCase("status")) {
|
||||
|
||||
msg(s, "&bThere are &f" + plugin.getExpansionCloud().getCloudExpansions().size() + " &bexpansions available on the cloud.",
|
||||
"&7A total of &f" + plugin.getExpansionCloud().getCloudAuthorCount() + " &7authors have contributed to the expansion cloud.");
|
||||
if (plugin.getExpansionCloud().getToUpdateCount() > 0) {
|
||||
msg(s, "&eYou have &f" + plugin.getExpansionCloud().getToUpdateCount()
|
||||
+ " &eexpansions installed that have updates available.");
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
if (args[1].equalsIgnoreCase("info")) {
|
||||
|
||||
if (args.length < 3) {
|
||||
msg(s, "&cAn expansion name must be specified!");
|
||||
return true;
|
||||
}
|
||||
|
||||
CloudExpansion expansion = plugin.getExpansionCloud().getCloudExpansion(args[2]);
|
||||
|
||||
if (expansion == null) {
|
||||
msg(s, "&cNo expansion found by the name: &f" + args[2]);
|
||||
return true;
|
||||
}
|
||||
|
||||
if (!(s instanceof Player)) {
|
||||
msg(s, (expansion.shouldUpdate() ? "&e" : "") + expansion.getName() + " &8&m-- &r" + expansion.getVersion().getUrl());
|
||||
return true;
|
||||
}
|
||||
|
||||
Player p = (Player) s;
|
||||
|
||||
msg(s, "&bExpansion&7: &f" + expansion.getName(),
|
||||
"&bAuthor: &f" + expansion.getAuthor(),
|
||||
"&bVerified: &f" + expansion.isVerified()
|
||||
);
|
||||
|
||||
// latest version
|
||||
JSONMessage latestVersion = JSONMessage.create(color("&bLatest version: &f" + expansion.getLatestVersion()));
|
||||
latestVersion.tooltip(color("&bReleased: &f" + expansion.getTimeSinceLastUpdate()
|
||||
+ "\n&bUpdate information: &f" + expansion.getVersion().getReleaseNotes()
|
||||
));
|
||||
latestVersion.send(p);
|
||||
|
||||
// versions
|
||||
JSONMessage versions = JSONMessage.create(color("&bVersions available: &f" + expansion.getVersions().size()));
|
||||
versions.tooltip(color(String.join("&b, &f", expansion.getAvailableVersions())));
|
||||
versions.suggestCommand("/papi ecloud versioninfo " + expansion.getName() + " " + expansion.getLatestVersion());
|
||||
versions.send(p);
|
||||
|
||||
// placeholders
|
||||
if (expansion.getPlaceholders() != null) {
|
||||
JSONMessage placeholders = JSONMessage.create(color("&bPlaceholders: &f" + expansion.getPlaceholders().size()));
|
||||
placeholders.tooltip(color(String.join("&b, &f", expansion.getPlaceholders())));
|
||||
placeholders.suggestCommand("/papi ecloud placeholders " + expansion.getName());
|
||||
placeholders.send(p);
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
if (args[1].equalsIgnoreCase("versioninfo")) {
|
||||
|
||||
if (args.length < 4) {
|
||||
msg(s, "&cAn expansion name and version must be specified!");
|
||||
return true;
|
||||
}
|
||||
|
||||
CloudExpansion expansion = plugin.getExpansionCloud().getCloudExpansion(args[2]);
|
||||
|
||||
if (expansion == null) {
|
||||
msg(s, "&cNo expansion found by the name: &f" + args[2]);
|
||||
return true;
|
||||
}
|
||||
|
||||
CloudExpansion.Version version = expansion.getVersion(args[3]);
|
||||
|
||||
if (version == null) {
|
||||
msg(s, "&cThe version specified does not exist for expansion: &f" + expansion.getName());
|
||||
return true;
|
||||
}
|
||||
|
||||
msg(s, "&bExpansion: " + (expansion.shouldUpdate() ? "&e" : "&f") + expansion.getName(),
|
||||
"&bVersion: &f" + version.getVersion(),
|
||||
"&bVersion info: &f" + version.getReleaseNotes());
|
||||
|
||||
if (!(s instanceof Player)) {
|
||||
msg(s, "&bDownload url: " + version.getUrl());
|
||||
return true;
|
||||
}
|
||||
|
||||
Player p = (Player) s;
|
||||
|
||||
JSONMessage download = JSONMessage.create(color("&7Click to download this version"));
|
||||
download.suggestCommand("/papi ecloud download " + expansion.getName() + " " + version.getVersion());
|
||||
download.send(p);
|
||||
return true;
|
||||
}
|
||||
|
||||
if (args[1].equalsIgnoreCase("placeholders")) {
|
||||
|
||||
if (args.length < 3) {
|
||||
msg(s, "&cAn expansion name must be specified!");
|
||||
return true;
|
||||
}
|
||||
|
||||
CloudExpansion expansion = plugin.getExpansionCloud().getCloudExpansion(args[2]);
|
||||
|
||||
if (expansion == null) {
|
||||
msg(s, "&cNo expansion found by the name: &f" + args[2]);
|
||||
return true;
|
||||
}
|
||||
|
||||
List<String> placeholders = expansion.getPlaceholders();
|
||||
|
||||
if (placeholders == null) {
|
||||
msg(s, "&cThe expansion: &f" + expansion.getName() + " &cdoes not have any placeholders listed.",
|
||||
"&7You should contact &f" + expansion.getAuthor() + " &7and ask for them to be added.");
|
||||
return true;
|
||||
}
|
||||
|
||||
if (!(s instanceof Player) || plugin.getExpansionManager().getRegisteredExpansion(expansion.getName()) == null) {
|
||||
msg(s, "&bPlaceholders: &f" + placeholders.size(),
|
||||
String.join("&a, &f", placeholders));
|
||||
return true;
|
||||
}
|
||||
|
||||
Player p = (Player) s;
|
||||
JSONMessage message = JSONMessage.create(color("&bPlaceholders: &f" + placeholders.size()));
|
||||
message.then("\n");
|
||||
|
||||
for (int i = 0 ; i < placeholders.size() ; i++) {
|
||||
if (i == placeholders.size()-1) {
|
||||
message.then(placeholders.get(i));
|
||||
} else {
|
||||
message.then(color(placeholders.get(i) + "&b, &f"));
|
||||
}
|
||||
message.tooltip(PlaceholderAPI.setPlaceholders(p, placeholders.get(i)));
|
||||
}
|
||||
|
||||
message.send(p);
|
||||
return true;
|
||||
}
|
||||
|
||||
if (args[1].equalsIgnoreCase("list")) {
|
||||
|
||||
int page = 1;
|
||||
|
||||
String author;
|
||||
boolean installed = false;
|
||||
|
||||
if (args.length < 3) {
|
||||
msg(s, "&cIncorrect usage! &7/papi ecloud list <all/author/installed> (page)");
|
||||
return true;
|
||||
}
|
||||
|
||||
author = args[2];
|
||||
|
||||
if (author.equalsIgnoreCase("all")) {
|
||||
author = null;
|
||||
} else if (author.equalsIgnoreCase("installed")) {
|
||||
author = null;
|
||||
installed = true;
|
||||
}
|
||||
|
||||
if (args.length >= 4) {
|
||||
try {
|
||||
page = Integer.parseInt(args[3]);
|
||||
} catch (NumberFormatException ex) {
|
||||
msg(s, "&cPage number must be an integer!");
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
if (page < 1) {
|
||||
msg(s, "&cPage must be greater than or equal to 1!");
|
||||
return true;
|
||||
}
|
||||
|
||||
int avail;
|
||||
|
||||
Map<Integer, CloudExpansion> ex;
|
||||
|
||||
if (installed) {
|
||||
ex = plugin.getExpansionCloud().getAllInstalled();
|
||||
} else if (author == null) {
|
||||
ex = plugin.getExpansionCloud().getCloudExpansions();
|
||||
} else {
|
||||
ex = plugin.getExpansionCloud().getAllByAuthor(author);
|
||||
}
|
||||
|
||||
if (ex == null || ex.isEmpty()) {
|
||||
msg(s, "&cNo expansions available" + (author != null ? " for author &f" + author : ""));
|
||||
return true;
|
||||
}
|
||||
|
||||
avail = plugin.getExpansionCloud().getPagesAvailable(ex, 10);
|
||||
|
||||
if (page > avail) {
|
||||
msg(s, "&cThere " + ((avail == 1) ? " is only &f" + avail + " &cpage available!" : "are only &f" + avail + " &cpages available!"));
|
||||
return true;
|
||||
}
|
||||
|
||||
msg(s, "&bShowing expansions for&7: &f" + (author != null ? author : (installed ? "all installed" : "all available"))+ " &8&m--&r &bamount&7: &f" + ex.size() + " &bpage&7: &f" + page + "&7/&f" + avail);
|
||||
|
||||
ex = plugin.getExpansionCloud().getPage(ex, page, 10);
|
||||
|
||||
if (ex == null) {
|
||||
msg(s, "&cThere was a problem getting the requested page...");
|
||||
return true;
|
||||
}
|
||||
|
||||
msg(s, "&aGreen = Expansions you have");
|
||||
msg(s, "&6Gold = Expansions which need updated");
|
||||
|
||||
if (!(s instanceof Player)) {
|
||||
|
||||
for (Entry<Integer, CloudExpansion> expansion : ex.entrySet()) {
|
||||
if (expansion == null || expansion.getValue() == null) continue;
|
||||
msg(s, "&b" + (expansion.getKey()+1) + "&7: " + (expansion.getValue().shouldUpdate() ? "&6" : (expansion.getValue().hasExpansion() ? "&a" : "&7")) + expansion.getValue().getName() + " &8&m-- &r" + expansion.getValue().getVersion().getUrl());
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
Player p = (Player) s;
|
||||
|
||||
for (Entry<Integer, CloudExpansion> expansion : ex.entrySet()) {
|
||||
|
||||
if (expansion == null || expansion.getValue() == null) {
|
||||
continue;
|
||||
}
|
||||
|
||||
StringBuilder sb = new StringBuilder();
|
||||
if (expansion.getValue().shouldUpdate()) {
|
||||
sb.append("&6Click to update to the latest version of this expansion\n\n");
|
||||
} else if (!expansion.getValue().hasExpansion()) {
|
||||
sb.append("&bClick to download this expansion\n\n");
|
||||
} else {
|
||||
sb.append("&aYou have the latest version of this expansion\n\n");
|
||||
}
|
||||
sb.append("&bAuthor&7: &f" + expansion.getValue().getAuthor() + "\n");
|
||||
sb.append("&bVerified&7: &f" + expansion.getValue().isVerified() + "\n");
|
||||
sb.append("&bLatest version&7: &f" + expansion.getValue().getVersion().getVersion() + "\n");
|
||||
sb.append("&bLast updated&7: &f" + expansion.getValue().getTimeSinceLastUpdate() + " ago\n");
|
||||
sb.append("\n" + expansion.getValue().getDescription());
|
||||
|
||||
String msg = color("&b" + (expansion.getKey()+1) + "&7: " + (expansion.getValue().shouldUpdate() ? "&6" : (expansion.getValue().hasExpansion() ? "&a" : "")) + expansion.getValue().getName());
|
||||
|
||||
String hover = color(sb.toString());
|
||||
|
||||
JSONMessage line = JSONMessage.create(msg);
|
||||
line.tooltip(hover);
|
||||
line.suggestCommand("/papi ecloud info " + expansion.getValue().getName());
|
||||
line.send(p);
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
if (args[1].equalsIgnoreCase("download")) {
|
||||
|
||||
if (args.length < 3) {
|
||||
msg(s, "&cAn expansion name must be specified!");
|
||||
return true;
|
||||
}
|
||||
|
||||
CloudExpansion expansion = plugin.getExpansionCloud().getCloudExpansion(args[2]);
|
||||
|
||||
if (expansion == null) {
|
||||
msg(s, "&cNo expansion found with the name: &f" + args[2]);
|
||||
return true;
|
||||
}
|
||||
|
||||
PlaceholderExpansion loaded = plugin.getExpansionManager().getRegisteredExpansion(args[2]);
|
||||
|
||||
if (loaded != null && loaded.isRegistered()) {
|
||||
PlaceholderAPI.unregisterPlaceholderHook(loaded.getIdentifier());
|
||||
}
|
||||
|
||||
String version = expansion.getLatestVersion();
|
||||
|
||||
if (args.length == 4) {
|
||||
version = args[3];
|
||||
if (expansion.getVersion(version) == null) {
|
||||
msg(s, "&cThe version you specified does not exist for &f" + expansion.getName());
|
||||
msg(s, "&7Available versions: &f" + expansion.getVersions().size());
|
||||
msg(s, String.join("&a, &f", expansion.getAvailableVersions()));
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
msg(s, "&aDownload starting for expansion: &f" + expansion.getName() + " &aversion: &f" + version);
|
||||
String player = ((s instanceof Player) ? s.getName() : null);
|
||||
plugin.getExpansionCloud().downloadExpansion(player, expansion, version);
|
||||
return true;
|
||||
}
|
||||
|
||||
msg(s, "&cIncorrect usage! &b/papi ecloud");
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
|
||||
}
|
@@ -22,7 +22,6 @@ package me.clip.placeholderapi.commands;
|
||||
|
||||
import me.clip.placeholderapi.PlaceholderAPI;
|
||||
import me.clip.placeholderapi.PlaceholderAPIPlugin;
|
||||
import me.clip.placeholderapi.commands.spigot.ExpansionCloudCommands;
|
||||
import me.clip.placeholderapi.expansion.PlaceholderExpansion;
|
||||
import me.clip.placeholderapi.util.Msg;
|
||||
import org.apache.commons.lang.StringUtils;
|
||||
@@ -40,13 +39,9 @@ public class PlaceholderAPICommands implements CommandExecutor {
|
||||
|
||||
private CommandExecutor eCloud;
|
||||
|
||||
public PlaceholderAPICommands(PlaceholderAPIPlugin i, boolean spigot) {
|
||||
public PlaceholderAPICommands(PlaceholderAPIPlugin i) {
|
||||
plugin = i;
|
||||
if (spigot) {
|
||||
eCloud = new me.clip.placeholderapi.commands.spigot.ExpansionCloudCommands(i);
|
||||
} else {
|
||||
eCloud = new me.clip.placeholderapi.commands.bukkit.ExpansionCloudCommands(i);
|
||||
}
|
||||
eCloud = new ExpansionCloudCommands(i);
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -74,7 +69,7 @@ public class PlaceholderAPICommands implements CommandExecutor {
|
||||
"&b/papi reload",
|
||||
"&fReload the config settings");
|
||||
|
||||
if (s.isOp()) {
|
||||
if (s.hasPermission("placeholderapi.ecloud")) {
|
||||
if (plugin.getExpansionCloud() == null) {
|
||||
Msg.msg(s, "&b/papi enablecloud",
|
||||
"&fEnable the expansion cloud");
|
||||
@@ -82,22 +77,14 @@ public class PlaceholderAPICommands implements CommandExecutor {
|
||||
Msg.msg(s, "&b/papi disablecloud",
|
||||
"&fDisable the expansion cloud",
|
||||
"&b/papi ecloud",
|
||||
"&fView information about the PlaceholderAPI expansion cloud",
|
||||
"&b/papi ecloud status",
|
||||
"&fView status of the PlaceholderAPI expansion cloud",
|
||||
"&b/papi ecloud list <all/author> <page>",
|
||||
"&fList all available expansions",
|
||||
"&b/papi ecloud info <expansion name>",
|
||||
"&fView information about a specific expansion on the cloud",
|
||||
"&b/papi ecloud download <expansion name>",
|
||||
"&fDownload a specific expansion from the cloud");
|
||||
"&fView ecloud command usage");
|
||||
}
|
||||
}
|
||||
|
||||
return true;
|
||||
|
||||
} else if (args[0].equalsIgnoreCase("ecloud")) {
|
||||
if (!s.isOp()) {
|
||||
if (!s.hasPermission("placeholderapi.ecloud")) {
|
||||
Msg.msg(s, "&cYou don't have permission to do that!");
|
||||
return true;
|
||||
}
|
||||
@@ -110,7 +97,7 @@ public class PlaceholderAPICommands implements CommandExecutor {
|
||||
return eCloud.onCommand(s, c, label, args);
|
||||
|
||||
} else if (args[0].equalsIgnoreCase("enablecloud")) {
|
||||
if (!s.isOp()) {
|
||||
if (!s.hasPermission("placeholderapi.ecloud")) {
|
||||
Msg.msg(s, "&cYou don't have permission to do that!");
|
||||
return true;
|
||||
}
|
||||
@@ -127,7 +114,7 @@ public class PlaceholderAPICommands implements CommandExecutor {
|
||||
|
||||
} else if (args[0].equalsIgnoreCase("disablecloud")) {
|
||||
|
||||
if (!s.isOp()) {
|
||||
if (!s.hasPermission("placeholderapi.ecloud")) {
|
||||
Msg.msg(s, "&cYou don't have permission to do that!");
|
||||
return true;
|
||||
}
|
||||
@@ -156,7 +143,7 @@ public class PlaceholderAPICommands implements CommandExecutor {
|
||||
return true;
|
||||
}
|
||||
|
||||
Msg.msg(s, "&7Placeholder expansion info for: &f%" + ex.getIdentifier() + "_<identifier>%");
|
||||
Msg.msg(s, "&7Placeholder expansion info for: &f" + ex.getName());
|
||||
|
||||
Msg.msg(s, "&7Status: " + (ex.isRegistered() ? "&aRegistered" : "&cNot registered"));
|
||||
|
||||
@@ -168,16 +155,8 @@ public class PlaceholderAPICommands implements CommandExecutor {
|
||||
Msg.msg(s, "&7Version: &f" + ex.getVersion());
|
||||
}
|
||||
|
||||
if (ex.getDescription() != null) {
|
||||
Msg.msg(s, ex.getDescription());
|
||||
}
|
||||
|
||||
if (ex.getLink() != null) {
|
||||
Msg.msg(s, "&7Link: &f" + ex.getLink());
|
||||
}
|
||||
|
||||
if (ex.getPlugin() != null) {
|
||||
Msg.msg(s, "&7Requires plugin: &f" + ex.getPlugin());
|
||||
if (ex.getRequiredPlugin() != null) {
|
||||
Msg.msg(s, "&7Requires plugin: &f" + ex.getRequiredPlugin());
|
||||
}
|
||||
|
||||
if (ex.getPlaceholders() != null) {
|
||||
|
@@ -1,232 +0,0 @@
|
||||
/*
|
||||
*
|
||||
* PlaceholderAPI
|
||||
* Copyright (C) 2018 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.commands.bukkit;
|
||||
|
||||
import me.clip.placeholderapi.PlaceholderAPI;
|
||||
import me.clip.placeholderapi.PlaceholderAPIPlugin;
|
||||
import me.clip.placeholderapi.expansion.PlaceholderExpansion;
|
||||
import me.clip.placeholderapi.expansion.cloud.CloudExpansion;
|
||||
import me.clip.placeholderapi.util.Msg;
|
||||
import org.bukkit.command.Command;
|
||||
import org.bukkit.command.CommandExecutor;
|
||||
import org.bukkit.command.CommandSender;
|
||||
import org.bukkit.entity.Player;
|
||||
|
||||
import java.util.Map;
|
||||
import java.util.Map.Entry;
|
||||
|
||||
public class ExpansionCloudCommands implements CommandExecutor {
|
||||
|
||||
private PlaceholderAPIPlugin plugin;
|
||||
|
||||
public ExpansionCloudCommands(PlaceholderAPIPlugin instance) {
|
||||
plugin = instance;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean onCommand(CommandSender s, Command c, String label, String[] args) {
|
||||
|
||||
if (args.length == 1) {
|
||||
Msg.msg(s, "&bExpansion cloud commands",
|
||||
" ",
|
||||
"&b/papi ecloud status",
|
||||
"&fView status of the cloud",
|
||||
"&b/papi ecloud list <all/author> (page)",
|
||||
"&fList all/author specific available expansions",
|
||||
"&b/papi ecloud info <expansion name>",
|
||||
"&fView information about a specific expansion available on the cloud",
|
||||
"&b/papi ecloud download <expansion name>",
|
||||
"&fDownload a specific expansion from the cloud",
|
||||
"&b/papi ecloud refresh",
|
||||
"&fFetch the most up to date list of expansions available.",
|
||||
"&b/papi ecloud clear",
|
||||
"&fClear the expansion cloud cache.");
|
||||
return true;
|
||||
}
|
||||
|
||||
if (args[1].equalsIgnoreCase("refresh")) {
|
||||
Msg.msg(s, "&aRefresh task started. Use &7/papi ecloud list all &fin a few!!");
|
||||
plugin.getExpansionCloud().clean();
|
||||
plugin.getExpansionCloud().fetch();
|
||||
return true;
|
||||
}
|
||||
|
||||
if (plugin.getExpansionCloud().getCloudExpansions().isEmpty()) {
|
||||
Msg.msg(s, "&7No cloud expansions are available at this time.");
|
||||
return true;
|
||||
}
|
||||
|
||||
if (args[1].equalsIgnoreCase("clear")) {
|
||||
plugin.getExpansionCloud().clean();
|
||||
Msg.msg(s, "&aThe cloud cache has been cleared!!");
|
||||
return true;
|
||||
}
|
||||
|
||||
if (args[1].equalsIgnoreCase("download")) {
|
||||
|
||||
if (args.length < 3) {
|
||||
Msg.msg(s, "&cAn expansion name must be specified!");
|
||||
return true;
|
||||
}
|
||||
|
||||
CloudExpansion expansion = plugin.getExpansionCloud().getCloudExpansion(args[2]);
|
||||
|
||||
if (expansion == null) {
|
||||
Msg.msg(s, "&cNo expansion found with the name: &f" + args[2]);
|
||||
return true;
|
||||
}
|
||||
|
||||
PlaceholderExpansion loaded = plugin.getExpansionManager().getRegisteredExpansion(args[2]);
|
||||
|
||||
if (loaded != null && loaded.isRegistered()) {
|
||||
PlaceholderAPI.unregisterPlaceholderHook(loaded.getIdentifier());
|
||||
}
|
||||
|
||||
Msg.msg(s, "&aAttempting download of expansion &f" + expansion.getName());
|
||||
|
||||
String player = ((s instanceof Player) ? s.getName() : null);
|
||||
|
||||
plugin.getExpansionCloud().downloadExpansion(player, expansion);
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
if (args[1].equalsIgnoreCase("status")) {
|
||||
|
||||
Msg.msg(s, "&bThere are &f" + plugin.getExpansionCloud().getCloudExpansions().size() + " &bcloud expansions available to download",
|
||||
"&bA total of &f" + plugin.getExpansionCloud().getCloudAuthorCount() + " &bauthors have contributed.");
|
||||
|
||||
return true;
|
||||
} else if (args[1].equalsIgnoreCase("info")) {
|
||||
|
||||
if (args.length < 3) {
|
||||
Msg.msg(s, "&cAn expansion name must be specified!");
|
||||
return true;
|
||||
}
|
||||
|
||||
CloudExpansion expansion = plugin.getExpansionCloud().getCloudExpansion(args[2]);
|
||||
|
||||
if (expansion == null) {
|
||||
Msg.msg(s, "&cNo expansion found with the name: &f" + args[2]);
|
||||
return true;
|
||||
}
|
||||
|
||||
PlaceholderExpansion exp = plugin.getExpansionManager().getRegisteredExpansion(args[2]);
|
||||
|
||||
boolean enabled = false;
|
||||
String version = null;
|
||||
|
||||
if (exp != null) {
|
||||
enabled = exp.isRegistered();
|
||||
version = exp.getVersion();
|
||||
}
|
||||
|
||||
Msg.msg(s, "&aExpansion: &f" + expansion.getName());
|
||||
if (enabled) {
|
||||
Msg.msg(s, "&aThis expansion is currently enabled!",
|
||||
"&bYour version&7: &f" + version);
|
||||
}
|
||||
|
||||
Msg.msg(s, "&bCloud version&7: &f" + expansion.getVersion(),
|
||||
"&bAuthor&7: &f" + expansion.getAuthor());
|
||||
|
||||
String desc = expansion.getVersion();
|
||||
|
||||
if (desc.indexOf("\n") > 0) {
|
||||
for (String line : desc.split("\n")) {
|
||||
Msg.msg(s, line);
|
||||
}
|
||||
} else {
|
||||
Msg.msg(s, desc);
|
||||
}
|
||||
|
||||
Msg.msg(s, "&bDownload with &7/papi ecloud download " + expansion.getName());
|
||||
return true;
|
||||
|
||||
} else if (args[1].equalsIgnoreCase("list")) {
|
||||
|
||||
int page = 1;
|
||||
|
||||
String author;
|
||||
|
||||
if (args.length < 3) {
|
||||
Msg.msg(s, "&cIncorrect usage! &7/papi ecloud list <all/author> (page)");
|
||||
return true;
|
||||
}
|
||||
|
||||
author = args[2];
|
||||
|
||||
if (author.equalsIgnoreCase("all")) {
|
||||
author = null;
|
||||
}
|
||||
|
||||
if (args.length >= 4) {
|
||||
try {
|
||||
page = Integer.parseInt(args[3]);
|
||||
} catch (NumberFormatException ex) {
|
||||
Msg.msg(s, "&cPage number must be an integer!");
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
if (page < 1) {
|
||||
Msg.msg(s, "&cPage must be greater than or equal to 1!");
|
||||
return true;
|
||||
}
|
||||
|
||||
int avail;
|
||||
|
||||
Map<Integer, CloudExpansion> ex;
|
||||
|
||||
if (author == null) {
|
||||
ex = plugin.getExpansionCloud().getCloudExpansions();
|
||||
} else {
|
||||
ex = plugin.getExpansionCloud().getAllByAuthor(author);
|
||||
}
|
||||
|
||||
if (ex == null) {
|
||||
Msg.msg(s, "&cNo expansions available" + (author != null ? " for author &f" + author : ""));
|
||||
return true;
|
||||
}
|
||||
|
||||
avail = plugin.getExpansionCloud().getPagesAvailable(ex, 10);
|
||||
|
||||
if (page > avail) {
|
||||
Msg.msg(s, "&cThere " + ((avail == 1) ? " is only &f" + avail + " &cpage available!" : "are only &f" + avail + " &cpages available!"));
|
||||
return true;
|
||||
}
|
||||
|
||||
Msg.msg(s, "&bExpansion cloud for &f" + (author != null ? author : "all available")+ " &8&m-- &r&bamount&7: &f" + ex.size() + " &bpage&7: &f" + page + "&7/&f" + avail);
|
||||
|
||||
ex = plugin.getExpansionCloud().getPage(ex, page, 10);
|
||||
|
||||
for (Entry<Integer, CloudExpansion> expansion : ex.entrySet()) {
|
||||
Msg.msg(s, "&b" + (expansion.getKey()+1) + "&7: &f" + expansion.getValue().getName() + " &8&m-- &r" + expansion.getValue().getLink());
|
||||
}
|
||||
Msg.msg(s, "&bDownload an expansion with &7/papi ecloud download <name>",
|
||||
"&bView more info on an expansion with &7/papi ecloud info <expansion>");
|
||||
return true;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
}
|
@@ -1,319 +0,0 @@
|
||||
/*
|
||||
*
|
||||
* PlaceholderAPI
|
||||
* Copyright (C) 2018 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.commands.spigot;
|
||||
|
||||
import me.clip.placeholderapi.PlaceholderAPI;
|
||||
import me.clip.placeholderapi.PlaceholderAPIPlugin;
|
||||
import me.clip.placeholderapi.expansion.PlaceholderExpansion;
|
||||
import me.clip.placeholderapi.expansion.cloud.CloudExpansion;
|
||||
import me.clip.placeholderapi.util.Msg;
|
||||
import net.md_5.bungee.api.chat.ClickEvent;
|
||||
import net.md_5.bungee.api.chat.ComponentBuilder;
|
||||
import net.md_5.bungee.api.chat.HoverEvent;
|
||||
import net.md_5.bungee.api.chat.TextComponent;
|
||||
import org.bukkit.ChatColor;
|
||||
import org.bukkit.command.Command;
|
||||
import org.bukkit.command.CommandExecutor;
|
||||
import org.bukkit.command.CommandSender;
|
||||
import org.bukkit.entity.Player;
|
||||
|
||||
import java.util.Map;
|
||||
import java.util.Map.Entry;
|
||||
|
||||
public class ExpansionCloudCommands implements CommandExecutor {
|
||||
|
||||
private PlaceholderAPIPlugin plugin;
|
||||
|
||||
public ExpansionCloudCommands(PlaceholderAPIPlugin instance) {
|
||||
plugin = instance;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean onCommand(CommandSender s, Command c, String label, String[] args) {
|
||||
|
||||
if (args.length == 1) {
|
||||
Msg.msg(s, "&bExpansion cloud commands",
|
||||
" ",
|
||||
"&b/papi ecloud status",
|
||||
"&fView status of the cloud",
|
||||
"&b/papi ecloud list <all/author> (page)",
|
||||
"&fList all/author specific available expansions",
|
||||
"&b/papi ecloud info <expansion name>",
|
||||
"&fView information about a specific expansion available on the cloud",
|
||||
"&b/papi ecloud download <expansion name>",
|
||||
"&fDownload a specific expansion from the cloud",
|
||||
"&b/papi ecloud refresh",
|
||||
"&fFetch the most up to date list of expansions available.",
|
||||
"&b/papi ecloud clear",
|
||||
"&fClear the expansion cloud cache.");
|
||||
return true;
|
||||
}
|
||||
|
||||
if (args[1].equalsIgnoreCase("refresh") || args[1].equalsIgnoreCase("update") || args[1].equalsIgnoreCase("fetch")) {
|
||||
Msg.msg(s, "&aRefresh task started. Use &f/papi ecloud list all &ain a few!!");
|
||||
plugin.getExpansionCloud().clean();
|
||||
plugin.getExpansionCloud().fetch();
|
||||
return true;
|
||||
}
|
||||
|
||||
if (plugin.getExpansionCloud().getCloudExpansions().isEmpty()) {
|
||||
Msg.msg(s, "&7No cloud expansions are available at this time.");
|
||||
return true;
|
||||
}
|
||||
|
||||
if (args[1].equalsIgnoreCase("clear")) {
|
||||
plugin.getExpansionCloud().clean();
|
||||
Msg.msg(s, "&aThe cloud cache has been cleared!!");
|
||||
return true;
|
||||
}
|
||||
|
||||
if (args[1].equalsIgnoreCase("status")) {
|
||||
|
||||
Msg.msg(s, "&bThere are &f" + plugin.getExpansionCloud().getCloudExpansions().size() + " &bexpansions available on the cloud.",
|
||||
"&7A total of &f" + plugin.getExpansionCloud().getCloudAuthorCount() + " &7authors have contributed to the expansion cloud.");
|
||||
if (plugin.getExpansionCloud().getToUpdateCount() > 0) {
|
||||
Msg.msg(s, "&eYou have &f" + plugin.getExpansionCloud().getToUpdateCount()
|
||||
+ " &eexpansions installed that have updates available.");
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
if (args[1].equalsIgnoreCase("info")) {
|
||||
|
||||
if (args.length < 3) {
|
||||
Msg.msg(s, "&cAn expansion name must be specified!");
|
||||
return true;
|
||||
}
|
||||
|
||||
CloudExpansion expansion = plugin.getExpansionCloud().getCloudExpansion(args[2]);
|
||||
|
||||
if (expansion == null) {
|
||||
Msg.msg(s, "&cNo expansion found with the name: &f" + args[2]);
|
||||
return true;
|
||||
}
|
||||
|
||||
if (!(s instanceof Player)) {
|
||||
Msg.msg(s, (expansion.shouldUpdate() ? "&e" : "") + expansion.getName() + " &8&m-- &r" + expansion.getLink());
|
||||
return true;
|
||||
}
|
||||
|
||||
Player p = (Player) s;
|
||||
|
||||
Msg.msg(s, "&bCloud expansion info for&7:" + (expansion.shouldUpdate() ? "&6" : (expansion.hasExpansion() ? "&e" : "")) + expansion.getName());
|
||||
|
||||
StringBuilder sb = new StringBuilder();
|
||||
|
||||
sb.append(expansion.getDescription());
|
||||
|
||||
if (expansion.getReleaseNotes() != null) {
|
||||
sb.append("\n\n" + expansion.getReleaseNotes());
|
||||
}
|
||||
|
||||
String hover = ChatColor.translateAlternateColorCodes('&', sb.toString());
|
||||
|
||||
if (expansion.hasExpansion()) {
|
||||
if (expansion.shouldUpdate()) {
|
||||
Msg.msg(s, "&6You have this expansion but there is a newer version available.");
|
||||
} else {
|
||||
Msg.msg(s, "&aYou have the latest version of this expansion!");
|
||||
}
|
||||
} else {
|
||||
Msg.msg(s, "&7You do not have this expansion installed");
|
||||
}
|
||||
|
||||
sms(p, "&bAuthor&7: &f" + expansion.getAuthor(), hover, null);
|
||||
sms(p, "&bVersion&7: &f" + expansion.getVersion(), hover, null);
|
||||
if (expansion.getLastUpdate() > 1) {
|
||||
sb.append("&bLast updated&7: &f" + expansion.getTimeSinceLastUpdate() + " ago");
|
||||
}
|
||||
sms(p, "&aClick here to download!", hover, expansion.getName());
|
||||
return true;
|
||||
}
|
||||
|
||||
if (args[1].equalsIgnoreCase("list")) {
|
||||
|
||||
int page = 1;
|
||||
|
||||
String author;
|
||||
boolean installed = false;
|
||||
|
||||
if (args.length < 3) {
|
||||
Msg.msg(s, "&cIncorrect usage! &7/papi ecloud list <all/author/installed> (page)");
|
||||
return true;
|
||||
}
|
||||
|
||||
author = args[2];
|
||||
|
||||
if (author.equalsIgnoreCase("all")) {
|
||||
author = null;
|
||||
} else if (author.equalsIgnoreCase("installed")) {
|
||||
author = null;
|
||||
installed = true;
|
||||
}
|
||||
|
||||
if (args.length >= 4) {
|
||||
try {
|
||||
page = Integer.parseInt(args[3]);
|
||||
} catch (NumberFormatException ex) {
|
||||
Msg.msg(s, "&cPage number must be an integer!");
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
if (page < 1) {
|
||||
Msg.msg(s, "&cPage must be greater than or equal to 1!");
|
||||
return true;
|
||||
}
|
||||
|
||||
int avail;
|
||||
|
||||
Map<Integer, CloudExpansion> ex;
|
||||
|
||||
if (installed) {
|
||||
ex = plugin.getExpansionCloud().getAllInstalled();
|
||||
} else if (author == null) {
|
||||
ex = plugin.getExpansionCloud().getCloudExpansions();
|
||||
} else {
|
||||
ex = plugin.getExpansionCloud().getAllByAuthor(author);
|
||||
}
|
||||
|
||||
if (ex == null || ex.isEmpty()) {
|
||||
Msg.msg(s, "&cNo expansions available" + (author != null ? " for author &f" + author : ""));
|
||||
return true;
|
||||
}
|
||||
|
||||
avail = plugin.getExpansionCloud().getPagesAvailable(ex, 10);
|
||||
|
||||
if (page > avail) {
|
||||
Msg.msg(s, "&cThere " + ((avail == 1) ? " is only &f" + avail + " &cpage available!" : "are only &f" + avail + " &cpages available!"));
|
||||
return true;
|
||||
}
|
||||
|
||||
Msg.msg(s, "&bShowing expansions for&7: &f" + (author != null ? author : (installed ? "all installed" : "all available"))+ " &8&m--&r &bamount&7: &f" + ex.size() + " &bpage&7: &f" + page + "&7/&f" + avail);
|
||||
|
||||
ex = plugin.getExpansionCloud().getPage(ex, page, 10);
|
||||
|
||||
if (ex == null) {
|
||||
Msg.msg(s, "&cThere was a problem getting the requested page...");
|
||||
return true;
|
||||
}
|
||||
|
||||
Msg.msg(s, "&aGreen = Expansions you have");
|
||||
Msg.msg(s, "&6Gold = Expansions which need updated");
|
||||
|
||||
if (!(s instanceof Player)) {
|
||||
|
||||
for (Entry<Integer, CloudExpansion> expansion : ex.entrySet()) {
|
||||
if (expansion == null || expansion.getKey() == null || expansion.getValue() == null) continue;
|
||||
Msg.msg(s, "&b" + (expansion.getKey()+1) + "&7: " + (expansion.getValue().shouldUpdate() ? "&6" : (expansion.getValue().hasExpansion() ? "&a" : "&7")) + expansion.getValue().getName() + " &8&m-- &r" + expansion.getValue().getLink());
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
Player p = (Player) s;
|
||||
|
||||
for (Entry<Integer, CloudExpansion> expansion : ex.entrySet()) {
|
||||
|
||||
if (expansion == null || expansion.getValue() == null) {
|
||||
continue;
|
||||
}
|
||||
|
||||
StringBuilder sb = new StringBuilder();
|
||||
if (expansion.getValue().shouldUpdate()) {
|
||||
sb.append("&6Click to update to the latest version of this expansion\n\n");
|
||||
} else if (!expansion.getValue().hasExpansion()) {
|
||||
sb.append("&bClick to download this expansion\n\n");
|
||||
} else {
|
||||
sb.append("&aYou have the latest version of this expansion\n\n");
|
||||
}
|
||||
sb.append("&bAuthor&7: &f" + expansion.getValue().getAuthor() + "\n");
|
||||
sb.append("&bVersion&7: &f" + expansion.getValue().getVersion() + "\n");
|
||||
if (expansion.getValue().getLastUpdate() > 1) {
|
||||
sb.append("&bLast updated&7: &f" + expansion.getValue().getTimeSinceLastUpdate() + " ago\n");
|
||||
}
|
||||
if (expansion.getValue().getReleaseNotes() != null) {
|
||||
sb.append("&bRelease Notes&7: &f" + expansion.getValue().getReleaseNotes() + "\n");
|
||||
}
|
||||
sb.append("\n" + expansion.getValue().getDescription());
|
||||
|
||||
String msg = ChatColor.translateAlternateColorCodes('&', "&b" + (expansion.getKey()+1) + "&7: " + (expansion.getValue().shouldUpdate() ? "&6" : (expansion.getValue().hasExpansion() ? "&a" : "")) + expansion.getValue().getName());
|
||||
|
||||
String hover = ChatColor.translateAlternateColorCodes('&', sb.toString());
|
||||
|
||||
sms(p, msg, hover, expansion.getValue().getName());
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
if (args[1].equalsIgnoreCase("download")) {
|
||||
|
||||
if (args.length < 3) {
|
||||
Msg.msg(s, "&cAn expansion name must be specified!");
|
||||
return true;
|
||||
}
|
||||
|
||||
CloudExpansion expansion = plugin.getExpansionCloud().getCloudExpansion(args[2]);
|
||||
|
||||
if (expansion == null) {
|
||||
Msg.msg(s, "&cNo expansion found with the name: &f" + args[2]);
|
||||
return true;
|
||||
}
|
||||
|
||||
if (expansion.hasExpansion() && !expansion.shouldUpdate()) {
|
||||
Msg.msg(s, "&aYou already have this expansion installed and your version is up to date!");
|
||||
return true;
|
||||
}
|
||||
|
||||
PlaceholderExpansion loaded = plugin.getExpansionManager().getRegisteredExpansion(args[2]);
|
||||
|
||||
if (loaded != null && loaded.isRegistered()) {
|
||||
PlaceholderAPI.unregisterPlaceholderHook(loaded.getIdentifier());
|
||||
}
|
||||
|
||||
Msg.msg(s, "&aAttempting download of expansion &f" + expansion.getName());
|
||||
|
||||
String player = ((s instanceof Player) ? s.getName() : null);
|
||||
|
||||
plugin.getExpansionCloud().downloadExpansion(player, expansion);
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
Msg.msg(s, "&cIncorrect usage! &b/papi ecloud");
|
||||
return true;
|
||||
}
|
||||
|
||||
private void sms(Player p, String text, String hover, String name) {
|
||||
TextComponent message = new TextComponent( ChatColor.translateAlternateColorCodes('&', text) );
|
||||
if (hover != null) {
|
||||
message.setHoverEvent( new HoverEvent( HoverEvent.Action.SHOW_TEXT, new ComponentBuilder(ChatColor.translateAlternateColorCodes('&', hover)).create() ) );
|
||||
}
|
||||
if (name != null) {
|
||||
message.setClickEvent( new ClickEvent( ClickEvent.Action.SUGGEST_COMMAND, "/papi ecloud download " + name) );
|
||||
}
|
||||
p.spigot().sendMessage( message );
|
||||
}
|
||||
|
||||
}
|
@@ -0,0 +1,48 @@
|
||||
/*
|
||||
*
|
||||
* PlaceholderAPI
|
||||
* Copyright (C) 2018 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.events;
|
||||
|
||||
import me.clip.placeholderapi.expansion.PlaceholderExpansion;
|
||||
import org.bukkit.event.Event;
|
||||
import org.bukkit.event.HandlerList;
|
||||
|
||||
public class ExpansionRegisterEvent extends Event {
|
||||
|
||||
private static final HandlerList HANDLERS = new HandlerList();
|
||||
private PlaceholderExpansion expansion;
|
||||
|
||||
public ExpansionRegisterEvent(PlaceholderExpansion expansion) {
|
||||
this.expansion = expansion;
|
||||
}
|
||||
|
||||
@Override
|
||||
public HandlerList getHandlers() {
|
||||
return HANDLERS;
|
||||
}
|
||||
|
||||
public static HandlerList getHandlerList() {
|
||||
return HANDLERS;
|
||||
}
|
||||
|
||||
public PlaceholderExpansion getExpansion() {
|
||||
return expansion;
|
||||
}
|
||||
}
|
@@ -27,7 +27,7 @@ import org.bukkit.event.HandlerList;
|
||||
|
||||
public class ExpansionUnregisterEvent extends Event {
|
||||
|
||||
private static final HandlerList handlers = new HandlerList();
|
||||
private static final HandlerList HANDLERS = new HandlerList();
|
||||
private PlaceholderExpansion expansion;
|
||||
|
||||
public ExpansionUnregisterEvent(PlaceholderExpansion expansion) {
|
||||
@@ -36,11 +36,11 @@ public class ExpansionUnregisterEvent extends Event {
|
||||
|
||||
@Override
|
||||
public HandlerList getHandlers() {
|
||||
return handlers;
|
||||
return HANDLERS;
|
||||
}
|
||||
|
||||
public static HandlerList getHandlerList() {
|
||||
return handlers;
|
||||
return HANDLERS;
|
||||
}
|
||||
|
||||
public PlaceholderExpansion getExpansion() {
|
||||
|
@@ -27,7 +27,7 @@ import org.bukkit.event.HandlerList;
|
||||
@Deprecated
|
||||
public class PlaceholderHookUnloadEvent extends Event {
|
||||
|
||||
private static final HandlerList handlers = new HandlerList();
|
||||
private static final HandlerList HANDLERS = new HandlerList();
|
||||
private String plugin;
|
||||
private PlaceholderHook hook;
|
||||
|
||||
@@ -38,11 +38,11 @@ public class PlaceholderHookUnloadEvent extends Event {
|
||||
|
||||
@Override
|
||||
public HandlerList getHandlers() {
|
||||
return handlers;
|
||||
return HANDLERS;
|
||||
}
|
||||
|
||||
public static HandlerList getHandlerList() {
|
||||
return handlers;
|
||||
return HANDLERS;
|
||||
}
|
||||
|
||||
public String getHookName() {
|
||||
|
@@ -116,16 +116,13 @@ public final class ExpansionManager {
|
||||
}
|
||||
|
||||
if (!c.canRegister()) {
|
||||
if (c.getPlugin() != null) {
|
||||
cache.put(c.getPlugin().toLowerCase(), c);
|
||||
if (c.getRequiredPlugin() != null) {
|
||||
cache.put(c.getRequiredPlugin().toLowerCase(), c);
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
if (!c.register()) {
|
||||
if (c.getPlugin() != null) {
|
||||
cache.put(c.getPlugin().toLowerCase(), c);
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
|
@@ -33,52 +33,59 @@ public abstract class PlaceholderExpansion extends PlaceholderHook {
|
||||
|
||||
/**
|
||||
* The name of this expansion
|
||||
* @return identifier used for expansion if no name present
|
||||
* @return {@link #getIdentifier()} by default, name of this expansion if specified
|
||||
*/
|
||||
public String getName() {
|
||||
return getIdentifier();
|
||||
}
|
||||
|
||||
/**
|
||||
* 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 the expansion should persist through reloads
|
||||
*/
|
||||
public boolean persist() {
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the identifier that this placeholder expansion uses to be passed placeholder requests
|
||||
* @return placeholder identifier that is associated with this class
|
||||
* The placeholder identifier of this expanion
|
||||
* @return placeholder identifier that is associated with this expansion
|
||||
*/
|
||||
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
|
||||
* The author of this expansion
|
||||
* @return name of the author for this expansion
|
||||
*/
|
||||
public abstract String getAuthor();
|
||||
|
||||
/**
|
||||
* Get the version of this PlaceholderExpansion
|
||||
* 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() {
|
||||
return getPlugin();
|
||||
}
|
||||
|
||||
/**
|
||||
* 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 a placeholder has already been registered with this identifier
|
||||
* @return true if the identifier for this expansion has already been registered
|
||||
* 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!");
|
||||
@@ -86,20 +93,21 @@ public abstract class PlaceholderExpansion extends PlaceholderHook {
|
||||
}
|
||||
|
||||
/**
|
||||
* If any requirements are required to be checked before this hook can register, add them here
|
||||
* 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 getPlugin() == null || Bukkit.getPluginManager().getPlugin(getPlugin()) != null;
|
||||
return getRequiredPlugin() == null || Bukkit.getPluginManager().getPlugin(getRequiredPlugin()) != null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Attempt to register this PlaceholderExpansion with PlaceholderAPI
|
||||
* @return true if this class and identifier have been successfully registered with PlaceholderAPI
|
||||
* 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.registerPlaceholderHook(getIdentifier(), this);
|
||||
return PlaceholderAPI.registerExpansion(this);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -110,30 +118,6 @@ public abstract class PlaceholderExpansion extends PlaceholderHook {
|
||||
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);
|
||||
}
|
||||
@@ -169,4 +153,25 @@ public abstract class PlaceholderExpansion extends PlaceholderHook {
|
||||
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
|
||||
public String getPlugin() {
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* @deprecated As of versions greater than 2.8.7, use the expansion cloud to show a description
|
||||
*/
|
||||
@Deprecated
|
||||
public String getDescription() { return null; }
|
||||
|
||||
/**
|
||||
* @deprecated As of versions greater than 2.8.7, use the expansion cloud to display a link
|
||||
*/
|
||||
@Deprecated
|
||||
public String getLink() { return null; }
|
||||
}
|
@@ -22,79 +22,181 @@ 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;
|
||||
|
||||
|
||||
public class CloudExpansion {
|
||||
|
||||
private String name, author, version, description, link, releaseNotes;
|
||||
|
||||
private boolean hasExpansion, shouldUpdate;
|
||||
|
||||
private long lastUpdate;
|
||||
private String name,
|
||||
author,
|
||||
latest_version,
|
||||
description,
|
||||
source_url,
|
||||
dependency_url;
|
||||
|
||||
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;
|
||||
private boolean hasExpansion,
|
||||
shouldUpdate,
|
||||
verified;
|
||||
|
||||
private long last_update,
|
||||
ratings_count;
|
||||
|
||||
private double average_rating;
|
||||
|
||||
private List<String> placeholders;
|
||||
|
||||
private List<Version> versions;
|
||||
|
||||
public CloudExpansion() {
|
||||
}
|
||||
|
||||
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);
|
||||
}
|
||||
|
||||
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 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;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@@ -20,6 +20,7 @@
|
||||
*/
|
||||
package me.clip.placeholderapi.expansion.cloud;
|
||||
|
||||
import com.google.gson.Gson;
|
||||
import me.clip.placeholderapi.PlaceholderAPI;
|
||||
import me.clip.placeholderapi.PlaceholderAPIPlugin;
|
||||
import me.clip.placeholderapi.expansion.PlaceholderExpansion;
|
||||
@@ -41,303 +42,301 @@ import java.util.stream.IntStream;
|
||||
|
||||
public class ExpansionCloudManager {
|
||||
|
||||
private PlaceholderAPIPlugin plugin;
|
||||
private final String API = "http://api.extendedclip.com/";
|
||||
private final File dir;
|
||||
private final TreeMap<Integer, CloudExpansion> remote = new TreeMap<>();
|
||||
private final List<String> downloading = new ArrayList<>();
|
||||
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;
|
||||
|
||||
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 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() {
|
||||
}
|
||||
|
||||
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() {
|
||||
}
|
||||
|
||||
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);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
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 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<>();
|
||||
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(1, 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++;
|
||||
}
|
||||
}
|
||||
return pages;
|
||||
}
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
public void fetch() {
|
||||
|
||||
plugin.getLogger().info("Fetching available expansion information...");
|
||||
return ex;
|
||||
}
|
||||
|
||||
new BukkitRunnable() {
|
||||
|
||||
@Override
|
||||
public void run() {
|
||||
public void fetch(boolean allowUnverified) {
|
||||
|
||||
StringBuilder sb;
|
||||
plugin.getLogger().info("Fetching available expansion information...");
|
||||
|
||||
try {
|
||||
new BukkitRunnable() {
|
||||
|
||||
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;
|
||||
@Override
|
||||
public void run() {
|
||||
|
||||
while ((line = br.readLine()) != null) {
|
||||
sb.append(line);
|
||||
}
|
||||
StringBuilder sb;
|
||||
|
||||
br.close();
|
||||
connection.disconnect();
|
||||
try {
|
||||
|
||||
} catch (Exception e) {
|
||||
return;
|
||||
}
|
||||
URL site = new URL(API);
|
||||
|
||||
String json = sb.toString();
|
||||
JSONParser parser = new JSONParser();
|
||||
Object obj = null;
|
||||
HttpURLConnection connection = (HttpURLConnection) site.openConnection();
|
||||
|
||||
try {
|
||||
obj = parser.parse(json);
|
||||
} catch (ParseException e) {
|
||||
e.printStackTrace();
|
||||
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);
|
||||
}
|
||||
|
||||
if (obj == null) {
|
||||
return;
|
||||
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);
|
||||
}
|
||||
|
||||
List<CloudExpansion> unsorted = new ArrayList<>();
|
||||
int count = 0;
|
||||
|
||||
if (obj instanceof JSONObject) {
|
||||
unsorted.sort(Comparator.comparing(CloudExpansion::getLastUpdate).reversed());
|
||||
|
||||
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);
|
||||
}
|
||||
}
|
||||
|
||||
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.");
|
||||
}
|
||||
for (CloudExpansion e : unsorted) {
|
||||
remote.put(count, e);
|
||||
count++;
|
||||
}
|
||||
}
|
||||
}.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");
|
||||
plugin.getLogger().info(count + " placeholder expansions are available on the cloud.");
|
||||
|
||||
byte[] buffer = new byte[is.available()];
|
||||
|
||||
int l;
|
||||
long updates = getToUpdateCount();
|
||||
|
||||
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());
|
||||
if (updates > 0) {
|
||||
plugin.getLogger().info(updates + " installed expansions have updates available.");
|
||||
}
|
||||
}
|
||||
}
|
||||
}.runTaskAsynchronously(plugin);
|
||||
}
|
||||
|
||||
Bukkit.getScheduler().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;
|
||||
}
|
||||
|
||||
final CloudExpansion.Version ver = ex.getVersions()
|
||||
.stream()
|
||||
.filter(v -> v.getVersion().equals(version))
|
||||
.findFirst()
|
||||
.orElse(null);
|
||||
|
||||
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(ex.getLink()), ex.getName());
|
||||
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: " + ex.getLink());
|
||||
plugin.getLogger().warning("Failed to download expansion: " + ex.getName() + " from: " + ver.getUrl());
|
||||
|
||||
Bukkit.getScheduler().runTask(plugin, () -> {
|
||||
|
||||
@@ -370,5 +369,5 @@ public class ExpansionCloudManager {
|
||||
});
|
||||
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@@ -1,8 +1,8 @@
|
||||
package me.clip.placeholderapi.updatechecker;
|
||||
|
||||
import me.clip.placeholderapi.PlaceholderAPIPlugin;
|
||||
import me.clip.placeholderapi.util.Msg;
|
||||
import org.bukkit.Bukkit;
|
||||
import org.bukkit.ChatColor;
|
||||
import org.bukkit.event.EventHandler;
|
||||
import org.bukkit.event.EventPriority;
|
||||
import org.bukkit.event.Listener;
|
||||
@@ -12,94 +12,73 @@ import javax.net.ssl.HttpsURLConnection;
|
||||
import java.io.BufferedReader;
|
||||
import java.io.InputStreamReader;
|
||||
import java.net.URL;
|
||||
import java.util.regex.Pattern;
|
||||
|
||||
/**
|
||||
* thanks maxim
|
||||
*/
|
||||
public class UpdateChecker implements Listener {
|
||||
|
||||
private PlaceholderAPIPlugin plugin;
|
||||
|
||||
private final int resourceId = 6245;
|
||||
|
||||
private static String latestVersion = "";
|
||||
|
||||
private static boolean updateAvailable = false;
|
||||
private final int RESOURCE_ID = 6245;
|
||||
private String spigotVersion, pluginVersion;
|
||||
private boolean updateAvailable;
|
||||
|
||||
public UpdateChecker(PlaceholderAPIPlugin i) {
|
||||
plugin = i;
|
||||
Bukkit.getScheduler().runTaskAsynchronously(plugin, () -> {
|
||||
if (checkForUpdate()) {
|
||||
Bukkit.getScheduler().runTask(plugin, () -> {
|
||||
plugin.getLogger().info("An update for PlaceholderAPI (v" + getLatestVersion() + ") is available at:");
|
||||
plugin.getLogger().info("https://www.spigotmc.org/resources/placeholderapi." + resourceId + "/");
|
||||
register();
|
||||
});
|
||||
pluginVersion = i.getDescription().getVersion();
|
||||
}
|
||||
|
||||
public boolean hasUpdateAvailable() { return updateAvailable; }
|
||||
|
||||
public String getSpigotVersion() {
|
||||
return spigotVersion;
|
||||
}
|
||||
|
||||
public void fetch() {
|
||||
Bukkit.getScheduler().runTaskAsynchronously(plugin, () -> {
|
||||
try {
|
||||
HttpsURLConnection con = (HttpsURLConnection) new URL("https://api.spigotmc.org/legacy/update.php?resource=" + RESOURCE_ID).openConnection();
|
||||
con.setRequestMethod("GET");
|
||||
spigotVersion = new BufferedReader(new InputStreamReader(con.getInputStream())).readLine();
|
||||
} catch (Exception ex) {
|
||||
plugin.getLogger().info("Failed to check for updates on spigot.");
|
||||
return;
|
||||
}
|
||||
|
||||
if (spigotVersion == null || spigotVersion.isEmpty()) {
|
||||
return;
|
||||
}
|
||||
|
||||
updateAvailable = spigotIsNewer();
|
||||
|
||||
if (!updateAvailable) {
|
||||
return;
|
||||
}
|
||||
|
||||
Bukkit.getScheduler().runTask(plugin, () -> {
|
||||
plugin.getLogger().info("An update for PlaceholderAPI (v" + getSpigotVersion() + ") is available at:");
|
||||
plugin.getLogger().info("https://www.spigotmc.org/resources/placeholderapi." + RESOURCE_ID + "/");
|
||||
Bukkit.getPluginManager().registerEvents(this, plugin);
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
private void register() {
|
||||
Bukkit.getPluginManager().registerEvents(this, plugin);
|
||||
}
|
||||
|
||||
@EventHandler(priority = EventPriority.MONITOR)
|
||||
public void onJoin(PlayerJoinEvent e) {
|
||||
if (e.getPlayer().isOp()) {
|
||||
e.getPlayer().sendMessage(ChatColor.translateAlternateColorCodes('&', "&bAn update for &fPlaceholder&7API &e(&fPlaceholder&7API &fv" + getLatestVersion() + "&e)"));
|
||||
e.getPlayer().sendMessage(ChatColor.translateAlternateColorCodes('&', "&bis available at &ehttps://www.spigotmc.org/resources/placeholderapi." + resourceId + "/"));
|
||||
}
|
||||
}
|
||||
|
||||
private String getSpigotVersion() {
|
||||
try {
|
||||
HttpsURLConnection con = (HttpsURLConnection) new URL("https://api.spigotmc.org/legacy/update.php?resource=" + resourceId).openConnection();
|
||||
con.setRequestMethod("GET");
|
||||
String version = new BufferedReader(new InputStreamReader(con.getInputStream())).readLine();
|
||||
if (version.length() <= 7) {
|
||||
return version;
|
||||
}
|
||||
} catch (Exception ex) {
|
||||
plugin.getLogger().info("Failed to check for a update on spigot.");
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
private boolean checkHigher(String currentVersion, String newVersion) {
|
||||
String current = toReadable(currentVersion);
|
||||
String newVers = toReadable(newVersion);
|
||||
return current.compareTo(newVers) < 0;
|
||||
}
|
||||
|
||||
public boolean checkForUpdate() {
|
||||
String version = getSpigotVersion();
|
||||
if (version != null) {
|
||||
if (checkHigher(plugin.getDescription().getVersion(), version)) {
|
||||
latestVersion = version;
|
||||
updateAvailable = true;
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
public static boolean updateAvailable() {
|
||||
return updateAvailable;
|
||||
}
|
||||
|
||||
public static String getLatestVersion() {
|
||||
return latestVersion;
|
||||
private boolean spigotIsNewer() {
|
||||
if (spigotVersion == null || spigotVersion.isEmpty()) return false;
|
||||
String plV = toReadable(pluginVersion);
|
||||
String spV = toReadable(spigotVersion);
|
||||
return plV.compareTo(spV) < 0;
|
||||
}
|
||||
|
||||
private String toReadable(String version) {
|
||||
String[] split = Pattern.compile(".", Pattern.LITERAL).split(version.replace("v", ""));
|
||||
version = "";
|
||||
for (String s : split) {
|
||||
version += String.format("%4s", s);
|
||||
}
|
||||
return version;
|
||||
if (version.contains("-DEV-")) {
|
||||
version = version.split("-DEV-")[0];
|
||||
}
|
||||
return version.replaceAll("\\.","");
|
||||
}
|
||||
|
||||
|
||||
@EventHandler(priority = EventPriority.MONITOR)
|
||||
public void onJoin(PlayerJoinEvent e) {
|
||||
if (e.getPlayer().hasPermission("placeholderapi.updatenotify")) {
|
||||
Msg.msg(e.getPlayer(), "&bAn update for &fPlaceholder&7API &e(&fPlaceholder&7API &fv" + getSpigotVersion() + "&e)"
|
||||
, "&bis available at &ehttps://www.spigotmc.org/resources/placeholderapi." + RESOURCE_ID + "/");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@@ -28,8 +28,10 @@ import java.util.Arrays;
|
||||
public class Msg {
|
||||
|
||||
public static void msg(CommandSender s, String... msg) {
|
||||
Arrays.stream(msg).forEach(text ->
|
||||
s.sendMessage(ChatColor.translateAlternateColorCodes('&', text)));
|
||||
Arrays.stream(msg).map(Msg::color).forEach(s::sendMessage);
|
||||
}
|
||||
|
||||
public static String color(String text) {
|
||||
return ChatColor.translateAlternateColorCodes('&', text);
|
||||
}
|
||||
}
|
||||
|
@@ -13,10 +13,12 @@ permissions:
|
||||
children:
|
||||
placeholderapi.list: true
|
||||
placeholderapi.reload: true
|
||||
placeholderapi.ecloud: true
|
||||
placeholderapi.parse: true
|
||||
placeholderapi.injector.chat.bypass: true
|
||||
placeholderapi.injector.signs.bypass: true
|
||||
placeholderapi.injector.anvil.bypass: true
|
||||
placeholderapi.updatenotify: true
|
||||
placeholderapi.list:
|
||||
description: ability to use the list command
|
||||
default: op
|
||||
@@ -26,6 +28,12 @@ permissions:
|
||||
placeholderapi.parse:
|
||||
description: ability to use parse command
|
||||
default: op
|
||||
placeholderapi.ecloud:
|
||||
description: allows the usage of ecloud commands
|
||||
default: op
|
||||
placeholderapi.updatenotify:
|
||||
description: notifies you when there is a PAPI update
|
||||
default: op
|
||||
commands:
|
||||
placeholderapi:
|
||||
description: PlaceholderAPI command
|
||||
|
Reference in New Issue
Block a user