diff --git a/src/main/java/org/betterx/bclib/items/elytra/BCLElytraItem.java b/src/main/java/org/betterx/bclib/items/elytra/BCLElytraItem.java index f2474357..4dea1c1e 100644 --- a/src/main/java/org/betterx/bclib/items/elytra/BCLElytraItem.java +++ b/src/main/java/org/betterx/bclib/items/elytra/BCLElytraItem.java @@ -1,6 +1,9 @@ package org.betterx.bclib.items.elytra; import net.minecraft.resources.ResourceLocation; +import net.minecraft.world.entity.LivingEntity; +import net.minecraft.world.item.ItemStack; +import net.minecraft.world.level.gameevent.GameEvent; import net.fabricmc.fabric.api.entity.event.v1.FabricElytraItem; @@ -8,4 +11,18 @@ public interface BCLElytraItem extends FabricElytraItem { ResourceLocation getModelTexture(); double getMovementFactor(); + + default void doVanillaElytraTick(LivingEntity entity, ItemStack chestStack) { + if (BCLElytraUtils.onBreak == null) ((FabricElytraItem) this).doVanillaElytraTick(entity, chestStack); + + int nextRoll = entity.getFallFlyingTicks() + 1; + + if (!entity.level.isClientSide && nextRoll % 10 == 0) { + if ((nextRoll / 10) % 2 == 0) { + chestStack.hurtAndBreak(1, entity, (e) -> BCLElytraUtils.onBreak.accept(e, chestStack)); + } + + entity.gameEvent(GameEvent.ELYTRA_GLIDE); + } + } } diff --git a/src/main/java/org/betterx/bclib/items/elytra/BCLElytraUtils.java b/src/main/java/org/betterx/bclib/items/elytra/BCLElytraUtils.java new file mode 100644 index 00000000..d8b86a17 --- /dev/null +++ b/src/main/java/org/betterx/bclib/items/elytra/BCLElytraUtils.java @@ -0,0 +1,18 @@ +package org.betterx.bclib.items.elytra; + +import net.minecraft.world.entity.EquipmentSlot; +import net.minecraft.world.entity.LivingEntity; +import net.minecraft.world.item.ItemStack; + +import java.util.function.BiConsumer; +import java.util.function.Function; + +public class BCLElytraUtils { + @FunctionalInterface + public interface SlotProvider { + ItemStack getElytra(LivingEntity entity, Function slotGetter); + } + + public static SlotProvider slotProvider = null; + public static BiConsumer onBreak = null; +} diff --git a/src/main/java/org/betterx/bclib/mixin/common/elytra/LivingEntityMixin.java b/src/main/java/org/betterx/bclib/mixin/common/elytra/LivingEntityMixin.java index c5f1115a..5a2e5825 100644 --- a/src/main/java/org/betterx/bclib/mixin/common/elytra/LivingEntityMixin.java +++ b/src/main/java/org/betterx/bclib/mixin/common/elytra/LivingEntityMixin.java @@ -1,6 +1,7 @@ package org.betterx.bclib.mixin.common.elytra; import org.betterx.bclib.items.elytra.BCLElytraItem; +import org.betterx.bclib.items.elytra.BCLElytraUtils; import net.minecraft.world.entity.EquipmentSlot; import net.minecraft.world.entity.LivingEntity; @@ -27,9 +28,13 @@ public abstract class LivingEntityMixin { at = @At(value = "INVOKE", target = "Lnet/minecraft/world/entity/LivingEntity;setDeltaMovement(Lnet/minecraft/world/phys/Vec3;)V") ) public Vec3 be_travel(Vec3 moveDelta) { - ItemStack itemStack = getItemBySlot(EquipmentSlot.CHEST); - double movementFactor = ((BCLElytraItem) itemStack.getItem()).getMovementFactor(); - moveDelta = moveDelta.multiply(movementFactor, 1.0D, movementFactor); + ItemStack itemStack; + if (BCLElytraUtils.slotProvider == null) itemStack = getItemBySlot(EquipmentSlot.CHEST); + else itemStack = BCLElytraUtils.slotProvider.getElytra((LivingEntity) (Object) this, this::getItemBySlot); + if (itemStack != null && itemStack.getItem() instanceof BCLElytraItem elytra) { + double movementFactor = elytra.getMovementFactor(); + moveDelta = moveDelta.multiply(movementFactor, 1.0D, movementFactor); + } return moveDelta; } }