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 --- .../bungeecord/BungeeCordClassSourceLookup.java | 52 ++++++++++++++++++++++ .../spark/bungeecord/BungeeCordSparkPlugin.java | 6 +++ 2 files changed, 58 insertions(+) create mode 100644 spark-bungeecord/src/main/java/me/lucko/spark/bungeecord/BungeeCordClassSourceLookup.java (limited to 'spark-bungeecord/src/main') diff --git a/spark-bungeecord/src/main/java/me/lucko/spark/bungeecord/BungeeCordClassSourceLookup.java b/spark-bungeecord/src/main/java/me/lucko/spark/bungeecord/BungeeCordClassSourceLookup.java new file mode 100644 index 0000000..e601f87 --- /dev/null +++ b/spark-bungeecord/src/main/java/me/lucko/spark/bungeecord/BungeeCordClassSourceLookup.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.bungeecord; + +import me.lucko.spark.common.util.ClassSourceLookup; + +import net.md_5.bungee.api.plugin.PluginDescription; + +import java.lang.reflect.Field; + +public class BungeeCordClassSourceLookup extends ClassSourceLookup.ByClassLoader { + private static final Class PLUGIN_CLASS_LOADER; + private static final Field DESC_FIELD; + + static { + try { + PLUGIN_CLASS_LOADER = Class.forName("net.md_5.bungee.api.plugin.PluginClassloader"); + DESC_FIELD = PLUGIN_CLASS_LOADER.getDeclaredField("desc"); + DESC_FIELD.setAccessible(true); + } catch (ReflectiveOperationException e) { + throw new ExceptionInInitializerError(e); + } + } + + @Override + public String identify(ClassLoader loader) throws ReflectiveOperationException { + if (PLUGIN_CLASS_LOADER.isInstance(loader)) { + PluginDescription desc = (PluginDescription) DESC_FIELD.get(loader); + return desc.getName(); + } + return null; + } +} + diff --git a/spark-bungeecord/src/main/java/me/lucko/spark/bungeecord/BungeeCordSparkPlugin.java b/spark-bungeecord/src/main/java/me/lucko/spark/bungeecord/BungeeCordSparkPlugin.java index 357c457..2fb44c3 100644 --- a/spark-bungeecord/src/main/java/me/lucko/spark/bungeecord/BungeeCordSparkPlugin.java +++ b/spark-bungeecord/src/main/java/me/lucko/spark/bungeecord/BungeeCordSparkPlugin.java @@ -23,6 +23,7 @@ package me.lucko.spark.bungeecord; import me.lucko.spark.common.SparkPlatform; import me.lucko.spark.common.SparkPlugin; import me.lucko.spark.common.platform.PlatformInfo; +import me.lucko.spark.common.util.ClassSourceLookup; import net.kyori.adventure.platform.bungeecord.BungeeAudiences; import net.md_5.bungee.api.CommandSender; @@ -78,6 +79,11 @@ public class BungeeCordSparkPlugin extends Plugin implements SparkPlugin { getProxy().getScheduler().runAsync(BungeeCordSparkPlugin.this, task); } + @Override + public ClassSourceLookup createClassSourceLookup() { + return new BungeeCordClassSourceLookup(); + } + @Override public PlatformInfo getPlatformInfo() { return new BungeeCordPlatformInfo(getProxy()); -- cgit