From 9e7c184f1f3b84a650eb5ddc0bfa7a8218e9250c Mon Sep 17 00:00:00 2001 From: alpha Date: Tue, 27 Feb 2024 20:42:56 -0600 Subject: [PATCH] Use new projectile impact event --- .../tconstruct/tools/logic/ToolEvents.java | 16 +++++++------- .../ability/armor/ReflectingModifier.java | 22 +++++++++++-------- 2 files changed, 21 insertions(+), 17 deletions(-) diff --git a/src/main/java/slimeknights/tconstruct/tools/logic/ToolEvents.java b/src/main/java/slimeknights/tconstruct/tools/logic/ToolEvents.java index fa7120c32f3..4d01d044ac8 100644 --- a/src/main/java/slimeknights/tconstruct/tools/logic/ToolEvents.java +++ b/src/main/java/slimeknights/tconstruct/tools/logic/ToolEvents.java @@ -1,8 +1,10 @@ package slimeknights.tconstruct.tools.logic; import com.google.common.collect.Multiset; +import io.github.fabricators_of_create.porting_lib.entity.events.EntityEvents; import io.github.fabricators_of_create.porting_lib.entity.events.ProjectileImpactCallback; import io.github.fabricators_of_create.porting_lib.core.event.BaseEvent.Result; +import io.github.fabricators_of_create.porting_lib.entity.events.ProjectileImpactEvent; import io.github.fabricators_of_create.porting_lib.event.common.GrindstoneEvents; import io.github.fabricators_of_create.porting_lib.entity.events.LivingEntityEvents; import io.github.fabricators_of_create.porting_lib.entity.events.PlayerEvents; @@ -85,7 +87,7 @@ public static void init() { LivingEntityEvents.HURT.register(ToolEvents::livingHurt); LivingEntityEvents.TICK.register(ToolEvents::livingWalk); LivingEntityEvents.VISIBILITY.register(ToolEvents::livingVisibility); - ProjectileImpactCallback.EVENT.register(ToolEvents::projectileHit); + EntityEvents.PROJECTILE_IMPACT.register(ToolEvents::projectileHit); GrindstoneEvents.ON_PLACE_ITEM.register(ToolEvents::onGrindstoneChange); } @@ -418,10 +420,12 @@ static double livingVisibility(LivingEntity living, @Nullable Entity lookingEnti } /** Implements projectile hit hook */ - static boolean projectileHit(Projectile projectile, HitResult hit) { + static void projectileHit(ProjectileImpactEvent event) { + Projectile projectile = event.getProjectile(); ModifierNBT modifiers = EntityModifierCapability.getOrEmpty(projectile); if (!modifiers.isEmpty()) { NamespacedNBT nbt = PersistentDataCapability.getOrWarn(projectile); + HitResult hit = event.getRayTraceResult(); HitResult.Type type = hit.getType(); // extract a firing entity as that is a common need LivingEntity attacker = projectile.getOwner() instanceof LivingEntity l ? l : null; @@ -433,27 +437,23 @@ static boolean projectileHit(Projectile projectile, HitResult hit) { if (entityHit.getEntity().getType() != EntityType.ENDERMAN || modifiers.getLevel(TinkerModifiers.enderference.getId()) > 0) { // extract a living target as that is the most common need LivingEntity target = ToolAttackUtil.getLivingEntity(entityHit.getEntity()); - boolean cancel = false; for (ModifierEntry entry : modifiers.getModifiers()) { if (entry.getHook(TinkerHooks.PROJECTILE_HIT).onProjectileHitEntity(modifiers, nbt, entry, projectile, entityHit, attacker, target)) { - cancel = true; + event.setCanceled(true); } } - if (cancel) - return true; } } case BLOCK -> { BlockHitResult blockHit = (BlockHitResult)hit; for (ModifierEntry entry : modifiers.getModifiers()) { if (entry.getHook(TinkerHooks.PROJECTILE_HIT).onProjectileHitBlock(modifiers, nbt, entry, projectile, blockHit, attacker)) { - return true; + event.setCanceled(true); } } } } } - return false; } static void onGrindstoneChange(GrindstoneEvents.OnplaceItem event) { diff --git a/src/main/java/slimeknights/tconstruct/tools/modifiers/ability/armor/ReflectingModifier.java b/src/main/java/slimeknights/tconstruct/tools/modifiers/ability/armor/ReflectingModifier.java index 96667d08ced..f5af411dfc1 100644 --- a/src/main/java/slimeknights/tconstruct/tools/modifiers/ability/armor/ReflectingModifier.java +++ b/src/main/java/slimeknights/tconstruct/tools/modifiers/ability/armor/ReflectingModifier.java @@ -1,9 +1,11 @@ package slimeknights.tconstruct.tools.modifiers.ability.armor; -import io.github.fabricators_of_create.porting_lib.entity.events.ProjectileImpactCallback; +import io.github.fabricators_of_create.porting_lib.entity.events.EntityEvents; +import io.github.fabricators_of_create.porting_lib.entity.events.ProjectileImpactEvent; import net.minecraft.network.protocol.game.ClientboundSetEntityMotionPacket; import net.minecraft.sounds.SoundEvents; import net.minecraft.sounds.SoundSource; +import net.minecraft.world.entity.Entity; import net.minecraft.world.entity.EntityType; import net.minecraft.world.entity.LivingEntity; import net.minecraft.world.entity.projectile.AbstractArrow; @@ -31,17 +33,20 @@ public class ReflectingModifier extends Modifier { public ReflectingModifier() { - ProjectileImpactCallback.EVENT.register(this::projectileImpact); + EntityEvents.PROJECTILE_IMPACT.register(this::projectileImpact); } - private boolean projectileImpact(Projectile projectile, HitResult hit) { + private void projectileImpact(ProjectileImpactEvent event) { + Entity entity = event.getEntity(); // first, need a projectile that is hitting a living entity - if (!projectile.level().isClientSide) { + if (!entity.level().isClientSide) { + Projectile projectile = event.getProjectile(); // handle blacklist for projectiles // living entity must be using one of our shields + HitResult hit = event.getRayTraceResult(); if (!RegistryHelper.contains(TinkerTags.EntityTypes.REFLECTING_BLACKLIST, projectile.getType()) - && hit.getType() == Type.ENTITY && ((EntityHitResult) hit).getEntity() instanceof LivingEntity living && living.isUsingItem() && living != projectile.getOwner()) { + && hit.getType() == Type.ENTITY && ((EntityHitResult) hit).getEntity() instanceof LivingEntity living && living.isUsingItem() && living != projectile.getOwner()) { ItemStack stack = living.getUseItem(); if (stack.is(TinkerTags.Items.SHIELDS)) { ToolStack tool = ToolStack.from(stack); @@ -55,8 +60,8 @@ private boolean projectileImpact(Projectile projectile, HitResult hit) { int time = hook.getUseDuration(tool, activeModifier) - living.getUseItemRemainingTicks(); // must be blocking, started blocking within the last 2*level seconds, and be within the block angle if (hook.getUseAction(tool, activeModifier) == UseAnim.BLOCK - && (time >= 5 && time < 40 * reflectingLevel) - && InteractionHandler.canBlock(living, projectile.position(), tool)) { + && (time >= 5 && time < 40 * reflectingLevel) + && InteractionHandler.canBlock(living, projectile.position(), tool)) { // time to actually reflect, this code is strongly based on code from the Parry mod // take ownership of the projectile so it counts as a player kill, except in the case of fishing bobbers @@ -85,13 +90,12 @@ private boolean projectileImpact(Projectile projectile, HitResult hit) { TinkerNetwork.getInstance().sendVanillaPacket(new ClientboundSetEntityMotionPacket(projectile), living); } living.level().playSound(null, living.blockPosition(), SoundEvents.SHIELD_BLOCK, SoundSource.PLAYERS, 1.0F, 1.5F + living.level().random.nextFloat() * 0.4F); - return true; + event.setCanceled(true); } } } } } } - return false; } }