[1.19.x] Add CommandBuildContext to Register Command Events (#8716)

This commit is contained in:
ChampionAsh5357 authored and GitHub committed 2022-06-12 23:36:25 +01:00
1 parent 597ebc3267
commit 0ec4c34a34
6 files changed
+47 -14

No files matched your search

@@ -17,6 +17,7 @@ import net.minecraft.ChatFormatting;
import net.minecraft.client.Minecraft;
import net.minecraft.client.multiplayer.ClientPacketListener;
import net.minecraft.client.player.LocalPlayer;
import net.minecraft.commands.CommandBuildContext;
import net.minecraft.commands.CommandRuntimeException;
import net.minecraft.commands.CommandSourceStack;
import net.minecraft.commands.SharedSuggestionProvider;
@@ -45,7 +46,7 @@ public class ClientCommandHandler
private static void handleClientPlayerLogin(ClientPlayerNetworkEvent.LoggedInEvent event)
{
// some custom server implementations do not send ClientboundCommandsPacket, provide a fallback
var suggestionDispatcher = mergeServerCommands(new CommandDispatcher<>());
var suggestionDispatcher = mergeServerCommands(new CommandDispatcher<>(), new CommandBuildContext(event.getPlayer().connection.registryAccess()));
if (event.getConnection().getPacketListener() instanceof ClientPacketListener listener)
{
// Must set this, so that suggestions for client-only commands work, if server never sends commands packet
@@ -59,10 +60,10 @@ public class ClientCommandHandler
* Merges command dispatcher use for suggestions to the command dispatcher used for client commands so they can be sent to the server, and vice versa so client commands appear
* with server commands in suggestions
*/
public static CommandDispatcher<SharedSuggestionProvider> mergeServerCommands(CommandDispatcher<SharedSuggestionProvider> serverCommands)
public static CommandDispatcher<SharedSuggestionProvider> mergeServerCommands(CommandDispatcher<SharedSuggestionProvider> serverCommands, CommandBuildContext buildContext)
{
CommandDispatcher<CommandSourceStack> commandsTemp = new CommandDispatcher<>();
MinecraftForge.EVENT_BUS.post(new RegisterClientCommandsEvent(commandsTemp));
MinecraftForge.EVENT_BUS.post(new RegisterClientCommandsEvent(commandsTemp, buildContext));
// Copies the client commands into another RootCommandNode so that redirects can't be used with server commands
commands = new CommandDispatcher<>();
@@ -6,6 +6,7 @@
package net.minecraftforge.client.event;
import com.mojang.brigadier.CommandDispatcher;
import net.minecraft.commands.CommandBuildContext;
import net.minecraft.commands.CommandSourceStack;
import net.minecraft.commands.arguments.ObjectiveArgument;
import net.minecraft.commands.arguments.ResourceLocationArgument;
@@ -36,14 +37,16 @@ public class RegisterClientCommandsEvent extends Event
{
private final CommandDispatcher<CommandSourceStack> dispatcher;
private final CommandBuildContext context;
/**
* @hidden
* @see net.minecraftforge.client.ClientCommandHandler#mergeServerCommands(CommandDispatcher)
* @see net.minecraftforge.client.ClientCommandHandler#mergeServerCommands(CommandDispatcher, CommandBuildContext)
*/
public RegisterClientCommandsEvent(CommandDispatcher<CommandSourceStack> dispatcher)
public RegisterClientCommandsEvent(CommandDispatcher<CommandSourceStack> dispatcher, CommandBuildContext context)
{
this.dispatcher = dispatcher;
this.context = context;
}
/**
@@ -53,4 +56,12 @@ public class RegisterClientCommandsEvent extends Event
{
return dispatcher;
}
/**
* {@return the context to build the commands for}
*/
public CommandBuildContext getBuildContext()
{
return context;
}
}
@@ -13,6 +13,7 @@ import java.util.function.Consumer;
import com.mojang.authlib.GameProfile;
import com.mojang.brigadier.CommandDispatcher;
import net.minecraft.commands.CommandBuildContext;
import net.minecraft.network.chat.ChatSender;
import net.minecraft.server.ReloadableServerResources;
import net.minecraft.server.players.PlayerList;
@@ -661,9 +662,9 @@ public class ForgeEventFactory
return event.getListeners();
}
public static void onCommandRegister(CommandDispatcher<CommandSourceStack> dispatcher, Commands.CommandSelection environment)
public static void onCommandRegister(CommandDispatcher<CommandSourceStack> dispatcher, Commands.CommandSelection environment, CommandBuildContext context)
{
RegisterCommandsEvent event = new RegisterCommandsEvent(dispatcher, environment);
RegisterCommandsEvent event = new RegisterCommandsEvent(dispatcher, environment, context);
MinecraftForge.EVENT_BUS.post(event);
}
@@ -6,6 +6,7 @@
package net.minecraftforge.event;
import com.mojang.brigadier.CommandDispatcher;
import net.minecraft.commands.CommandBuildContext;
import net.minecraft.server.ReloadableServerResources;
import net.minecraftforge.common.MinecraftForge;
import net.minecraftforge.eventbus.api.Event;
@@ -24,20 +25,36 @@ public class RegisterCommandsEvent extends Event
{
private final CommandDispatcher<CommandSourceStack> dispatcher;
private final Commands.CommandSelection environment;
private final CommandBuildContext context;
public RegisterCommandsEvent(CommandDispatcher<CommandSourceStack> dispatcher, Commands.CommandSelection environment)
public RegisterCommandsEvent(CommandDispatcher<CommandSourceStack> dispatcher, Commands.CommandSelection environment, CommandBuildContext context)
{
this.dispatcher = dispatcher;
this.environment = environment;
this.context = context;
}
/**
* {@return the command dispatcher for registering commands to be executed on the client}
*/
public CommandDispatcher<CommandSourceStack> getDispatcher()
{
return dispatcher;
}
/**
* {@return the environment the command is being registered for}
*/
public Commands.CommandSelection getEnvironment()
{
return environment;
}
/**
* {@return the context to build the commands for}
*/
public CommandBuildContext getBuildContext()
{
return context;
}
}