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

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

There is a newer version: 8.10
Show newest version
package com.magistuarmory.item;

import dev.architectury.registry.item.ItemPropertiesRegistry;
import net.fabricmc.api.EnvType;
import net.fabricmc.api.Environment;
import net.minecraft.class_1268;
import net.minecraft.class_1271;
import net.minecraft.class_1309;
import net.minecraft.class_1657;
import net.minecraft.class_1744;
import net.minecraft.class_1764;
import net.minecraft.class_1799;
import net.minecraft.class_1802;
import net.minecraft.class_1890;
import net.minecraft.class_1893;
import net.minecraft.class_1937;
import net.minecraft.class_2487;
import net.minecraft.class_2499;
import net.minecraft.class_2960;
import net.minecraft.class_3414;
import net.minecraft.class_3417;
import net.minecraft.class_3419;

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

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

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

	public void method_7852(class_1937 p_40910_, class_1309 p_40911_, class_1799 p_40912_, int p_40913_) {
		if (!p_40910_.field_9236) {
			int i = class_1890.method_8225(class_1893.field_9098, p_40912_);
			class_3414 soundevent = this.method_7773(i);
			class_3414 soundevent1 = i == 0 ? class_3417.field_14860 : null;
			float f = (float)(p_40912_.method_7935() - 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_.method_43128((class_1657)null, p_40911_.method_23317(), p_40911_.method_23318(), p_40911_.method_23321(), soundevent, class_3419.field_15248, 0.5F, 1.0F);
			}

			if (f >= 0.5F && soundevent1 != null && !this.midLoadSoundPlayed) {
				this.midLoadSoundPlayed = true;
				p_40910_.method_43128((class_1657)null, p_40911_.method_23317(), p_40911_.method_23318(), p_40911_.method_23321(), soundevent1, class_3419.field_15248, 0.5F, 1.0F);
			}
		}

	}

	public void method_7840(class_1799 p_40875_, class_1937 p_40876_, class_1309 p_40877_, int p_40878_) {
		int i = this.method_7881(p_40875_) - p_40878_;
		float f = getPower(i, p_40875_);
		if (f >= 1.0F && !method_7781(p_40875_) && method_7767(p_40877_, p_40875_)) {
			method_7782(p_40875_, true);
			class_3419 soundsource = p_40877_ instanceof class_1657 ? class_3419.field_15248 : class_3419.field_15251;
			p_40876_.method_43128((class_1657)null, p_40877_.method_23317(), p_40877_.method_23318(), p_40877_.method_23321(), class_3417.field_14626, soundsource, 1.0F, 1.0F / (p_40876_.method_8409().method_43057() * 0.5F + 1.0F) + 0.2F);
		}

	}

	private static boolean method_7767(class_1309 p_40860_, class_1799 p_40861_) {
		int i = class_1890.method_8225(class_1893.field_9108, p_40861_);
		int j = i == 0 ? 1 : 3;
		boolean flag = p_40860_ instanceof class_1657 && ((class_1657)p_40860_).method_31549().field_7477;
		class_1799 itemstack = p_40860_.method_18808(p_40861_);
		class_1799 itemstack1 = itemstack.method_7972();

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

			if (itemstack.method_7960() && flag) {
				itemstack = new class_1799(class_1802.field_8107);
				itemstack1 = itemstack.method_7972();
			}

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

		return true;
	}

	private static boolean method_7765(class_1309 p_40863_, class_1799 p_40864_, class_1799 p_40865_, boolean p_40866_, boolean p_40867_) {
		if (p_40865_.method_7960()) {
			return false;
		} else {
			boolean flag = p_40867_ && p_40865_.method_7909() instanceof class_1744;
			class_1799 itemstack;
			if (!flag && !p_40867_ && !p_40866_) {
				itemstack = p_40865_.method_7971(1);
				if (p_40865_.method_7960() && p_40863_ instanceof class_1657) {
					((class_1657)p_40863_).method_31548().method_7378(p_40865_);
				}
			} else {
				itemstack = p_40865_.method_7972();
			}

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

	private static void method_7778(class_1799 p_40929_, class_1799 p_40930_) {
		class_2487 compoundtag = p_40929_.method_7948();
		class_2499 listtag;
		if (compoundtag.method_10573("ChargedProjectiles", 9)) {
			listtag = compoundtag.method_10554("ChargedProjectiles", 10);
		} else {
			listtag = new class_2499();
		}

		class_2487 compoundtag1 = new class_2487();
		p_40930_.method_7953(compoundtag1);
		listtag.add(compoundtag1);
		compoundtag.method_10566("ChargedProjectiles", listtag);
	}

	public class_1271 method_7836(class_1937 p_40920_, class_1657 p_40921_, class_1268 p_40922_) {
		class_1799 itemstack = p_40921_.method_5998(p_40922_);
		if (method_7781(itemstack)) {
			method_7777(p_40920_, p_40921_, p_40922_, itemstack, getProjectileSpeed(itemstack), 1.0F);
			method_7782(itemstack, false);
			return class_1271.method_22428(itemstack);
		} else if (!p_40921_.method_18808(itemstack).method_7960()) {
			if (!method_7781(itemstack)) {
				this.startSoundPlayed = false;
				this.midLoadSoundPlayed = false;
				p_40921_.method_6019(p_40922_);
			}

			return class_1271.method_22428(itemstack);
		} else {
			return class_1271.method_22431(itemstack);
		}
	}

	private class_3414 method_7773(int p_40852_) {
		switch (p_40852_) {
			case 1:
				return class_3417.field_15011;
			case 2:
				return class_3417.field_14916;
			case 3:
				return class_3417.field_15089;
			default:
				return class_3417.field_14765;
		}
	}

	public int getPullTime(class_1799 p_40940_) {
		int i = class_1890.method_8225(class_1893.field_9098, p_40940_);
		return i == 0 ? pullTime : pullTime - 5 * i;
	}

	public float getProjectileSpeed(class_1799 p_40946_) {
		return method_7772(p_40946_, class_1802.field_8639) ? 1.6F : projectileSpeed;
	}

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

		return f;
	}

	@Override
	public int method_7881(class_1799 p_40938_) {
		return getPullTime(p_40938_) + 3;
	}

    @Override
	@Environment(EnvType.CLIENT)
	public void registerModelProperty()
	{
		ItemPropertiesRegistry.register(this, new class_2960("pull"), (stack, level, entity, i) -> {
			if (entity == null) {
				return 0.0F;
			} else {
				return class_1764.method_7781(stack) ? 0.0F : (float)(stack.method_7935() - entity.method_6014()) / (float)getPullTime(stack);
			}
		});
		ItemPropertiesRegistry.register(this, new class_2960("pulling"), (stack, level, entity, i) -> entity != null && entity.method_6115() && entity.method_6030() == stack && !class_1764.method_7781(stack) ? 1.0F : 0.0F);
		ItemPropertiesRegistry.register(this, new class_2960("charged"), (stack, level, entity, i) -> entity != null && class_1764.method_7781(stack) ? 1.0F : 0.0F);
		ItemPropertiesRegistry.register(this, new class_2960("firework"), (stack, level, entity, i) -> entity != null && class_1764.method_7781(stack) && class_1764.method_7772(stack, class_1802.field_8639) ? 1.0F : 0.0F);
	}
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy