diff options
Diffstat (limited to 'spark-minestom')
9 files changed, 535 insertions, 0 deletions
diff --git a/spark-minestom/build.gradle b/spark-minestom/build.gradle new file mode 100644 index 0000000..26cdc2c --- /dev/null +++ b/spark-minestom/build.gradle @@ -0,0 +1,50 @@ +plugins { + id 'com.github.johnrengelman.shadow' version '7.0.0' +} + +tasks.withType(JavaCompile) { + // override, compile targeting J17 + options.release = 17 +} + +dependencies { + implementation project(':spark-common') + compileOnly 'com.github.Minestom:Minestom:367c389bc6' + implementation 'com.google.guava:guava:19.0' +} + +processResources { + from(sourceSets.main.resources.srcDirs) { + expand ( + 'pluginVersion': project.pluginVersion, + 'pluginDescription': project.pluginDescription + ) + include 'extension.json' + } +} + +shadowJar { + archiveName = "spark-${project.pluginVersion}-minestom.jar" + + dependencies { + exclude(dependency('net.kyori:^(?!adventure-text-feature-pagination).+$')) + } + + relocate 'okio', 'me.lucko.spark.lib.okio' + relocate 'okhttp3', 'me.lucko.spark.lib.okhttp3' + relocate 'net.kyori.adventure.text.feature.pagination', 'me.lucko.spark.lib.adventure.pagination' + relocate 'net.bytebuddy', 'me.lucko.spark.lib.bytebuddy' + relocate 'org.tukaani.xz', 'me.lucko.spark.lib.xz' + relocate 'com.google.protobuf', 'me.lucko.spark.lib.protobuf' + relocate 'org.objectweb.asm', 'me.lucko.spark.lib.asm' + relocate 'one.profiler', 'me.lucko.spark.lib.asyncprofiler' + + exclude 'module-info.class' + exclude 'META-INF/maven/**' + exclude 'META-INF/proguard/**' +} + +artifacts { + archives shadowJar + shadow shadowJar +} diff --git a/spark-minestom/src/main/java/me/lucko/spark/minestom/MinestomClassSourceLookup.java b/spark-minestom/src/main/java/me/lucko/spark/minestom/MinestomClassSourceLookup.java new file mode 100644 index 0000000..252060e --- /dev/null +++ b/spark-minestom/src/main/java/me/lucko/spark/minestom/MinestomClassSourceLookup.java @@ -0,0 +1,49 @@ +/* + * 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.minestom; + +import me.lucko.spark.common.util.ClassSourceLookup; + +import net.minestom.server.MinecraftServer; +import net.minestom.server.extensions.Extension; +import net.minestom.server.extensions.ExtensionClassLoader; + +import java.util.HashMap; +import java.util.Map; + +public class MinestomClassSourceLookup extends ClassSourceLookup.ByClassLoader { + private final Map<ClassLoader, String> classLoaderToExtensions; + + public MinestomClassSourceLookup() { + this.classLoaderToExtensions = new HashMap<>(); + for (Extension extension : MinecraftServer.getExtensionManager().getExtensions()) { + this.classLoaderToExtensions.put(extension.getClass().getClassLoader(), extension.getOrigin().getName()); + } + } + + @Override + public String identify(ClassLoader loader) { + if (loader instanceof ExtensionClassLoader) { + return this.classLoaderToExtensions.get(loader); + } + return null; + } +} diff --git a/spark-minestom/src/main/java/me/lucko/spark/minestom/MinestomCommandSender.java b/spark-minestom/src/main/java/me/lucko/spark/minestom/MinestomCommandSender.java new file mode 100644 index 0000000..da46224 --- /dev/null +++ b/spark-minestom/src/main/java/me/lucko/spark/minestom/MinestomCommandSender.java @@ -0,0 +1,65 @@ +/* + * 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.minestom; + +import me.lucko.spark.common.command.sender.AbstractCommandSender; + +import net.kyori.adventure.text.Component; +import net.minestom.server.command.CommandSender; +import net.minestom.server.command.ConsoleSender; +import net.minestom.server.entity.Player; + +import java.util.UUID; + +public class MinestomCommandSender extends AbstractCommandSender<CommandSender> { + public MinestomCommandSender(CommandSender delegate) { + super(delegate); + } + + @Override + public String getName() { + if (this.delegate instanceof Player player) { + return player.getUsername(); + } else if (this.delegate instanceof ConsoleSender) { + return "Console"; + }else { + return "unknown:" + this.delegate.getClass().getSimpleName(); + } + } + + @Override + public UUID getUniqueId() { + if (super.delegate instanceof Player player) { + return player.getUuid(); + } + return null; + } + + @Override + public void sendMessage(Component message) { + this.delegate.sendMessage(message); + } + + @Override + public boolean hasPermission(String permission) { + return this.delegate.hasPermission(permission); + } +} diff --git a/spark-minestom/src/main/java/me/lucko/spark/minestom/MinestomPlatformInfo.java b/spark-minestom/src/main/java/me/lucko/spark/minestom/MinestomPlatformInfo.java new file mode 100644 index 0000000..add258a --- /dev/null +++ b/spark-minestom/src/main/java/me/lucko/spark/minestom/MinestomPlatformInfo.java @@ -0,0 +1,47 @@ +/* + * 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.minestom; + +import me.lucko.spark.common.platform.PlatformInfo; + +import net.minestom.server.MinecraftServer; + +public class MinestomPlatformInfo implements PlatformInfo { + @Override + public Type getType() { + return Type.SERVER; + } + + @Override + public String getName() { + return "Minestom"; + } + + @Override + public String getVersion() { + return MinecraftServer.VERSION_NAME + "-" + MinecraftServer.getBrandName(); + } + + @Override + public String getMinecraftVersion() { + return MinecraftServer.VERSION_NAME; + } +} diff --git a/spark-minestom/src/main/java/me/lucko/spark/minestom/MinestomPlayerPingProvider.java b/spark-minestom/src/main/java/me/lucko/spark/minestom/MinestomPlayerPingProvider.java new file mode 100644 index 0000000..0b50c0a --- /dev/null +++ b/spark-minestom/src/main/java/me/lucko/spark/minestom/MinestomPlayerPingProvider.java @@ -0,0 +1,41 @@ +/* + * 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.minestom; + +import com.google.common.collect.ImmutableMap; + +import me.lucko.spark.common.monitor.ping.PlayerPingProvider; + +import net.minestom.server.MinecraftServer; +import net.minestom.server.entity.Player; + +import java.util.Map; + +public class MinestomPlayerPingProvider implements PlayerPingProvider { + @Override + public Map<String, Integer> poll() { + ImmutableMap.Builder<String, Integer> builder = ImmutableMap.builder(); + for (Player player : MinecraftServer.getConnectionManager().getOnlinePlayers()) { + builder.put(player.getUsername(), player.getLatency()); + } + return builder.build(); + } +} diff --git a/spark-minestom/src/main/java/me/lucko/spark/minestom/MinestomSparkPlugin.java b/spark-minestom/src/main/java/me/lucko/spark/minestom/MinestomSparkPlugin.java new file mode 100644 index 0000000..2b43cae --- /dev/null +++ b/spark-minestom/src/main/java/me/lucko/spark/minestom/MinestomSparkPlugin.java @@ -0,0 +1,183 @@ +/* + * 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.minestom; + +import me.lucko.spark.common.SparkPlatform; +import me.lucko.spark.common.SparkPlugin; +import me.lucko.spark.common.monitor.ping.PlayerPingProvider; +import me.lucko.spark.common.platform.PlatformInfo; +import me.lucko.spark.common.tick.TickHook; +import me.lucko.spark.common.tick.TickReporter; +import me.lucko.spark.common.util.ClassSourceLookup; + +import net.minestom.server.MinecraftServer; +import net.minestom.server.command.CommandSender; +import net.minestom.server.command.builder.Command; +import net.minestom.server.command.builder.CommandContext; +import net.minestom.server.command.builder.CommandExecutor; +import net.minestom.server.command.builder.arguments.ArgumentStringArray; +import net.minestom.server.command.builder.arguments.ArgumentType; +import net.minestom.server.command.builder.suggestion.Suggestion; +import net.minestom.server.command.builder.suggestion.SuggestionCallback; +import net.minestom.server.command.builder.suggestion.SuggestionEntry; +import net.minestom.server.extensions.Extension; +import net.minestom.server.timer.ExecutionType; + +import org.jetbrains.annotations.NotNull; + +import java.nio.file.Path; +import java.util.Arrays; +import java.util.logging.Level; +import java.util.stream.Stream; + +public class MinestomSparkPlugin extends Extension implements SparkPlugin { + private SparkPlatform platform; + private MinestomSparkCommand command; + + @Override + public void initialize() { + this.platform = new SparkPlatform(this); + this.platform.enable(); + this.command = new MinestomSparkCommand(this.platform); + MinecraftServer.getCommandManager().register(this.command); + } + + @Override + public void terminate() { + this.platform.disable(); + MinecraftServer.getCommandManager().unregister(this.command); + } + + @Override + public String getVersion() { + return getOrigin().getVersion(); + } + + @Override + public Path getPluginDirectory() { + return getDataDirectory(); + } + + @Override + public String getCommandName() { + return "spark"; + } + + @Override + public Stream<MinestomCommandSender> getCommandSenders() { + return Stream.concat( + MinecraftServer.getConnectionManager().getOnlinePlayers().stream(), + Stream.of(MinecraftServer.getCommandManager().getConsoleSender()) + ).map(MinestomCommandSender::new); + } + + @Override + public void executeAsync(Runnable task) { + MinecraftServer.getSchedulerManager().scheduleNextTick(task, ExecutionType.ASYNC); + } + + @Override + public void log(Level level, String msg) { + if (level == Level.INFO) { + this.getLogger().info(msg); + } else if (level == Level.WARNING) { + this.getLogger().warn(msg); + } else if (level == Level.SEVERE) { + this.getLogger().error(msg); + } else { + throw new IllegalArgumentException(level.getName()); + } + } + + @Override + public PlatformInfo getPlatformInfo() { + return new MinestomPlatformInfo(); + } + + @Override + public ClassSourceLookup createClassSourceLookup() { + return new MinestomClassSourceLookup(); + } + + @Override + public PlayerPingProvider createPlayerPingProvider() { + return new MinestomPlayerPingProvider(); + } + + @Override + public TickReporter createTickReporter() { + return new MinestomTickReporter(); + } + + @Override + public TickHook createTickHook() { + return new MinestomTickHook(); + } + + private static final class MinestomSparkCommand extends Command implements CommandExecutor, SuggestionCallback { + private final SparkPlatform platform; + + public MinestomSparkCommand(SparkPlatform platform) { + super("spark"); + this.platform = platform; + + ArgumentStringArray arrayArgument = ArgumentType.StringArray("args"); + arrayArgument.setSuggestionCallback(this); + + addSyntax(this, arrayArgument); + setDefaultExecutor((sender, context) -> platform.executeCommand(new MinestomCommandSender(sender), new String[0])); + } + + // execute + @Override + public void apply(@NotNull CommandSender sender, @NotNull CommandContext context) { + String[] args = processArgs(context, false); + if (args == null) { + return; + } + + this.platform.executeCommand(new MinestomCommandSender(sender), args); + } + + // tab complete + @Override + public void apply(@NotNull CommandSender sender, @NotNull CommandContext context, @NotNull Suggestion suggestion) { + String[] args = processArgs(context, true); + if (args == null) { + return; + } + + Iterable<String> suggestionEntries = this.platform.tabCompleteCommand(new MinestomCommandSender(sender), args); + for (String suggestionEntry : suggestionEntries) { + suggestion.addEntry(new SuggestionEntry(suggestionEntry)); + } + } + + private static String [] processArgs(CommandContext context, boolean tabComplete) { + String[] split = context.getInput().split(" ", tabComplete ? -1 : 0); + if (split.length == 0 || !split[0].equals("/spark") && !split[0].equals("spark")) { + return null; + } + + return Arrays.copyOfRange(split, 1, split.length); + } + } +} diff --git a/spark-minestom/src/main/java/me/lucko/spark/minestom/MinestomTickHook.java b/spark-minestom/src/main/java/me/lucko/spark/minestom/MinestomTickHook.java new file mode 100644 index 0000000..44c1c2d --- /dev/null +++ b/spark-minestom/src/main/java/me/lucko/spark/minestom/MinestomTickHook.java @@ -0,0 +1,47 @@ +/* + * 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.minestom; + +import me.lucko.spark.common.tick.AbstractTickHook; + +import net.minestom.server.MinecraftServer; +import net.minestom.server.timer.Task; +import net.minestom.server.timer.TaskSchedule; + +public class MinestomTickHook extends AbstractTickHook { + private Task task; + + @Override + public void start() { + this.task = MinecraftServer.getSchedulerManager() + .buildTask(this::onTick) + .delay(TaskSchedule.tick(1)) + .repeat(TaskSchedule.tick(1)) + .schedule(); + } + + @Override + public void close() { + if (this.task != null) { + this.task.cancel(); + } + } +} diff --git a/spark-minestom/src/main/java/me/lucko/spark/minestom/MinestomTickReporter.java b/spark-minestom/src/main/java/me/lucko/spark/minestom/MinestomTickReporter.java new file mode 100644 index 0000000..ae25f92 --- /dev/null +++ b/spark-minestom/src/main/java/me/lucko/spark/minestom/MinestomTickReporter.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.minestom; + +import me.lucko.spark.common.tick.AbstractTickReporter; + +import net.minestom.server.MinecraftServer; +import net.minestom.server.event.Event; +import net.minestom.server.event.EventNode; +import net.minestom.server.event.server.ServerTickMonitorEvent; + +import java.util.UUID; + +public class MinestomTickReporter extends AbstractTickReporter { + private final EventNode<Event> node = EventNode.all("sparkTickReporter-" + UUID.randomUUID()); + + public MinestomTickReporter() { + this.node.addListener(ServerTickMonitorEvent.class, event -> onTick(event.getTickMonitor().getTickTime())); + } + + @Override + public void start() { + MinecraftServer.getGlobalEventHandler().addChild(this.node); + } + + @Override + public void close() { + MinecraftServer.getGlobalEventHandler().removeChild(this.node); + } +} diff --git a/spark-minestom/src/main/resources/extension.json b/spark-minestom/src/main/resources/extension.json new file mode 100644 index 0000000..bea56bb --- /dev/null +++ b/spark-minestom/src/main/resources/extension.json @@ -0,0 +1,5 @@ +{ + "entrypoint": "me.lucko.spark.minestom.MinestomSparkPlugin", + "name": "spark", + "version": "${pluginVersion}" +}
\ No newline at end of file |