From 171aa3b7445844d09822e5aa3a1afe0cab099f8b Mon Sep 17 00:00:00 2001 From: Sollace Date: Wed, 25 Aug 2021 21:43:38 +0200 Subject: [PATCH] Jars will now use the correct damage and enchantment modifiers of their contained item the striking and entity --- .../unicopia/item/FilledJarItem.java | 77 ++++++++++++++++++- 1 file changed, 75 insertions(+), 2 deletions(-) diff --git a/src/main/java/com/minelittlepony/unicopia/item/FilledJarItem.java b/src/main/java/com/minelittlepony/unicopia/item/FilledJarItem.java index 0cf3ecec1..dc856e94e 100644 --- a/src/main/java/com/minelittlepony/unicopia/item/FilledJarItem.java +++ b/src/main/java/com/minelittlepony/unicopia/item/FilledJarItem.java @@ -5,13 +5,22 @@ import net.minecraft.block.Block; import net.minecraft.block.Blocks; +import net.minecraft.enchantment.EnchantmentHelper; +import net.minecraft.enchantment.Enchantments; +import net.minecraft.entity.Entity; +import net.minecraft.entity.EntityGroup; import net.minecraft.entity.EquipmentSlot; +import net.minecraft.entity.LivingEntity; import net.minecraft.entity.attribute.EntityAttributeInstance; import net.minecraft.entity.attribute.EntityAttributes; +import net.minecraft.entity.damage.DamageSource; import net.minecraft.item.ItemStack; +import net.minecraft.particle.ParticleTypes; +import net.minecraft.server.world.ServerWorld; import net.minecraft.text.Text; import net.minecraft.text.TranslatableText; import net.minecraft.util.ActionResult; +import net.minecraft.util.math.MathHelper; import net.minecraft.world.WorldEvents; public class FilledJarItem extends JarItem implements ChameleonItem { @@ -37,7 +46,34 @@ public ActionResult onGroundTick(IItemEntity item) { @Override protected float getProjectileDamage(ItemStack stack) { - stack = getAppearanceStack(stack); + return 0; + } + + @Override + public void onImpact(MagicProjectileEntity projectile, Entity entity) { + super.onImpact(projectile, entity); + + if (!entity.isAttackable()) { + return; + } + + ItemStack stack = getAppearanceStack(projectile.getStack()); + + boolean onFire = false; + + float prevHealth = 0.0F; + int fire = EnchantmentHelper.getLevel(Enchantments.FIRE_ASPECT, stack); + + if (entity instanceof LivingEntity) { + prevHealth = ((LivingEntity)entity).getHealth(); + + if (fire > 0 && !entity.isOnFire()) { + onFire = true; + entity.setOnFireFor(1); + } + } + + float damage = EnchantmentHelper.getAttackDamage(stack, entity instanceof LivingEntity ? ((LivingEntity)entity).getGroup() : EntityGroup.DEFAULT); EntityAttributeInstance instance = new EntityAttributeInstance(EntityAttributes.GENERIC_ATTACK_DAMAGE, i -> {}); @@ -45,13 +81,50 @@ protected float getProjectileDamage(ItemStack stack) { instance.addTemporaryModifier(modifier); }); - return (float)instance.getValue(); + damage += instance.getValue(); + + if (entity.damage(DamageSource.thrownProjectile(projectile, projectile.getOwner()), damage)) { + + int knockback = EnchantmentHelper.getLevel(Enchantments.KNOCKBACK, stack); + + final float toRadians = (float)Math.PI / 180F; + + if (entity instanceof LivingEntity) { + ((LivingEntity)entity).takeKnockback( + knockback / 2F, + MathHelper.sin(projectile.getYaw() * toRadians), + -MathHelper.cos(projectile.getYaw() * toRadians) + ); + + if (fire > 0) { + entity.setOnFireFor(fire * 4); + } + + float healthDiff = prevHealth - ((LivingEntity)entity).getHealth(); + + if (projectile.world instanceof ServerWorld && healthDiff > 2) { + ((ServerWorld)projectile.world).spawnParticles(ParticleTypes.DAMAGE_INDICATOR, entity.getX(), entity.getBodyY(0.5D), entity.getZ(), (int)(healthDiff / 2F), 0.1, 0, 0.1, 0.2); + } + } else { + entity.addVelocity( + -MathHelper.sin(projectile.getYaw() * toRadians) * knockback / 2F, 0.1D, + MathHelper.cos(projectile.getYaw() * toRadians) * knockback / 2F + ); + } + } else { + if (onFire) { + entity.setOnFire(false); + } + } } @Override protected void onImpact(MagicProjectileEntity projectile) { ItemStack stack = getAppearanceStack(projectile.getStack()); stack.damage(1, projectile.world.random, null); + + + projectile.dropStack(stack); projectile.world.syncWorldEvent(WorldEvents.BLOCK_BROKEN, projectile.getBlockPos(), Block.getRawIdFromState(Blocks.GLASS.getDefaultState())); }