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

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

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

import com.google.common.collect.ImmutableMultimap;
import com.google.common.collect.ImmutableMultimap.Builder;
import com.google.common.collect.Multimap;
import com.magistuarmory.EpicKnights;
import com.magistuarmory.effects.LacerationEffect;
import com.magistuarmory.network.PacketLongReachAttack;
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_1271;
import net.minecraft.class_1280;
import net.minecraft.class_1282;
import net.minecraft.class_1297;
import net.minecraft.class_1304;
import net.minecraft.class_1309;
import net.minecraft.class_1310;
import net.minecraft.class_1320;
import net.minecraft.class_1322;
import net.minecraft.class_1322.class_1323;
import net.minecraft.class_1657;
import net.minecraft.class_1799;
import net.minecraft.class_1802;
import net.minecraft.class_1819;
import net.minecraft.class_1829;
import net.minecraft.class_1836;
import net.minecraft.class_1839;
import net.minecraft.class_1937;
import net.minecraft.class_2487;
import net.minecraft.class_2561;
import net.minecraft.class_2960;
import net.minecraft.class_5134;
import net.minecraft.class_5819;
import net.minecraft.class_8111;
import net.minecraft.world.item.*;
import org.jetbrains.annotations.NotNull;
import me.shedaniel.cloth.clothconfig.shadowed.blue.endless.jankson.annotation.Nullable;
import java.util.List;


public class MedievalWeaponItem extends class_1829 implements IHasModelProperty
{
	private final Multimap defaultModifiers;
	private final Multimap decreasedModifiers;

	public final WeaponType type;
	protected final float attackDamage;
	protected final float attackSpeed;
	private final float decreasedAttackDamage;
	private final float decreasedAttackSpeed;
	private boolean isSilver = false;
	private float silverAttackDamage = 0.0f;
	private boolean blockingPriority = false;

	public MedievalWeaponItem(class_1793 properties, ModItemTier material, WeaponType type)
	{
		super(material, (int) CombatHelper.getBaseAttackDamage(material, type), CombatHelper.getBaseAttackSpeed(material, type), properties.method_7895(type.getDurability(material)));
		this.type = type;
		this.attackDamage = CombatHelper.getBaseAttackDamage(material, type);
		this.attackSpeed = CombatHelper.getBaseAttackSpeed(material, type);
		this.decreasedAttackDamage = CombatHelper.getDecreasedAttackDamage(this.attackDamage, type);
		this.decreasedAttackSpeed = CombatHelper.getDecreasedAttackSpeed(this.attackSpeed, type);
		
		if (material.equals(ModItemTier.SILVER))
		{
			this.isSilver = true;
			this.silverAttackDamage = CombatHelper.getSilverAttackDamage(material, type);
		}
		
		Builder builder = ImmutableMultimap.builder();
		builder.put(class_5134.field_23721, new class_1322(field_8006, "Weapon modifier", this.attackDamage, class_1323.field_6328));
		builder.put(class_5134.field_23723, new class_1322(field_8001, "Weapon modifier", this.attackSpeed, class_1323.field_6328));
		this.defaultModifiers = builder.build();

		Builder builder2 = ImmutableMultimap.builder();
		builder2.put(class_5134.field_23721, new class_1322(field_8006, "Weapon modifier", this.decreasedAttackDamage, class_1323.field_6328));
		builder2.put(class_5134.field_23723, new class_1322(field_8001, "Weapon modifier", this.decreasedAttackSpeed, class_1323.field_6328));
		this.decreasedModifiers = builder2.build();
	}

	public boolean onAttackClickEntity(class_1799 stack, class_1657 player, class_1297 entity)
	{
		if (!this.isLong())
			return true;
		
		if (entity != player && entity != player.method_5854())
			PacketLongReachAttack.sendToServer(entity.method_5628());
		
		player.method_7350();

		return false;
	}
	
	public Multimap getAttributeModifiers(class_1799 stack, @NotNull class_1304 slot)
	{
		return this.getAttributeModifiers(this.hasTwoHandedPenalty(stack), slot);
	}

	public Multimap getAttributeModifiers(boolean decreased, @NotNull class_1304 slot)
	{
		return slot == class_1304.field_6173 && decreased ? this.decreasedModifiers : this.method_7844(slot);
	}

	@Override
	public @NotNull Multimap method_7844(class_1304 slot)
	{
		return slot == class_1304.field_6173 ? this.defaultModifiers : super.method_7844(slot);
	}

	@Override
	public void method_7888(class_1799 stack, class_1937 level, class_1297 entity, int i, boolean selected)
	{
		if (entity instanceof class_1309 livingentity)
		{
			boolean flag = type.getTwoHanded() > 0 && !livingentity.method_6079().method_7909().equals(class_1802.field_8162);
			if (this.hasTwoHandedPenalty(stack) != flag)
				this.setTwoHandedPenalty(stack, flag);
			
			if (this.canBlock()) 
				this.blockingPriority = !(livingentity.method_6047().method_7909() instanceof class_1819) && !(livingentity.method_6079().method_7909() instanceof class_1819);
		}
		super.method_7888(stack, level, entity, i, selected);
	}

	public boolean onHurtEntity(class_1282 source, class_1309 victim, float damage)
	{
		if (victim.method_37908().method_8608() || ModDamageSources.isAdditional(source) || !(source.method_5529() instanceof class_1309 attacker))
			return true;
		
		if (type.isFlamebladed())
			LacerationEffect.apply(victim, damage);
		
		if (type.isHalberd() && victim.method_5765() && victim.method_37908().method_8409().method_43048(20) >= 14)
			victim.method_5848();
		
		boolean flag = false;
		if (this.isSilver())
			flag = this.dealSilverDamage(attacker, victim, damage);
		if (!flag && this.type.getArmorPiercing() != 0 && victim.method_6096() > 0)
			flag = this.dealArmorPiercingDamage(attacker, victim, damage);
		return flag;
	}

	@Override
	public void method_7851(class_1799 stack, @Nullable class_1937 level, List tooltip, class_1836 flag)
	{
		if (this.isSilver)
			tooltip.add(class_2561.method_43469("silvertools.hurt", this.silverAttackDamage).method_27692(class_124.field_1060));
		if (type.isFlamebladed())
			tooltip.add(class_2561.method_43471("flamebladed.hurt").method_27692(class_124.field_1078));
		if (type.isHalberd())
			tooltip.add(class_2561.method_43471("halberd.hurt").method_27692(class_124.field_1078));
		if (type.getArmorPiercing() != 0)
			tooltip.add(class_2561.method_43469("armorpiercing", this.type.getArmorPiercing()).method_27692(class_124.field_1078));
		if (this.isLong())
			tooltip.add(class_2561.method_43469("bonusattackreach", this.type.getBonusAttackReach()).method_27692(class_124.field_1078));
		if (type.getTwoHanded() == 1)
			tooltip.add(class_2561.method_43471("twohandedi").method_27692(class_124.field_1078));
		else if (type.getTwoHanded() > 1)
			tooltip.add(class_2561.method_43471("twohandedii").method_27692(class_124.field_1078));
		if (this.canBlock())
			tooltip.add(class_2561.method_43469("maxdamageblock", this.getMaxBlockDamage()).method_27692(class_124.field_1078));
		tooltip.add(class_2561.method_43469("kgweight", this.getWeight()).method_27692(class_124.field_1078));
		if (this.hasTwoHandedPenalty(stack))
		{
			tooltip.add(class_2561.method_43471("twohandedpenalty_1").method_27692(class_124.field_1061));
			tooltip.add(class_2561.method_43471("twohandedpenalty_2").method_27692(class_124.field_1061));
		}
		super.method_7851(stack, level, tooltip, flag);
	}

	public void setTwoHandedPenalty(class_1799 stack, boolean b)
	{
		class_2487 nbt = stack.method_7948();
		nbt.method_10556("twoHandedPenalty", b);
		stack.method_7980(nbt);
	}

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

		return false;
	}

	public float getAttackDamage(class_1799 stack)
	{
		return hasTwoHandedPenalty(stack) ? this.attackDamage : this.decreasedAttackDamage;
	}

	public float getAttackSpeed(class_1799 stack)
	{
		return hasTwoHandedPenalty(stack) ? this.attackSpeed : this.decreasedAttackSpeed;
	}
	
	@Override
	public float method_8020()
	{
		return this.attackDamage;
	}

	public float getAttackReach(float baseReach)
	{
		return baseReach + getBonusAttackReach();
	}

	public float getBonusAttackReach()
	{
		return EpicKnights.BC_or_EF_installed ? 0.0f : type.getBonusAttackReach();
	}
	
	public boolean isLong()
	{
		return this.getBonusAttackReach() > 0.0;
	}

	public float getSilverDamage(class_1799 stack, float damage)
	{
		return this.silverAttackDamage * damage / this.getAttackDamage(stack);
	}

	public float getMaxBlockDamage()
	{
		return type.getMaxBlockDamage();
	}

	public float getWeight()
	{
		return type.getWeight();
	}

	public boolean isSilver()
	{
		return this.isSilver;
	}

	public boolean canBlock(class_1657 player)
	{
		return player.method_7261(0.0f) == 1.0f && this.canBlock();
	}

	public boolean canBlock()
	{
		return type.canBlock();
	}

	boolean haveBlocked(class_5819 rand, class_1282 source)
	{
		return !source.method_49708(class_8111.field_42323) && rand.method_43048(18) > this.getWeight();
	}

	@Override
	public class_1271 method_7836(class_1937 level, class_1657 player, class_1268 hand)
	{
		if (canBlock(player) && blockingPriority)
		{
			class_1799 stack = player.method_5998(hand);
			player.method_6019(hand);

			return class_1271.method_22428(stack);
		}

		return super.method_7836(level, player, hand);
	}

	public int method_7881(class_1799 stack)
	{
		return this.canBlock() ? (int) (500 / this.getWeight()) : 0;
	}

	@Override
	public class_1839 method_7853(class_1799 stack)
	{
		return (canBlock() && blockingPriority) ? class_1839.field_8949 : super.method_7853(stack);
	}

	public void onBlocked(class_1799 stack, float damage, class_1309 victim, class_1282 source)
	{
		if (!this.canBlock() || ModDamageSources.isAdditional(source))
			return;

		class_1297 attacker = source.method_5529();
		float f = CombatHelper.getArmorPiercingFactor(attacker);

		if (source.method_49708(class_8111.field_42331))
		{
			victim.method_5643(ModDamageSources.additional(), damage);
		}
		else if (!haveBlocked(victim.method_37908().method_8409(), source))
		{
			victim.method_5643(ModDamageSources.additional(), damage);
		}
		else if (damage > this.getMaxBlockDamage())
		{
			f *= 1.5f;
			float damage1 = damage - getMaxBlockDamage();
			victim.method_5643(ModDamageSources.additional(), damage1);
		}

		stack.method_7956((int) (f * damage), victim, entity -> entity.method_20235(class_1304.field_6173));
	}

	public boolean dealSilverDamage(class_1309 attacker, class_1309 victim, float damage)
	{
		if (victim.method_6046().equals(class_1310.field_6289))
		{
			victim.method_5643(ModDamageSources.silverAttack(attacker), damage + this.getSilverDamage(attacker.method_6047(), damage));
			return true;
		}
		return false;
	}

	public boolean dealArmorPiercingDamage(class_1309 attacker, class_1309 victim, float damage)
	{
		float afterabsorb = class_1280.method_5496(damage, (float) victim.method_6096(), (float) victim.method_26825(class_5134.field_23725));
		afterabsorb = Math.max(afterabsorb - victim.method_6067(), 0.0f);
		float pierced = Math.max(((float) type.getArmorPiercing()) / 100.0f * (damage - afterabsorb), 0.0f);
		victim.method_5643(ModDamageSources.armorPiercing(attacker), afterabsorb + pierced);
		return true;
	}

	@Override
	@Environment(EnvType.CLIENT)
	public void registerModelProperty()
	{
		if (this.canBlock())
		{
			ItemPropertiesRegistry.register(this, new class_2960("blocking"), (stack, level, entity, i) ->
					entity != null && entity.method_6115() && entity.method_6030() == stack ? 1.0F : 0.0F);
		}
	}
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy