From f5bb628319d57c8d1ed26e1673d9f781cc939f83 Mon Sep 17 00:00:00 2001 From: Luck Date: Thu, 27 May 2021 23:27:38 +0100 Subject: Extract class sources (plugin/mod names) and include in data payload --- .../spark/bukkit/BukkitClassSourceLookup.java | 52 ++++++++++++++++++++++ .../me/lucko/spark/bukkit/BukkitSparkPlugin.java | 6 +++ 2 files changed, 58 insertions(+) create mode 100644 spark-bukkit/src/main/java/me/lucko/spark/bukkit/BukkitClassSourceLookup.java (limited to 'spark-bukkit/src/main/java') 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) + * 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 . + */ + +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; @@ -159,6 +160,11 @@ public class BukkitSparkPlugin extends JavaPlugin implements SparkPlugin { return null; } + @Override + public ClassSourceLookup createClassSourceLookup() { + return new BukkitClassSourceLookup(); + } + @Override public PlatformInfo getPlatformInfo() { return new BukkitPlatformInfo(getServer()); -- cgit