mirror of
				https://github.com/PlaceholderAPI/PlaceholderAPI
				synced 2025-10-30 18:03:43 +01:00 
			
		
		
		
	This class was trash
This commit is contained in:
		| @@ -21,130 +21,116 @@ | ||||
| package me.clip.placeholderapi.expansion.cloud; | ||||
|  | ||||
| import com.google.gson.Gson; | ||||
|  | ||||
| import java.io.BufferedReader; | ||||
| import java.io.File; | ||||
| import java.io.FileOutputStream; | ||||
| import java.io.IOException; | ||||
| import java.io.InputStream; | ||||
| import java.io.InputStreamReader; | ||||
| import java.net.HttpURLConnection; | ||||
| import java.net.URL; | ||||
| import java.net.URLConnection; | ||||
| import java.util.ArrayList; | ||||
| import java.util.Comparator; | ||||
| import java.util.List; | ||||
| import java.util.Map; | ||||
| import java.util.TreeMap; | ||||
| import java.util.*; | ||||
| import java.util.stream.Collectors; | ||||
| import java.util.stream.IntStream; | ||||
|  | ||||
| import com.google.gson.reflect.TypeToken; | ||||
| import me.clip.placeholderapi.PlaceholderAPI; | ||||
| import me.clip.placeholderapi.PlaceholderAPIPlugin; | ||||
| import me.clip.placeholderapi.expansion.PlaceholderExpansion; | ||||
| import me.clip.placeholderapi.util.Msg; | ||||
| import org.bukkit.Bukkit; | ||||
| import org.bukkit.entity.Player; | ||||
| import org.bukkit.scheduler.BukkitRunnable; | ||||
| import org.json.simple.JSONObject; | ||||
| import org.json.simple.parser.JSONParser; | ||||
| import org.json.simple.parser.ParseException; | ||||
|  | ||||
| public class ExpansionCloudManager { | ||||
|  | ||||
|   private final String API = "http://api.extendedclip.com/v2/"; | ||||
|   private final File dir; | ||||
|   private final TreeMap<Integer, CloudExpansion> remote = new TreeMap<>(); | ||||
|   private final List<String> downloading = new ArrayList<>(); | ||||
|   private PlaceholderAPIPlugin plugin; | ||||
|   private Gson gson; | ||||
|     private static final String API_URL = "http://api.extendedclip.com/v2/"; | ||||
|     private static final Gson GSON = new Gson(); | ||||
|  | ||||
|   public ExpansionCloudManager(PlaceholderAPIPlugin instance) { | ||||
|     plugin = instance; | ||||
|     gson = new Gson(); | ||||
|     dir = new File(instance.getDataFolder() + File.separator + "expansions"); | ||||
|     if (!dir.exists()) { | ||||
|       try { | ||||
|         dir.mkdirs(); | ||||
|       } catch (Exception ex) { | ||||
|         ex.printStackTrace(); | ||||
|       } | ||||
|  | ||||
|     private final PlaceholderAPIPlugin plugin; | ||||
|     private final File expansionsDir; | ||||
|  | ||||
|     private final List<String> downloading = new ArrayList<>(); | ||||
|     private final Map<Integer, CloudExpansion> remote = new TreeMap<>(); | ||||
|  | ||||
|  | ||||
|     public ExpansionCloudManager(PlaceholderAPIPlugin plugin) { | ||||
|         this.plugin = plugin; | ||||
|  | ||||
|         expansionsDir = new File(plugin.getDataFolder(), "expansions"); | ||||
|  | ||||
|         final boolean result = expansionsDir.mkdirs(); | ||||
|         if (result) { | ||||
|             plugin.getLogger().info("Created Expansions Directory"); | ||||
|         } | ||||
|  | ||||
|     } | ||||
|  | ||||
|  | ||||
|     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() | ||||
|         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(); | ||||
|         return remote.values() | ||||
|                      .stream() | ||||
|                      .collect(Collectors.groupingBy(CloudExpansion::getAuthor, Collectors.counting())) | ||||
|                      .size(); | ||||
|     } | ||||
|  | ||||
|   public long getToUpdateCount() { | ||||
|     return PlaceholderAPI.getExpansions().stream().filter( | ||||
|         ex -> getCloudExpansion(ex.getName()) != null && getCloudExpansion(ex.getName()) | ||||
|             .shouldUpdate()).count(); | ||||
|     public int getToUpdateCount() { | ||||
|         return ((int) 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; | ||||
|         if (remote.isEmpty()) return new HashMap<>(); | ||||
|  | ||||
|         Map<Integer, CloudExpansion> byAuthor = new TreeMap<>(); | ||||
|  | ||||
|         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 (!ex.getAuthor().equalsIgnoreCase(author)) continue; | ||||
|  | ||||
|             byAuthor.put(byAuthor.size(), 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; | ||||
|         if (remote.isEmpty()) return new HashMap<>(); | ||||
|  | ||||
|         Map<Integer, CloudExpansion> has = new TreeMap<>(); | ||||
|  | ||||
|         for (CloudExpansion ex : remote.values()) { | ||||
|       if (ex.hasExpansion()) { | ||||
|         if (first) { | ||||
|           first = false; | ||||
|           has.put(1, ex); | ||||
|         } else { | ||||
|           has.put(has.lastKey() + 1, ex); | ||||
|         } | ||||
|       } | ||||
|             if (!ex.hasExpansion()) continue; | ||||
|  | ||||
|             has.put(has.size(), ex); | ||||
|         } | ||||
|  | ||||
|     if (has.isEmpty()) { | ||||
|       return null; | ||||
|     } | ||||
|         return has; | ||||
|     } | ||||
|  | ||||
|  | ||||
|     public int getPagesAvailable(Map<Integer, CloudExpansion> map, int amount) { | ||||
|         if (map == null) { | ||||
|             return 0; | ||||
| @@ -162,113 +148,56 @@ public class ExpansionCloudManager { | ||||
|         return pages; | ||||
|     } | ||||
|  | ||||
|   public Map<Integer, CloudExpansion> getPage(Map<Integer, CloudExpansion> map, int page, | ||||
|       int size) { | ||||
|     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; | ||||
|             return new HashMap<>(); | ||||
|         } | ||||
|  | ||||
|         int end = size * page; | ||||
|         int start = end - size; | ||||
|     TreeMap<Integer, CloudExpansion> ex = new TreeMap<>(); | ||||
|  | ||||
|         Map<Integer, CloudExpansion> ex = new TreeMap<>(); | ||||
|         IntStream.range(start, end).forEach(n -> ex.put(n, map.get(n))); | ||||
|  | ||||
|         return ex; | ||||
|     } | ||||
|  | ||||
|  | ||||
|     public void fetch(boolean allowUnverified) { | ||||
|  | ||||
|         plugin.getLogger().info("Fetching available expansion information..."); | ||||
|  | ||||
|     new BukkitRunnable() { | ||||
|         plugin.getServer().getScheduler().runTaskAsynchronously(plugin, () -> { | ||||
|  | ||||
|       @Override | ||||
|       public void run() { | ||||
|             final String readJson = URLReader.read(API_URL); | ||||
|             final Map<String, CloudExpansion> data = GSON.fromJson(readJson, new TypeToken<Map<String, CloudExpansion>>() { | ||||
|             }.getType()); | ||||
|  | ||||
|         StringBuilder sb; | ||||
|             final List<CloudExpansion> unsorted = new ArrayList<>(); | ||||
|  | ||||
|         try { | ||||
|             data.forEach((name, cexp) -> { | ||||
|  | ||||
|           URL site = new URL(API); | ||||
|                 if ((allowUnverified || cexp.isVerified()) && cexp.getLatestVersion() != null && cexp.getVersion(cexp.getLatestVersion()) != null) { | ||||
|                     cexp.setName(name); | ||||
|  | ||||
|           HttpURLConnection connection = (HttpURLConnection) site.openConnection(); | ||||
|  | ||||
|           connection.setRequestMethod("GET"); | ||||
|  | ||||
|           connection.connect(); | ||||
|  | ||||
|           BufferedReader br = new BufferedReader( | ||||
|               new InputStreamReader(connection.getInputStream())); | ||||
|  | ||||
|           sb = new StringBuilder(); | ||||
|  | ||||
|           String line; | ||||
|  | ||||
|           while ((line = br.readLine()) != null) { | ||||
|             sb.append(line); | ||||
|           } | ||||
|  | ||||
|           br.close(); | ||||
|           connection.disconnect(); | ||||
|  | ||||
|         } catch (Exception e) { | ||||
|           return; | ||||
|         } | ||||
|  | ||||
|         String json = sb.toString(); | ||||
|         JSONParser parser = new JSONParser(); | ||||
|         Object obj = null; | ||||
|  | ||||
|         try { | ||||
|           obj = parser.parse(json); | ||||
|         } catch (ParseException e) { | ||||
|           e.printStackTrace(); | ||||
|         } | ||||
|  | ||||
|         if (obj == null) { | ||||
|           return; | ||||
|         } | ||||
|  | ||||
|         List<CloudExpansion> unsorted = new ArrayList<>(); | ||||
|  | ||||
|         if (obj instanceof JSONObject) { | ||||
|  | ||||
|           JSONObject jo = (JSONObject) obj; | ||||
|  | ||||
|           for (Object o : jo.keySet()) { | ||||
|  | ||||
|             JSONObject sub = (JSONObject) jo.get(o); | ||||
|  | ||||
|             CloudExpansion ce = gson.fromJson(sub.toJSONString(), CloudExpansion.class); | ||||
|  | ||||
|             if (!allowUnverified && !ce.isVerified()) { | ||||
|               continue; | ||||
|             } | ||||
|  | ||||
|             if (ce.getLatestVersion() == null || ce.getVersion(ce.getLatestVersion()) == null) { | ||||
|               continue; | ||||
|             } | ||||
|  | ||||
|             ce.setName(o.toString()); | ||||
|  | ||||
|             PlaceholderExpansion ex = plugin.getExpansionManager() | ||||
|                 .getRegisteredExpansion(ce.getName()); | ||||
|                     PlaceholderExpansion ex = plugin.getExpansionManager().getRegisteredExpansion(cexp.getName()); | ||||
|  | ||||
|                     if (ex != null && ex.isRegistered()) { | ||||
|               ce.setHasExpansion(true); | ||||
|               if (!ex.getVersion().equals(ce.getLatestVersion())) { | ||||
|                 ce.setShouldUpdate(true); | ||||
|                         cexp.setHasExpansion(true); | ||||
|                         if (!ex.getVersion().equals(cexp.getLatestVersion())) { | ||||
|                             cexp.setShouldUpdate(true); | ||||
|                         } | ||||
|                     } | ||||
|  | ||||
|             unsorted.add(ce); | ||||
|                     unsorted.add(cexp); | ||||
|                 } | ||||
|  | ||||
|           int count = 0; | ||||
|             }); | ||||
|  | ||||
|             unsorted.sort(Comparator.comparing(CloudExpansion::getLastUpdate).reversed()); | ||||
|  | ||||
|             int count = 0; | ||||
|             for (CloudExpansion e : unsorted) { | ||||
|             remote.put(count, e); | ||||
|             count++; | ||||
|                 remote.put(count++, e); | ||||
|             } | ||||
|  | ||||
|             plugin.getLogger().info(count + " placeholder expansions are available on the cloud."); | ||||
| @@ -278,9 +207,13 @@ public class ExpansionCloudManager { | ||||
|             if (updates > 0) { | ||||
|                 plugin.getLogger().info(updates + " installed expansions have updates available."); | ||||
|             } | ||||
|  | ||||
|         }); | ||||
|     } | ||||
|       } | ||||
|     }.runTaskAsynchronously(plugin); | ||||
|  | ||||
|  | ||||
|     public boolean isDownloading(String expansion) { | ||||
|         return downloading.contains(expansion); | ||||
|     } | ||||
|  | ||||
|     private void download(URL url, String name) throws IOException { | ||||
| @@ -296,7 +229,7 @@ public class ExpansionCloudManager { | ||||
|             is = urlConn.getInputStream(); | ||||
|  | ||||
|             fos = new FileOutputStream( | ||||
|           dir.getAbsolutePath() + File.separator + "Expansion-" + name + ".jar"); | ||||
|                     expansionsDir.getAbsolutePath() + File.separator + "Expansion-" + name + ".jar"); | ||||
|  | ||||
|             byte[] buffer = new byte[is.available()]; | ||||
|  | ||||
| @@ -318,12 +251,12 @@ public class ExpansionCloudManager { | ||||
|         } | ||||
|     } | ||||
|  | ||||
|  | ||||
|     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) { | ||||
|     public void downloadExpansion(final String player, final CloudExpansion ex, final String version) { | ||||
|  | ||||
|         if (downloading.contains(ex.getName())) { | ||||
|             return; | ||||
| @@ -341,9 +274,7 @@ public class ExpansionCloudManager { | ||||
|  | ||||
|         downloading.add(ex.getName()); | ||||
|  | ||||
|     plugin.getLogger().info( | ||||
|         "Attempting download of expansion: " + ex.getName() + (player != null ? " by user: " | ||||
|             + player : "") + " from url: " + ver.getUrl()); | ||||
|         plugin.getLogger().info("Attempting download of expansion: " + ex.getName() + (player != null ? " by user: " + player : "") + " from url: " + ver.getUrl()); | ||||
|  | ||||
|         Bukkit.getScheduler().runTaskAsynchronously(plugin, () -> { | ||||
|  | ||||
| @@ -390,4 +321,25 @@ public class ExpansionCloudManager { | ||||
|  | ||||
|         }); | ||||
|     } | ||||
|  | ||||
|  | ||||
|     private static class URLReader { | ||||
|  | ||||
|         static String read(String url) { | ||||
|             StringBuilder builder = new StringBuilder(); | ||||
|  | ||||
|             try (BufferedReader reader = new BufferedReader(new InputStreamReader(new URL(url).openStream()))) { | ||||
|  | ||||
|                 String inputLine; | ||||
|                 while ((inputLine = reader.readLine()) != null) { | ||||
|                     builder.append(inputLine); | ||||
|                 } | ||||
|             } catch (Exception ex) { | ||||
|                 builder.setLength(0); | ||||
|             } | ||||
|  | ||||
|             return builder.toString(); | ||||
|         } | ||||
|     } | ||||
|  | ||||
| } | ||||
|   | ||||
		Reference in New Issue
	
	Block a user