Skip to content

Commit

Permalink
chore: replace inject mixins with redirects if they're always cancelled
Browse files Browse the repository at this point in the history
See #28
  • Loading branch information
chrrs committed Nov 6, 2024
1 parent 2d90d3b commit 8196585
Showing 1 changed file with 11 additions and 8 deletions.
19 changes: 11 additions & 8 deletions src/main/java/me/chrr/scribble/mixin/BookEditScreenMixin.java
Original file line number Diff line number Diff line change
Expand Up @@ -622,11 +622,13 @@ private void removeEmptyPages(CallbackInfo ci) {
}
}

@Inject(method = "appendNewPage", at = @At(value = "INVOKE", target = "Ljava/util/List;add(Ljava/lang/Object;)Z"), cancellable = true)
private void appendNewPage(CallbackInfo ci) {
@Redirect(method = "appendNewPage", at = @At(value = "INVOKE", target = "Ljava/util/List;add(Ljava/lang/Object;)Z"))
private boolean appendNewPage(List<String> page, Object empty) {
// FIXME: It feels slightly confusing that we pass in richPages here, but at the same time
// use PagesListener to add plain-text pages. It makes it hard to follow.
Command command = new InsertPageCommand(richPages, richPages.size(), this);
commandManager.execute(command);
ci.cancel();
return true;
}

@Override
Expand Down Expand Up @@ -674,12 +676,13 @@ public void setPageContent(String newContent) {
Scribble.LOGGER.warn("setPageContent() was called, but ignored.");
}

@Inject(method = "charTyped", at = @At(value = "INVOKE", target = "Lnet/minecraft/client/util/SelectionManager;insert(Ljava/lang/String;)V"), cancellable = true)
private void charTypedEditMode(char chr, int modifiers, CallbackInfoReturnable<Boolean> cir) {
Command command = new ActionCommand<>(this, () -> this.getRichSelectionManager().insert(chr));
// NOTE: There are two "insert" calls in the original method. One is editing the book title, which
// takes a char, the other one is editing the page, which takes a String. We're targeting the
// second one.
@Redirect(method = "charTyped", at = @At(value = "INVOKE", target = "Lnet/minecraft/client/util/SelectionManager;insert(Ljava/lang/String;)V"))
private void charTypedEditMode(SelectionManager instance, String string) {
Command command = new ActionCommand<>(this, () -> this.getRichSelectionManager().insert(string));
commandManager.execute(command);
cir.setReturnValue(true);
cir.cancel();
}

@Inject(method = "keyPressedEditMode", at = @At(value = "HEAD"), cancellable = true)
Expand Down

0 comments on commit 8196585

Please sign in to comment.