Skip to content

Commit 5acd7b6

Browse files
committed
feat: Port book scroll to 1.21.1
1 parent 5c98111 commit 5acd7b6

File tree

6 files changed

+165
-1
lines changed

6 files changed

+165
-1
lines changed

common/src/main/java/com/euphony/better_client/config/BetterClientConfig.java

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ public static void save() {
4242
private static final String BETTER_CHAT_GROUP = "better_chat";
4343
private static final String BIOME_TITLE_GROUP = "biome_title";
4444
private static final String FASTER_CLIMBING_GROUP = "faster_climbing";
45+
private static final String BOOK_SCROLL_GROUP = "book_scroll";
4546
private static final String OTHER_GROUP = "other";
4647

4748
@SerialEntry public boolean enableFadingNightVision = true;
@@ -73,6 +74,10 @@ public static void save() {
7374
@SerialEntry public boolean enableModName = false;
7475
@SerialEntry public boolean enableUndergroundUpdate = false;
7576

77+
@SerialEntry public boolean enableBookScroll = true;
78+
@SerialEntry public int ctrlSpeedMultiplier = 5;
79+
@SerialEntry public boolean enablePageTurnSound = true;
80+
7681
@SerialEntry public boolean enableBeeInfo = true;
7782
@SerialEntry public boolean enableAxolotlBucketFix = true;
7883
@SerialEntry public boolean enableChatHistoryRetention = true;
@@ -263,6 +268,29 @@ public static YetAnotherConfigLib makeScreen() {
263268
.range(1.0, 10.0).step(0.5))
264269
.build();
265270

271+
// Book Scroll
272+
Option<Boolean> enableBookScrollOpt = ConfigUtils.<Boolean>getGenericOption("enableBookScroll")
273+
.binding(defaults.enableBookScroll,
274+
() -> config.enableBookScroll,
275+
newVal -> config.enableBookScroll = newVal)
276+
.controller(opt -> BooleanControllerBuilder.create(opt).trueFalseFormatter())
277+
.build();
278+
279+
Option<Integer> ctrlSpeedMultiplierOpt = ConfigUtils.<Integer>getGenericOption("ctrlSpeedMultiplier")
280+
.binding(defaults.ctrlSpeedMultiplier,
281+
() -> config.ctrlSpeedMultiplier,
282+
newVal -> config.ctrlSpeedMultiplier = newVal)
283+
.controller(opt -> IntegerSliderControllerBuilder.create(opt)
284+
.range(1, 10).step(1))
285+
.build();
286+
287+
Option<Boolean> enablePageTurnSoundOpt = ConfigUtils.<Boolean>getGenericOption("enablePageTurnSound")
288+
.binding(defaults.enablePageTurnSound,
289+
() -> config.enablePageTurnSound,
290+
newVal -> config.enablePageTurnSound = newVal)
291+
.controller(opt -> BooleanControllerBuilder.create(opt).trueFalseFormatter())
292+
.build();
293+
266294
// Other
267295
Option<Boolean> enableBeeInfoOpt = ConfigUtils.<Boolean>getGenericOption("enableBeeInfo", "bee_info")
268296
.binding(defaults.enableBeeInfo,
@@ -349,6 +377,14 @@ public static YetAnotherConfigLib makeScreen() {
349377
speedMultiplierOpt
350378
))
351379
.build())
380+
.group(OptionGroup.createBuilder()
381+
.name(ConfigUtils.getGroupName(CLIENT_CATEGORY, BOOK_SCROLL_GROUP))
382+
.options(List.of(
383+
enableBookScrollOpt,
384+
ctrlSpeedMultiplierOpt,
385+
enablePageTurnSoundOpt
386+
))
387+
.build())
352388
.group(OptionGroup.createBuilder()
353389
.name(ConfigUtils.getGroupName(CLIENT_CATEGORY, OTHER_GROUP))
354390
.options(List.of(

common/src/main/java/com/euphony/better_client/mixin/BookEditScreenMixin.java

Lines changed: 49 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,28 @@
44
import net.minecraft.client.gui.screens.ConfirmScreen;
55
import net.minecraft.client.gui.screens.Screen;
66
import net.minecraft.client.gui.screens.inventory.BookEditScreen;
7+
import net.minecraft.client.resources.sounds.SimpleSoundInstance;
78
import net.minecraft.network.chat.Component;
9+
import net.minecraft.sounds.SoundEvents;
810
import org.spongepowered.asm.mixin.Mixin;
911
import org.spongepowered.asm.mixin.Shadow;
12+
import org.spongepowered.asm.mixin.Unique;
1013

1114
@Mixin(BookEditScreen.class)
12-
public class BookEditScreenMixin extends Screen {
15+
public abstract class BookEditScreenMixin extends Screen {
1316
@Shadow private boolean isModified;
1417

18+
@Shadow protected abstract void pageBack();
19+
20+
@Shadow protected abstract void pageForward();
21+
22+
@Shadow private int currentPage;
23+
24+
@Shadow protected abstract int getNumPages();
25+
26+
@Unique
27+
double better_client$progress = 0;
28+
1529
protected BookEditScreenMixin(Component component) {
1630
super(component);
1731
}
@@ -35,4 +49,38 @@ public void onClose() {
3549
super.onClose();
3650
}
3751
}
52+
53+
@Override
54+
public boolean mouseScrolled(double mouseX, double mouseY, double horizontalAmount, double verticalAmount) {
55+
if(!BetterClientConfig.HANDLER.instance().enableBookScroll) return super.mouseScrolled(mouseX, mouseY, horizontalAmount, verticalAmount);
56+
57+
double scrollDelta = verticalAmount + horizontalAmount;
58+
59+
double better_client$speedFactor = 1.0;
60+
if (hasControlDown()) better_client$speedFactor *= BetterClientConfig.HANDLER.instance().ctrlSpeedMultiplier;
61+
62+
better_client$progress += scrollDelta * better_client$speedFactor;
63+
64+
boolean pageTurned = false;
65+
if (better_client$progress >= 1.0) {
66+
while (better_client$progress >= 1.0) {
67+
better_client$progress -= 1.0;
68+
this.pageBack();
69+
pageTurned = true;
70+
}
71+
} else if (better_client$progress < 0.0) {
72+
while (better_client$progress < 0.0) {
73+
better_client$progress += 1.0;
74+
if (this.currentPage < this.getNumPages() - 1) {
75+
this.pageForward();
76+
}
77+
pageTurned = true;
78+
}
79+
}
80+
81+
if (pageTurned && BetterClientConfig.HANDLER.instance().enablePageTurnSound) {
82+
this.minecraft.getSoundManager().play(SimpleSoundInstance.forUI(SoundEvents.BOOK_PAGE_TURN, 1.0F));
83+
}
84+
return true;
85+
}
3886
}
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
package com.euphony.better_client.mixin;
2+
3+
import com.euphony.better_client.config.BetterClientConfig;
4+
import net.minecraft.client.gui.screens.Screen;
5+
import net.minecraft.client.gui.screens.inventory.BookViewScreen;
6+
import net.minecraft.client.resources.sounds.SimpleSoundInstance;
7+
import net.minecraft.network.chat.Component;
8+
import net.minecraft.sounds.SoundEvents;
9+
import org.spongepowered.asm.mixin.Mixin;
10+
import org.spongepowered.asm.mixin.Shadow;
11+
import org.spongepowered.asm.mixin.Unique;
12+
13+
@Mixin(BookViewScreen.class)
14+
public abstract class BookViewScreenMixin extends Screen {
15+
@Shadow protected abstract void pageBack();
16+
17+
@Shadow private int currentPage;
18+
19+
@Shadow protected abstract int getNumPages();
20+
21+
@Shadow protected abstract void pageForward();
22+
23+
@Unique
24+
double better_client$progress = 0;
25+
26+
protected BookViewScreenMixin(Component component) {
27+
super(component);
28+
}
29+
30+
@Override
31+
public boolean mouseScrolled(double mouseX, double mouseY, double horizontalAmount, double verticalAmount) {
32+
if(!BetterClientConfig.HANDLER.instance().enableBookScroll) return super.mouseScrolled(mouseX, mouseY, horizontalAmount, verticalAmount);
33+
34+
double scrollDelta = verticalAmount + horizontalAmount;
35+
36+
double better_client$speedFactor = 1.0;
37+
if (hasControlDown()) better_client$speedFactor *= BetterClientConfig.HANDLER.instance().ctrlSpeedMultiplier;
38+
39+
better_client$progress += scrollDelta * better_client$speedFactor;
40+
41+
boolean pageTurned = false;
42+
if (better_client$progress >= 1.0) {
43+
while (better_client$progress >= 1.0) {
44+
better_client$progress -= 1.0;
45+
this.pageBack();
46+
pageTurned = true;
47+
}
48+
} else if (better_client$progress < 0.0) {
49+
while (better_client$progress < 0.0) {
50+
better_client$progress += 1.0;
51+
if (this.currentPage < this.getNumPages() - 1) {
52+
this.pageForward();
53+
}
54+
pageTurned = true;
55+
}
56+
}
57+
58+
if (pageTurned && BetterClientConfig.HANDLER.instance().enablePageTurnSound) {
59+
this.minecraft.getSoundManager().play(SimpleSoundInstance.forUI(SoundEvents.BOOK_PAGE_TURN, 1.0F));
60+
}
61+
return true;
62+
}
63+
}

common/src/main/resources/assets/better_client/lang/en_us.json

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,14 @@
6868
"yacl3.config.better_client:config.speedMultiplier": "Speed Multiplier",
6969
"yacl3.config.better_client:config.speedMultiplier.desc": "Speed multiplier, multiplied by the angle sin value, 2.0 corresponds to 30 degrees for the original speed.",
7070

71+
"yacl3.config.better_client:config.category.client.group.book_scroll": "Book Scroll",
72+
"yacl3.config.better_client:config.enableBookScroll": "Enable Book Scroll",
73+
"yacl3.config.better_client:config.enableBookScroll.desc": "Use the mouse wheel to turn book pages.",
74+
"yacl3.config.better_client:config.ctrlSpeedMultiplier": "Ctrl Speed Multiplier",
75+
"yacl3.config.better_client:config.ctrlSpeedMultiplier.desc": "Scroll acceleration multiplier while holding Ctrl.",
76+
"yacl3.config.better_client:config.enablePageTurnSound": "Enable Page Turn Sound",
77+
"yacl3.config.better_client:config.enablePageTurnSound.desc": "Play a page-turn sound when scrolling pages.",
78+
7179
"yacl3.config.better_client:config.category.client.group.other": "Other",
7280
"yacl3.config.better_client:config.enableBeeInfo": "Enable Bee Info",
7381
"yacl3.config.better_client:config.enableBeeInfo.desc": "You can now see the number of bees and the amount of honey in beehives and bee nests, just like the feature added natively in 1.21.2.",

common/src/main/resources/assets/better_client/lang/zh_cn.json

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,14 @@
6565
"yacl3.config.better_client:config.speedMultiplier": "速度乘量",
6666
"yacl3.config.better_client:config.speedMultiplier.desc": "速度乘量,乘以角度 sin 值,2.0 相当于 30 度为原始速度。",
6767

68+
"yacl3.config.better_client:config.category.client.group.book_scroll": "书籍滚动",
69+
"yacl3.config.better_client:config.enableBookScroll": "启用书籍滚动",
70+
"yacl3.config.better_client:config.enableBookScroll.desc": "使用鼠标滚轮翻动书页。",
71+
"yacl3.config.better_client:config.ctrlSpeedMultiplier": "Ctrl 速度倍率",
72+
"yacl3.config.better_client:config.ctrlSpeedMultiplier.desc": "按住 Ctrl 键时的滚动加速倍数。",
73+
"yacl3.config.better_client:config.enablePageTurnSound": "启用翻页音效",
74+
"yacl3.config.better_client:config.enablePageTurnSound.desc": "在滚动页面时播放翻页音效。",
75+
6876
"yacl3.config.better_client:config.category.client.group.other": "其他",
6977
"yacl3.config.better_client:config.enableBeeInfo": "启用蜂巢信息",
7078
"yacl3.config.better_client:config.enableBeeInfo.desc": "您现在可以看到蜂箱和蜂巢中蜜蜂的数量和蜂蜜的含量,就像 1.21.2 中原版添加的功能一样。",

common/src/main/resources/better_client.mixins.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
"minVersion": "0.8",
66
"client": [
77
"BookEditScreenMixin",
8+
"BookViewScreenMixin",
89
"ChatComponentMixin",
910
"GameRendererMixin",
1011
"MinecraftMixin",

0 commit comments

Comments
 (0)