diff options
author | Luck <git@lucko.me> | 2021-10-03 18:50:38 +0100 |
---|---|---|
committer | Luck <git@lucko.me> | 2021-10-03 18:50:38 +0100 |
commit | 347095e3b689ed8cedaaf5e0a76282964058ff12 (patch) | |
tree | 2a764814a9fa0016bde7a9f94ace375c6df3ef9f /spark-waterdog/src/main/java/me/lucko | |
parent | dfd397d90a98c9edb110dcfdf4098a4350fa15ac (diff) | |
download | spark-347095e3b689ed8cedaaf5e0a76282964058ff12.tar.gz spark-347095e3b689ed8cedaaf5e0a76282964058ff12.tar.bz2 spark-347095e3b689ed8cedaaf5e0a76282964058ff12.zip |
Waterdog support
Diffstat (limited to 'spark-waterdog/src/main/java/me/lucko')
4 files changed, 278 insertions, 0 deletions
diff --git a/spark-waterdog/src/main/java/me/lucko/spark/waterdog/WaterdogClassSourceLookup.java b/spark-waterdog/src/main/java/me/lucko/spark/waterdog/WaterdogClassSourceLookup.java new file mode 100644 index 0000000..36e6a57 --- /dev/null +++ b/spark-waterdog/src/main/java/me/lucko/spark/waterdog/WaterdogClassSourceLookup.java @@ -0,0 +1,62 @@ +/* + * 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.waterdog; + +import me.lucko.spark.common.util.ClassSourceLookup; + +import dev.waterdog.waterdogpe.ProxyServer; +import dev.waterdog.waterdogpe.plugin.Plugin; +import dev.waterdog.waterdogpe.plugin.PluginClassLoader; + +import java.util.Map; +import java.util.WeakHashMap; + +public class WaterdogClassSourceLookup extends ClassSourceLookup.ByClassLoader { + private final ProxyServer proxy; + private final Map<ClassLoader, String> cache; + + public WaterdogClassSourceLookup(ProxyServer proxy) { + this.proxy = proxy; + this.cache = new WeakHashMap<>(); + } + + @Override + public String identify(ClassLoader loader) throws ReflectiveOperationException { + if (loader instanceof PluginClassLoader) { + String name = this.cache.get(loader); + if (name != null) { + return name; + } + + for (Plugin plugin : this.proxy.getPluginManager().getPlugins()) { + if (plugin.getClass().getClassLoader() == loader) { + name = plugin.getName(); + break; + } + } + + this.cache.put(loader, name); + return name; + } + return null; + } +} + diff --git a/spark-waterdog/src/main/java/me/lucko/spark/waterdog/WaterdogCommandSender.java b/spark-waterdog/src/main/java/me/lucko/spark/waterdog/WaterdogCommandSender.java new file mode 100644 index 0000000..8d7223a --- /dev/null +++ b/spark-waterdog/src/main/java/me/lucko/spark/waterdog/WaterdogCommandSender.java @@ -0,0 +1,61 @@ +/* + * 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.waterdog; + +import me.lucko.spark.common.command.sender.AbstractCommandSender; + +import net.kyori.adventure.text.Component; +import net.kyori.adventure.text.serializer.legacy.LegacyComponentSerializer; + +import dev.waterdog.waterdogpe.command.CommandSender; +import dev.waterdog.waterdogpe.player.ProxiedPlayer; + +import java.util.UUID; + +public class WaterdogCommandSender extends AbstractCommandSender<CommandSender> { + + public WaterdogCommandSender(CommandSender sender) { + super(sender); + } + + @Override + public String getName() { + return super.delegate.getName(); + } + + @Override + public UUID getUniqueId() { + if (super.delegate instanceof ProxiedPlayer) { + return ((ProxiedPlayer) super.delegate).getUniqueId(); + } + return null; + } + + @Override + public void sendMessage(Component message) { + super.delegate.sendMessage(LegacyComponentSerializer.legacySection().serialize(message)); + } + + @Override + public boolean hasPermission(String permission) { + return super.delegate.hasPermission(permission); + } +} diff --git a/spark-waterdog/src/main/java/me/lucko/spark/waterdog/WaterdogPlatformInfo.java b/spark-waterdog/src/main/java/me/lucko/spark/waterdog/WaterdogPlatformInfo.java new file mode 100644 index 0000000..bfbe768 --- /dev/null +++ b/spark-waterdog/src/main/java/me/lucko/spark/waterdog/WaterdogPlatformInfo.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.waterdog; + +import me.lucko.spark.common.platform.AbstractPlatformInfo; + +import dev.waterdog.waterdogpe.WaterdogPE; + +public class WaterdogPlatformInfo extends AbstractPlatformInfo { + + @Override + public Type getType() { + return Type.PROXY; + } + + @Override + public String getName() { + return "Waterdog"; + } + + @Override + public String getVersion() { + return WaterdogPE.version().baseVersion(); + } + + @Override + public String getMinecraftVersion() { + return null; + } +} diff --git a/spark-waterdog/src/main/java/me/lucko/spark/waterdog/WaterdogSparkPlugin.java b/spark-waterdog/src/main/java/me/lucko/spark/waterdog/WaterdogSparkPlugin.java new file mode 100644 index 0000000..8c90ea8 --- /dev/null +++ b/spark-waterdog/src/main/java/me/lucko/spark/waterdog/WaterdogSparkPlugin.java @@ -0,0 +1,107 @@ +/* + * 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.waterdog; + +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 dev.waterdog.waterdogpe.ProxyServer; +import dev.waterdog.waterdogpe.command.Command; +import dev.waterdog.waterdogpe.command.CommandSender; +import dev.waterdog.waterdogpe.plugin.Plugin; + +import java.nio.file.Path; +import java.util.stream.Stream; + +public class WaterdogSparkPlugin extends Plugin implements SparkPlugin { + private SparkPlatform platform; + + public ProxyServer getProxy() { + return ProxyServer.getInstance(); + } + + @Override + public void onEnable() { + this.platform = new SparkPlatform(this); + this.platform.enable(); + getProxy().getCommandMap().registerCommand(new SparkCommand(this)); + } + + @Override + public void onDisable() { + this.platform.disable(); + } + + @Override + public String getVersion() { + return getDescription().getVersion(); + } + + @Override + public Path getPluginDirectory() { + return getDataFolder().toPath(); + } + + @Override + public String getCommandName() { + return "sparkw"; + } + + @Override + public Stream<WaterdogCommandSender> getCommandSenders() { + return Stream.concat( + getProxy().getPlayers().values().stream(), + Stream.of(getProxy().getConsoleSender()) + ).map(WaterdogCommandSender::new); + } + + @Override + public void executeAsync(Runnable task) { + getProxy().getScheduler().scheduleAsync(task); + } + + @Override + public ClassSourceLookup createClassSourceLookup() { + return new WaterdogClassSourceLookup(getProxy()); + } + + @Override + public PlatformInfo getPlatformInfo() { + return new WaterdogPlatformInfo(); + } + + private static final class SparkCommand extends Command { + private final WaterdogSparkPlugin plugin; + + SparkCommand(WaterdogSparkPlugin plugin) { + super("sparkw"); + this.plugin = plugin; + } + + @Override + public boolean onExecute(CommandSender sender, String alias, String[] args) { + this.plugin.platform.executeCommand(new WaterdogCommandSender(sender), args); + return true; + } + } +} |