[Feature] Generalized Elytra API to simplify Trinkets integration

This commit is contained in:
Frank 2022-06-10 18:24:09 +02:00
parent 104b87d874
commit b9ee21085b
3 changed files with 43 additions and 3 deletions

View file

@ -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);
}
}
}

View file

@ -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<EquipmentSlot, ItemStack> slotGetter);
}
public static SlotProvider slotProvider = null;
public static BiConsumer<LivingEntity, ItemStack> onBreak = null;
}

View file

@ -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();
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;
}
}