Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix wrong redirect behavior (MC-256419) #124

Merged
merged 2 commits into from
Mar 30, 2023
Merged

Fix wrong redirect behavior (MC-256419) #124

merged 2 commits into from
Mar 30, 2023

Conversation

zly2006
Copy link
Contributor

@zly2006 zly2006 commented Dec 24, 2022

Overview

Brigadier set the variable foundCommand to true HERE even if no Command<S> was executed, and this cause a serious problem.

An example to this is in minecraft: if you execute the command /xp query Steve, you will not receive an error message from the server. This is because that xp is redirected from experience, and when brigadier found this command is redirected, it set foundCommand to true--even if nothing is actually executed.

Ways to fix

I think the best way is to remove this wrong assignment. I tried in fabric to modify this code like below, and it works.

package com.github.zly2006.testmod.mixin;

import com.mojang.brigadier.CommandDispatcher;
import com.mojang.brigadier.ParseResults;
import com.mojang.brigadier.context.CommandContext;
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.ModifyVariable;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfoReturnable;
import org.spongepowered.asm.mixin.injection.callback.LocalCapture;

import java.util.ArrayList;
import java.util.List;

@Mixin(value = CommandDispatcher.class, remap = false)
public class MixinCommandDispatcher {
    boolean modify = false;
    boolean valueBefore = false;
    @ModifyVariable(method = "execute(Lcom/mojang/brigadier/ParseResults;)I", at = @At(value = "STORE"), ordinal = 1)
    private boolean modify(boolean v) {
        if (modify) {
            modify = false;
            return valueBefore;
        }
        return v;
    }

    @Inject(method = "execute(Lcom/mojang/brigadier/ParseResults;)I", at = @At(value = "INVOKE", target = "Lcom/mojang/brigadier/context/CommandContext;hasNodes()Z"), locals = LocalCapture.CAPTURE_FAILSOFT)
    private void checkFix(ParseResults<?> parse, CallbackInfoReturnable<Integer> cir, int result, int successfulForks, boolean forked, boolean foundCommand, String command, CommandContext<?> original, List<?> contexts, ArrayList<?> next, int size, int i, CommandContext<?> context, CommandContext<?> child) {
        if (child.hasNodes()) {
            modify = true;
            valueBefore = foundCommand;
        }
        else {
            modify = false;
        }
    }
}

The result after my modification was tested in Minecraft 1.19. With this modification, /xp query Steve will throw a CommandSyntaxException as expected and an error message from the server was received.

@zly2006
Copy link
Contributor Author

zly2006 commented Dec 24, 2022

This bug is also reported at JIRA.

@zly2006 zly2006 changed the title Fix wrong redirect behavior Fix wrong redirect behavior (MC-256419) Dec 24, 2022
@Gegy Gegy self-requested a review March 22, 2023 08:47
@slicedlime slicedlime self-requested a review March 22, 2023 09:32
Copy link
Member

@Gegy Gegy left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you so much for this PR! 😊 Noticed a tiny issue, but should be a small change 🙂

Copy link
Member

@Gegy Gegy left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks great to me, thanks again! 🎉

Copy link
Member

@slicedlime slicedlime left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good to me - thank you for also adding tests!

@slicedlime slicedlime merged commit f20bede into Mojang:master Mar 30, 2023
@zly2006 zly2006 deleted the patch-1 branch March 30, 2023 12:34
@zly2006
Copy link
Contributor Author

zly2006 commented Mar 30, 2023

@slicedlime
Copy link
Member

Not quite - they will be closed when a Minecraft version is released using the updated library.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants