diff options
author | Luck <git@lucko.me> | 2021-05-27 23:27:38 +0100 |
---|---|---|
committer | Luck <git@lucko.me> | 2021-05-30 21:51:37 +0100 |
commit | f5bb628319d57c8d1ed26e1673d9f781cc939f83 (patch) | |
tree | dabba3da9c6bc37447d6eacfd42eddb30e6fd2d2 /spark-bukkit/src/main/java | |
parent | 767995e05d46b416292a713756782f939b16f61f (diff) | |
download | spark-f5bb628319d57c8d1ed26e1673d9f781cc939f83.tar.gz spark-f5bb628319d57c8d1ed26e1673d9f781cc939f83.tar.bz2 spark-f5bb628319d57c8d1ed26e1673d9f781cc939f83.zip |
Extract class sources (plugin/mod names) and include in data payload
Diffstat (limited to 'spark-bukkit/src/main/java')
-rw-r--r-- | spark-bukkit/src/main/java/me/lucko/spark/bukkit/BukkitClassSourceLookup.java | 52 | ||||
-rw-r--r-- | spark-bukkit/src/main/java/me/lucko/spark/bukkit/BukkitSparkPlugin.java | 6 |
2 files changed, 58 insertions, 0 deletions
diff --git a/spark-bukkit/src/main/java/me/lucko/spark/bukkit/BukkitClassSourceLookup.java b/spark-bukkit/src/main/java/me/lucko/spark/bukkit/BukkitClassSourceLookup.java new file mode 100644 index 0000000..a87be6f --- /dev/null +++ b/spark-bukkit/src/main/java/me/lucko/spark/bukkit/BukkitClassSourceLookup.java @@ -0,0 +1,52 @@ +/* + * 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; + +import me.lucko.spark.common.util.ClassSourceLookup; + +import org.bukkit.plugin.java.JavaPlugin; + +import java.lang.reflect.Method; + +public class BukkitClassSourceLookup extends ClassSourceLookup.ByClassLoader { + private static final Class<?> PLUGIN_CLASS_LOADER; + private static final Method GET_PLUGIN_METHOD; + + static { + try { + PLUGIN_CLASS_LOADER = Class.forName("org.bukkit.plugin.java.PluginClassLoader"); + GET_PLUGIN_METHOD = PLUGIN_CLASS_LOADER.getDeclaredMethod("getPlugin"); + GET_PLUGIN_METHOD.setAccessible(true); + } catch (ReflectiveOperationException e) { + throw new ExceptionInInitializerError(e); + } + } + + @Override + public String identify(ClassLoader loader) throws ReflectiveOperationException { + if (PLUGIN_CLASS_LOADER.isInstance(loader)) { + JavaPlugin plugin = (JavaPlugin) GET_PLUGIN_METHOD.invoke(loader); + return plugin.getName(); + } + return null; + } +} + 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 970c4b4..e746d60 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 @@ -29,6 +29,7 @@ import me.lucko.spark.common.platform.PlatformInfo; import me.lucko.spark.common.sampler.ThreadDumper; import me.lucko.spark.common.tick.TickHook; import me.lucko.spark.common.tick.TickReporter; +import me.lucko.spark.common.util.ClassSourceLookup; import net.kyori.adventure.platform.bukkit.BukkitAudiences; @@ -160,6 +161,11 @@ public class BukkitSparkPlugin extends JavaPlugin implements SparkPlugin { } @Override + public ClassSourceLookup createClassSourceLookup() { + return new BukkitClassSourceLookup(); + } + + @Override public PlatformInfo getPlatformInfo() { return new BukkitPlatformInfo(getServer()); } |