All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.magistuarmory.item.MedievalCrossbowItem Maven / Gradle / Ivy

package com.magistuarmory.item;

import dev.architectury.registry.item.ItemPropertiesRegistry;
import net.fabricmc.api.EnvType;
import net.fabricmc.api.Environment;
import net.minecraft.client.renderer.item.ItemProperties;
import net.minecraft.nbt.CompoundTag;
import net.minecraft.nbt.ListTag;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.sounds.SoundEvent;
import net.minecraft.sounds.SoundEvents;
import net.minecraft.sounds.SoundSource;
import net.minecraft.world.InteractionHand;
import net.minecraft.world.InteractionResultHolder;
import net.minecraft.world.entity.LivingEntity;
import net.minecraft.world.entity.player.Player;
import net.minecraft.world.item.ArrowItem;
import net.minecraft.world.item.CrossbowItem;
import net.minecraft.world.item.ItemStack;
import net.minecraft.world.item.Items;
import net.minecraft.world.item.enchantment.EnchantmentHelper;
import net.minecraft.world.item.enchantment.Enchantments;
import net.minecraft.world.level.Level;

public class MedievalCrossbowItem extends CrossbowItem implements IHasModelProperty
{
	private final int pullTime;
    private final float projectileSpeed;

	protected boolean startSoundPlayed = false;
	protected boolean midLoadSoundPlayed = false;

    public MedievalCrossbowItem(Properties p_40660_, float projectileSpeed, int pullTime)
    {
        super(p_40660_);
		this.projectileSpeed = projectileSpeed;
        this.pullTime = pullTime;
    }

	public void m_5929_(Level p_40910_, LivingEntity p_40911_, ItemStack p_40912_, int p_40913_) {
		if (!p_40910_.f_46443_) {
			int i = EnchantmentHelper.m_44843_(Enchantments.f_44960_, p_40912_);
			SoundEvent soundevent = this.m_40851_(i);
			SoundEvent soundevent1 = i == 0 ? SoundEvents.f_11842_ : null;
			float f = (float)(p_40912_.m_41779_() - p_40913_) / (float)getPullTime(p_40912_);
			if (f < 0.2F) {
				this.startSoundPlayed = false;
				this.midLoadSoundPlayed = false;
			}

			if (f >= 0.2F && !this.startSoundPlayed) {
				this.startSoundPlayed = true;
				p_40910_.m_6263_((Player)null, p_40911_.m_20185_(), p_40911_.m_20186_(), p_40911_.m_20189_(), soundevent, SoundSource.PLAYERS, 0.5F, 1.0F);
			}

			if (f >= 0.5F && soundevent1 != null && !this.midLoadSoundPlayed) {
				this.midLoadSoundPlayed = true;
				p_40910_.m_6263_((Player)null, p_40911_.m_20185_(), p_40911_.m_20186_(), p_40911_.m_20189_(), soundevent1, SoundSource.PLAYERS, 0.5F, 1.0F);
			}
		}

	}

	public void m_5551_(ItemStack p_40875_, Level p_40876_, LivingEntity p_40877_, int p_40878_) {
		int i = this.m_8105_(p_40875_) - p_40878_;
		float f = getPower(i, p_40875_);
		if (f >= 1.0F && !m_40932_(p_40875_) && m_40859_(p_40877_, p_40875_)) {
			m_40884_(p_40875_, true);
			SoundSource soundsource = p_40877_ instanceof Player ? SoundSource.PLAYERS : SoundSource.HOSTILE;
			p_40876_.m_6263_((Player)null, p_40877_.m_20185_(), p_40877_.m_20186_(), p_40877_.m_20189_(), SoundEvents.f_11841_, soundsource, 1.0F, 1.0F / (p_40876_.m_213780_().m_188501_() * 0.5F + 1.0F) + 0.2F);
		}

	}

	private static boolean m_40859_(LivingEntity p_40860_, ItemStack p_40861_) {
		int i = EnchantmentHelper.m_44843_(Enchantments.f_44959_, p_40861_);
		int j = i == 0 ? 1 : 3;
		boolean flag = p_40860_ instanceof Player && ((Player)p_40860_).m_150110_().f_35937_;
		ItemStack itemstack = p_40860_.m_6298_(p_40861_);
		ItemStack itemstack1 = itemstack.m_41777_();

		for(int k = 0; k < j; ++k) {
			if (k > 0) {
				itemstack = itemstack1.m_41777_();
			}

			if (itemstack.m_41619_() && flag) {
				itemstack = new ItemStack(Items.f_42412_);
				itemstack1 = itemstack.m_41777_();
			}

			if (!m_40862_(p_40860_, p_40861_, itemstack, k > 0, flag)) {
				return false;
			}
		}

		return true;
	}

	private static boolean m_40862_(LivingEntity p_40863_, ItemStack p_40864_, ItemStack p_40865_, boolean p_40866_, boolean p_40867_) {
		if (p_40865_.m_41619_()) {
			return false;
		} else {
			boolean flag = p_40867_ && p_40865_.m_41720_() instanceof ArrowItem;
			ItemStack itemstack;
			if (!flag && !p_40867_ && !p_40866_) {
				itemstack = p_40865_.m_41620_(1);
				if (p_40865_.m_41619_() && p_40863_ instanceof Player) {
					((Player)p_40863_).m_150109_().m_36057_(p_40865_);
				}
			} else {
				itemstack = p_40865_.m_41777_();
			}

			m_40928_(p_40864_, itemstack);
			return true;
		}
	}

	private static void m_40928_(ItemStack p_40929_, ItemStack p_40930_) {
		CompoundTag compoundtag = p_40929_.m_41784_();
		ListTag listtag;
		if (compoundtag.m_128425_("ChargedProjectiles", 9)) {
			listtag = compoundtag.m_128437_("ChargedProjectiles", 10);
		} else {
			listtag = new ListTag();
		}

		CompoundTag compoundtag1 = new CompoundTag();
		p_40930_.m_41739_(compoundtag1);
		listtag.add(compoundtag1);
		compoundtag.m_128365_("ChargedProjectiles", listtag);
	}

	public InteractionResultHolder m_7203_(Level p_40920_, Player p_40921_, InteractionHand p_40922_) {
		ItemStack itemstack = p_40921_.m_21120_(p_40922_);
		if (m_40932_(itemstack)) {
			m_40887_(p_40920_, p_40921_, p_40922_, itemstack, getProjectileSpeed(itemstack), 1.0F);
			m_40884_(itemstack, false);
			return InteractionResultHolder.m_19096_(itemstack);
		} else if (!p_40921_.m_6298_(itemstack).m_41619_()) {
			if (!m_40932_(itemstack)) {
				this.startSoundPlayed = false;
				this.midLoadSoundPlayed = false;
				p_40921_.m_6672_(p_40922_);
			}

			return InteractionResultHolder.m_19096_(itemstack);
		} else {
			return InteractionResultHolder.m_19100_(itemstack);
		}
	}

	private SoundEvent m_40851_(int p_40852_) {
		switch (p_40852_) {
			case 1:
				return SoundEvents.f_11844_;
			case 2:
				return SoundEvents.f_11845_;
			case 3:
				return SoundEvents.f_11846_;
			default:
				return SoundEvents.f_11843_;
		}
	}

	public int getPullTime(ItemStack p_40940_) {
		int i = EnchantmentHelper.m_44843_(Enchantments.f_44960_, p_40940_);
		return i == 0 ? pullTime : pullTime - 5 * i;
	}

	public float getProjectileSpeed(ItemStack p_40946_) {
		return m_40871_(p_40946_, Items.f_42688_) ? 1.6F : projectileSpeed;
	}

	private float getPower(int p_40854_, ItemStack p_40855_) {
		float f = (float)p_40854_ / (float)getPullTime(p_40855_);
		if (f > 1.0F) {
			f = 1.0F;
		}

		return f;
	}

	@Override
	public int m_8105_(ItemStack p_40938_) {
		return getPullTime(p_40938_) + 3;
	}

    @Override
	@Environment(EnvType.CLIENT)
	public void registerModelProperty()
	{
		ItemPropertiesRegistry.register(this, new ResourceLocation("pull"), (stack, level, entity, i) -> {
			if (entity == null) {
				return 0.0F;
			} else {
				return CrossbowItem.m_40932_(stack) ? 0.0F : (float)(stack.m_41779_() - entity.m_21212_()) / (float)getPullTime(stack);
			}
		});
		ItemPropertiesRegistry.register(this, new ResourceLocation("pulling"), (stack, level, entity, i) -> entity != null && entity.m_6117_() && entity.m_21211_() == stack && !CrossbowItem.m_40932_(stack) ? 1.0F : 0.0F);
		ItemPropertiesRegistry.register(this, new ResourceLocation("charged"), (stack, level, entity, i) -> entity != null && CrossbowItem.m_40932_(stack) ? 1.0F : 0.0F);
		ItemPropertiesRegistry.register(this, new ResourceLocation("firework"), (stack, level, entity, i) -> entity != null && CrossbowItem.m_40932_(stack) && CrossbowItem.m_40871_(stack, Items.f_42688_) ? 1.0F : 0.0F);
	}
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy