diff options
author | Luck <git@lucko.me> | 2019-08-09 20:41:54 +0100 |
---|---|---|
committer | Luck <git@lucko.me> | 2019-08-09 20:41:54 +0100 |
commit | ad3202b1f8f8f45ba611d5b29561ab0a09ad4b5d (patch) | |
tree | a310dd8e0f2dc4c8c40166ba9dd34ea6518966fc /spark-bukkit | |
parent | dc6968c4b56d9f04fa1b56808c298bdda77a442c (diff) | |
download | spark-ad3202b1f8f8f45ba611d5b29561ab0a09ad4b5d.tar.gz spark-ad3202b1f8f8f45ba611d5b29561ab0a09ad4b5d.tar.bz2 spark-ad3202b1f8f8f45ba611d5b29561ab0a09ad4b5d.zip |
Placeholders!
Diffstat (limited to 'spark-bukkit')
6 files changed, 241 insertions, 0 deletions
diff --git a/spark-bukkit/build.gradle b/spark-bukkit/build.gradle index ec2d88d..7de38d3 100644 --- a/spark-bukkit/build.gradle +++ b/spark-bukkit/build.gradle @@ -5,6 +5,12 @@ dependencies { exclude(module: 'text-serializer-gson') } compileOnly 'org.spigotmc:spigot-api:1.14-R0.1-SNAPSHOT' + + // placeholders + compileOnly 'me.clip:placeholderapi:2.10.3' + compileOnly('be.maximvdw:MVdWPlaceholderAPI:3.0.1-SNAPSHOT') { + exclude(module: 'MVdWUpdater') + } } processResources { diff --git a/spark-bukkit/src/main/java/me/lucko/spark/bukkit/BukkitSparkPlugin.java b/spark-bukkit/src/main/java/me/lucko/spark/bukkit/BukkitSparkPlugin.java index 5920bd5..edeeb50 100644 --- a/spark-bukkit/src/main/java/me/lucko/spark/bukkit/BukkitSparkPlugin.java +++ b/spark-bukkit/src/main/java/me/lucko/spark/bukkit/BukkitSparkPlugin.java @@ -20,6 +20,8 @@ package me.lucko.spark.bukkit; +import me.lucko.spark.bukkit.placeholder.SparkMVdWPlaceholders; +import me.lucko.spark.bukkit.placeholder.SparkPlaceholderApi; import me.lucko.spark.common.SparkPlatform; import me.lucko.spark.common.SparkPlugin; import me.lucko.spark.common.sampler.ThreadDumper; @@ -63,6 +65,15 @@ public class BukkitSparkPlugin extends JavaPlugin implements SparkPlugin { }; CommandMapUtil.registerCommand(this, this.tpsCommand, "tps"); } + + if (getServer().getPluginManager().isPluginEnabled("PlaceholderAPI")) { + new SparkPlaceholderApi(this, this.platform); + getLogger().info("Registered PlaceholderAPI placeholders"); + } + if (getServer().getPluginManager().isPluginEnabled("MVdWPlaceholderAPI")) { + new SparkMVdWPlaceholders(this, this.platform); + getLogger().info("Registered MVdWPlaceholderAPI placeholders"); + } } @Override diff --git a/spark-bukkit/src/main/java/me/lucko/spark/bukkit/placeholder/SparkMVdWPlaceholders.java b/spark-bukkit/src/main/java/me/lucko/spark/bukkit/placeholder/SparkMVdWPlaceholders.java new file mode 100644 index 0000000..15aba81 --- /dev/null +++ b/spark-bukkit/src/main/java/me/lucko/spark/bukkit/placeholder/SparkMVdWPlaceholders.java @@ -0,0 +1,48 @@ +/* + * This file is part of spark. + * + * Copyright (c) lucko (Luck) <luck@lucko.me> + * Copyright (c) contributors + * + * 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.lucko.spark.bukkit.placeholder; + +import be.maximvdw.placeholderapi.PlaceholderAPI; +import be.maximvdw.placeholderapi.PlaceholderReplaceEvent; +import be.maximvdw.placeholderapi.PlaceholderReplacer; +import me.lucko.spark.bukkit.BukkitSparkPlugin; +import me.lucko.spark.common.SparkPlatform; + +public class SparkMVdWPlaceholders implements PlaceholderReplacer { + + private final SparkPlatform platform; + + public SparkMVdWPlaceholders(BukkitSparkPlugin plugin, SparkPlatform platform) { + this.platform = platform; + PlaceholderAPI.registerPlaceholder(plugin, "spark_*", this); + } + + @Override + public String onPlaceholderReplace(PlaceholderReplaceEvent event) { + String placeholder = event.getPlaceholder(); + if (!placeholder.startsWith("spark_")) { + return null; + } + + String identifier = placeholder.substring("spark_".length()); + return SparkPlaceholderProvider.respond(this.platform, identifier); + } +} diff --git a/spark-bukkit/src/main/java/me/lucko/spark/bukkit/placeholder/SparkPlaceholderApi.java b/spark-bukkit/src/main/java/me/lucko/spark/bukkit/placeholder/SparkPlaceholderApi.java new file mode 100644 index 0000000..e5ada99 --- /dev/null +++ b/spark-bukkit/src/main/java/me/lucko/spark/bukkit/placeholder/SparkPlaceholderApi.java @@ -0,0 +1,75 @@ +/* + * This file is part of spark. + * + * Copyright (c) lucko (Luck) <luck@lucko.me> + * Copyright (c) contributors + * + * 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.lucko.spark.bukkit.placeholder; + +import me.clip.placeholderapi.expansion.PlaceholderExpansion; +import me.lucko.spark.bukkit.BukkitSparkPlugin; +import me.lucko.spark.common.SparkPlatform; +import org.bukkit.OfflinePlayer; +import org.bukkit.entity.Player; + +import java.util.stream.Collectors; + +public class SparkPlaceholderApi extends PlaceholderExpansion { + private final BukkitSparkPlugin plugin; + private final SparkPlatform platform; + + public SparkPlaceholderApi(BukkitSparkPlugin plugin, SparkPlatform platform) { + this.plugin = plugin; + this.platform = platform; + register(); + } + + @Override + public String onPlaceholderRequest(Player p, String params) { + return onRequest(null, params); + } + + @Override + public String onRequest(OfflinePlayer p, String params) { + return SparkPlaceholderProvider.respond(this.platform, params); + } + + @Override + public String getIdentifier() { + return "spark"; + } + + @Override + public String getAuthor() { + return plugin.getDescription().getAuthors().stream().collect(Collectors.joining(", ")); + } + + @Override + public String getVersion() { + return plugin.getVersion(); + } + + @Override + public boolean persist() { + return true; + } + + @Override + public boolean canRegister() { + return true; + } +} diff --git a/spark-bukkit/src/main/java/me/lucko/spark/bukkit/placeholder/SparkPlaceholderProvider.java b/spark-bukkit/src/main/java/me/lucko/spark/bukkit/placeholder/SparkPlaceholderProvider.java new file mode 100644 index 0000000..d458148 --- /dev/null +++ b/spark-bukkit/src/main/java/me/lucko/spark/bukkit/placeholder/SparkPlaceholderProvider.java @@ -0,0 +1,99 @@ +/* + * This file is part of spark. + * + * Copyright (c) lucko (Luck) <luck@lucko.me> + * Copyright (c) contributors + * + * 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.lucko.spark.bukkit.placeholder; + +import me.lucko.spark.common.SparkPlatform; +import me.lucko.spark.common.command.modules.HealthModule; +import me.lucko.spark.common.monitor.cpu.CpuMonitor; +import me.lucko.spark.common.monitor.tick.TpsCalculator; +import net.kyori.text.TextComponent; +import net.kyori.text.serializer.legacy.LegacyComponentSerializer; + +enum SparkPlaceholderProvider { + ; + + public static String respond(SparkPlatform platform, String placeholder) { + if (placeholder.startsWith("tps")) { + TpsCalculator tpsCalculator = platform.getTpsCalculator(); + if (tpsCalculator == null) { + return null; + } + + switch (placeholder) { + case "tps": + TextComponent c = TextComponent.builder(" ") + .append(HealthModule.formatTps(tpsCalculator.avg5Sec())).append(TextComponent.of(", ")) + .append(HealthModule.formatTps(tpsCalculator.avg10Sec())).append(TextComponent.of(", ")) + .append(HealthModule.formatTps(tpsCalculator.avg1Min())).append(TextComponent.of(", ")) + .append(HealthModule.formatTps(tpsCalculator.avg5Min())).append(TextComponent.of(", ")) + .append(HealthModule.formatTps(tpsCalculator.avg15Min())) + .build(); + return LegacyComponentSerializer.legacy().serialize(c); + case "tps_5s": + return LegacyComponentSerializer.legacy().serialize(HealthModule.formatTps(tpsCalculator.avg5Sec())); + case "tps_10s": + return LegacyComponentSerializer.legacy().serialize(HealthModule.formatTps(tpsCalculator.avg10Sec())); + case "tps_1m": + return LegacyComponentSerializer.legacy().serialize(HealthModule.formatTps(tpsCalculator.avg1Min())); + case "tps_5m": + return LegacyComponentSerializer.legacy().serialize(HealthModule.formatTps(tpsCalculator.avg5Min())); + case "tps_15m": + return LegacyComponentSerializer.legacy().serialize(HealthModule.formatTps(tpsCalculator.avg15Min())); + } + } + + if (placeholder.startsWith("cpu")) { + switch (placeholder) { + case "cpu_system": { + TextComponent c = TextComponent.builder(" ") + .append(HealthModule.formatCpuUsage(CpuMonitor.systemLoad10SecAvg())).append(TextComponent.of(", ")) + .append(HealthModule.formatCpuUsage(CpuMonitor.systemLoad1MinAvg())).append(TextComponent.of(", ")) + .append(HealthModule.formatCpuUsage(CpuMonitor.systemLoad15MinAvg())) + .build(); + return LegacyComponentSerializer.legacy().serialize(c); + } + case "cpu_system_10s": + return LegacyComponentSerializer.legacy().serialize(HealthModule.formatCpuUsage(CpuMonitor.systemLoad10SecAvg())); + case "cpu_system_1m": + return LegacyComponentSerializer.legacy().serialize(HealthModule.formatCpuUsage(CpuMonitor.systemLoad1MinAvg())); + case "cpu_system_15m": + return LegacyComponentSerializer.legacy().serialize(HealthModule.formatCpuUsage(CpuMonitor.systemLoad15MinAvg())); + case "cpu_process": { + TextComponent c = TextComponent.builder(" ") + .append(HealthModule.formatCpuUsage(CpuMonitor.processLoad10SecAvg())).append(TextComponent.of(", ")) + .append(HealthModule.formatCpuUsage(CpuMonitor.processLoad1MinAvg())).append(TextComponent.of(", ")) + .append(HealthModule.formatCpuUsage(CpuMonitor.processLoad15MinAvg())) + .build(); + return LegacyComponentSerializer.legacy().serialize(c); + } + case "cpu_process_10s": + return LegacyComponentSerializer.legacy().serialize(HealthModule.formatCpuUsage(CpuMonitor.processLoad10SecAvg())); + case "cpu_process_1m": + return LegacyComponentSerializer.legacy().serialize(HealthModule.formatCpuUsage(CpuMonitor.processLoad1MinAvg())); + case "cpu_process_15m": + return LegacyComponentSerializer.legacy().serialize(HealthModule.formatCpuUsage(CpuMonitor.processLoad15MinAvg())); + } + } + + return null; + } + +} diff --git a/spark-bukkit/src/main/resources/plugin.yml b/spark-bukkit/src/main/resources/plugin.yml index c940592..7b25e76 100644 --- a/spark-bukkit/src/main/resources/plugin.yml +++ b/spark-bukkit/src/main/resources/plugin.yml @@ -9,6 +9,8 @@ main: me.lucko.spark.bukkit.BukkitSparkPlugin # spark is still compatible with pre-1.13 releases. api-version: 1.13 +softdepend: [PlaceholderAPI, MVdWPlaceholderAPI] + commands: spark: description: Main plugin command
\ No newline at end of file |