diff options
author | Huynh Tien <huynhqtienvtag@gmail.com> | 2022-05-20 00:09:51 +0700 |
---|---|---|
committer | GitHub <noreply@github.com> | 2022-05-19 18:09:51 +0100 |
commit | d3e6d73b6be762558a3c7ff991ba52d4d6cfb74d (patch) | |
tree | 45c75401d3245d55fb7d4ef9216b3b72173d2ff9 /spark-minestom/src/main/java/me/lucko/spark/minestom | |
parent | 3b124e7995f0ce976f69ae940566e2ba7dd56287 (diff) | |
download | spark-d3e6d73b6be762558a3c7ff991ba52d4d6cfb74d.tar.gz spark-d3e6d73b6be762558a3c7ff991ba52d4d6cfb74d.tar.bz2 spark-d3e6d73b6be762558a3c7ff991ba52d4d6cfb74d.zip |
Minestom platform (#203)
Diffstat (limited to 'spark-minestom/src/main/java/me/lucko/spark/minestom')
7 files changed, 441 insertions, 0 deletions
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..a3cf04c --- /dev/null +++ b/spark-minestom/src/main/java/me/lucko/spark/minestom/MinestomClassSourceLookup.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.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..3fc1a82 --- /dev/null +++ b/spark-minestom/src/main/java/me/lucko/spark/minestom/MinestomCommandSender.java @@ -0,0 +1,64 @@ +/* + * 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 (delegate instanceof Player player) { + return player.getUsername(); + } else if (delegate instanceof ConsoleSender) { + return "Console"; + }else { + return "unknown:" + delegate.getClass().getSimpleName(); + } + } + + @Override + public UUID getUniqueId() { + if (super.delegate instanceof Player player) { + return player.getUuid(); + } + return null; + } + + @Override + public void sendMessage(Component message) { + delegate.sendMessage(message); + } + + @Override + public boolean hasPermission(String permission) { + return 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..64dea89 --- /dev/null +++ b/spark-minestom/src/main/java/me/lucko/spark/minestom/MinestomPlatformInfo.java @@ -0,0 +1,46 @@ +/* + * 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..8fb42e0 --- /dev/null +++ b/spark-minestom/src/main/java/me/lucko/spark/minestom/MinestomPlayerPingProvider.java @@ -0,0 +1,39 @@ +/* + * 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/MinestomSparkExtension.java b/spark-minestom/src/main/java/me/lucko/spark/minestom/MinestomSparkExtension.java new file mode 100644 index 0000000..a99f49c --- /dev/null +++ b/spark-minestom/src/main/java/me/lucko/spark/minestom/MinestomSparkExtension.java @@ -0,0 +1,151 @@ +/* + * 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.command.sender.CommandSender; +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.builder.Command; +import net.minestom.server.command.builder.arguments.ArgumentStringArray; +import net.minestom.server.command.builder.arguments.ArgumentType; +import net.minestom.server.command.builder.suggestion.SuggestionEntry; +import net.minestom.server.extensions.Extension; +import net.minestom.server.timer.ExecutionType; + +import java.nio.file.Path; +import java.util.logging.Level; +import java.util.stream.Stream; + +public class MinestomSparkExtension extends Extension implements SparkPlugin { + private SparkPlatform platform; + private MinestomSparkCommand command; + + @Override + public String getVersion() { + return getOrigin().getVersion(); + } + + @Override + public Path getPluginDirectory() { + return getDataDirectory(); + } + + @Override + public String getCommandName() { + return "spark"; + } + + @Override + public Stream<? extends CommandSender> 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(); + } + + @Override + public void initialize() { + this.platform = new SparkPlatform(this); + this.platform.enable(); + this.command = new MinestomSparkCommand(this); + MinecraftServer.getCommandManager().register(command); + } + + @Override + public void terminate() { + this.platform.disable(); + MinecraftServer.getCommandManager().unregister(command); + } + + private static final class MinestomSparkCommand extends Command { + public MinestomSparkCommand(MinestomSparkExtension extension) { + super("spark", "sparkms"); + setDefaultExecutor((sender, context) -> extension.platform.executeCommand(new MinestomCommandSender(sender), new String[0])); + ArgumentStringArray arrayArgument = ArgumentType.StringArray("query"); + arrayArgument.setSuggestionCallback((sender, context, suggestion) -> { + String[] args = context.get(arrayArgument); + if (args == null) { + args = new String[0]; + } + Iterable<String> suggestionEntries = extension.platform.tabCompleteCommand(new MinestomCommandSender(sender), args); + for (String suggestionEntry : suggestionEntries) { + suggestion.addEntry(new SuggestionEntry(suggestionEntry)); + } + }); + addSyntax((sender, context) -> { + String[] args = context.get(arrayArgument); + if (args == null) { + args = new String[0]; + } + extension.platform.executeCommand(new MinestomCommandSender(sender), args); + }, arrayArgument); + } + } +} 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..e5a1895 --- /dev/null +++ b/spark-minestom/src/main/java/me/lucko/spark/minestom/MinestomTickHook.java @@ -0,0 +1,46 @@ +/* + * 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() { + task = MinecraftServer.getSchedulerManager() + .buildTask(this::onTick) + .delay(TaskSchedule.tick(1)) + .repeat(TaskSchedule.tick(1)) + .schedule(); + } + + @Override + public void close() { + if (task != null) { + 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..dbb09ea --- /dev/null +++ b/spark-minestom/src/main/java/me/lucko/spark/minestom/MinestomTickReporter.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.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() { + node.addListener(ServerTickMonitorEvent.class, event -> onTick(event.getTickMonitor().getTickTime())); + } + + @Override + public void start() { + MinecraftServer.getGlobalEventHandler().addChild(node); + } + + @Override + public void close() { + MinecraftServer.getGlobalEventHandler().removeChild(node); + } +} |