diff --git a/dependency-reduced-pom.xml b/dependency-reduced-pom.xml index e337d84..7945291 100644 --- a/dependency-reduced-pom.xml +++ b/dependency-reduced-pom.xml @@ -4,7 +4,7 @@ us.ajg0702 ajQueue ajQueue - 1.3.8 + 1.4.1 diff --git a/pom.xml b/pom.xml index e827e01..25f7d5f 100644 --- a/pom.xml +++ b/pom.xml @@ -2,7 +2,7 @@ 4.0.0 us.ajg0702 ajQueue - 1.3.8 + 1.4.1 ajQueue diff --git a/src/main/java/us/ajg0702/queue/Main.java b/src/main/java/us/ajg0702/queue/Main.java index 3ab8696..de83cfe 100644 --- a/src/main/java/us/ajg0702/queue/Main.java +++ b/src/main/java/us/ajg0702/queue/Main.java @@ -4,6 +4,8 @@ import java.io.ByteArrayInputStream; import java.io.DataInputStream; import java.io.IOException; import java.util.List; +import java.util.concurrent.Callable; + import net.md_5.bungee.api.chat.BaseComponent; import net.md_5.bungee.api.chat.TextComponent; import net.md_5.bungee.api.connection.ProxiedPlayer; @@ -68,7 +70,12 @@ public class Main extends Plugin implements Listener { metrics = new BungeeStats(this, 7404); - + metrics.addCustomChart(new BungeeStats.SimplePie("premium", new Callable() { + @Override + public String call() throws Exception { + return isp+""; + } + })); } diff --git a/src/main/java/us/ajg0702/queue/Manager.java b/src/main/java/us/ajg0702/queue/Manager.java index a0b70e3..68e2ad8 100644 --- a/src/main/java/us/ajg0702/queue/Manager.java +++ b/src/main/java/us/ajg0702/queue/Manager.java @@ -339,12 +339,21 @@ public class Manager { } int pos = list.indexOf(p)+1; int len = list.size(); - p.sendMessage(Main.formatMessage( - msgs.get("status.now-in-queue") - .replaceAll("\\{POS\\}", pos+"") - .replaceAll("\\{LEN\\}", len+"") - .replaceAll("\\{SERVER\\}", s) - )); + if(list.size() <= 1) { + p.sendMessage(Main.formatMessage( + msgs.get("status.now-in-empty-queue") + .replaceAll("\\{POS\\}", pos+"") + .replaceAll("\\{LEN\\}", len+"") + .replaceAll("\\{SERVER\\}", s) + )); + } else { + p.sendMessage(Main.formatMessage( + msgs.get("status.now-in-queue") + .replaceAll("\\{POS\\}", pos+"") + .replaceAll("\\{LEN\\}", len+"") + .replaceAll("\\{SERVER\\}", s) + )); + } BungeeUtils.sendCustomData(p, "position", pos+""); BungeeUtils.sendCustomData(p, "positionof", len+""); diff --git a/src/main/java/us/ajg0702/queue/spigot/Main.java b/src/main/java/us/ajg0702/queue/spigot/Main.java index 112c1d3..6b77837 100644 --- a/src/main/java/us/ajg0702/queue/spigot/Main.java +++ b/src/main/java/us/ajg0702/queue/spigot/Main.java @@ -58,7 +58,7 @@ public class Main extends JavaPlugin implements PluginMessageListener,Listener { VersionSupport.sendActionBar(p, text); return; } - if(subchannel.equals("queuename")) { + if(subchannel.equals("queuename") && papi) { String playername = in.readUTF(); Player p = Bukkit.getPlayer(playername); if(p == null) return; @@ -70,7 +70,7 @@ public class Main extends JavaPlugin implements PluginMessageListener,Listener { phs.put("queued", data); placeholders.responseCache.put(p, phs); } - if(subchannel.equals("position")) { + if(subchannel.equals("position") && papi) { String playername = in.readUTF(); Player p = Bukkit.getPlayer(playername); if(p == null) return; @@ -82,7 +82,7 @@ public class Main extends JavaPlugin implements PluginMessageListener,Listener { phs.put("position", data); placeholders.responseCache.put(p, phs); } - if(subchannel.equals("positionof")) { + if(subchannel.equals("positionof") && papi) { String playername = in.readUTF(); Player p = Bukkit.getPlayer(playername); if(p == null) return; diff --git a/src/main/java/us/ajg0702/queue/utils/BungeeMessages.java b/src/main/java/us/ajg0702/queue/utils/BungeeMessages.java index 39c47d4..4f50ebb 100644 --- a/src/main/java/us/ajg0702/queue/utils/BungeeMessages.java +++ b/src/main/java/us/ajg0702/queue/utils/BungeeMessages.java @@ -60,6 +60,7 @@ public class BungeeMessages { d.put("status.online.base", "&7You are in position &f{POS}&7 of &f{LEN}&7. Estimated time: {TIME}"); d.put("status.left-last-queue", "&aYou left the last queue you were in."); d.put("status.now-in-queue", "&aYou are now queued! &7You are in position &f{POS}&7 of &f{LEN}&7.\n&7Type &f/leavequeue&7 to leave the queue!"); + d.put("status.now-in-empty-queue", ""); d.put("errors.server-not-exist", "&cThat server does not exist!"); d.put("errors.already-queued", "&cYou are already queued for that server!"); diff --git a/src/main/java/us/ajg0702/queue/utils/BungeeStats.java b/src/main/java/us/ajg0702/queue/utils/BungeeStats.java index 4502833..eee66a2 100644 --- a/src/main/java/us/ajg0702/queue/utils/BungeeStats.java +++ b/src/main/java/us/ajg0702/queue/utils/BungeeStats.java @@ -2,6 +2,7 @@ package us.ajg0702.queue.utils; import com.google.gson.JsonArray; import com.google.gson.JsonObject; +import com.google.gson.JsonPrimitive; import net.md_5.bungee.api.plugin.Plugin; import net.md_5.bungee.config.Configuration; import net.md_5.bungee.config.ConfigurationProvider; @@ -14,9 +15,12 @@ import java.net.URL; import java.nio.charset.StandardCharsets; import java.util.ArrayList; import java.util.List; +import java.util.Map; import java.util.UUID; +import java.util.concurrent.Callable; import java.util.concurrent.TimeUnit; import java.util.logging.Level; +import java.util.logging.Logger; import java.util.zip.GZIPOutputStream; /** @@ -24,6 +28,7 @@ import java.util.zip.GZIPOutputStream; *

* Check out https://bStats.org/ to learn more about bStats! */ +@SuppressWarnings({"WeakerAccess", "unused"}) public class BungeeStats { static { @@ -70,6 +75,9 @@ public class BungeeStats { // A list with all known metrics class objects including this one private static final List knownMetricsInstances = new ArrayList<>(); + // A list with all custom charts + private final List charts = new ArrayList<>(); + /** * Class constructor. * @@ -124,6 +132,18 @@ public class BungeeStats { return enabled; } + /** + * Adds a custom chart. + * + * @param chart The chart to add. + */ + public void addCustomChart(CustomChart chart) { + if (chart == null) { + plugin.getLogger().log(Level.WARNING, "Chart cannot be null"); + } + charts.add(chart); + } + /** * Links an other metrics class with this class. * This method is called using Reflection. @@ -151,6 +171,14 @@ public class BungeeStats { data.addProperty("pluginVersion", pluginVersion); JsonArray customCharts = new JsonArray(); + for (CustomChart customChart : charts) { + // Add the data of the custom charts + JsonObject chart = customChart.getRequestJsonObject(plugin.getLogger(), logFailedRequests); + if (chart == null) { // If the chart is null, we skip it + continue; + } + customCharts.add(chart); + } data.add("customCharts", customCharts); return data; @@ -170,8 +198,7 @@ public class BungeeStats { * * @return The server specific data. */ - @SuppressWarnings("deprecation") - private JsonObject getServerData() { + private JsonObject getServerData() { // Minecraft specific data int playerAmount = Math.min(plugin.getProxy().getOnlineCount(), 500); int onlineMode = plugin.getProxy().getConfig().isOnlineMode() ? 1 : 0; @@ -361,6 +388,7 @@ public class BungeeStats { } StringBuilder builder = new StringBuilder(); + try (BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(connection.getInputStream()))) { String line; while ((line = bufferedReader.readLine()) != null) { @@ -391,4 +419,336 @@ public class BungeeStats { return outputStream.toByteArray(); } + + /** + * Represents a custom chart. + */ + public static abstract class CustomChart { + + // The id of the chart + private final String chartId; + + /** + * Class constructor. + * + * @param chartId The id of the chart. + */ + CustomChart(String chartId) { + if (chartId == null || chartId.isEmpty()) { + throw new IllegalArgumentException("ChartId cannot be null or empty!"); + } + this.chartId = chartId; + } + + private JsonObject getRequestJsonObject(Logger logger, boolean logFailedRequests) { + JsonObject chart = new JsonObject(); + chart.addProperty("chartId", chartId); + try { + JsonObject data = getChartData(); + if (data == null) { + // If the data is null we don't send the chart. + return null; + } + chart.add("data", data); + } catch (Throwable t) { + if (logFailedRequests) { + logger.log(Level.WARNING, "Failed to get data for custom chart with id " + chartId, t); + } + return null; + } + return chart; + } + + protected abstract JsonObject getChartData() throws Exception; + + } + + /** + * Represents a custom simple pie. + */ + public static class SimplePie extends CustomChart { + + private final Callable callable; + + /** + * Class constructor. + * + * @param chartId The id of the chart. + * @param callable The callable which is used to request the chart data. + */ + public SimplePie(String chartId, Callable callable) { + super(chartId); + this.callable = callable; + } + + @Override + protected JsonObject getChartData() throws Exception { + JsonObject data = new JsonObject(); + String value = callable.call(); + if (value == null || value.isEmpty()) { + // Null = skip the chart + return null; + } + data.addProperty("value", value); + return data; + } + } + + /** + * Represents a custom advanced pie. + */ + public static class AdvancedPie extends CustomChart { + + private final Callable> callable; + + /** + * Class constructor. + * + * @param chartId The id of the chart. + * @param callable The callable which is used to request the chart data. + */ + public AdvancedPie(String chartId, Callable> callable) { + super(chartId); + this.callable = callable; + } + + @Override + protected JsonObject getChartData() throws Exception { + JsonObject data = new JsonObject(); + JsonObject values = new JsonObject(); + Map map = callable.call(); + if (map == null || map.isEmpty()) { + // Null = skip the chart + return null; + } + boolean allSkipped = true; + for (Map.Entry entry : map.entrySet()) { + if (entry.getValue() == 0) { + continue; // Skip this invalid + } + allSkipped = false; + values.addProperty(entry.getKey(), entry.getValue()); + } + if (allSkipped) { + // Null = skip the chart + return null; + } + data.add("values", values); + return data; + } + } + + /** + * Represents a custom drilldown pie. + */ + public static class DrilldownPie extends CustomChart { + + private final Callable>> callable; + + /** + * Class constructor. + * + * @param chartId The id of the chart. + * @param callable The callable which is used to request the chart data. + */ + public DrilldownPie(String chartId, Callable>> callable) { + super(chartId); + this.callable = callable; + } + + @Override + public JsonObject getChartData() throws Exception { + JsonObject data = new JsonObject(); + JsonObject values = new JsonObject(); + Map> map = callable.call(); + if (map == null || map.isEmpty()) { + // Null = skip the chart + return null; + } + boolean reallyAllSkipped = true; + for (Map.Entry> entryValues : map.entrySet()) { + JsonObject value = new JsonObject(); + boolean allSkipped = true; + for (Map.Entry valueEntry : map.get(entryValues.getKey()).entrySet()) { + value.addProperty(valueEntry.getKey(), valueEntry.getValue()); + allSkipped = false; + } + if (!allSkipped) { + reallyAllSkipped = false; + values.add(entryValues.getKey(), value); + } + } + if (reallyAllSkipped) { + // Null = skip the chart + return null; + } + data.add("values", values); + return data; + } + } + + /** + * Represents a custom single line chart. + */ + public static class SingleLineChart extends CustomChart { + + private final Callable callable; + + /** + * Class constructor. + * + * @param chartId The id of the chart. + * @param callable The callable which is used to request the chart data. + */ + public SingleLineChart(String chartId, Callable callable) { + super(chartId); + this.callable = callable; + } + + @Override + protected JsonObject getChartData() throws Exception { + JsonObject data = new JsonObject(); + int value = callable.call(); + if (value == 0) { + // Null = skip the chart + return null; + } + data.addProperty("value", value); + return data; + } + + } + + /** + * Represents a custom multi line chart. + */ + public static class MultiLineChart extends CustomChart { + + private final Callable> callable; + + /** + * Class constructor. + * + * @param chartId The id of the chart. + * @param callable The callable which is used to request the chart data. + */ + public MultiLineChart(String chartId, Callable> callable) { + super(chartId); + this.callable = callable; + } + + @Override + protected JsonObject getChartData() throws Exception { + JsonObject data = new JsonObject(); + JsonObject values = new JsonObject(); + Map map = callable.call(); + if (map == null || map.isEmpty()) { + // Null = skip the chart + return null; + } + boolean allSkipped = true; + for (Map.Entry entry : map.entrySet()) { + if (entry.getValue() == 0) { + continue; // Skip this invalid + } + allSkipped = false; + values.addProperty(entry.getKey(), entry.getValue()); + } + if (allSkipped) { + // Null = skip the chart + return null; + } + data.add("values", values); + return data; + } + + } + + /** + * Represents a custom simple bar chart. + */ + public static class SimpleBarChart extends CustomChart { + + private final Callable> callable; + + /** + * Class constructor. + * + * @param chartId The id of the chart. + * @param callable The callable which is used to request the chart data. + */ + public SimpleBarChart(String chartId, Callable> callable) { + super(chartId); + this.callable = callable; + } + + @Override + protected JsonObject getChartData() throws Exception { + JsonObject data = new JsonObject(); + JsonObject values = new JsonObject(); + Map map = callable.call(); + if (map == null || map.isEmpty()) { + // Null = skip the chart + return null; + } + for (Map.Entry entry : map.entrySet()) { + JsonArray categoryValues = new JsonArray(); + categoryValues.add(new JsonPrimitive(entry.getValue())); + values.add(entry.getKey(), categoryValues); + } + data.add("values", values); + return data; + } + + } + + /** + * Represents a custom advanced bar chart. + */ + public static class AdvancedBarChart extends CustomChart { + + private final Callable> callable; + + /** + * Class constructor. + * + * @param chartId The id of the chart. + * @param callable The callable which is used to request the chart data. + */ + public AdvancedBarChart(String chartId, Callable> callable) { + super(chartId); + this.callable = callable; + } + + @Override + protected JsonObject getChartData() throws Exception { + JsonObject data = new JsonObject(); + JsonObject values = new JsonObject(); + Map map = callable.call(); + if (map == null || map.isEmpty()) { + // Null = skip the chart + return null; + } + boolean allSkipped = true; + for (Map.Entry entry : map.entrySet()) { + if (entry.getValue().length == 0) { + continue; // Skip this invalid + } + allSkipped = false; + JsonArray categoryValues = new JsonArray(); + for (int categoryValue : entry.getValue()) { + categoryValues.add(new JsonPrimitive(categoryValue)); + } + values.add(entry.getKey(), categoryValues); + } + if (allSkipped) { + // Null = skip the chart + return null; + } + data.add("values", values); + return data; + } + + } + } \ No newline at end of file diff --git a/src/main/resources/plugin.yml b/src/main/resources/plugin.yml index d4a001e..45dcb65 100644 --- a/src/main/resources/plugin.yml +++ b/src/main/resources/plugin.yml @@ -1,5 +1,6 @@ main: us.ajg0702.queue.spigot.Main version: ${project.version} +api-version: 1.13 author: ajgeiss0702 name: ajQueue softdepend: [PlaceholderAPI]