mirror of
https://github.com/MinecraftForge/MinecraftForge.git
synced 2026-11-18 11:29:15 +00:00
1.21.3 update
This commit is contained in:
806 files changed
+6314
-10501
No files matched your search
@@ -18,6 +18,7 @@ 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.CommandSource;
|
||||
import net.minecraft.commands.CommandSourceStack;
|
||||
import net.minecraft.commands.SharedSuggestionProvider;
|
||||
import net.minecraft.commands.synchronization.SuggestionProviders;
|
||||
@@ -33,18 +34,15 @@ import org.jetbrains.annotations.ApiStatus;
|
||||
import java.util.IdentityHashMap;
|
||||
import java.util.Map;
|
||||
|
||||
public class ClientCommandHandler
|
||||
{
|
||||
public class ClientCommandHandler {
|
||||
private static final Logger LOGGER = LogManager.getLogger();
|
||||
private static CommandDispatcher<CommandSourceStack> commands = null;
|
||||
|
||||
public static void init()
|
||||
{
|
||||
public static void init() {
|
||||
MinecraftForge.EVENT_BUS.addListener(ClientCommandHandler::handleClientPlayerLogin);
|
||||
}
|
||||
|
||||
private static void handleClientPlayerLogin(ClientPlayerNetworkEvent.LoggingIn event)
|
||||
{
|
||||
private static void handleClientPlayerLogin(ClientPlayerNetworkEvent.LoggingIn event) {
|
||||
final ClientPacketListener connection = event.getPlayer().connection;
|
||||
// Some custom server implementations do not send ClientboundCommandsPacket, so we provide a fallback:
|
||||
// Must set this, so that suggestions for client-only commands work, if server never sends commands packet
|
||||
@@ -58,8 +56,7 @@ public class ClientCommandHandler
|
||||
* with server commands in suggestions
|
||||
*/
|
||||
@ApiStatus.Internal
|
||||
public static CommandDispatcher<SharedSuggestionProvider> mergeServerCommands(CommandDispatcher<SharedSuggestionProvider> serverCommands, CommandBuildContext buildContext)
|
||||
{
|
||||
public static CommandDispatcher<SharedSuggestionProvider> mergeServerCommands(CommandDispatcher<SharedSuggestionProvider> serverCommands, CommandBuildContext buildContext) {
|
||||
CommandDispatcher<CommandSourceStack> commandsTemp = new CommandDispatcher<>();
|
||||
MinecraftForge.EVENT_BUS.post(new RegisterClientCommandsEvent(commandsTemp, buildContext));
|
||||
|
||||
@@ -74,17 +71,15 @@ public class ClientCommandHandler
|
||||
|
||||
// Copies the client side commands into the server side commands to be used for suggestions
|
||||
CommandHelper.mergeCommandNode(commands.getRoot(), newServerCommands.getRoot(), new IdentityHashMap<>(), getSource(), (context) -> 0, (suggestions) -> {
|
||||
SuggestionProvider<SharedSuggestionProvider> suggestionProvider = SuggestionProviders
|
||||
.safelySwap((SuggestionProvider<SharedSuggestionProvider>) (SuggestionProvider<?>) suggestions);
|
||||
if (suggestionProvider == SuggestionProviders.ASK_SERVER)
|
||||
{
|
||||
@SuppressWarnings("unchecked")
|
||||
var shared = (SuggestionProvider<SharedSuggestionProvider>)(SuggestionProvider<?>)suggestions;
|
||||
var suggestionProvider = SuggestionProviders.safelySwap(shared);
|
||||
if (suggestionProvider == SuggestionProviders.ASK_SERVER) {
|
||||
suggestionProvider = (context, builder) -> {
|
||||
ClientCommandSourceStack source = getSource();
|
||||
StringReader reader = new StringReader(context.getInput());
|
||||
if (reader.canRead() && reader.peek() == '/')
|
||||
{
|
||||
reader.skip();
|
||||
}
|
||||
|
||||
ParseResults<CommandSourceStack> parse = commands.parse(reader, source);
|
||||
return commands.getCompletionSuggestions(parse);
|
||||
@@ -99,19 +94,45 @@ public class ClientCommandHandler
|
||||
/**
|
||||
* @return The command dispatcher for client side commands
|
||||
*/
|
||||
public static CommandDispatcher<CommandSourceStack> getDispatcher()
|
||||
{
|
||||
public static CommandDispatcher<CommandSourceStack> getDispatcher() {
|
||||
return commands;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return A {@link ClientCommandSourceStack} for the player in the current client
|
||||
*/
|
||||
public static ClientCommandSourceStack getSource()
|
||||
{
|
||||
LocalPlayer player = Minecraft.getInstance().player;
|
||||
return new ClientCommandSourceStack(player, player.position(), player.getRotationVector(), player.getPermissionLevel(),
|
||||
player.getName().getString(), player.getDisplayName(), player);
|
||||
public static ClientCommandSourceStack getSource() {
|
||||
var mc = Minecraft.getInstance();
|
||||
LocalPlayer player = mc.player;
|
||||
return new ClientCommandSourceStack(
|
||||
new CommandSource() {
|
||||
@Override
|
||||
public void sendSystemMessage(Component message) {
|
||||
mc.gui.getChat().addMessage(message);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean acceptsSuccess() {
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean acceptsFailure() {
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean shouldInformAdmins() {
|
||||
return true;
|
||||
}
|
||||
},
|
||||
player.position(),
|
||||
player.getRotationVector(),
|
||||
player.getPermissionLevel(),
|
||||
player.getName().getString(),
|
||||
player.getDisplayName(),
|
||||
player
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -149,51 +170,49 @@ public class ClientCommandHandler
|
||||
* @param command the full command to execute, no preceding slash
|
||||
* @return {@code false} leaves the message to be sent to the server, while {@code true} means it should be caught before LocalPlayer#sendCommand
|
||||
*/
|
||||
public static boolean runCommand(String command)
|
||||
{
|
||||
public static boolean runCommand(String command) {
|
||||
StringReader reader = new StringReader(command);
|
||||
|
||||
ClientCommandSourceStack source = getSource();
|
||||
var mc = Minecraft.getInstance();
|
||||
|
||||
try {
|
||||
commands.execute(reader, source);
|
||||
//} catch (CommandRuntimeException execution) { // Probably thrown by the command
|
||||
// Minecraft.getInstance().player.sendSystemMessage(Component.literal("").append(execution.getComponent()).withStyle(ChatFormatting.RED));
|
||||
} catch (CommandSyntaxException syntax) {
|
||||
if (syntax.getType() == CommandSyntaxException.BUILT_IN_EXCEPTIONS.dispatcherUnknownCommand() || syntax.getType() == CommandSyntaxException.BUILT_IN_EXCEPTIONS.dispatcherUnknownArgument())
|
||||
{
|
||||
if (syntax.getType() == CommandSyntaxException.BUILT_IN_EXCEPTIONS.dispatcherUnknownCommand() ||
|
||||
syntax.getType() == CommandSyntaxException.BUILT_IN_EXCEPTIONS.dispatcherUnknownArgument()
|
||||
) {
|
||||
// in case of unknown command, let the server try and handle it
|
||||
return false;
|
||||
}
|
||||
Minecraft.getInstance().player.sendSystemMessage(
|
||||
Component.literal("").append(ComponentUtils.fromMessage(syntax.getRawMessage())).withStyle(ChatFormatting.RED));
|
||||
if (syntax.getInput() != null && syntax.getCursor() >= 0)
|
||||
{
|
||||
mc.gui.getChat().addMessage(
|
||||
Component.literal("").append(ComponentUtils.fromMessage(syntax.getRawMessage())).withStyle(ChatFormatting.RED)
|
||||
);
|
||||
if (syntax.getInput() != null && syntax.getCursor() >= 0) {
|
||||
int position = Math.min(syntax.getInput().length(), syntax.getCursor());
|
||||
MutableComponent details = Component.literal("")
|
||||
.withStyle(ChatFormatting.GRAY)
|
||||
.withStyle((style) -> style
|
||||
.withClickEvent(new ClickEvent(ClickEvent.Action.SUGGEST_COMMAND, reader.getString())));
|
||||
if (position > 10)
|
||||
{
|
||||
details.append("...");
|
||||
}
|
||||
|
||||
details.append(syntax.getInput().substring(Math.max(0, position - 10), position));
|
||||
if (position < syntax.getInput().length())
|
||||
{
|
||||
details.append(Component.literal(syntax.getInput().substring(position)).withStyle(ChatFormatting.RED, ChatFormatting.UNDERLINE));
|
||||
}
|
||||
|
||||
details.append(Component.translatable("command.context.here").withStyle(ChatFormatting.RED, ChatFormatting.ITALIC));
|
||||
Minecraft.getInstance().player.sendSystemMessage(Component.literal("").append(details).withStyle(ChatFormatting.RED));
|
||||
mc.gui.getChat().addMessage(Component.literal("").append(details).withStyle(ChatFormatting.RED));
|
||||
}
|
||||
}
|
||||
catch (Exception generic)// Probably thrown by the command
|
||||
{
|
||||
} catch (Exception generic) { // Probably thrown by the command{
|
||||
MutableComponent message = Component.literal(generic.getMessage() == null ? generic.getClass().getName() : generic.getMessage());
|
||||
Minecraft.getInstance().player.sendSystemMessage(Component.translatable("command.failed")
|
||||
mc.gui.getChat().addMessage(
|
||||
Component.translatable("command.failed")
|
||||
.withStyle(ChatFormatting.RED)
|
||||
.withStyle((style) -> style
|
||||
.withHoverEvent(new HoverEvent(HoverEvent.Action.SHOW_TEXT, message))));
|
||||
.withStyle(style -> style.withHoverEvent(new HoverEvent(HoverEvent.Action.SHOW_TEXT, message)))
|
||||
);
|
||||
LOGGER.error("Error executing client command \"{}\"", command, generic);
|
||||
}
|
||||
return true;
|
||||
|
||||
Reference in new issue
Block a user