diff options
| author | Luck <git@lucko.me> | 2019-08-26 09:02:55 +0100 |
|---|---|---|
| committer | Luck <git@lucko.me> | 2019-08-26 09:02:55 +0100 |
| commit | bf2262c392c7e234c8662aa7be5d2970dcacee65 (patch) | |
| tree | eedcfee731264ea5ba9d687a5dfd70eaf36a835e /spark-fabric/src/main/java/me/lucko/spark/fabric/plugin | |
| parent | 8a7ca8ef4a29bd1d684a29f925f3a9db1f69fe0b (diff) | |
| download | spark-bf2262c392c7e234c8662aa7be5d2970dcacee65.tar.gz spark-bf2262c392c7e234c8662aa7be5d2970dcacee65.tar.bz2 spark-bf2262c392c7e234c8662aa7be5d2970dcacee65.zip | |
Various misc changes to spark-forge and spark-fabric
Diffstat (limited to 'spark-fabric/src/main/java/me/lucko/spark/fabric/plugin')
3 files changed, 299 insertions, 0 deletions
diff --git a/spark-fabric/src/main/java/me/lucko/spark/fabric/plugin/FabricClientSparkPlugin.java b/spark-fabric/src/main/java/me/lucko/spark/fabric/plugin/FabricClientSparkPlugin.java new file mode 100644 index 0000000..d87d5be --- /dev/null +++ b/spark-fabric/src/main/java/me/lucko/spark/fabric/plugin/FabricClientSparkPlugin.java @@ -0,0 +1,110 @@ +/* + * 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.fabric.plugin; + +import com.mojang.brigadier.CommandDispatcher; +import me.lucko.spark.common.sampler.TickCounter; +import me.lucko.spark.fabric.FabricCommandSender; +import me.lucko.spark.fabric.FabricSparkGameHooks; +import me.lucko.spark.fabric.FabricSparkMod; +import me.lucko.spark.fabric.FabricTickCounter; +import net.minecraft.client.MinecraftClient; +import net.minecraft.client.network.ClientPlayNetworkHandler; +import net.minecraft.client.network.ClientPlayerEntity; +import net.minecraft.server.command.CommandOutput; +import net.minecraft.server.command.CommandSource; + +import java.util.Arrays; +import java.util.concurrent.TimeUnit; +import java.util.stream.Stream; + +public class FabricClientSparkPlugin extends FabricSparkPlugin { + + public static void register(FabricSparkMod mod, MinecraftClient client) { + FabricClientSparkPlugin plugin = new FabricClientSparkPlugin(mod, client); + + plugin.scheduler.scheduleWithFixedDelay(plugin::checkCommandRegistered, 10, 10, TimeUnit.SECONDS); + } + + private final MinecraftClient minecraft; + private CommandDispatcher<CommandSource> dispatcher; + + public FabricClientSparkPlugin(FabricSparkMod mod, MinecraftClient minecraft) { + super(mod); + this.minecraft = minecraft; + } + + private void checkCommandRegistered() { + ClientPlayerEntity player = this.minecraft.player; + if (player == null) { + return; + } + + ClientPlayNetworkHandler connection = player.networkHandler; + if (connection == null) { + return; + } + + try { + CommandDispatcher<CommandSource> dispatcher = connection.getCommandDispatcher(); + if (dispatcher != this.dispatcher) { + this.dispatcher = dispatcher; + registerCommands(this.dispatcher, c -> 0, "sparkc", "sparkclient"); + FabricSparkGameHooks.INSTANCE.setChatSendCallback(this::onClientChat); + } + } catch (Exception e) { + e.printStackTrace(); + } + } + + public boolean onClientChat(String chat) { + String[] split = chat.split(" "); + if (split.length == 0 || (!split[0].equals("/sparkc") && !split[0].equals("/sparkclient"))) { + return false; + } + + String[] args = Arrays.copyOfRange(split, 1, split.length); + this.platform.executeCommand(new FabricCommandSender(this.minecraft.player, this), args); + this.minecraft.inGameHud.getChatHud().addToMessageHistory(chat); + return true; + } + + @Override + public boolean hasPermission(CommandOutput sender, String permission) { + return true; + } + + @Override + public Stream<FabricCommandSender> getSendersWithPermission(String permission) { + return Stream.of(new FabricCommandSender(this.minecraft.player, this)); + } + + @Override + public TickCounter createTickCounter() { + return new FabricTickCounter.Client(); + } + + @Override + public String getCommandName() { + return "sparkc"; + } + +} diff --git a/spark-fabric/src/main/java/me/lucko/spark/fabric/plugin/FabricServerSparkPlugin.java b/spark-fabric/src/main/java/me/lucko/spark/fabric/plugin/FabricServerSparkPlugin.java new file mode 100644 index 0000000..9729e55 --- /dev/null +++ b/spark-fabric/src/main/java/me/lucko/spark/fabric/plugin/FabricServerSparkPlugin.java @@ -0,0 +1,95 @@ +/* + * 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.fabric.plugin; + +import com.mojang.brigadier.Command; +import com.mojang.brigadier.CommandDispatcher; +import com.mojang.brigadier.context.CommandContext; +import com.mojang.brigadier.exceptions.CommandSyntaxException; +import me.lucko.spark.common.sampler.TickCounter; +import me.lucko.spark.fabric.FabricCommandSender; +import me.lucko.spark.fabric.FabricSparkMod; +import me.lucko.spark.fabric.FabricTickCounter; +import net.minecraft.entity.player.PlayerEntity; +import net.minecraft.server.MinecraftServer; +import net.minecraft.server.command.CommandOutput; +import net.minecraft.server.command.ServerCommandSource; + +import java.util.Arrays; +import java.util.stream.Stream; + +public class FabricServerSparkPlugin extends FabricSparkPlugin implements Command<ServerCommandSource> { + + public static void register(FabricSparkMod mod, MinecraftServer server) { + CommandDispatcher<ServerCommandSource> dispatcher = server.getCommandManager().getDispatcher(); + + FabricServerSparkPlugin plugin = new FabricServerSparkPlugin(mod, server); + registerCommands(dispatcher, plugin, "spark"); + } + + private final MinecraftServer server; + + public FabricServerSparkPlugin(FabricSparkMod mod, MinecraftServer server) { + super(mod); + this.server = server; + } + + @Override + public int run(CommandContext<ServerCommandSource> context) throws CommandSyntaxException { + String[] split = context.getInput().split(" "); + if (split.length == 0 || !split[0].equals("/spark")) { + return 0; + } + + String[] args = Arrays.copyOfRange(split, 1, split.length); + + this.platform.executeCommand(new FabricCommandSender(context.getSource().getPlayer(), this), args); + return 1; + } + + @Override + public boolean hasPermission(CommandOutput sender, String permission) { + if (sender instanceof PlayerEntity) { + return this.server.getPermissionLevel(((PlayerEntity) sender).getGameProfile()) >= 4; + } else { + return true; + } + } + + @Override + public Stream<FabricCommandSender> getSendersWithPermission(String permission) { + return Stream.concat( + this.server.getPlayerManager().getPlayerList().stream() + .filter(player -> hasPermission(player, permission)), + Stream.of(this.server) + ).map(sender -> new FabricCommandSender(sender, this)); + } + + @Override + public TickCounter createTickCounter() { + return new FabricTickCounter.Server(); + } + + @Override + public String getCommandName() { + return "spark"; + } +} diff --git a/spark-fabric/src/main/java/me/lucko/spark/fabric/plugin/FabricSparkPlugin.java b/spark-fabric/src/main/java/me/lucko/spark/fabric/plugin/FabricSparkPlugin.java new file mode 100644 index 0000000..6672358 --- /dev/null +++ b/spark-fabric/src/main/java/me/lucko/spark/fabric/plugin/FabricSparkPlugin.java @@ -0,0 +1,94 @@ +/* + * 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.fabric.plugin; + +import com.google.common.util.concurrent.ThreadFactoryBuilder; +import com.mojang.brigadier.Command; +import com.mojang.brigadier.CommandDispatcher; +import com.mojang.brigadier.arguments.StringArgumentType; +import com.mojang.brigadier.builder.LiteralArgumentBuilder; +import com.mojang.brigadier.builder.RequiredArgumentBuilder; +import com.mojang.brigadier.tree.LiteralCommandNode; +import me.lucko.spark.common.SparkPlatform; +import me.lucko.spark.common.SparkPlugin; +import me.lucko.spark.common.sampler.ThreadDumper; +import me.lucko.spark.fabric.FabricSparkMod; +import net.minecraft.server.command.CommandOutput; + +import java.nio.file.Path; +import java.util.concurrent.Executors; +import java.util.concurrent.ScheduledExecutorService; + +public abstract class FabricSparkPlugin implements SparkPlugin { + + public static <T> void registerCommands(CommandDispatcher<T> dispatcher, Command<T> executor, String... aliases) { + if (aliases.length == 0) { + return; + } + + String mainName = aliases[0]; + LiteralArgumentBuilder<T> command = LiteralArgumentBuilder.<T>literal(mainName) + .executes(executor) + .then(RequiredArgumentBuilder.<T, String>argument("args", StringArgumentType.greedyString()) + .executes(executor) + ); + + LiteralCommandNode<T> node = dispatcher.register(command); + for (int i = 1; i < aliases.length; i++) { + dispatcher.register(LiteralArgumentBuilder.<T>literal(aliases[i]).redirect(node)); + } + } + + private final FabricSparkMod mod; + protected final ScheduledExecutorService scheduler; + protected final SparkPlatform platform; + + protected FabricSparkPlugin(FabricSparkMod mod) { + this.mod = mod; + this.scheduler = Executors.newSingleThreadScheduledExecutor( + new ThreadFactoryBuilder().setNameFormat("spark-fabric-async-worker").build() + ); + this.platform = new SparkPlatform(this); + this.platform.enable(); + } + + public abstract boolean hasPermission(CommandOutput sender, String permission); + + @Override + public String getVersion() { + return this.mod.getVersion(); + } + + @Override + public Path getPluginDirectory() { + return this.mod.getConfigDirectory(); + } + + @Override + public void executeAsync(Runnable task) { + this.scheduler.execute(task); + } + + @Override + public ThreadDumper getDefaultThreadDumper() { + return new ThreadDumper.Specific(new long[]{Thread.currentThread().getId()}); + } +} |
