-
-
Notifications
You must be signed in to change notification settings - Fork 150
Allow copying chat lines when translating using /ctranslate #802
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
Merged
Merged
Changes from 2 commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
5 changes: 5 additions & 0 deletions
5
src/main/java/net/earthcomputer/clientcommands/interfaces/IChatScreen.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| package net.earthcomputer.clientcommands.interfaces; | ||
|
|
||
| public interface IChatScreen { | ||
| boolean clientcommands_isTranslating(); | ||
| } |
55 changes: 55 additions & 0 deletions
55
.../java/net/earthcomputer/clientcommands/mixin/commands/translate/ChatComponent$1Mixin.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,55 @@ | ||
| package net.earthcomputer.clientcommands.mixin.commands.translate; | ||
|
|
||
| import com.llamalad7.mixinextras.injector.wrapoperation.Operation; | ||
| import com.llamalad7.mixinextras.injector.wrapoperation.WrapOperation; | ||
| import net.earthcomputer.clientcommands.interfaces.IChatScreen; | ||
| import net.minecraft.client.Minecraft; | ||
| import net.minecraft.client.gui.ActiveTextCollector; | ||
| import net.minecraft.client.gui.Font; | ||
| import net.minecraft.client.gui.TextAlignment; | ||
| import net.minecraft.client.gui.components.ChatComponent; | ||
| import net.minecraft.client.gui.screens.ChatScreen; | ||
| import net.minecraft.client.multiplayer.chat.GuiMessage; | ||
| import net.minecraft.client.renderer.state.gui.GuiTextRenderState; | ||
| import net.minecraft.util.ARGB; | ||
| import net.minecraft.util.FormattedCharSequence; | ||
| import org.spongepowered.asm.mixin.Mixin; | ||
| import org.spongepowered.asm.mixin.injection.At; | ||
|
|
||
| @Mixin(targets = "net/minecraft/client/gui/components/ChatComponent$1") | ||
| public abstract class ChatComponent$1Mixin { | ||
|
xpple marked this conversation as resolved.
Outdated
|
||
| @WrapOperation(method = "accept", at = @At(value = "INVOKE", target = "Lnet/minecraft/client/gui/components/ChatComponent$ChatGraphicsAccess;handleMessage(IFLnet/minecraft/util/FormattedCharSequence;)Z")) | ||
| private boolean copyText(ChatComponent.ChatGraphicsAccess instance, int textTop, float opacity, FormattedCharSequence text, Operation<Boolean> original, GuiMessage.Line line) { | ||
| if (shouldCallOriginal(instance, textTop, text, line)) { | ||
| return original.call(instance, textTop, opacity, text); | ||
| } | ||
| // original.call() always returns false | ||
|
xpple marked this conversation as resolved.
Outdated
|
||
| return false; | ||
| } | ||
|
|
||
| private static boolean shouldCallOriginal(ChatComponent.ChatGraphicsAccess instance, int textTop, FormattedCharSequence text, GuiMessage.Line line) { | ||
|
xpple marked this conversation as resolved.
Outdated
|
||
| if (!(instance instanceof ChatComponent.ClickableTextOnlyGraphicsAccess clickableTextOnlyGraphicsAccess)) { | ||
| return true; | ||
| } | ||
| Minecraft minecraft = Minecraft.getInstance(); | ||
| if (!(minecraft.gui.screen() instanceof ChatScreen chatScreen)) { | ||
| return true; | ||
| } | ||
| if (!((IChatScreen) chatScreen).clientcommands_isTranslating()) { | ||
| return true; | ||
| } | ||
| if (!(clickableTextOnlyGraphicsAccess.output instanceof ActiveTextCollector.ClickableStyleFinder clickableStyleFinder)) { | ||
| return true; | ||
| } | ||
| Font font = minecraft.font; | ||
| ActiveTextCollector.Parameters parameters = clickableStyleFinder.defaultParameters(); | ||
| int leftX = TextAlignment.LEFT.calculateLeft(0, font, text); | ||
| GuiTextRenderState renderState = new GuiTextRenderState(font, text, parameters.pose(), leftX, textTop, ARGB.white(parameters.opacity()), 0, true, true, parameters.scissor()); | ||
| boolean[] found = {false}; | ||
| ActiveTextCollector.findElementUnderCursor(renderState, clickableStyleFinder.testX, clickableStyleFinder.testY, _ -> { | ||
| minecraft.keyboardHandler.setClipboard(line.parent().content().getString()); | ||
| found[0] = true; | ||
| }); | ||
| return !found[0]; | ||
| } | ||
| } | ||
34 changes: 34 additions & 0 deletions
34
src/main/java/net/earthcomputer/clientcommands/mixin/commands/translate/ChatScreenMixin.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,34 @@ | ||
| package net.earthcomputer.clientcommands.mixin.commands.translate; | ||
|
|
||
| import net.earthcomputer.clientcommands.command.TranslateCommand; | ||
| import net.earthcomputer.clientcommands.interfaces.IChatScreen; | ||
| import net.minecraft.client.gui.screens.ChatScreen; | ||
| import net.minecraft.client.gui.screens.Screen; | ||
| import net.minecraft.commands.Commands; | ||
| import net.minecraft.network.chat.Component; | ||
| import org.spongepowered.asm.mixin.Mixin; | ||
| import org.spongepowered.asm.mixin.Unique; | ||
| import org.spongepowered.asm.mixin.injection.At; | ||
| import org.spongepowered.asm.mixin.injection.Inject; | ||
| import org.spongepowered.asm.mixin.injection.callback.CallbackInfo; | ||
|
|
||
| @Mixin(ChatScreen.class) | ||
| public abstract class ChatScreenMixin extends Screen implements IChatScreen { | ||
|
|
||
| @Unique | ||
| private boolean isTranslating = false; | ||
|
|
||
| protected ChatScreenMixin(Component title) { | ||
| super(title); | ||
| } | ||
|
|
||
| @Inject(method = "onEdited", at = @At("TAIL")) | ||
| private void onEdited(String value, CallbackInfo ci) { | ||
| this.isTranslating = value.startsWith(Commands.COMMAND_PREFIX + TranslateCommand.LITERAL); | ||
|
xpple marked this conversation as resolved.
Outdated
|
||
| } | ||
|
|
||
| @Override | ||
| public boolean clientcommands_isTranslating() { | ||
| return this.isTranslating; | ||
| } | ||
| } | ||
4 changes: 4 additions & 0 deletions
4
src/main/java/net/earthcomputer/clientcommands/mixin/commands/translate/package-info.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,4 @@ | ||
| @NullMarked | ||
| package net.earthcomputer.clientcommands.mixin.commands.translate; | ||
|
|
||
| import org.jspecify.annotations.NullMarked; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.