superb-warfare/src/main/java/com/atsuishio/superbwarfare/item/LungeMine.java
2025-01-17 03:20:04 +08:00

149 lines
No EOL
6.1 KiB
Java

package com.atsuishio.superbwarfare.item;
import com.atsuishio.superbwarfare.client.renderer.item.LungeMineRenderer;
import com.atsuishio.superbwarfare.event.ClientEventHandler;
import com.atsuishio.superbwarfare.init.ModSounds;
import net.minecraft.client.Minecraft;
import net.minecraft.client.model.HumanoidModel;
import net.minecraft.client.player.LocalPlayer;
import net.minecraft.client.renderer.BlockEntityWithoutLevelRenderer;
import net.minecraft.core.BlockPos;
import net.minecraft.server.level.ServerPlayer;
import net.minecraft.sounds.SoundSource;
import net.minecraft.util.Mth;
import net.minecraft.world.InteractionHand;
import net.minecraft.world.InteractionResultHolder;
import net.minecraft.world.effect.MobEffectInstance;
import net.minecraft.world.effect.MobEffects;
import net.minecraft.world.entity.HumanoidArm;
import net.minecraft.world.entity.LivingEntity;
import net.minecraft.world.entity.player.Player;
import net.minecraft.world.item.Item;
import net.minecraft.world.item.ItemDisplayContext;
import net.minecraft.world.item.ItemStack;
import net.minecraft.world.level.Level;
import net.minecraft.world.level.block.state.BlockState;
import net.minecraftforge.client.extensions.common.IClientItemExtensions;
import org.jetbrains.annotations.NotNull;
import software.bernie.geckolib.animatable.GeoItem;
import software.bernie.geckolib.core.animatable.instance.AnimatableInstanceCache;
import software.bernie.geckolib.core.animation.AnimatableManager;
import software.bernie.geckolib.core.animation.AnimationController;
import software.bernie.geckolib.core.animation.AnimationState;
import software.bernie.geckolib.core.animation.RawAnimation;
import software.bernie.geckolib.core.object.PlayState;
import software.bernie.geckolib.util.GeckoLibUtil;
import java.util.function.Consumer;
public class LungeMine extends Item implements GeoItem, AnimatedItem {
private final AnimatableInstanceCache cache = GeckoLibUtil.createInstanceCache(this);
public static ItemDisplayContext transformType;
public LungeMine() {
super(new Properties().stacksTo(4));
}
@Override
public void initializeClient(Consumer<IClientItemExtensions> consumer) {
super.initializeClient(consumer);
consumer.accept(new IClientItemExtensions() {
private final BlockEntityWithoutLevelRenderer renderer = new LungeMineRenderer();
@Override
public BlockEntityWithoutLevelRenderer getCustomRenderer() {
return renderer;
}
private static final HumanoidModel.ArmPose LungeMinePose = HumanoidModel.ArmPose.create("LungeMine", false, (model, entity, arm) -> {
if (arm != HumanoidArm.LEFT) {
model.rightArm.xRot = 20f * Mth.DEG_TO_RAD + model.head.xRot;
model.rightArm.yRot = -12f * Mth.DEG_TO_RAD;
model.leftArm.xRot = -45f * Mth.DEG_TO_RAD + model.head.xRot;
model.leftArm.yRot = 40f * Mth.DEG_TO_RAD;
}
});
@Override
public HumanoidModel.ArmPose getArmPose(LivingEntity entityLiving, InteractionHand hand, ItemStack itemStack) {
if (!itemStack.isEmpty()) {
if (entityLiving.getUsedItemHand() == hand) {
return LungeMinePose;
}
}
return HumanoidModel.ArmPose.EMPTY;
}
});
}
public void getTransformType(ItemDisplayContext type) {
transformType = type;
}
private PlayState idlePredicate(AnimationState<LungeMine> event) {
LocalPlayer player = Minecraft.getInstance().player;
if (player == null) return PlayState.STOP;
if (ClientEventHandler.lungeSprint > 0) {
return event.setAndContinue(RawAnimation.begin().thenPlay("animation.lunge_mine.sprint"));
}
if (ClientEventHandler.lungeDraw > 0) {
return event.setAndContinue(RawAnimation.begin().thenPlay("animation.lunge_mine.draw"));
}
if (ClientEventHandler.lungeAttack > 0) {
return event.setAndContinue(RawAnimation.begin().thenPlay("animation.lunge_mine.fire"));
}
if (player.isSprinting() && player.onGround() && ClientEventHandler.lungeDraw == 0) {
return event.setAndContinue(RawAnimation.begin().thenLoop("animation.lunge_mine.run"));
}
return event.setAndContinue(RawAnimation.begin().thenLoop("animation.lunge_mine.idle"));
}
@Override
public void registerControllers(AnimatableManager.ControllerRegistrar data) {
var idleController = new AnimationController<>(this, "idleController", 2, this::idlePredicate);
data.add(idleController);
}
@Override
public AnimatableInstanceCache getAnimatableInstanceCache() {
return this.cache;
}
@Override
public void setAnimationProcedure(String procedure) {
}
@Override
public boolean onEntitySwing(ItemStack stack, LivingEntity entity) {
return false;
}
@Override
public boolean shouldCauseReequipAnimation(ItemStack oldStack, ItemStack newStack, boolean slotChanged) {
return false;
}
@Override
public @NotNull InteractionResultHolder<ItemStack> use(Level worldIn, Player playerIn, InteractionHand handIn) {
ItemStack stack = playerIn.getItemInHand(handIn);
if (playerIn instanceof ServerPlayer serverPlayer) {
serverPlayer.level().playSound(null, serverPlayer.getOnPos(), ModSounds.LUNGE_MINE_GROWL.get(), SoundSource.PLAYERS, 2, 1);
}
if (!playerIn.level().isClientSide()) {
playerIn.addEffect(new MobEffectInstance(MobEffects.MOVEMENT_SPEED, 100, (playerIn.hasEffect(MobEffects.MOVEMENT_SPEED) ? playerIn.getEffect(MobEffects.MOVEMENT_SPEED).getAmplifier() : 0) + 2));
} else {
ClientEventHandler.lungeSprint = 180;
}
playerIn.getCooldowns().addCooldown(stack.getItem(), 300);
return InteractionResultHolder.consume(stack);
}
@Override
public boolean canAttackBlock(BlockState p_41441_, Level p_41442_, BlockPos p_41443_, Player p_41444_) {
return false;
}
}