aboutsummaryrefslogtreecommitdiff
path: root/spark-forge/src/main/java
diff options
context:
space:
mode:
authorLuck <git@lucko.me>2022-06-09 21:21:29 +0100
committerLuck <git@lucko.me>2022-06-09 21:21:29 +0100
commit0ac8713eaaefe7336db2e0369bbe547dc6c0da7d (patch)
tree07293873c21d89ef4b2035be56c00d2efa624a45 /spark-forge/src/main/java
parentf051a29659c659d9b491bced1acf3cac59074f88 (diff)
downloadspark-0ac8713eaaefe7336db2e0369bbe547dc6c0da7d.tar.gz
spark-0ac8713eaaefe7336db2e0369bbe547dc6c0da7d.tar.bz2
spark-0ac8713eaaefe7336db2e0369bbe547dc6c0da7d.zip
1.19
Diffstat (limited to 'spark-forge/src/main/java')
-rw-r--r--spark-forge/src/main/java/me/lucko/spark/forge/ForgeCommandSender.java9
-rw-r--r--spark-forge/src/main/java/me/lucko/spark/forge/plugin/ForgeClientSparkPlugin.java68
-rw-r--r--spark-forge/src/main/java/me/lucko/spark/forge/plugin/ForgeServerSparkPlugin.java17
-rw-r--r--spark-forge/src/main/java/me/lucko/spark/forge/plugin/ForgeSparkPlugin.java49
4 files changed, 54 insertions, 89 deletions
diff --git a/spark-forge/src/main/java/me/lucko/spark/forge/ForgeCommandSender.java b/spark-forge/src/main/java/me/lucko/spark/forge/ForgeCommandSender.java
index 9ca6f03..f3b746d 100644
--- a/spark-forge/src/main/java/me/lucko/spark/forge/ForgeCommandSender.java
+++ b/spark-forge/src/main/java/me/lucko/spark/forge/ForgeCommandSender.java
@@ -25,14 +25,14 @@ import me.lucko.spark.forge.plugin.ForgeSparkPlugin;
import net.kyori.adventure.text.Component;
import net.kyori.adventure.text.serializer.gson.GsonComponentSerializer;
-import net.minecraft.Util;
import net.minecraft.commands.CommandSource;
+import net.minecraft.network.chat.Component.Serializer;
import net.minecraft.network.chat.MutableComponent;
-import net.minecraft.network.chat.TextComponent;
import net.minecraft.server.MinecraftServer;
import net.minecraft.server.rcon.RconConsoleSource;
import net.minecraft.world.entity.player.Player;
+import java.util.Objects;
import java.util.UUID;
public class ForgeCommandSender extends AbstractCommandSender<CommandSource> {
@@ -66,8 +66,9 @@ public class ForgeCommandSender extends AbstractCommandSender<CommandSource> {
@Override
public void sendMessage(Component message) {
- MutableComponent component = TextComponent.Serializer.fromJson(GsonComponentSerializer.gson().serialize(message));
- super.delegate.sendMessage(component, Util.NIL_UUID);
+ MutableComponent component = Serializer.fromJson(GsonComponentSerializer.gson().serialize(message));
+ Objects.requireNonNull(component, "component");
+ super.delegate.sendSystemMessage(component);
}
@Override
diff --git a/spark-forge/src/main/java/me/lucko/spark/forge/plugin/ForgeClientSparkPlugin.java b/spark-forge/src/main/java/me/lucko/spark/forge/plugin/ForgeClientSparkPlugin.java
index 4d1bebe..cf5c89b 100644
--- a/spark-forge/src/main/java/me/lucko/spark/forge/plugin/ForgeClientSparkPlugin.java
+++ b/spark-forge/src/main/java/me/lucko/spark/forge/plugin/ForgeClientSparkPlugin.java
@@ -21,7 +21,6 @@
package me.lucko.spark.forge.plugin;
import com.mojang.brigadier.Command;
-import com.mojang.brigadier.CommandDispatcher;
import com.mojang.brigadier.context.CommandContext;
import com.mojang.brigadier.exceptions.CommandSyntaxException;
import com.mojang.brigadier.suggestion.SuggestionProvider;
@@ -38,22 +37,18 @@ import me.lucko.spark.forge.ForgeTickHook;
import me.lucko.spark.forge.ForgeTickReporter;
import net.minecraft.client.Minecraft;
-import net.minecraft.client.multiplayer.ClientPacketListener;
import net.minecraft.commands.CommandSource;
-import net.minecraft.commands.SharedSuggestionProvider;
-import net.minecraftforge.client.event.ClientChatEvent;
+import net.minecraft.commands.CommandSourceStack;
+import net.minecraftforge.client.event.RegisterClientCommandsEvent;
import net.minecraftforge.common.MinecraftForge;
import net.minecraftforge.event.TickEvent;
import net.minecraftforge.eventbus.api.SubscribeEvent;
import net.minecraftforge.fml.event.lifecycle.FMLClientSetupEvent;
-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 ForgeClientSparkPlugin extends ForgeSparkPlugin implements SuggestionProvider<SharedSuggestionProvider> {
+public class ForgeClientSparkPlugin extends ForgeSparkPlugin implements Command<CommandSourceStack>, SuggestionProvider<CommandSourceStack> {
public static void register(ForgeSparkMod mod, FMLClientSetupEvent event) {
ForgeClientSparkPlugin plugin = new ForgeClientSparkPlugin(mod, Minecraft.getInstance());
@@ -61,7 +56,6 @@ public class ForgeClientSparkPlugin extends ForgeSparkPlugin implements Suggesti
}
private final Minecraft minecraft;
- private CommandDispatcher<SharedSuggestionProvider> dispatcher;
public ForgeClientSparkPlugin(ForgeSparkMod mod, Minecraft minecraft) {
super(mod);
@@ -72,66 +66,35 @@ public class ForgeClientSparkPlugin extends ForgeSparkPlugin implements Suggesti
public void enable() {
super.enable();
- // ensure commands are registered
- this.scheduler.scheduleWithFixedDelay(this::checkCommandRegistered, 10, 10, TimeUnit.SECONDS);
-
// register listeners
MinecraftForge.EVENT_BUS.register(this);
}
- private CommandDispatcher<SharedSuggestionProvider> getPlayerCommandDispatcher() {
- return Optional.ofNullable(this.minecraft.player)
- .map(player -> player.connection)
- .map(ClientPacketListener::getCommands)
- .orElse(null);
- }
-
- private void checkCommandRegistered() {
- CommandDispatcher<SharedSuggestionProvider> dispatcher = getPlayerCommandDispatcher();
- if (dispatcher == null) {
- return;
- }
-
- try {
- if (dispatcher != this.dispatcher) {
- this.dispatcher = dispatcher;
- registerCommands(this.dispatcher, context -> Command.SINGLE_SUCCESS, this, "sparkc", "sparkclient");
- }
- } catch (Exception e) {
- e.printStackTrace();
- }
+ @SubscribeEvent
+ public void onCommandRegister(RegisterClientCommandsEvent e) {
+ registerCommands(e.getDispatcher(), this, this, "sparkc", "sparkclient");
}
- @SubscribeEvent
- public void onClientChat(ClientChatEvent event) {
- String[] args = processArgs(event.getMessage(), false);
+ @Override
+ public int run(CommandContext<CommandSourceStack> context) throws CommandSyntaxException {
+ String[] args = processArgs(context, false, "sparkc", "sparkclient");
if (args == null) {
- return;
+ return 0;
}
this.threadDumper.ensureSetup();
- this.platform.executeCommand(new ForgeCommandSender(this.minecraft.player, this), args);
- this.minecraft.gui.getChat().addRecentChat(event.getMessage());
- event.setCanceled(true);
+ this.platform.executeCommand(new ForgeCommandSender(context.getSource().getEntity(), this), args);
+ return Command.SINGLE_SUCCESS;
}
@Override
- public CompletableFuture<Suggestions> getSuggestions(CommandContext<SharedSuggestionProvider> context, SuggestionsBuilder builder) throws CommandSyntaxException {
- String[] args = processArgs(context.getInput(), true);
+ public CompletableFuture<Suggestions> getSuggestions(CommandContext<CommandSourceStack> context, SuggestionsBuilder builder) throws CommandSyntaxException {
+ String[] args = processArgs(context, true, "/sparkc", "/sparkclient");
if (args == null) {
return Suggestions.empty();
}
- return generateSuggestions(new ForgeCommandSender(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 ForgeCommandSender(context.getSource().getEntity(), this), args, builder);
}
@Override
@@ -163,5 +126,4 @@ public class ForgeClientSparkPlugin extends ForgeSparkPlugin implements Suggesti
public String getCommandName() {
return "sparkc";
}
-
}
diff --git a/spark-forge/src/main/java/me/lucko/spark/forge/plugin/ForgeServerSparkPlugin.java b/spark-forge/src/main/java/me/lucko/spark/forge/plugin/ForgeServerSparkPlugin.java
index bbfb8a5..e341d6f 100644
--- a/spark-forge/src/main/java/me/lucko/spark/forge/plugin/ForgeServerSparkPlugin.java
+++ b/spark-forge/src/main/java/me/lucko/spark/forge/plugin/ForgeServerSparkPlugin.java
@@ -56,7 +56,6 @@ import net.minecraftforge.server.permission.nodes.PermissionNode;
import net.minecraftforge.server.permission.nodes.PermissionNode.PermissionResolver;
import net.minecraftforge.server.permission.nodes.PermissionTypes;
-import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.Map;
@@ -138,19 +137,20 @@ public class ForgeServerSparkPlugin extends ForgeSparkPlugin implements Command<
@Override
public int run(CommandContext<CommandSourceStack> context) throws CommandSyntaxException {
- String[] args = processArgs(context, false);
+ String[] args = processArgs(context, false, "/spark", "spark");
if (args == null) {
return 0;
}
this.threadDumper.ensureSetup();
- this.platform.executeCommand(new ForgeCommandSender(context.getSource().source, this), args);
+ CommandSource source = context.getSource().getEntity() != null ? context.getSource().getEntity() : context.getSource().getServer();
+ this.platform.executeCommand(new ForgeCommandSender(source, this), args);
return Command.SINGLE_SUCCESS;
}
@Override
public CompletableFuture<Suggestions> getSuggestions(CommandContext<CommandSourceStack> context, SuggestionsBuilder builder) throws CommandSyntaxException {
- String[] args = processArgs(context, true);
+ String[] args = processArgs(context, true, "/spark", "spark");
if (args == null) {
return Suggestions.empty();
}
@@ -158,15 +158,6 @@ public class ForgeServerSparkPlugin extends ForgeSparkPlugin implements Command<
return generateSuggestions(new ForgeCommandSender(context.getSource().getPlayerOrException(), this), args, builder);
}
- private static String [] processArgs(CommandContext<CommandSourceStack> 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(CommandSource sender, String permission) {
if (sender instanceof ServerPlayer) {
diff --git a/spark-forge/src/main/java/me/lucko/spark/forge/plugin/ForgeSparkPlugin.java b/spark-forge/src/main/java/me/lucko/spark/forge/plugin/ForgeSparkPlugin.java
index 022b620..f257e34 100644
--- a/spark-forge/src/main/java/me/lucko/spark/forge/plugin/ForgeSparkPlugin.java
+++ b/spark-forge/src/main/java/me/lucko/spark/forge/plugin/ForgeSparkPlugin.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;
@@ -52,25 +54,6 @@ import java.util.logging.Level;
public abstract class ForgeSparkPlugin implements SparkPlugin {
- public static <T> void registerCommands(CommandDispatcher<T> dispatcher, Command<T> executor, SuggestionProvider<T> suggestor, 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())
- .suggests(suggestor)
- .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 ForgeSparkMod mod;
private final Logger logger;
protected final ScheduledExecutorService scheduler;
@@ -151,4 +134,32 @@ public abstract class ForgeSparkPlugin implements SparkPlugin {
return suggestions.build();
});
}
+
+ protected static <T> void registerCommands(CommandDispatcher<T> dispatcher, Command<T> executor, SuggestionProvider<T> suggestor, 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())
+ .suggests(suggestor)
+ .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));
+ }
+ }
+
+ 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);
+ }
}