Skip to content

Commit

Permalink
Use new projectile impact event
Browse files Browse the repository at this point in the history
  • Loading branch information
AlphaMode committed Feb 28, 2024
1 parent c53b5e6 commit 9e7c184
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 17 deletions.
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -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);
}

Expand Down Expand Up @@ -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;
Expand All @@ -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) {
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -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);
Expand All @@ -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
Expand Down Expand Up @@ -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;
}
}

0 comments on commit 9e7c184

Please sign in to comment.