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