1111import net .minecraft .client .MinecraftClient ;
1212import net .minecraft .client .option .KeyBinding ;
1313import net .minecraft .client .util .InputUtil ;
14+ import net .minecraft .util .Identifier ;
1415import net .minecraft .entity .Entity ;
1516import net .minecraft .text .Text ;
1617import net .minecraft .util .math .Vec3d ;
@@ -37,34 +38,33 @@ public class BoatFlyClient implements ClientModInitializer {
3738 private static final double JUMP_VELOCITY = 0.3 ;
3839 private static final double MIN_SPEED = 0.0 ;
3940
41+ private static final KeyBinding .Category KEY_CATEGORY = new KeyBinding .Category (Identifier .of (MOD_ID , "main" ));
42+
4043 @ Override
4144 public void onInitializeClient () {
4245 // 註冊按鍵綁定
4346 boatFlightKey = KeyBindingHelper .registerKeyBinding (new KeyBinding (
4447 "key." + MOD_ID + ".toggle_fly" ,
4548 InputUtil .Type .KEYSYM ,
4649 GLFW .GLFW_KEY_B ,
47- "category." + MOD_ID + ".main"
50+ KEY_CATEGORY
4851 ));
49-
5052 increaseSpeedKey = KeyBindingHelper .registerKeyBinding (new KeyBinding (
5153 "key." + MOD_ID + ".increase_speed" ,
5254 InputUtil .Type .KEYSYM ,
5355 GLFW .GLFW_KEY_I ,
54- "category." + MOD_ID + ".main"
56+ KEY_CATEGORY
5557 ));
56-
5758 decreaseSpeedKey = KeyBindingHelper .registerKeyBinding (new KeyBinding (
5859 "key." + MOD_ID + ".decrease_speed" ,
5960 InputUtil .Type .KEYSYM ,
6061 GLFW .GLFW_KEY_O ,
61- "category." + MOD_ID + ".main"
62+ KEY_CATEGORY
6263 ));
6364
64- // 註冊客戶端 Tick 事件監聽器
65+ // 註冊用戶端 Tick 事件監聽器
6566 ClientTickEvents .END_CLIENT_TICK .register (this ::onClientTick );
66-
67- // 註冊客戶端指令 (使用 Fabric API v2)
67+ // 註冊用戶端指令 (使用 Fabric API v2)
6868 ClientCommandRegistrationCallback .EVENT .register ((dispatcher , registryAccess ) -> {
6969 LiteralArgumentBuilder <FabricClientCommandSource > command = ClientCommandManager .literal ("boatspeed" )
7070 .then (ClientCommandManager .argument ("speed" , FloatArgumentType .floatArg ((float ) MIN_SPEED ))
@@ -79,10 +79,6 @@ public void onInitializeClient() {
7979 });
8080 }
8181
82- /**
83- * 在每個客戶端 tick 結束時調用此方法,處理所有邏輯。
84- * @param client MinecraftClient 實例
85- */
8682 private void onClientTick (MinecraftClient client ) {
8783 // 確保玩家實例存在,否則不執行
8884 if (client .player == null ) return ;
@@ -92,43 +88,32 @@ private void onClientTick(MinecraftClient client) {
9288 isBoatFlyEnabled = !isBoatFlyEnabled ;
9389 if (isBoatFlyEnabled ) {
9490 changeSpeed (DEFAULT_SPEED );
95- if (client .player != null ) {
96- client .player .sendMessage (Text .translatable ("message." + MOD_ID + ".fly_enabled" , String .format ("%.2f" , currentBoatVelocity )), false );
97- }
91+ client .player .sendMessage (Text .translatable ("message." + MOD_ID + ".fly_enabled" , String .format ("%.2f" , currentBoatVelocity )), false );
9892 } else {
99- if (client .player != null ) {
100- client .player .sendMessage (Text .translatable ("message." + MOD_ID + ".fly_disabled" ), false );
101- }
93+ client .player .sendMessage (Text .translatable ("message." + MOD_ID + ".fly_disabled" ), false );
10294 }
10395 }
10496
10597 // 若功能未啟用,直接返回
106- if (!isBoatFlyEnabled ) {
107- return ;
108- }
98+ if (!isBoatFlyEnabled ) return ;
10999
110100 // 處理增加速度按鍵
111101 if (increaseSpeedKey .wasPressed ()) {
112102 changeSpeed (currentBoatVelocity + SPEED_INCREMENT );
113- if (client .player != null ) {
114- client .player .sendMessage (Text .translatable ("message." + MOD_ID + ".speed_changed" , String .format ("%.2f" , currentBoatVelocity )), false );
115- }
103+ client .player .sendMessage (Text .translatable ("message." + MOD_ID + ".speed_changed" , String .format ("%.2f" , currentBoatVelocity )), false );
116104 }
117105
118106 // 處理降低速度按鍵
119107 if (decreaseSpeedKey .wasPressed ()) {
120108 if (currentBoatVelocity > MIN_SPEED ) {
121109 changeSpeed (currentBoatVelocity + SPEED_DECREMENT );
122- if (client .player != null ) {
123- client .player .sendMessage (Text .translatable ("message." + MOD_ID + ".speed_changed" , String .format ("%.2f" , currentBoatVelocity )), false );
124- }
110+ client .player .sendMessage (Text .translatable ("message." + MOD_ID + ".speed_changed" , String .format ("%.2f" , currentBoatVelocity )), false );
125111 }
126112 }
127-
128- if (client .player == null || !client .player .hasVehicle ()) {
129- return ;
130- }
113+
114+ if (!client .player .hasVehicle ()) return ;
131115 Entity vehicle = client .player .getVehicle ();
116+ if (vehicle == null ) return ;
132117
133118 // 處理跳躍鍵 (控制垂直向上速度)
134119 if (client .options .jumpKey .isPressed ()) {
@@ -144,22 +129,12 @@ private void onClientTick(MinecraftClient client) {
144129 }
145130 }
146131
147- /**
148- * 改變船的速度設定值,並計算相應的速度乘數。
149- * @param newSpeed 新的目標速度(單位:blocks/s)
150- */
151132 private void changeSpeed (double newSpeed ) {
152133 currentBoatVelocity = Math .max (MIN_SPEED , newSpeed );
153134 boatSpeedMultiplier = calculateMultiplier (currentBoatVelocity );
154-
155135 LOGGER .info ("Boat speed set to {} b/s, multiplier is {}." , String .format ("%.2f" , currentBoatVelocity ), String .format ("%.5f" , boatSpeedMultiplier ));
156136 }
157137
158- /**
159- * 根據目標速度計算一個內部乘數。
160- * @param velocity 目標速度
161- * @return 計算出的速度乘數
162- */
163138 private double calculateMultiplier (double velocity ) {
164139 // f(v) = (-5.33893 * (ln(v - 8 + 11.9072))^(-3.31832) + 1.26253)^(0.470998)
165140 if (velocity <= 0 ) return 0 ;
@@ -173,4 +148,4 @@ private double calculateMultiplier(double velocity) {
173148
174149 return Math .pow (base , 0.470998 );
175150 }
176- }
151+ }
0 commit comments