diff options
author | Luck <git@lucko.me> | 2022-06-09 21:21:29 +0100 |
---|---|---|
committer | Luck <git@lucko.me> | 2022-06-09 21:21:29 +0100 |
commit | 0ac8713eaaefe7336db2e0369bbe547dc6c0da7d (patch) | |
tree | 07293873c21d89ef4b2035be56c00d2efa624a45 /spark-fabric/src | |
parent | f051a29659c659d9b491bced1acf3cac59074f88 (diff) | |
download | spark-0ac8713eaaefe7336db2e0369bbe547dc6c0da7d.tar.gz spark-0ac8713eaaefe7336db2e0369bbe547dc6c0da7d.tar.bz2 spark-0ac8713eaaefe7336db2e0369bbe547dc6c0da7d.zip |
1.19
Diffstat (limited to 'spark-fabric/src')
10 files changed, 40 insertions, 181 deletions
diff --git a/spark-fabric/src/main/java/me/lucko/spark/fabric/FabricCommandSender.java b/spark-fabric/src/main/java/me/lucko/spark/fabric/FabricCommandSender.java index 14b3442..2138dbe 100644 --- a/spark-fabric/src/main/java/me/lucko/spark/fabric/FabricCommandSender.java +++ b/spark-fabric/src/main/java/me/lucko/spark/fabric/FabricCommandSender.java @@ -34,8 +34,6 @@ import net.minecraft.text.Text; import java.util.UUID; public class FabricCommandSender extends AbstractCommandSender<CommandOutput> { - private static final UUID NIL_UUID = new UUID(0, 0); - private final FabricSparkPlugin plugin; public FabricCommandSender(CommandOutput commandOutput, FabricSparkPlugin plugin) { @@ -67,7 +65,7 @@ public class FabricCommandSender extends AbstractCommandSender<CommandOutput> { @Override public void sendMessage(Component message) { Text component = Text.Serializer.fromJson(GsonComponentSerializer.gson().serialize(message)); - super.delegate.sendSystemMessage(component, NIL_UUID); + super.delegate.sendMessage(component); } @Override diff --git a/spark-fabric/src/main/java/me/lucko/spark/fabric/FabricPlatformInfo.java b/spark-fabric/src/main/java/me/lucko/spark/fabric/FabricPlatformInfo.java index 392d173..e298121 100644 --- a/spark-fabric/src/main/java/me/lucko/spark/fabric/FabricPlatformInfo.java +++ b/spark-fabric/src/main/java/me/lucko/spark/fabric/FabricPlatformInfo.java @@ -35,7 +35,7 @@ public class FabricPlatformInfo implements PlatformInfo { @Override public Type getType() { - return type; + return this.type; } @Override diff --git a/spark-fabric/src/main/java/me/lucko/spark/fabric/FabricSparkGameHooks.java b/spark-fabric/src/main/java/me/lucko/spark/fabric/FabricSparkGameHooks.java deleted file mode 100644 index 5eec53d..0000000 --- a/spark-fabric/src/main/java/me/lucko/spark/fabric/FabricSparkGameHooks.java +++ /dev/null @@ -1,40 +0,0 @@ -/* - * 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; - -import java.util.function.Predicate; - -public enum FabricSparkGameHooks { - INSTANCE; - - // Use events from Fabric API later - // Return true to abort sending to server - private Predicate<String> chatSendCallback = s -> false; - - public void setChatSendCallback(Predicate<String> callback) { - this.chatSendCallback = callback; - } - - public boolean tryProcessChat(String message) { - return this.chatSendCallback.test(message); - } - -} diff --git a/spark-fabric/src/main/java/me/lucko/spark/fabric/FabricSparkMod.java b/spark-fabric/src/main/java/me/lucko/spark/fabric/FabricSparkMod.java index fdb359e..ad419f7 100644 --- a/spark-fabric/src/main/java/me/lucko/spark/fabric/FabricSparkMod.java +++ b/spark-fabric/src/main/java/me/lucko/spark/fabric/FabricSparkMod.java @@ -26,12 +26,14 @@ import me.lucko.spark.fabric.plugin.FabricClientSparkPlugin; import me.lucko.spark.fabric.plugin.FabricServerSparkPlugin; import net.fabricmc.api.ModInitializer; -import net.fabricmc.fabric.api.command.v1.CommandRegistrationCallback; +import net.fabricmc.fabric.api.command.v2.CommandRegistrationCallback; import net.fabricmc.fabric.api.event.lifecycle.v1.ServerLifecycleEvents; import net.fabricmc.loader.api.FabricLoader; import net.fabricmc.loader.api.ModContainer; import net.minecraft.client.MinecraftClient; +import net.minecraft.command.CommandRegistryAccess; import net.minecraft.server.MinecraftServer; +import net.minecraft.server.command.CommandManager.RegistrationEnvironment; import net.minecraft.server.command.ServerCommandSource; import java.nio.file.Path; @@ -54,20 +56,19 @@ public class FabricSparkMod implements ModInitializer { .orElseThrow(() -> new IllegalStateException("Unable to get container for spark")); this.configDirectory = loader.getConfigDir().resolve("spark"); - // lifecycle hooks + // server event hooks ServerLifecycleEvents.SERVER_STARTING.register(this::initializeServer); ServerLifecycleEvents.SERVER_STOPPING.register(this::onServerStopping); - - // events to propagate to active server plugin - CommandRegistrationCallback.EVENT.register(this::onCommandRegister); + CommandRegistrationCallback.EVENT.register(this::onServerCommandRegister); } - // called be entrypoint defined in fabric.mod.json + // client (called be entrypoint defined in fabric.mod.json) public static void initializeClient() { Objects.requireNonNull(FabricSparkMod.mod, "mod"); FabricClientSparkPlugin.register(FabricSparkMod.mod, MinecraftClient.getInstance()); } + // server public void initializeServer(MinecraftServer server) { this.activeServerPlugin = FabricServerSparkPlugin.register(this, server); } @@ -79,7 +80,7 @@ public class FabricSparkMod implements ModInitializer { } } - public void onCommandRegister(CommandDispatcher<ServerCommandSource> dispatcher, boolean isDedicated) { + public void onServerCommandRegister(CommandDispatcher<ServerCommandSource> dispatcher, CommandRegistryAccess access, RegistrationEnvironment env) { if (this.activeServerPlugin != null) { this.activeServerPlugin.registerCommands(dispatcher); } diff --git a/spark-fabric/src/main/java/me/lucko/spark/fabric/mixin/ClientPlayerEntityMixin.java b/spark-fabric/src/main/java/me/lucko/spark/fabric/mixin/ClientPlayerEntityMixin.java deleted file mode 100644 index 495b213..0000000 --- a/spark-fabric/src/main/java/me/lucko/spark/fabric/mixin/ClientPlayerEntityMixin.java +++ /dev/null @@ -1,52 +0,0 @@ -/* - * 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.mixin; - -import com.mojang.authlib.GameProfile; - -import me.lucko.spark.fabric.FabricSparkGameHooks; - -import net.minecraft.client.network.AbstractClientPlayerEntity; -import net.minecraft.client.network.ClientPlayerEntity; -import net.minecraft.client.world.ClientWorld; - -import org.spongepowered.asm.mixin.Mixin; -import org.spongepowered.asm.mixin.injection.At; -import org.spongepowered.asm.mixin.injection.Inject; -import org.spongepowered.asm.mixin.injection.callback.CallbackInfo; -import org.spongepowered.asm.mixin.injection.callback.LocalCapture; - -@Mixin(ClientPlayerEntity.class) -public abstract class ClientPlayerEntityMixin extends AbstractClientPlayerEntity { - - public ClientPlayerEntityMixin(ClientWorld clientWorld_1, GameProfile gameProfile_1) { - super(clientWorld_1, gameProfile_1); - } - - @Inject(method = "sendChatMessage(Ljava/lang/String;)V", at = @At("HEAD"), - locals = LocalCapture.CAPTURE_FAILHARD, - cancellable = true) - public void onSendChatMessage(String message, CallbackInfo ci) { - if (FabricSparkGameHooks.INSTANCE.tryProcessChat(message)) { - ci.cancel(); - } - } -} 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 index c173a0b..e94d697 100644 --- 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 @@ -33,24 +33,21 @@ import me.lucko.spark.common.tick.TickHook; import me.lucko.spark.common.tick.TickReporter; import me.lucko.spark.fabric.FabricCommandSender; import me.lucko.spark.fabric.FabricPlatformInfo; -import me.lucko.spark.fabric.FabricSparkGameHooks; import me.lucko.spark.fabric.FabricSparkMod; import me.lucko.spark.fabric.FabricTickHook; import me.lucko.spark.fabric.FabricTickReporter; +import net.fabricmc.fabric.api.client.command.v2.ClientCommandRegistrationCallback; +import net.fabricmc.fabric.api.client.command.v2.FabricClientCommandSource; import net.fabricmc.fabric.api.client.event.lifecycle.v1.ClientLifecycleEvents; import net.minecraft.client.MinecraftClient; -import net.minecraft.client.network.ClientPlayNetworkHandler; -import net.minecraft.command.CommandSource; +import net.minecraft.command.CommandRegistryAccess; import net.minecraft.server.command.CommandOutput; -import java.util.Arrays; -import java.util.Optional; import java.util.concurrent.CompletableFuture; -import java.util.concurrent.TimeUnit; import java.util.stream.Stream; -public class FabricClientSparkPlugin extends FabricSparkPlugin implements SuggestionProvider<CommandSource> { +public class FabricClientSparkPlugin extends FabricSparkPlugin implements Command<FabricClientCommandSource>, SuggestionProvider<FabricClientCommandSource> { public static void register(FabricSparkMod mod, MinecraftClient client) { FabricClientSparkPlugin plugin = new FabricClientSparkPlugin(mod, client); @@ -58,7 +55,6 @@ public class FabricClientSparkPlugin extends FabricSparkPlugin implements Sugges } private final MinecraftClient minecraft; - private CommandDispatcher<CommandSource> dispatcher; public FabricClientSparkPlugin(FabricSparkMod mod, MinecraftClient minecraft) { super(mod); @@ -69,11 +65,9 @@ public class FabricClientSparkPlugin extends FabricSparkPlugin implements Sugges public void enable() { super.enable(); - // ensure commands are registered - this.scheduler.scheduleWithFixedDelay(this::checkCommandRegistered, 10, 10, TimeUnit.SECONDS); - // events ClientLifecycleEvents.CLIENT_STOPPING.register(this::onDisable); + ClientCommandRegistrationCallback.EVENT.register(this::onCommandRegister); } private void onDisable(MinecraftClient stoppingClient) { @@ -82,59 +76,30 @@ public class FabricClientSparkPlugin extends FabricSparkPlugin implements Sugges } } - private CommandDispatcher<CommandSource> getPlayerCommandDispatcher() { - return Optional.ofNullable(this.minecraft.player) - .map(player -> player.networkHandler) - .map(ClientPlayNetworkHandler::getCommandDispatcher) - .orElse(null); - } - - private void checkCommandRegistered() { - CommandDispatcher<CommandSource> dispatcher = getPlayerCommandDispatcher(); - if (dispatcher == null) { - return; - } - - try { - if (dispatcher != this.dispatcher) { - this.dispatcher = dispatcher; - registerCommands(this.dispatcher, c -> Command.SINGLE_SUCCESS, this, "sparkc", "sparkclient"); - FabricSparkGameHooks.INSTANCE.setChatSendCallback(this::onClientChat); - } - } catch (Exception e) { - e.printStackTrace(); - } + public void onCommandRegister(CommandDispatcher<FabricClientCommandSource> dispatcher, CommandRegistryAccess registryAccess) { + registerCommands(dispatcher, this, this, "sparkc", "sparkclient"); } - public boolean onClientChat(String chat) { - String[] args = processArgs(chat, false); + @Override + public int run(CommandContext<FabricClientCommandSource> context) throws CommandSyntaxException { + String[] args = processArgs(context, false, "sparkc", "sparkclient"); if (args == null) { - return false; + return 0; } this.threadDumper.ensureSetup(); - this.platform.executeCommand(new FabricCommandSender(this.minecraft.player, this), args); - this.minecraft.inGameHud.getChatHud().addToMessageHistory(chat); - return true; + this.platform.executeCommand(new FabricCommandSender(context.getSource().getEntity(), this), args); + return Command.SINGLE_SUCCESS; } @Override - public CompletableFuture<Suggestions> getSuggestions(CommandContext<CommandSource> context, SuggestionsBuilder builder) throws CommandSyntaxException { - String[] args = processArgs(context.getInput(), true); + public CompletableFuture<Suggestions> getSuggestions(CommandContext<FabricClientCommandSource> context, SuggestionsBuilder builder) throws CommandSyntaxException { + String[] args = processArgs(context, true, "/sparkc", "/sparkclient"); if (args == null) { return Suggestions.empty(); } - return generateSuggestions(new FabricCommandSender(this.minecraft.player, this), args, builder); - } - - private static String[] processArgs(String input, boolean tabComplete) { - String[] split = input.split(" ", tabComplete ? -1 : 0); - if (split.length == 0 || !split[0].equals("/sparkc") && !split[0].equals("/sparkclient")) { - return null; - } - - return Arrays.copyOfRange(split, 1, split.length); + return generateSuggestions(new FabricCommandSender(context.getSource().getEntity(), this), args, builder); } @Override 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 index 6dc5483..428ac4c 100644 --- 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 @@ -47,7 +47,6 @@ import net.minecraft.server.MinecraftServer; import net.minecraft.server.command.CommandOutput; import net.minecraft.server.command.ServerCommandSource; -import java.util.Arrays; import java.util.concurrent.CompletableFuture; import java.util.stream.Stream; @@ -85,7 +84,7 @@ public class FabricServerSparkPlugin extends FabricSparkPlugin implements Comman @Override public int run(CommandContext<ServerCommandSource> context) throws CommandSyntaxException { - String[] args = processArgs(context, false); + String[] args = processArgs(context, false, "/spark", "spark"); if (args == null) { return 0; } @@ -98,7 +97,7 @@ public class FabricServerSparkPlugin extends FabricSparkPlugin implements Comman @Override public CompletableFuture<Suggestions> getSuggestions(CommandContext<ServerCommandSource> context, SuggestionsBuilder builder) throws CommandSyntaxException { - String[] args = processArgs(context, true); + String[] args = processArgs(context, true, "/spark", "spark"); if (args == null) { return Suggestions.empty(); } @@ -106,15 +105,6 @@ public class FabricServerSparkPlugin extends FabricSparkPlugin implements Comman return generateSuggestions(new FabricCommandSender(context.getSource().getPlayer(), this), args, builder); } - private static String[] processArgs(CommandContext<ServerCommandSource> 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); - } - @Override public boolean hasPermission(CommandOutput sender, String permission) { if (sender instanceof PlayerEntity) { 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 index 7d0a989..b1392d4 100644 --- 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 @@ -25,6 +25,7 @@ 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.context.CommandContext; import com.mojang.brigadier.suggestion.SuggestionProvider; import com.mojang.brigadier.suggestion.Suggestions; import com.mojang.brigadier.suggestion.SuggestionsBuilder; @@ -45,6 +46,7 @@ import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.Logger; import java.nio.file.Path; +import java.util.Arrays; import java.util.concurrent.CompletableFuture; import java.util.concurrent.Executors; import java.util.concurrent.ScheduledExecutorService; @@ -152,4 +154,13 @@ public abstract class FabricSparkPlugin implements SparkPlugin { } } + protected static String[] processArgs(CommandContext<?> context, boolean tabComplete, String... aliases) { + String[] split = context.getInput().split(" ", tabComplete ? -1 : 0); + if (split.length == 0 || !Arrays.asList(aliases).contains(split[0])) { + return null; + } + + return Arrays.copyOfRange(split, 1, split.length); + } + } diff --git a/spark-fabric/src/main/resources/fabric.mod.json b/spark-fabric/src/main/resources/fabric.mod.json index a9e15e0..e2e600d 100644 --- a/spark-fabric/src/main/resources/fabric.mod.json +++ b/spark-fabric/src/main/resources/fabric.mod.json @@ -23,13 +23,10 @@ "me.lucko.spark.fabric.FabricSparkMod::initializeClient" ] }, - "mixins": [ - "spark.mixins.json" - ], "depends": { "fabricloader": ">=0.4.0", "fabric-api-base": "*", - "fabric-command-api-v1": "*", + "fabric-command-api-v2": "*", "fabric-lifecycle-events-v1" : "*" } } diff --git a/spark-fabric/src/main/resources/spark.mixins.json b/spark-fabric/src/main/resources/spark.mixins.json deleted file mode 100644 index 3f495ea..0000000 --- a/spark-fabric/src/main/resources/spark.mixins.json +++ /dev/null @@ -1,11 +0,0 @@ -{ - "required": true, - "package": "me.lucko.spark.fabric.mixin", - "compatibilityLevel": "JAVA_8", - "client": [ - "ClientPlayerEntityMixin" - ], - "injectors": { - "defaultRequire": 1 - } -} |