Cuz Java8

This commit is contained in:
extendedclip 2018-03-24 11:47:43 -04:00
parent 7ddc304167
commit 6e34670bd8

View File

@ -20,6 +20,7 @@
*/ */
package me.clip.placeholderapi.expansion.cloud; package me.clip.placeholderapi.expansion.cloud;
import me.clip.placeholderapi.PlaceholderAPI;
import me.clip.placeholderapi.PlaceholderAPIPlugin; import me.clip.placeholderapi.PlaceholderAPIPlugin;
import me.clip.placeholderapi.expansion.PlaceholderExpansion; import me.clip.placeholderapi.expansion.PlaceholderExpansion;
import me.clip.placeholderapi.util.Msg; import me.clip.placeholderapi.util.Msg;
@ -35,92 +36,60 @@ import java.net.HttpURLConnection;
import java.net.URL; import java.net.URL;
import java.net.URLConnection; import java.net.URLConnection;
import java.util.*; import java.util.*;
import java.util.stream.Collectors;
import java.util.stream.IntStream;
public class ExpansionCloudManager { public class ExpansionCloudManager {
private final File dir;
private PlaceholderAPIPlugin plugin; 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 TreeMap<Integer, CloudExpansion> remote = new TreeMap<>();
private final List<String> downloading = new ArrayList<>(); private final List<String> downloading = new ArrayList<>();
private int toUpdate = 0;
public ExpansionCloudManager(PlaceholderAPIPlugin instance) { public ExpansionCloudManager(PlaceholderAPIPlugin instance) {
plugin = instance; plugin = instance;
dir = new File(instance.getDataFolder() + File.separator + "expansions"); dir = new File(instance.getDataFolder() + File.separator + "expansions");
if (!dir.exists()) { if (!dir.exists()) {
try { try {
dir.mkdirs(); dir.mkdirs();
} catch (Exception ex) { } catch (Exception ex) {
ex.printStackTrace(); ex.printStackTrace();
} }
} }
} }
public void clean() { public void clean() {
remote.clear(); remote.clear();
toUpdate = 0;
downloading.clear(); downloading.clear();
} }
public boolean isDownloading(String expansion) { public boolean isDownloading(String expansion) {
return downloading.contains(expansion); return downloading.contains(expansion);
} }
public Map<Integer, CloudExpansion> getCloudExpansions() { public Map<Integer, CloudExpansion> getCloudExpansions() {
return remote; return remote;
} }
public CloudExpansion getCloudExpansion(String name) { public CloudExpansion getCloudExpansion(String name) {
return remote.values().stream().filter(ex -> ex.getName().equalsIgnoreCase(name)).findFirst().orElse(null);
for (CloudExpansion ex : remote.values()) {
if (ex.getName().equalsIgnoreCase(name)) {
return ex;
}
}
return null;
} }
public int getCloudAuthorCount() { public int getCloudAuthorCount() {
return remote.values().stream().collect(Collectors.groupingBy(CloudExpansion::getAuthor, Collectors.counting())).size();
if (remote == null) {
return 0;
}
List<String> temp = new ArrayList<>();
for (CloudExpansion ex : remote.values()) {
if (!temp.contains(ex.getAuthor())) {
temp.add(ex.getAuthor());
}
}
return temp.size();
} }
public int getToUpdateCount() { public long getToUpdateCount() {
return toUpdate; return PlaceholderAPI.getExpansions().stream().filter(ex -> getCloudExpansion(ex.getName()) != null && getCloudExpansion(ex.getName()).shouldUpdate()).count();
} }
public void decrementToUpdateCount() {
if (toUpdate > 0) {
toUpdate--;
}
}
public Map<Integer, CloudExpansion> getAllByAuthor(String author) { public Map<Integer, CloudExpansion> getAllByAuthor(String author) {
if (remote.isEmpty()) { if (remote.isEmpty()) {
return null; return null;
} }
TreeMap<Integer, CloudExpansion> byAuthor = new TreeMap<>(); TreeMap<Integer, CloudExpansion> byAuthor = new TreeMap<>();
boolean first = true; boolean first = true;
for (CloudExpansion ex : remote.values()) { for (CloudExpansion ex : remote.values()) {
if (ex.getAuthor().equalsIgnoreCase(author)) { if (ex.getAuthor().equalsIgnoreCase(author)) {
if (first) { if (first) {
@ -139,20 +108,16 @@ public class ExpansionCloudManager {
} }
public Map<Integer, CloudExpansion> getAllInstalled() { public Map<Integer, CloudExpansion> getAllInstalled() {
if (remote.isEmpty()) { if (remote.isEmpty()) {
return null; return null;
} }
TreeMap<Integer, CloudExpansion> has = new TreeMap<>(); TreeMap<Integer, CloudExpansion> has = new TreeMap<>();
boolean first = true; boolean first = true;
for (CloudExpansion ex : remote.values()) { for (CloudExpansion ex : remote.values()) {
if (ex.hasExpansion()) { if (ex.hasExpansion()) {
if (first) { if (first) {
first = false; first = false;
has.put(0, ex); has.put(1, ex);
} else { } else {
has.put(has.lastKey()+1, ex); has.put(has.lastKey()+1, ex);
} }
@ -166,63 +131,36 @@ public class ExpansionCloudManager {
} }
public int getPagesAvailable(Map<Integer, CloudExpansion> map, int amount) { public int getPagesAvailable(Map<Integer, CloudExpansion> map, int amount) {
if (map == null) { if (map == null) {
return 0; return 0;
} }
int pages = map.size() > 0 ? 1 : 0; int pages = map.size() > 0 ? 1 : 0;
if (pages == 0) { if (pages == 0) {
return pages; return pages;
} }
if (map.size() > amount) { if (map.size() > amount) {
pages = map.size()/amount; pages = map.size()/amount;
if (map.size() % amount > 0) { if (map.size() % amount > 0) {
pages = pages+1; pages++;
} }
} }
return pages; return pages;
} }
public Map<Integer, CloudExpansion> getPage(Map<Integer, CloudExpansion> map, int page) { public Map<Integer, CloudExpansion> getPage(Map<Integer, CloudExpansion> map, int page, int size) {
if (map == null || map.size() == 0 || page > getPagesAvailable(map, size)) {
if (map == null || map.size() == 0) {
return null; return null;
} }
int end = size*page;
if (page > getPagesAvailable(map, 10)) { int start = end-size;
return null;
}
int end = 10*page;
int start = end-10;
end = end-1;
int size = map.size();
if (end > size) {
end = size-1;
}
TreeMap<Integer, CloudExpansion> ex = new TreeMap<>(); TreeMap<Integer, CloudExpansion> ex = new TreeMap<>();
IntStream.range(start, end).forEach(n -> ex.put(n, map.get(n)));
for (int i = start ; i <= end ; i++) { return ex;
ex.put(i, map.get(i));
}
return ex.isEmpty() ? null : ex;
} }
public void fetch() { public void fetch() {
plugin.getLogger().info("Fetching available expansion list..."); plugin.getLogger().info("Fetching available expansion information...");
toUpdate = 0;
new BukkitRunnable() { new BukkitRunnable() {
@ -233,9 +171,9 @@ public class ExpansionCloudManager {
try { try {
URL api = new URL("http://api.extendedclip.com/"); URL site = new URL(API);
HttpURLConnection connection = (HttpURLConnection) api.openConnection(); HttpURLConnection connection = (HttpURLConnection) site.openConnection();
connection.setRequestMethod("GET"); connection.setRequestMethod("GET");
@ -314,7 +252,6 @@ public class ExpansionCloudManager {
ce.setHasExpansion(true); ce.setHasExpansion(true);
if (!ex.getVersion().equals(version)) { if (!ex.getVersion().equals(version)) {
ce.setShouldUpdate(true); ce.setShouldUpdate(true);
toUpdate++;
} }
} }
@ -331,7 +268,12 @@ public class ExpansionCloudManager {
} }
plugin.getLogger().info(count + " placeholder expansions are available on the cloud."); plugin.getLogger().info(count + " placeholder expansions are available on the cloud.");
plugin.getLogger().info(toUpdate + " expansions you use have updates.");
long updates = getToUpdateCount();
if (updates > 0) {
plugin.getLogger().info(updates + " installed expansions have updates available.");
}
} }
} }
}.runTaskAsynchronously(plugin); }.runTaskAsynchronously(plugin);
@ -351,7 +293,7 @@ public class ExpansionCloudManager {
fos = new FileOutputStream(dir.getAbsolutePath() + File.separator + "Expansion-" + name + ".jar"); fos = new FileOutputStream(dir.getAbsolutePath() + File.separator + "Expansion-" + name + ".jar");
byte[] buffer = new byte[1024]; byte[] buffer = new byte[is.available()];
int l; int l;