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

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

The newest version!
package com.magistuarmory.item;

import com.magistuarmory.EpicKnights;
import com.magistuarmory.client.ClientHelper;
import com.magistuarmory.network.PacketLanceCollision;
import com.magistuarmory.util.CombatHelper;
import com.magistuarmory.util.ModDamageSources;
import dev.architectury.registry.item.ItemPropertiesRegistry;
import net.fabricmc.api.EnvType;
import net.fabricmc.api.Environment;
import net.minecraft.class_124;
import net.minecraft.class_1268;
import net.minecraft.class_1269;
import net.minecraft.class_1271;
import net.minecraft.class_1282;
import net.minecraft.class_1297;
import net.minecraft.class_1308;
import net.minecraft.class_1309;
import net.minecraft.class_1498;
import net.minecraft.class_1657;
import net.minecraft.class_1738;
import net.minecraft.class_1799;
import net.minecraft.class_1802;
import net.minecraft.class_1836;
import net.minecraft.class_1838;
import net.minecraft.class_1839;
import net.minecraft.class_1937;
import net.minecraft.class_238;
import net.minecraft.class_239;
import net.minecraft.class_243;
import net.minecraft.class_2487;
import net.minecraft.class_2561;
import net.minecraft.class_2960;
import net.minecraft.class_310;
import net.minecraft.class_3417;
import net.minecraft.class_3966;
import net.minecraft.class_5134;
import net.minecraft.world.item.*;
import me.shedaniel.cloth.clothconfig.shadowed.blue.endless.jankson.annotation.Nullable;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;


public class LanceItem extends MedievalWeaponItem
{
	private List dropItems = new ArrayList<>();
	protected int clickedticks = 0;

	public LanceItem(class_1793 properties, ModItemTier material, WeaponType type)
	{
		super(properties, material, type);
		this.setDropItems(material);
	}

	public LanceItem setDropItems(ModItemTier material)
	{
		this.dropItems.add(new class_1799(class_1802.field_8600, 2));
		Arrays.stream(material.method_8023().method_8105())
				.filter(stack -> stack.method_31574(class_1802.field_8077))
				.findFirst()
				.ifPresent(stack -> this.dropItems.add(stack));
		return this;
	}

	@Override
	public class_1271 method_7836(class_1937 level, class_1657 player, class_1268 hand)
	{
		this.setRaised(player, !this.isRaised(player));
		return super.method_7836(level, player, hand);
	}

	@Override
	public boolean onAttackClickEntity(class_1799 stack, class_1657 player, class_1297 entity)
	{
		if (EpicKnights.GENERAL_CONFIG.disableLanceCollision)
			return super.onAttackClickEntity(stack, player, entity);

		if (player.method_5765() && !this.isRaised(player) && !player.method_7357().method_7904(this))
			this.clickedticks = 15;
		player.method_6104(class_1268.field_5808);

		return false;
	}

	public void collide(class_1657 player, class_1309 victim, class_1937 level)
	{
		if (player == null || level == null)
			return;

		if (!this.isRaised(player) && !player.method_7357().method_7904(this) && player.method_5765())
		{
			float speed = this.getVelocityProjection(player);

			if ((player.method_5668() instanceof class_1498 &&
					speed >= ((class_1498)player.method_5668()).method_5996(class_5134.field_23719).method_6194())
					|| (!(player.method_5668() instanceof class_1498) && speed >= 0.233))
			{
				boolean dismount = level.method_8409().method_43058() * (this.clickedticks / 15.0 * 0.5 + 0.5) > 0.45;
				PacketLanceCollision.sendToServer(victim.method_5628(), speed, dismount);
				player.method_7350();
			}
		}
	}

	@Override
	public boolean onHurtEntity(class_1282 source, class_1309 victim, float damage)
	{
		if (EpicKnights.GENERAL_CONFIG.disableLanceCollision)
			return super.onHurtEntity(source, victim, damage);

		if (victim.field_6002.method_8608() || ModDamageSources.isAdditional(source) || !(source.method_5529() instanceof class_1309 attacker))
			return true;

		float speed = 0.0f;
		float bonusdamage = 0.0f;
		boolean dismount = false;

		if (attacker instanceof class_1308 mob)
		{
			class_1799 stack = mob.method_6047();
			speed = this.getRideSpeed(stack);
			this.setRideSpeed(stack, 0.0f);
			bonusdamage = this.calcBonusDamage(attacker, speed);
			dismount = this.getDismount(stack);
			this.setDismount(stack, false);
		}
		else if (attacker instanceof class_1657 player)
		{
			class_1799 stack = player.method_6047();
			speed = this.getRideSpeed(stack);
			this.setRideSpeed(stack, 0.0f);
			bonusdamage = this.calcBonusDamage(attacker, speed);
			dismount = this.getDismount(stack);
			this.setDismount(stack, false);

			if (stack.method_7919() >= stack.method_7936() - 1)
				this.onBroken(player);
			else if (!player.method_7337() && (victim.method_6096() >= 18 || victim.method_6039()))
				stack.method_7974(stack.method_7919() + (int) ((0.6 + bonusdamage / 20) * victim.field_6002.method_8409().method_43058() * stack.method_7936()));

			for (class_1799 stack0 : player.method_31548().field_7547)
			{
				this.setRaised(player, true);
				player.method_7357().method_7906(stack0.method_7909(), (int) (40 / (4 + this.getAttackSpeed(stack))));
			}

			if (stack.method_7919() >= stack.method_7936())
			{
				this.onBroken(player);
				stack.method_7939(0);
			}
		}

		class_243 vec = attacker.method_5828(1.0f);
		double magnitude = Math.min(1.0f, speed * this.getTotalMass(attacker) / this.getTotalMass(victim));
		class_243 vel = vec.method_18805(magnitude, magnitude, magnitude);
		
		if (victim.method_5765())
		{
			victim.method_5668().method_18799(victim.method_5668().method_18798().method_1019(vel));
			victim.method_5668().field_6007 = true;
			if (dismount)
				victim.method_5848();
		}
		else
		{
			victim.method_18799(victim.method_18798().method_1019(vel));
			victim.field_6007 = true;
		}

		if (!super.onHurtEntity(source, victim, bonusdamage + damage) && bonusdamage != 0.0f)
		{
			victim.method_5643(ModDamageSources.additional(attacker), bonusdamage + damage);
			return true;
		}

		return false;
	}

	@Override
	public class_1839 method_7853(class_1799 stack)
	{
		return class_1839.field_8953;
	}

	@Override
	public void method_7888(class_1799 stack, class_1937 level, class_1297 entity, int i, boolean selected)
	{
		if (EpicKnights.GENERAL_CONFIG.disableLanceCollision)
		{
			super.method_7888(stack, level, entity, i, selected);
			return;
		}

		if (entity instanceof class_1657 player)
		{
			if (level.field_9236 && player.method_6047().method_7909() instanceof LanceItem)
			{
				class_239 hit = ClientHelper.getMouseOver(class_310.method_1551(), CombatHelper.getAttackReach(player, this));
				if (hit instanceof class_3966 entityhit)
				{
					class_1297 victim = entityhit.method_17782();
					if (player.method_5765() && victim instanceof class_1309 && victim.method_5805() && victim.method_5628() != player.method_5854().method_5628())
						this.collide(player, (class_1309) victim, level);
				}

				if (this.clickedticks > 0)
					this.clickedticks--;
			}

			if (!this.isRaised(player) && player.method_7357().method_7904(this))
				this.setRaised(player, true);
		}
		super.method_7888(stack, level, entity, i, selected);
	}

	@Override
	public class_1269 method_7884(class_1838 context)
	{
		return class_1269.field_5811;
	}

	public float calcBonusDamage(class_1297 entity, float speed)
	{
		float bonusdamage = 3.0f * this.getTotalMass(entity) * speed;
		return Math.min(Math.max(0.0f, bonusdamage), 0.7f * this.attackDamage);
	}

	@Override
	public void method_7851(class_1799 stack, @Nullable class_1937 level, List tooltip, class_1836 flag)
	{
		tooltip.add(class_2561.method_43471("lance.rideronly").method_27692(class_124.field_1078));
		tooltip.add(class_2561.method_43471("lance.leftclick").method_27692(class_124.field_1078));
		tooltip.add(class_2561.method_43471("lance.bonusdamage").method_27692(class_124.field_1078));

		super.method_7851(stack, level, tooltip, flag);
	}


	float getVelocityProjection(class_1297 entity)
	{
		class_243 velocity;
		if (entity.method_5765())
			velocity = entity.method_5668().method_18798();
		else
			velocity = entity.method_18798();

		class_243 vec = entity.method_5828(1.0f);

		if (vec.method_1027() == 0.0f)
			return 0.0f;

		return (float) (velocity.method_1026(vec) / vec.method_1033());
	}


	public float getTotalMass(class_1297 entity)
	{
		float totalmass = this.getMass(entity);

		class_1297 entity2 = entity;
		while (entity2.method_5765())
		{
			entity2 = entity2.method_5854();
			totalmass += this.getMass(entity2);
		}

		for (class_1297 passenger : entity.method_5685())
			totalmass += this.getMass(passenger);

		return totalmass;
	}


	public float getMass(class_1297 entity)
	{
		float mass = 0.0f;

		class_238 box = entity.method_5829();
		if (box != null)
			mass += (box.method_17939()) * (box.method_17940()) * (box.method_17941());

		for (class_1799 armorpiece : entity.method_5661())
		{
			if (!armorpiece.method_7960() && armorpiece.method_7909() instanceof class_1738)
				mass += (((class_1738)armorpiece.method_7909()).method_7687() + ((class_1738)armorpiece.method_7909()).method_26353()) / 20.0;
		}

		return mass;
	}

	public void onBroken(class_1657 player)
	{
		if (player.method_6047() != null && player.method_6047().method_7909() == this)
		{
			for (class_1799 stack : this.dropItems)
				player.method_7328(stack, true);
			player.method_5783(class_3417.field_15075, 1.0f, 1.0f);
		}
	}

	public boolean isRaised(@Nullable class_1309 entity)
	{
		if (entity == null)
			return false;

		class_1799 stack = entity.method_6047();
		if (stack.method_7985())
		{
			class_2487 nbt = stack.method_7969();
			return nbt.method_10545("raised") && nbt.method_10550("raised") == 1;
		}

		return false;
	}

	public void setRaised(class_1309 entity, boolean raised)
	{
		class_1799 stack = entity.method_6047();
		class_2487 nbt = stack.method_7985() ? stack.method_7969() : new class_2487();
		nbt.method_10569("raised", raised ? 1 : 0);
		stack.method_7980(nbt);
	}

	public boolean getDismount(class_1799 stack)
	{
		if (stack.method_7985())
		{
			class_2487 nbt = stack.method_7969();
			if (nbt.method_10545("dismount"))
				return nbt.method_10577("dismount");
		}

		return false;
	}

	public void setDismount(class_1799 stack, boolean dismount)
	{
		class_2487 nbt = stack.method_7948();
		nbt.method_10556("dismount", dismount);
		stack.method_7980(nbt);
	}

	public float getRideSpeed(class_1799 stack)
	{
		if (stack.method_7985())
		{
			class_2487 nbt = stack.method_7969();
			if (nbt.method_10545("rideSpeed"))
				return nbt.method_10583("rideSpeed");
		}

		return 0.0f;
	}

	public void setRideSpeed(class_1799 stack, float speed)
	{
		class_2487 nbt = stack.method_7948();
		nbt.method_10548("rideSpeed", speed);
		stack.method_7980(nbt);
	}

	@Override
	@Environment(EnvType.CLIENT)
	public void registerModelProperty()
	{
		ItemPropertiesRegistry.register(this, new class_2960(EpicKnights.ID, "raised"), (stack, level, entity, i) -> this.isRaised(entity) ? 1 : 0);
	}
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy