From 86a4f705ddfce134f1d2393bb7c45348c07049a8 Mon Sep 17 00:00:00 2001 From: fishshi <2855691008@qq.com> Date: Sun, 22 Sep 2024 17:57:11 +0800 Subject: [PATCH 01/14] add MODIFY_PLAYER_MOVEMENT_DURING_USINGITEM --- .../entity/event/ClientPlayerEvents.java | 41 +++++++++++++++++++ .../entity/event/ClientPlayerMixin.java | 38 +++++++++++++++++ ...fabric-entity-events-v1.client.mixins.json | 3 +- .../event/client/EntityEventTestsClient.java | 17 ++++++++ 4 files changed, 98 insertions(+), 1 deletion(-) create mode 100644 fabric-entity-events-v1/src/client/java/net/fabricmc/fabric/api/client/entity/event/ClientPlayerEvents.java create mode 100644 fabric-entity-events-v1/src/client/java/net/fabricmc/fabric/mixin/client/entity/event/ClientPlayerMixin.java diff --git a/fabric-entity-events-v1/src/client/java/net/fabricmc/fabric/api/client/entity/event/ClientPlayerEvents.java b/fabric-entity-events-v1/src/client/java/net/fabricmc/fabric/api/client/entity/event/ClientPlayerEvents.java new file mode 100644 index 0000000000..54b41ee716 --- /dev/null +++ b/fabric-entity-events-v1/src/client/java/net/fabricmc/fabric/api/client/entity/event/ClientPlayerEvents.java @@ -0,0 +1,41 @@ +/* + * Copyright (c) 2016, 2017, 2018, 2019 FabricMC + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package net.fabricmc.fabric.api.client.entity.event; + +import net.fabricmc.fabric.api.event.Event; +import net.fabricmc.fabric.api.event.EventFactory; + +import net.minecraft.client.network.ClientPlayerEntity; + +public final class ClientPlayerEvents { + /* + * An event that is called when a player is moving during using an item. + */ + public static final Event MODIFY_PLAYER_MOVEMENT_DURING_USINGITEM = EventFactory.createArrayBacked(ModifyPlayerMovementDuringUsingitem.class, callbacks -> player -> { + for (ModifyPlayerMovementDuringUsingitem callback : callbacks) { + callback.modifyPlayerMovementDuringUsingitem(player); + } + }); + + @FunctionalInterface + public interface ModifyPlayerMovementDuringUsingitem { + /** + * @param player the player is moving during using an item. + */ + void modifyPlayerMovementDuringUsingitem(ClientPlayerEntity player); + } +} diff --git a/fabric-entity-events-v1/src/client/java/net/fabricmc/fabric/mixin/client/entity/event/ClientPlayerMixin.java b/fabric-entity-events-v1/src/client/java/net/fabricmc/fabric/mixin/client/entity/event/ClientPlayerMixin.java new file mode 100644 index 0000000000..75070579d2 --- /dev/null +++ b/fabric-entity-events-v1/src/client/java/net/fabricmc/fabric/mixin/client/entity/event/ClientPlayerMixin.java @@ -0,0 +1,38 @@ +/* + * Copyright (c) 2016, 2017, 2018, 2019 FabricMC + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package net.fabricmc.fabric.mixin.client.entity.event; + +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.callback.CallbackInfo; + +import net.fabricmc.fabric.api.client.entity.event.ClientPlayerEvents; + +import net.minecraft.client.network.ClientPlayerEntity; + +@Mixin(ClientPlayerEntity.class) +abstract class ClientPlayerMixin { + @Inject(method = "tickMovement", at = @At(value = "INVOKE", target = "Lnet/minecraft/client/network/ClientPlayerEntity;isUsingItem()Z")) + private void beforeTickMovement(CallbackInfo ci) { + ClientPlayerEntity player = (ClientPlayerEntity) (Object) this; + + if (player.isUsingItem() && !player.hasVehicle() && (player.input.movementForward != 0.0F || player.input.movementSideways != 0.0F)) { + ClientPlayerEvents.MODIFY_PLAYER_MOVEMENT_DURING_USINGITEM.invoker().modifyPlayerMovementDuringUsingitem(player); + } + } +} diff --git a/fabric-entity-events-v1/src/client/resources/fabric-entity-events-v1.client.mixins.json b/fabric-entity-events-v1/src/client/resources/fabric-entity-events-v1.client.mixins.json index c4866c6e99..c86d331963 100644 --- a/fabric-entity-events-v1/src/client/resources/fabric-entity-events-v1.client.mixins.json +++ b/fabric-entity-events-v1/src/client/resources/fabric-entity-events-v1.client.mixins.json @@ -3,7 +3,8 @@ "package": "net.fabricmc.fabric.mixin.client.entity.event", "compatibilityLevel": "JAVA_17", "client": [ - "elytra.ClientPlayerEntityMixin" + "elytra.ClientPlayerEntityMixin", + "ClientPlayerMixin" ], "injectors": { "defaultRequire": 1, diff --git a/fabric-entity-events-v1/src/testmodClient/java/net/fabricmc/fabric/test/entity/event/client/EntityEventTestsClient.java b/fabric-entity-events-v1/src/testmodClient/java/net/fabricmc/fabric/test/entity/event/client/EntityEventTestsClient.java index ef9c828c1a..498feb1e5a 100644 --- a/fabric-entity-events-v1/src/testmodClient/java/net/fabricmc/fabric/test/entity/event/client/EntityEventTestsClient.java +++ b/fabric-entity-events-v1/src/testmodClient/java/net/fabricmc/fabric/test/entity/event/client/EntityEventTestsClient.java @@ -17,16 +17,33 @@ package net.fabricmc.fabric.test.entity.event.client; import net.minecraft.entity.EquipmentSlot; +import net.minecraft.item.Items; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; import net.fabricmc.api.ClientModInitializer; +import net.fabricmc.fabric.api.client.entity.event.ClientPlayerEvents; import net.fabricmc.fabric.api.client.rendering.v1.LivingEntityFeatureRenderEvents; import net.fabricmc.fabric.test.entity.event.EntityEventTests; public class EntityEventTestsClient implements ClientModInitializer { + private static final Logger LOGGER = LoggerFactory.getLogger(EntityEventTestsClient.class); + @Override public void onInitializeClient() { LivingEntityFeatureRenderEvents.ALLOW_CAPE_RENDER.register(player -> { return !player.getEquippedStack(EquipmentSlot.CHEST).isOf(EntityEventTests.DIAMOND_ELYTRA); }); + + ClientPlayerEvents.MODIFY_PLAYER_MOVEMENT_DURING_USINGITEM.register(player -> { + LOGGER.info("Player {} is moving during using item.", player); + + if(player.getMainHandStack().isOf(Items.BOW) && player.getEquippedStack(EquipmentSlot.FEET).isOf(Items.DIAMOND_BOOTS)) { + LOGGER.info("Player {} can move without slowdown becase of diamond boots on feet.", player); + player.input.movementForward *= 5F; + player.input.movementSideways *= 5F; + } + }); } } From fe5207979308401ca04ebdce531daa819d1113d6 Mon Sep 17 00:00:00 2001 From: fishshi <2855691008@qq.com> Date: Mon, 23 Sep 2024 22:50:54 +0800 Subject: [PATCH 02/14] improve comment --- .../fabric/api/client/entity/event/ClientPlayerEvents.java | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/fabric-entity-events-v1/src/client/java/net/fabricmc/fabric/api/client/entity/event/ClientPlayerEvents.java b/fabric-entity-events-v1/src/client/java/net/fabricmc/fabric/api/client/entity/event/ClientPlayerEvents.java index 54b41ee716..ed3cdcf044 100644 --- a/fabric-entity-events-v1/src/client/java/net/fabricmc/fabric/api/client/entity/event/ClientPlayerEvents.java +++ b/fabric-entity-events-v1/src/client/java/net/fabricmc/fabric/api/client/entity/event/ClientPlayerEvents.java @@ -22,7 +22,7 @@ import net.minecraft.client.network.ClientPlayerEntity; public final class ClientPlayerEvents { - /* + /** * An event that is called when a player is moving during using an item. */ public static final Event MODIFY_PLAYER_MOVEMENT_DURING_USINGITEM = EventFactory.createArrayBacked(ModifyPlayerMovementDuringUsingitem.class, callbacks -> player -> { @@ -34,6 +34,8 @@ public final class ClientPlayerEvents { @FunctionalInterface public interface ModifyPlayerMovementDuringUsingitem { /** + * Called when a player is moving during using an item. + * * @param player the player is moving during using an item. */ void modifyPlayerMovementDuringUsingitem(ClientPlayerEntity player); From 859be4f0afe09b21f48c42cb5b28daa16b2f71b8 Mon Sep 17 00:00:00 2001 From: fishshi <2855691008@qq.com> Date: Sun, 29 Sep 2024 11:10:27 +0800 Subject: [PATCH 03/14] fix --- .../event => entity/event/client}/ClientPlayerEvents.java | 8 ++++---- .../mixin/client/entity/event/ClientPlayerMixin.java | 4 ++-- .../test/entity/event/client/EntityEventTestsClient.java | 8 ++++---- 3 files changed, 10 insertions(+), 10 deletions(-) rename fabric-entity-events-v1/src/client/java/net/fabricmc/fabric/api/{client/entity/event => entity/event/client}/ClientPlayerEvents.java (96%) diff --git a/fabric-entity-events-v1/src/client/java/net/fabricmc/fabric/api/client/entity/event/ClientPlayerEvents.java b/fabric-entity-events-v1/src/client/java/net/fabricmc/fabric/api/entity/event/client/ClientPlayerEvents.java similarity index 96% rename from fabric-entity-events-v1/src/client/java/net/fabricmc/fabric/api/client/entity/event/ClientPlayerEvents.java rename to fabric-entity-events-v1/src/client/java/net/fabricmc/fabric/api/entity/event/client/ClientPlayerEvents.java index ed3cdcf044..fb6f798ac0 100644 --- a/fabric-entity-events-v1/src/client/java/net/fabricmc/fabric/api/client/entity/event/ClientPlayerEvents.java +++ b/fabric-entity-events-v1/src/client/java/net/fabricmc/fabric/api/entity/event/client/ClientPlayerEvents.java @@ -14,13 +14,13 @@ * limitations under the License. */ -package net.fabricmc.fabric.api.client.entity.event; +package net.fabricmc.fabric.api.entity.event.client; + +import net.minecraft.client.network.ClientPlayerEntity; import net.fabricmc.fabric.api.event.Event; import net.fabricmc.fabric.api.event.EventFactory; -import net.minecraft.client.network.ClientPlayerEntity; - public final class ClientPlayerEvents { /** * An event that is called when a player is moving during using an item. @@ -35,7 +35,7 @@ public final class ClientPlayerEvents { public interface ModifyPlayerMovementDuringUsingitem { /** * Called when a player is moving during using an item. - * + * * @param player the player is moving during using an item. */ void modifyPlayerMovementDuringUsingitem(ClientPlayerEntity player); diff --git a/fabric-entity-events-v1/src/client/java/net/fabricmc/fabric/mixin/client/entity/event/ClientPlayerMixin.java b/fabric-entity-events-v1/src/client/java/net/fabricmc/fabric/mixin/client/entity/event/ClientPlayerMixin.java index 75070579d2..18e785332b 100644 --- a/fabric-entity-events-v1/src/client/java/net/fabricmc/fabric/mixin/client/entity/event/ClientPlayerMixin.java +++ b/fabric-entity-events-v1/src/client/java/net/fabricmc/fabric/mixin/client/entity/event/ClientPlayerMixin.java @@ -21,10 +21,10 @@ import org.spongepowered.asm.mixin.injection.Inject; import org.spongepowered.asm.mixin.injection.callback.CallbackInfo; -import net.fabricmc.fabric.api.client.entity.event.ClientPlayerEvents; - import net.minecraft.client.network.ClientPlayerEntity; +import net.fabricmc.fabric.api.entity.event.client.ClientPlayerEvents; + @Mixin(ClientPlayerEntity.class) abstract class ClientPlayerMixin { @Inject(method = "tickMovement", at = @At(value = "INVOKE", target = "Lnet/minecraft/client/network/ClientPlayerEntity;isUsingItem()Z")) diff --git a/fabric-entity-events-v1/src/testmodClient/java/net/fabricmc/fabric/test/entity/event/client/EntityEventTestsClient.java b/fabric-entity-events-v1/src/testmodClient/java/net/fabricmc/fabric/test/entity/event/client/EntityEventTestsClient.java index 498feb1e5a..0350a87c38 100644 --- a/fabric-entity-events-v1/src/testmodClient/java/net/fabricmc/fabric/test/entity/event/client/EntityEventTestsClient.java +++ b/fabric-entity-events-v1/src/testmodClient/java/net/fabricmc/fabric/test/entity/event/client/EntityEventTestsClient.java @@ -16,15 +16,15 @@ package net.fabricmc.fabric.test.entity.event.client; -import net.minecraft.entity.EquipmentSlot; -import net.minecraft.item.Items; - import org.slf4j.Logger; import org.slf4j.LoggerFactory; +import net.minecraft.entity.EquipmentSlot; +import net.minecraft.item.Items; + import net.fabricmc.api.ClientModInitializer; -import net.fabricmc.fabric.api.client.entity.event.ClientPlayerEvents; import net.fabricmc.fabric.api.client.rendering.v1.LivingEntityFeatureRenderEvents; +import net.fabricmc.fabric.api.entity.event.client.ClientPlayerEvents; import net.fabricmc.fabric.test.entity.event.EntityEventTests; public class EntityEventTestsClient implements ClientModInitializer { From 9a0f5bf67986de005e84504af30606c57304fdc3 Mon Sep 17 00:00:00 2001 From: fishshi <2855691008@qq.com> Date: Sun, 29 Sep 2024 21:36:58 +0800 Subject: [PATCH 04/14] fix --- .../fabric/test/entity/event/client/EntityEventTestsClient.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/fabric-entity-events-v1/src/testmodClient/java/net/fabricmc/fabric/test/entity/event/client/EntityEventTestsClient.java b/fabric-entity-events-v1/src/testmodClient/java/net/fabricmc/fabric/test/entity/event/client/EntityEventTestsClient.java index 0350a87c38..11d4024706 100644 --- a/fabric-entity-events-v1/src/testmodClient/java/net/fabricmc/fabric/test/entity/event/client/EntityEventTestsClient.java +++ b/fabric-entity-events-v1/src/testmodClient/java/net/fabricmc/fabric/test/entity/event/client/EntityEventTestsClient.java @@ -39,7 +39,7 @@ public void onInitializeClient() { ClientPlayerEvents.MODIFY_PLAYER_MOVEMENT_DURING_USINGITEM.register(player -> { LOGGER.info("Player {} is moving during using item.", player); - if(player.getMainHandStack().isOf(Items.BOW) && player.getEquippedStack(EquipmentSlot.FEET).isOf(Items.DIAMOND_BOOTS)) { + if (player.getMainHandStack().isOf(Items.BOW) && player.getEquippedStack(EquipmentSlot.FEET).isOf(Items.DIAMOND_BOOTS)) { LOGGER.info("Player {} can move without slowdown becase of diamond boots on feet.", player); player.input.movementForward *= 5F; player.input.movementSideways *= 5F; From 78e96054bf684e55d36d47d2146042f01f2a4b5a Mon Sep 17 00:00:00 2001 From: fishshi <2855691008@qq.com> Date: Sat, 19 Oct 2024 13:07:44 +0800 Subject: [PATCH 05/14] change event to DISABLE_USINGITEM_SLOWDOWN --- .../entity/event/client/ClientPlayerEvents.java | 15 ++++++++++----- .../client/entity/event/ClientPlayerMixin.java | 5 ++++- .../event/client/EntityEventTestsClient.java | 9 ++++----- 3 files changed, 18 insertions(+), 11 deletions(-) diff --git a/fabric-entity-events-v1/src/client/java/net/fabricmc/fabric/api/entity/event/client/ClientPlayerEvents.java b/fabric-entity-events-v1/src/client/java/net/fabricmc/fabric/api/entity/event/client/ClientPlayerEvents.java index fb6f798ac0..1fcb77d3c0 100644 --- a/fabric-entity-events-v1/src/client/java/net/fabricmc/fabric/api/entity/event/client/ClientPlayerEvents.java +++ b/fabric-entity-events-v1/src/client/java/net/fabricmc/fabric/api/entity/event/client/ClientPlayerEvents.java @@ -25,19 +25,24 @@ public final class ClientPlayerEvents { /** * An event that is called when a player is moving during using an item. */ - public static final Event MODIFY_PLAYER_MOVEMENT_DURING_USINGITEM = EventFactory.createArrayBacked(ModifyPlayerMovementDuringUsingitem.class, callbacks -> player -> { - for (ModifyPlayerMovementDuringUsingitem callback : callbacks) { - callback.modifyPlayerMovementDuringUsingitem(player); + public static final Event DISABLE_USINGITEM_SLOWDOWN = EventFactory.createArrayBacked(DisableUsingitemSlowdown.class, callbacks -> player -> { + for (DisableUsingitemSlowdown callback : callbacks) { + if (callback.disableUsingitemSlowdown(player)) { + return true; + } } + + return false; }); @FunctionalInterface - public interface ModifyPlayerMovementDuringUsingitem { + public interface DisableUsingitemSlowdown { /** * Called when a player is moving during using an item. * * @param player the player is moving during using an item. + * @return true if the player can move without slowdown during using an item, false otherwise. */ - void modifyPlayerMovementDuringUsingitem(ClientPlayerEntity player); + boolean disableUsingitemSlowdown(ClientPlayerEntity player); } } diff --git a/fabric-entity-events-v1/src/client/java/net/fabricmc/fabric/mixin/client/entity/event/ClientPlayerMixin.java b/fabric-entity-events-v1/src/client/java/net/fabricmc/fabric/mixin/client/entity/event/ClientPlayerMixin.java index 18e785332b..70b49d01d9 100644 --- a/fabric-entity-events-v1/src/client/java/net/fabricmc/fabric/mixin/client/entity/event/ClientPlayerMixin.java +++ b/fabric-entity-events-v1/src/client/java/net/fabricmc/fabric/mixin/client/entity/event/ClientPlayerMixin.java @@ -32,7 +32,10 @@ private void beforeTickMovement(CallbackInfo ci) { ClientPlayerEntity player = (ClientPlayerEntity) (Object) this; if (player.isUsingItem() && !player.hasVehicle() && (player.input.movementForward != 0.0F || player.input.movementSideways != 0.0F)) { - ClientPlayerEvents.MODIFY_PLAYER_MOVEMENT_DURING_USINGITEM.invoker().modifyPlayerMovementDuringUsingitem(player); + if (ClientPlayerEvents.DISABLE_USINGITEM_SLOWDOWN.invoker().disableUsingitemSlowdown(player)) { + player.input.movementSideways *= 5.0F; + player.input.movementForward *= 5.0F; + } } } } diff --git a/fabric-entity-events-v1/src/testmodClient/java/net/fabricmc/fabric/test/entity/event/client/EntityEventTestsClient.java b/fabric-entity-events-v1/src/testmodClient/java/net/fabricmc/fabric/test/entity/event/client/EntityEventTestsClient.java index 11d4024706..1a2394b917 100644 --- a/fabric-entity-events-v1/src/testmodClient/java/net/fabricmc/fabric/test/entity/event/client/EntityEventTestsClient.java +++ b/fabric-entity-events-v1/src/testmodClient/java/net/fabricmc/fabric/test/entity/event/client/EntityEventTestsClient.java @@ -36,14 +36,13 @@ public void onInitializeClient() { return !player.getEquippedStack(EquipmentSlot.CHEST).isOf(EntityEventTests.DIAMOND_ELYTRA); }); - ClientPlayerEvents.MODIFY_PLAYER_MOVEMENT_DURING_USINGITEM.register(player -> { - LOGGER.info("Player {} is moving during using item.", player); - + ClientPlayerEvents.DISABLE_USINGITEM_SLOWDOWN.register(player -> { if (player.getMainHandStack().isOf(Items.BOW) && player.getEquippedStack(EquipmentSlot.FEET).isOf(Items.DIAMOND_BOOTS)) { LOGGER.info("Player {} can move without slowdown becase of diamond boots on feet.", player); - player.input.movementForward *= 5F; - player.input.movementSideways *= 5F; + return true; } + + return false; }); } } From 3a98e720fe831a418e216e8221bdb99c259be7bc Mon Sep 17 00:00:00 2001 From: fishshi <2855691008@qq.com> Date: Mon, 21 Oct 2024 12:38:47 +0800 Subject: [PATCH 06/14] change to ADJUST_USING_ITEM_SPEED --- .../event/client/ClientPlayerEvents.java | 31 ++++++++++++++----- .../entity/event/ClientPlayerMixin.java | 7 ++--- .../event/client/EntityEventTestsClient.java | 24 +++++++++++--- 3 files changed, 45 insertions(+), 17 deletions(-) diff --git a/fabric-entity-events-v1/src/client/java/net/fabricmc/fabric/api/entity/event/client/ClientPlayerEvents.java b/fabric-entity-events-v1/src/client/java/net/fabricmc/fabric/api/entity/event/client/ClientPlayerEvents.java index 1fcb77d3c0..0efb11b9b0 100644 --- a/fabric-entity-events-v1/src/client/java/net/fabricmc/fabric/api/entity/event/client/ClientPlayerEvents.java +++ b/fabric-entity-events-v1/src/client/java/net/fabricmc/fabric/api/entity/event/client/ClientPlayerEvents.java @@ -22,27 +22,42 @@ import net.fabricmc.fabric.api.event.EventFactory; public final class ClientPlayerEvents { + /* + * Flag for using default slowdown during using an item. + */ + public static final float USING_DEFAULT_SLOWDOWN_SPEED = -1.0F; + + /* + * Default slowdown speed when using an item in Minecraft. + */ + public static final float DEFAULT_SLOWDOWN_SPEED = 0.2F; + /** * An event that is called when a player is moving during using an item. */ - public static final Event DISABLE_USINGITEM_SLOWDOWN = EventFactory.createArrayBacked(DisableUsingitemSlowdown.class, callbacks -> player -> { - for (DisableUsingitemSlowdown callback : callbacks) { - if (callback.disableUsingitemSlowdown(player)) { - return true; + public static final Event ADJUST_USING_ITEM_SPEED = EventFactory.createArrayBacked(AdjustUsingItemSpeed.class, callbacks -> player -> { + float maxSpeed = -0.1F; + + for (AdjustUsingItemSpeed callback : callbacks) { + float currentSpeed = callback.adjustUsingItemSpeed(player); + + if (currentSpeed >= 0.0F && currentSpeed <= 1.0F) { + maxSpeed = currentSpeed > maxSpeed ? currentSpeed : maxSpeed; } } - return false; + return maxSpeed == -0.1F ? DEFAULT_SLOWDOWN_SPEED : maxSpeed; }); @FunctionalInterface - public interface DisableUsingitemSlowdown { + public interface AdjustUsingItemSpeed { /** * Called when a player is moving during using an item. * * @param player the player is moving during using an item. - * @return true if the player can move without slowdown during using an item, false otherwise. + * @return the percentage of the player's speed from 0.0F to 1.0F. + * {@link #DEFAULT_SLOWDOWN_SPEED} indicates that no adjustment should be applied. */ - boolean disableUsingitemSlowdown(ClientPlayerEntity player); + float adjustUsingItemSpeed(ClientPlayerEntity player); } } diff --git a/fabric-entity-events-v1/src/client/java/net/fabricmc/fabric/mixin/client/entity/event/ClientPlayerMixin.java b/fabric-entity-events-v1/src/client/java/net/fabricmc/fabric/mixin/client/entity/event/ClientPlayerMixin.java index 70b49d01d9..27cc9f12ef 100644 --- a/fabric-entity-events-v1/src/client/java/net/fabricmc/fabric/mixin/client/entity/event/ClientPlayerMixin.java +++ b/fabric-entity-events-v1/src/client/java/net/fabricmc/fabric/mixin/client/entity/event/ClientPlayerMixin.java @@ -32,10 +32,9 @@ private void beforeTickMovement(CallbackInfo ci) { ClientPlayerEntity player = (ClientPlayerEntity) (Object) this; if (player.isUsingItem() && !player.hasVehicle() && (player.input.movementForward != 0.0F || player.input.movementSideways != 0.0F)) { - if (ClientPlayerEvents.DISABLE_USINGITEM_SLOWDOWN.invoker().disableUsingitemSlowdown(player)) { - player.input.movementSideways *= 5.0F; - player.input.movementForward *= 5.0F; - } + float slowdown = ClientPlayerEvents.ADJUST_USING_ITEM_SPEED.invoker().adjustUsingItemSpeed(player) * 5.0F; + player.input.movementSideways *= slowdown; + player.input.movementForward *= slowdown; } } } diff --git a/fabric-entity-events-v1/src/testmodClient/java/net/fabricmc/fabric/test/entity/event/client/EntityEventTestsClient.java b/fabric-entity-events-v1/src/testmodClient/java/net/fabricmc/fabric/test/entity/event/client/EntityEventTestsClient.java index 1a2394b917..d3e2916e36 100644 --- a/fabric-entity-events-v1/src/testmodClient/java/net/fabricmc/fabric/test/entity/event/client/EntityEventTestsClient.java +++ b/fabric-entity-events-v1/src/testmodClient/java/net/fabricmc/fabric/test/entity/event/client/EntityEventTestsClient.java @@ -36,13 +36,27 @@ public void onInitializeClient() { return !player.getEquippedStack(EquipmentSlot.CHEST).isOf(EntityEventTests.DIAMOND_ELYTRA); }); - ClientPlayerEvents.DISABLE_USINGITEM_SLOWDOWN.register(player -> { - if (player.getMainHandStack().isOf(Items.BOW) && player.getEquippedStack(EquipmentSlot.FEET).isOf(Items.DIAMOND_BOOTS)) { - LOGGER.info("Player {} can move without slowdown becase of diamond boots on feet.", player); - return true; + ClientPlayerEvents.ADJUST_USING_ITEM_SPEED.register(player -> { + if (player.getMainHandStack().isOf(Items.BOW) && player.getEquippedStack(EquipmentSlot.FEET).isOf(Items.LEATHER_BOOTS)) { + LOGGER.info("Player {} can move with normal speed becase of leather boots on feet.", player); + return 1.0F; } - return false; + if (player.getMainHandStack().isOf(Items.BOW) && player.getEquippedStack(EquipmentSlot.FEET).isOf(Items.IRON_BOOTS)) { + LOGGER.info("Player {} can't move during using bow becase of iron boots on feet.", player); + return 0.0F; + } + + return ClientPlayerEvents.USING_DEFAULT_SLOWDOWN_SPEED; + }); + + ClientPlayerEvents.ADJUST_USING_ITEM_SPEED.register(player -> { + if (player.getMainHandStack().isOf(Items.BOW) && player.getOffHandStack().isOf(Items.FEATHER)) { + LOGGER.info("Player {} can move with half speed becase of feather in offhand.", player); + return 0.5F; + } + + return ClientPlayerEvents.USING_DEFAULT_SLOWDOWN_SPEED; }); } } From 25151b6ccb1b579a91c1d05a6bec1652d3327eb5 Mon Sep 17 00:00:00 2001 From: fishshi <2855691008@qq.com> Date: Mon, 21 Oct 2024 13:25:10 +0800 Subject: [PATCH 07/14] change DEFAULT_SLOWDOWN_SPEED to private --- .../fabric/api/entity/event/client/ClientPlayerEvents.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/fabric-entity-events-v1/src/client/java/net/fabricmc/fabric/api/entity/event/client/ClientPlayerEvents.java b/fabric-entity-events-v1/src/client/java/net/fabricmc/fabric/api/entity/event/client/ClientPlayerEvents.java index 0efb11b9b0..30750d6f81 100644 --- a/fabric-entity-events-v1/src/client/java/net/fabricmc/fabric/api/entity/event/client/ClientPlayerEvents.java +++ b/fabric-entity-events-v1/src/client/java/net/fabricmc/fabric/api/entity/event/client/ClientPlayerEvents.java @@ -28,9 +28,9 @@ public final class ClientPlayerEvents { public static final float USING_DEFAULT_SLOWDOWN_SPEED = -1.0F; /* - * Default slowdown speed when using an item in Minecraft. + * Default percentage of speed slowdown when using an item in Minecraft. */ - public static final float DEFAULT_SLOWDOWN_SPEED = 0.2F; + private static final float DEFAULT_SLOWDOWN_SPEED = 0.2F; /** * An event that is called when a player is moving during using an item. From e2f166d44f968d63cacaf69aadffac4206c0751f Mon Sep 17 00:00:00 2001 From: fishshi <2855691008@qq.com> Date: Mon, 21 Oct 2024 22:45:06 +0800 Subject: [PATCH 08/14] rename USE_DEFAULT_SLOWDOWN_SPEED --- .../api/entity/event/client/ClientPlayerEvents.java | 8 ++++---- .../test/entity/event/client/EntityEventTestsClient.java | 4 ++-- 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/fabric-entity-events-v1/src/client/java/net/fabricmc/fabric/api/entity/event/client/ClientPlayerEvents.java b/fabric-entity-events-v1/src/client/java/net/fabricmc/fabric/api/entity/event/client/ClientPlayerEvents.java index 30750d6f81..61da2b324b 100644 --- a/fabric-entity-events-v1/src/client/java/net/fabricmc/fabric/api/entity/event/client/ClientPlayerEvents.java +++ b/fabric-entity-events-v1/src/client/java/net/fabricmc/fabric/api/entity/event/client/ClientPlayerEvents.java @@ -25,7 +25,7 @@ public final class ClientPlayerEvents { /* * Flag for using default slowdown during using an item. */ - public static final float USING_DEFAULT_SLOWDOWN_SPEED = -1.0F; + public static final float USE_DEFAULT_SLOWDOWN_SPEED = -1.0F; /* * Default percentage of speed slowdown when using an item in Minecraft. @@ -36,7 +36,7 @@ public final class ClientPlayerEvents { * An event that is called when a player is moving during using an item. */ public static final Event ADJUST_USING_ITEM_SPEED = EventFactory.createArrayBacked(AdjustUsingItemSpeed.class, callbacks -> player -> { - float maxSpeed = -0.1F; + float maxSpeed = USE_DEFAULT_SLOWDOWN_SPEED; for (AdjustUsingItemSpeed callback : callbacks) { float currentSpeed = callback.adjustUsingItemSpeed(player); @@ -46,7 +46,7 @@ public final class ClientPlayerEvents { } } - return maxSpeed == -0.1F ? DEFAULT_SLOWDOWN_SPEED : maxSpeed; + return maxSpeed == USE_DEFAULT_SLOWDOWN_SPEED ? DEFAULT_SLOWDOWN_SPEED : maxSpeed; }); @FunctionalInterface @@ -56,7 +56,7 @@ public interface AdjustUsingItemSpeed { * * @param player the player is moving during using an item. * @return the percentage of the player's speed from 0.0F to 1.0F. - * {@link #DEFAULT_SLOWDOWN_SPEED} indicates that no adjustment should be applied. + * Return {@link #USE_DEFAULT_SLOWDOWN_SPEED} indicates that no adjustment should be applied. */ float adjustUsingItemSpeed(ClientPlayerEntity player); } diff --git a/fabric-entity-events-v1/src/testmodClient/java/net/fabricmc/fabric/test/entity/event/client/EntityEventTestsClient.java b/fabric-entity-events-v1/src/testmodClient/java/net/fabricmc/fabric/test/entity/event/client/EntityEventTestsClient.java index d3e2916e36..fffcd79615 100644 --- a/fabric-entity-events-v1/src/testmodClient/java/net/fabricmc/fabric/test/entity/event/client/EntityEventTestsClient.java +++ b/fabric-entity-events-v1/src/testmodClient/java/net/fabricmc/fabric/test/entity/event/client/EntityEventTestsClient.java @@ -47,7 +47,7 @@ public void onInitializeClient() { return 0.0F; } - return ClientPlayerEvents.USING_DEFAULT_SLOWDOWN_SPEED; + return ClientPlayerEvents.USE_DEFAULT_SLOWDOWN_SPEED; }); ClientPlayerEvents.ADJUST_USING_ITEM_SPEED.register(player -> { @@ -56,7 +56,7 @@ public void onInitializeClient() { return 0.5F; } - return ClientPlayerEvents.USING_DEFAULT_SLOWDOWN_SPEED; + return ClientPlayerEvents.USE_DEFAULT_SLOWDOWN_SPEED; }); } } From 3e10bb3667a80abd120d65153ef2d84c4b47734e Mon Sep 17 00:00:00 2001 From: fishshi <2855691008@qq.com> Date: Tue, 22 Oct 2024 19:42:18 +0800 Subject: [PATCH 09/14] change return value to nullable Float and imporve something --- .../event/client/ClientPlayerEvents.java | 29 +++++++------------ .../entity/event/ClientPlayerMixin.java | 11 ++++--- .../event/client/EntityEventTestsClient.java | 4 +-- 3 files changed, 20 insertions(+), 24 deletions(-) diff --git a/fabric-entity-events-v1/src/client/java/net/fabricmc/fabric/api/entity/event/client/ClientPlayerEvents.java b/fabric-entity-events-v1/src/client/java/net/fabricmc/fabric/api/entity/event/client/ClientPlayerEvents.java index 61da2b324b..85464661fc 100644 --- a/fabric-entity-events-v1/src/client/java/net/fabricmc/fabric/api/entity/event/client/ClientPlayerEvents.java +++ b/fabric-entity-events-v1/src/client/java/net/fabricmc/fabric/api/entity/event/client/ClientPlayerEvents.java @@ -16,37 +16,29 @@ package net.fabricmc.fabric.api.entity.event.client; +import org.jetbrains.annotations.Nullable; + import net.minecraft.client.network.ClientPlayerEntity; import net.fabricmc.fabric.api.event.Event; import net.fabricmc.fabric.api.event.EventFactory; public final class ClientPlayerEvents { - /* - * Flag for using default slowdown during using an item. - */ - public static final float USE_DEFAULT_SLOWDOWN_SPEED = -1.0F; - - /* - * Default percentage of speed slowdown when using an item in Minecraft. - */ - private static final float DEFAULT_SLOWDOWN_SPEED = 0.2F; - /** * An event that is called when a player is moving during using an item. */ public static final Event ADJUST_USING_ITEM_SPEED = EventFactory.createArrayBacked(AdjustUsingItemSpeed.class, callbacks -> player -> { - float maxSpeed = USE_DEFAULT_SLOWDOWN_SPEED; + Float maxSpeedPercentage = null; for (AdjustUsingItemSpeed callback : callbacks) { - float currentSpeed = callback.adjustUsingItemSpeed(player); + Float speedPercentage = callback.adjustUsingItemSpeed(player); - if (currentSpeed >= 0.0F && currentSpeed <= 1.0F) { - maxSpeed = currentSpeed > maxSpeed ? currentSpeed : maxSpeed; + if (speedPercentage != null && speedPercentage >= 0.0F && speedPercentage <= 1.0F) { + maxSpeedPercentage = maxSpeedPercentage == null ? speedPercentage : Math.max(speedPercentage, maxSpeedPercentage); } } - return maxSpeed == USE_DEFAULT_SLOWDOWN_SPEED ? DEFAULT_SLOWDOWN_SPEED : maxSpeed; + return maxSpeedPercentage; }); @FunctionalInterface @@ -55,9 +47,10 @@ public interface AdjustUsingItemSpeed { * Called when a player is moving during using an item. * * @param player the player is moving during using an item. - * @return the percentage of the player's speed from 0.0F to 1.0F. - * Return {@link #USE_DEFAULT_SLOWDOWN_SPEED} indicates that no adjustment should be applied. + * @return the percentage of player's speed from 0.0F to 1.0F, + * or {@code null} indicates that no adjustment should be applied. */ - float adjustUsingItemSpeed(ClientPlayerEntity player); + @Nullable + Float adjustUsingItemSpeed(ClientPlayerEntity player); } } diff --git a/fabric-entity-events-v1/src/client/java/net/fabricmc/fabric/mixin/client/entity/event/ClientPlayerMixin.java b/fabric-entity-events-v1/src/client/java/net/fabricmc/fabric/mixin/client/entity/event/ClientPlayerMixin.java index 27cc9f12ef..7ef51e8aec 100644 --- a/fabric-entity-events-v1/src/client/java/net/fabricmc/fabric/mixin/client/entity/event/ClientPlayerMixin.java +++ b/fabric-entity-events-v1/src/client/java/net/fabricmc/fabric/mixin/client/entity/event/ClientPlayerMixin.java @@ -28,13 +28,16 @@ @Mixin(ClientPlayerEntity.class) abstract class ClientPlayerMixin { @Inject(method = "tickMovement", at = @At(value = "INVOKE", target = "Lnet/minecraft/client/network/ClientPlayerEntity;isUsingItem()Z")) - private void beforeTickMovement(CallbackInfo ci) { + private void invokeAdjustUsingItemSpeedEvent(CallbackInfo ci) { ClientPlayerEntity player = (ClientPlayerEntity) (Object) this; if (player.isUsingItem() && !player.hasVehicle() && (player.input.movementForward != 0.0F || player.input.movementSideways != 0.0F)) { - float slowdown = ClientPlayerEvents.ADJUST_USING_ITEM_SPEED.invoker().adjustUsingItemSpeed(player) * 5.0F; - player.input.movementSideways *= slowdown; - player.input.movementForward *= slowdown; + Float speedPercentage = ClientPlayerEvents.ADJUST_USING_ITEM_SPEED.invoker().adjustUsingItemSpeed(player); + + if (speedPercentage != null) { + player.input.movementSideways *= speedPercentage * 5.0F; + player.input.movementForward *= speedPercentage * 5.0F; + } } } } diff --git a/fabric-entity-events-v1/src/testmodClient/java/net/fabricmc/fabric/test/entity/event/client/EntityEventTestsClient.java b/fabric-entity-events-v1/src/testmodClient/java/net/fabricmc/fabric/test/entity/event/client/EntityEventTestsClient.java index fffcd79615..33c0c7ad39 100644 --- a/fabric-entity-events-v1/src/testmodClient/java/net/fabricmc/fabric/test/entity/event/client/EntityEventTestsClient.java +++ b/fabric-entity-events-v1/src/testmodClient/java/net/fabricmc/fabric/test/entity/event/client/EntityEventTestsClient.java @@ -47,7 +47,7 @@ public void onInitializeClient() { return 0.0F; } - return ClientPlayerEvents.USE_DEFAULT_SLOWDOWN_SPEED; + return null; }); ClientPlayerEvents.ADJUST_USING_ITEM_SPEED.register(player -> { @@ -56,7 +56,7 @@ public void onInitializeClient() { return 0.5F; } - return ClientPlayerEvents.USE_DEFAULT_SLOWDOWN_SPEED; + return null; }); } } From 5051dff2390d2bd639ef7ca089f3fadeabf937db Mon Sep 17 00:00:00 2001 From: fishshi <2855691008@qq.com> Date: Tue, 22 Oct 2024 19:53:07 +0800 Subject: [PATCH 10/14] remove the limit on percentage --- .../fabric/api/entity/event/client/ClientPlayerEvents.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/fabric-entity-events-v1/src/client/java/net/fabricmc/fabric/api/entity/event/client/ClientPlayerEvents.java b/fabric-entity-events-v1/src/client/java/net/fabricmc/fabric/api/entity/event/client/ClientPlayerEvents.java index 85464661fc..fa4054c283 100644 --- a/fabric-entity-events-v1/src/client/java/net/fabricmc/fabric/api/entity/event/client/ClientPlayerEvents.java +++ b/fabric-entity-events-v1/src/client/java/net/fabricmc/fabric/api/entity/event/client/ClientPlayerEvents.java @@ -33,7 +33,7 @@ public final class ClientPlayerEvents { for (AdjustUsingItemSpeed callback : callbacks) { Float speedPercentage = callback.adjustUsingItemSpeed(player); - if (speedPercentage != null && speedPercentage >= 0.0F && speedPercentage <= 1.0F) { + if (speedPercentage != null) { maxSpeedPercentage = maxSpeedPercentage == null ? speedPercentage : Math.max(speedPercentage, maxSpeedPercentage); } } @@ -47,7 +47,7 @@ public interface AdjustUsingItemSpeed { * Called when a player is moving during using an item. * * @param player the player is moving during using an item. - * @return the percentage of player's speed from 0.0F to 1.0F, + * @return a Float representing the speed adjustment as a percentage (e.g., 0.8 for 80% speed), * or {@code null} indicates that no adjustment should be applied. */ @Nullable From b971c7949cf497fe07dc4218d0ce86dbac0eabf2 Mon Sep 17 00:00:00 2001 From: fishshi <2855691008@qq.com> Date: Mon, 28 Oct 2024 10:36:06 +0800 Subject: [PATCH 11/14] improve test cases --- .../event/client/EntityEventTestsClient.java | 21 +++++++++++++++++-- 1 file changed, 19 insertions(+), 2 deletions(-) diff --git a/fabric-entity-events-v1/src/testmodClient/java/net/fabricmc/fabric/test/entity/event/client/EntityEventTestsClient.java b/fabric-entity-events-v1/src/testmodClient/java/net/fabricmc/fabric/test/entity/event/client/EntityEventTestsClient.java index 33c0c7ad39..20f61f3110 100644 --- a/fabric-entity-events-v1/src/testmodClient/java/net/fabricmc/fabric/test/entity/event/client/EntityEventTestsClient.java +++ b/fabric-entity-events-v1/src/testmodClient/java/net/fabricmc/fabric/test/entity/event/client/EntityEventTestsClient.java @@ -36,12 +36,28 @@ public void onInitializeClient() { return !player.getEquippedStack(EquipmentSlot.CHEST).isOf(EntityEventTests.DIAMOND_ELYTRA); }); + // the most common usage is to modify using item speed based on main hand item, most for custom items. ClientPlayerEvents.ADJUST_USING_ITEM_SPEED.register(player -> { - if (player.getMainHandStack().isOf(Items.BOW) && player.getEquippedStack(EquipmentSlot.FEET).isOf(Items.LEATHER_BOOTS)) { - LOGGER.info("Player {} can move with normal speed becase of leather boots on feet.", player); + if (player.getMainHandStack().isOf(Items.GOLDEN_APPLE)) { + LOGGER.info("Player {} can move with half speed when eating golden apple.", player); + return 0.5F; + } + + if (player.getMainHandStack().isOf(Items.ENCHANTED_GOLDEN_APPLE)) { + LOGGER.info("Player {} can move with normal speed when eating enchanted golden apple.", player); return 1.0F; } + return null; + }); + + // another possible usages is to modify using item speed when certain conditions (e.g. equipped items) are met. + ClientPlayerEvents.ADJUST_USING_ITEM_SPEED.register(player -> { + if (player.getMainHandStack().isOf(Items.BOW) && player.getEquippedStack(EquipmentSlot.FEET).isOf(Items.LEATHER_BOOTS)) { + LOGGER.info("Player {} can move with 80% speed becase of leather boots on feet.", player); + return 0.8F; + } + if (player.getMainHandStack().isOf(Items.BOW) && player.getEquippedStack(EquipmentSlot.FEET).isOf(Items.IRON_BOOTS)) { LOGGER.info("Player {} can't move during using bow becase of iron boots on feet.", player); return 0.0F; @@ -50,6 +66,7 @@ public void onInitializeClient() { return null; }); + // this is used to test when multiple mods modify the using item speed together. ClientPlayerEvents.ADJUST_USING_ITEM_SPEED.register(player -> { if (player.getMainHandStack().isOf(Items.BOW) && player.getOffHandStack().isOf(Items.FEATHER)) { LOGGER.info("Player {} can move with half speed becase of feather in offhand.", player); From fadeaadab78e00e768d88a93ee95e9ad40535fc5 Mon Sep 17 00:00:00 2001 From: fishshi <2855691008@qq.com> Date: Sat, 9 Nov 2024 01:25:50 +0800 Subject: [PATCH 12/14] rename to MODIFY_ITEM_USE_SPEED --- .../entity/event/client/ClientPlayerEvents.java | 14 +++++++------- .../client/entity/event/ClientPlayerMixin.java | 4 ++-- .../event/client/EntityEventTestsClient.java | 6 +++--- 3 files changed, 12 insertions(+), 12 deletions(-) diff --git a/fabric-entity-events-v1/src/client/java/net/fabricmc/fabric/api/entity/event/client/ClientPlayerEvents.java b/fabric-entity-events-v1/src/client/java/net/fabricmc/fabric/api/entity/event/client/ClientPlayerEvents.java index fa4054c283..52d856a548 100644 --- a/fabric-entity-events-v1/src/client/java/net/fabricmc/fabric/api/entity/event/client/ClientPlayerEvents.java +++ b/fabric-entity-events-v1/src/client/java/net/fabricmc/fabric/api/entity/event/client/ClientPlayerEvents.java @@ -27,11 +27,11 @@ public final class ClientPlayerEvents { /** * An event that is called when a player is moving during using an item. */ - public static final Event ADJUST_USING_ITEM_SPEED = EventFactory.createArrayBacked(AdjustUsingItemSpeed.class, callbacks -> player -> { + public static final Event MODIFY_ITEM_USE_SPEED = EventFactory.createArrayBacked(ModifyItemUseSpeed.class, callbacks -> player -> { Float maxSpeedPercentage = null; - for (AdjustUsingItemSpeed callback : callbacks) { - Float speedPercentage = callback.adjustUsingItemSpeed(player); + for (ModifyItemUseSpeed callback : callbacks) { + Float speedPercentage = callback.modifyItemUseSpeed(player); if (speedPercentage != null) { maxSpeedPercentage = maxSpeedPercentage == null ? speedPercentage : Math.max(speedPercentage, maxSpeedPercentage); @@ -42,15 +42,15 @@ public final class ClientPlayerEvents { }); @FunctionalInterface - public interface AdjustUsingItemSpeed { + public interface ModifyItemUseSpeed { /** * Called when a player is moving during using an item. * * @param player the player is moving during using an item. - * @return a Float representing the speed adjustment as a percentage (e.g., 0.8 for 80% speed), - * or {@code null} indicates that no adjustment should be applied. + * @return a Float representing the speed modifyment as a percentage (e.g., 0.8 for 80% speed), + * or {@code null} indicates that no modifyment should be applied. */ @Nullable - Float adjustUsingItemSpeed(ClientPlayerEntity player); + Float modifyItemUseSpeed(ClientPlayerEntity player); } } diff --git a/fabric-entity-events-v1/src/client/java/net/fabricmc/fabric/mixin/client/entity/event/ClientPlayerMixin.java b/fabric-entity-events-v1/src/client/java/net/fabricmc/fabric/mixin/client/entity/event/ClientPlayerMixin.java index 7ef51e8aec..9cdf491e57 100644 --- a/fabric-entity-events-v1/src/client/java/net/fabricmc/fabric/mixin/client/entity/event/ClientPlayerMixin.java +++ b/fabric-entity-events-v1/src/client/java/net/fabricmc/fabric/mixin/client/entity/event/ClientPlayerMixin.java @@ -28,11 +28,11 @@ @Mixin(ClientPlayerEntity.class) abstract class ClientPlayerMixin { @Inject(method = "tickMovement", at = @At(value = "INVOKE", target = "Lnet/minecraft/client/network/ClientPlayerEntity;isUsingItem()Z")) - private void invokeAdjustUsingItemSpeedEvent(CallbackInfo ci) { + private void invokeModifyItemUseSpeedEvent(CallbackInfo ci) { ClientPlayerEntity player = (ClientPlayerEntity) (Object) this; if (player.isUsingItem() && !player.hasVehicle() && (player.input.movementForward != 0.0F || player.input.movementSideways != 0.0F)) { - Float speedPercentage = ClientPlayerEvents.ADJUST_USING_ITEM_SPEED.invoker().adjustUsingItemSpeed(player); + Float speedPercentage = ClientPlayerEvents.MODIFY_ITEM_USE_SPEED.invoker().modifyItemUseSpeed(player); if (speedPercentage != null) { player.input.movementSideways *= speedPercentage * 5.0F; diff --git a/fabric-entity-events-v1/src/testmodClient/java/net/fabricmc/fabric/test/entity/event/client/EntityEventTestsClient.java b/fabric-entity-events-v1/src/testmodClient/java/net/fabricmc/fabric/test/entity/event/client/EntityEventTestsClient.java index 20f61f3110..0a79e6d422 100644 --- a/fabric-entity-events-v1/src/testmodClient/java/net/fabricmc/fabric/test/entity/event/client/EntityEventTestsClient.java +++ b/fabric-entity-events-v1/src/testmodClient/java/net/fabricmc/fabric/test/entity/event/client/EntityEventTestsClient.java @@ -37,7 +37,7 @@ public void onInitializeClient() { }); // the most common usage is to modify using item speed based on main hand item, most for custom items. - ClientPlayerEvents.ADJUST_USING_ITEM_SPEED.register(player -> { + ClientPlayerEvents.MODIFY_ITEM_USE_SPEED.register(player -> { if (player.getMainHandStack().isOf(Items.GOLDEN_APPLE)) { LOGGER.info("Player {} can move with half speed when eating golden apple.", player); return 0.5F; @@ -52,7 +52,7 @@ public void onInitializeClient() { }); // another possible usages is to modify using item speed when certain conditions (e.g. equipped items) are met. - ClientPlayerEvents.ADJUST_USING_ITEM_SPEED.register(player -> { + ClientPlayerEvents.MODIFY_ITEM_USE_SPEED.register(player -> { if (player.getMainHandStack().isOf(Items.BOW) && player.getEquippedStack(EquipmentSlot.FEET).isOf(Items.LEATHER_BOOTS)) { LOGGER.info("Player {} can move with 80% speed becase of leather boots on feet.", player); return 0.8F; @@ -67,7 +67,7 @@ public void onInitializeClient() { }); // this is used to test when multiple mods modify the using item speed together. - ClientPlayerEvents.ADJUST_USING_ITEM_SPEED.register(player -> { + ClientPlayerEvents.MODIFY_ITEM_USE_SPEED.register(player -> { if (player.getMainHandStack().isOf(Items.BOW) && player.getOffHandStack().isOf(Items.FEATHER)) { LOGGER.info("Player {} can move with half speed becase of feather in offhand.", player); return 0.5F; From 61163e236d325df961910eb3fcd3694ba3f0d072 Mon Sep 17 00:00:00 2001 From: fishshi <2855691008@qq.com> Date: Sat, 9 Nov 2024 11:59:21 +0800 Subject: [PATCH 13/14] Update javadoc Co-authored-by: Alexander01998 --- .../api/entity/event/client/ClientPlayerEvents.java | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/fabric-entity-events-v1/src/client/java/net/fabricmc/fabric/api/entity/event/client/ClientPlayerEvents.java b/fabric-entity-events-v1/src/client/java/net/fabricmc/fabric/api/entity/event/client/ClientPlayerEvents.java index 52d856a548..ac59a57a62 100644 --- a/fabric-entity-events-v1/src/client/java/net/fabricmc/fabric/api/entity/event/client/ClientPlayerEvents.java +++ b/fabric-entity-events-v1/src/client/java/net/fabricmc/fabric/api/entity/event/client/ClientPlayerEvents.java @@ -44,11 +44,11 @@ public final class ClientPlayerEvents { @FunctionalInterface public interface ModifyItemUseSpeed { /** - * Called when a player is moving during using an item. + * Called when a player is moving while using an item. * - * @param player the player is moving during using an item. - * @return a Float representing the speed modifyment as a percentage (e.g., 0.8 for 80% speed), - * or {@code null} indicates that no modifyment should be applied. + * @param player the player that is moving while using an item. + * @return a Float representing the speed modification as a percentage (e.g., 0.8 for 80% speed), + * or {@code null} indicates that no modification should be applied. */ @Nullable Float modifyItemUseSpeed(ClientPlayerEntity player); From 987de610d3ea2f0540a31533685fb7a3126bf172 Mon Sep 17 00:00:00 2001 From: fishshi <2855691008@qq.com> Date: Sat, 9 Nov 2024 23:14:22 +0800 Subject: [PATCH 14/14] rename to MODIFY_ITEM_USE_MOVEMENT_SPEED --- .../api/entity/event/client/ClientPlayerEvents.java | 12 ++++++------ .../mixin/client/entity/event/ClientPlayerMixin.java | 4 ++-- .../entity/event/client/EntityEventTestsClient.java | 6 +++--- 3 files changed, 11 insertions(+), 11 deletions(-) diff --git a/fabric-entity-events-v1/src/client/java/net/fabricmc/fabric/api/entity/event/client/ClientPlayerEvents.java b/fabric-entity-events-v1/src/client/java/net/fabricmc/fabric/api/entity/event/client/ClientPlayerEvents.java index ac59a57a62..20a570d614 100644 --- a/fabric-entity-events-v1/src/client/java/net/fabricmc/fabric/api/entity/event/client/ClientPlayerEvents.java +++ b/fabric-entity-events-v1/src/client/java/net/fabricmc/fabric/api/entity/event/client/ClientPlayerEvents.java @@ -25,13 +25,13 @@ public final class ClientPlayerEvents { /** - * An event that is called when a player is moving during using an item. + * An event that is called when a player is moving while using an item. */ - public static final Event MODIFY_ITEM_USE_SPEED = EventFactory.createArrayBacked(ModifyItemUseSpeed.class, callbacks -> player -> { + public static final Event MODIFY_ITEM_USE_MOVEMENT_SPEED = EventFactory.createArrayBacked(ModifyItemUseMovementSpeed.class, callbacks -> player -> { Float maxSpeedPercentage = null; - for (ModifyItemUseSpeed callback : callbacks) { - Float speedPercentage = callback.modifyItemUseSpeed(player); + for (ModifyItemUseMovementSpeed callback : callbacks) { + Float speedPercentage = callback.modifyItemUseMovementSpeed(player); if (speedPercentage != null) { maxSpeedPercentage = maxSpeedPercentage == null ? speedPercentage : Math.max(speedPercentage, maxSpeedPercentage); @@ -42,7 +42,7 @@ public final class ClientPlayerEvents { }); @FunctionalInterface - public interface ModifyItemUseSpeed { + public interface ModifyItemUseMovementSpeed { /** * Called when a player is moving while using an item. * @@ -51,6 +51,6 @@ public interface ModifyItemUseSpeed { * or {@code null} indicates that no modification should be applied. */ @Nullable - Float modifyItemUseSpeed(ClientPlayerEntity player); + Float modifyItemUseMovementSpeed(ClientPlayerEntity player); } } diff --git a/fabric-entity-events-v1/src/client/java/net/fabricmc/fabric/mixin/client/entity/event/ClientPlayerMixin.java b/fabric-entity-events-v1/src/client/java/net/fabricmc/fabric/mixin/client/entity/event/ClientPlayerMixin.java index 9cdf491e57..911f4c534b 100644 --- a/fabric-entity-events-v1/src/client/java/net/fabricmc/fabric/mixin/client/entity/event/ClientPlayerMixin.java +++ b/fabric-entity-events-v1/src/client/java/net/fabricmc/fabric/mixin/client/entity/event/ClientPlayerMixin.java @@ -28,11 +28,11 @@ @Mixin(ClientPlayerEntity.class) abstract class ClientPlayerMixin { @Inject(method = "tickMovement", at = @At(value = "INVOKE", target = "Lnet/minecraft/client/network/ClientPlayerEntity;isUsingItem()Z")) - private void invokeModifyItemUseSpeedEvent(CallbackInfo ci) { + private void invokeModifyItemUseMovementSpeedEvent(CallbackInfo ci) { ClientPlayerEntity player = (ClientPlayerEntity) (Object) this; if (player.isUsingItem() && !player.hasVehicle() && (player.input.movementForward != 0.0F || player.input.movementSideways != 0.0F)) { - Float speedPercentage = ClientPlayerEvents.MODIFY_ITEM_USE_SPEED.invoker().modifyItemUseSpeed(player); + Float speedPercentage = ClientPlayerEvents.MODIFY_ITEM_USE_MOVEMENT_SPEED.invoker().modifyItemUseMovementSpeed(player); if (speedPercentage != null) { player.input.movementSideways *= speedPercentage * 5.0F; diff --git a/fabric-entity-events-v1/src/testmodClient/java/net/fabricmc/fabric/test/entity/event/client/EntityEventTestsClient.java b/fabric-entity-events-v1/src/testmodClient/java/net/fabricmc/fabric/test/entity/event/client/EntityEventTestsClient.java index 0a79e6d422..1a78032059 100644 --- a/fabric-entity-events-v1/src/testmodClient/java/net/fabricmc/fabric/test/entity/event/client/EntityEventTestsClient.java +++ b/fabric-entity-events-v1/src/testmodClient/java/net/fabricmc/fabric/test/entity/event/client/EntityEventTestsClient.java @@ -37,7 +37,7 @@ public void onInitializeClient() { }); // the most common usage is to modify using item speed based on main hand item, most for custom items. - ClientPlayerEvents.MODIFY_ITEM_USE_SPEED.register(player -> { + ClientPlayerEvents.MODIFY_ITEM_USE_MOVEMENT_SPEED.register(player -> { if (player.getMainHandStack().isOf(Items.GOLDEN_APPLE)) { LOGGER.info("Player {} can move with half speed when eating golden apple.", player); return 0.5F; @@ -52,7 +52,7 @@ public void onInitializeClient() { }); // another possible usages is to modify using item speed when certain conditions (e.g. equipped items) are met. - ClientPlayerEvents.MODIFY_ITEM_USE_SPEED.register(player -> { + ClientPlayerEvents.MODIFY_ITEM_USE_MOVEMENT_SPEED.register(player -> { if (player.getMainHandStack().isOf(Items.BOW) && player.getEquippedStack(EquipmentSlot.FEET).isOf(Items.LEATHER_BOOTS)) { LOGGER.info("Player {} can move with 80% speed becase of leather boots on feet.", player); return 0.8F; @@ -67,7 +67,7 @@ public void onInitializeClient() { }); // this is used to test when multiple mods modify the using item speed together. - ClientPlayerEvents.MODIFY_ITEM_USE_SPEED.register(player -> { + ClientPlayerEvents.MODIFY_ITEM_USE_MOVEMENT_SPEED.register(player -> { if (player.getMainHandStack().isOf(Items.BOW) && player.getOffHandStack().isOf(Items.FEATHER)) { LOGGER.info("Player {} can move with half speed becase of feather in offhand.", player); return 0.5F;