Skip to content

Commit

Permalink
Improve interaction of enderporting and enderference, and enderportin…
Browse files Browse the repository at this point in the history
…g in general

Enderporting melee/arrows now swap you with the target instead of just teleporting you
Adding in enderference to either side, you can prevent one side from teleporting. SImpliest case is you teleport to the target but they stay still for a tool with both traits
  • Loading branch information
KnightMiner committed Dec 18, 2022
1 parent 2059b98 commit a296f0e
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 22 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ public interface ProjectileHitModifierHook {
ProjectileHitModifierHook EMPTY = new ProjectileHitModifierHook() {};

/** Merger instance */
Function<Collection<ProjectileHitModifierHook>,ProjectileHitModifierHook> FIRST_MERGER = FirstMerger::new;
Function<Collection<ProjectileHitModifierHook>,ProjectileHitModifierHook> FIRST_MERGER = AllMerger::new;

/**
* Called when a projectile hits an entity
Expand All @@ -29,7 +29,7 @@ public interface ProjectileHitModifierHook {
* @param hit Hit result
* @param attacker Living entity who fired the projectile, null if non-living or not fired
* @param target Living target, will be null if not living
* @return true if the hit should be canceled, preventing further modifiers from running
* @return true if the hit should be canceled, preventing vanilla logic
*/
default boolean onProjectileHitEntity(ModifierNBT modifiers, NamespacedNBT persistentData, ModifierEntry modifier, Projectile projectile, EntityHitResult hit, @Nullable LivingEntity attacker, @Nullable LivingEntity target) {
return false;
Expand All @@ -43,32 +43,30 @@ default boolean onProjectileHitEntity(ModifierNBT modifiers, NamespacedNBT persi
* @param projectile Projectile that hit the entity
* @param hit Hit result
* @param attacker Living entity who fired the projectile, null if non-living or not fired
* @return true if the hit should be canceled, preventing further modifiers from running
* @return true if the hit should be canceled
*/
default boolean onProjectileHitBlock(ModifierNBT modifiers, NamespacedNBT persistentData, ModifierEntry modifier, Projectile projectile, BlockHitResult hit, @Nullable LivingEntity attacker) {
return false;
}

/** Merger that returns when the first hook returns true */
record FirstMerger(Collection<ProjectileHitModifierHook> modules) implements ProjectileHitModifierHook {
/** Merger that runs all hooks and returns true if any did */
record AllMerger(Collection<ProjectileHitModifierHook> modules) implements ProjectileHitModifierHook {
@Override
public boolean onProjectileHitEntity(ModifierNBT modifiers, NamespacedNBT persistentData, ModifierEntry modifier, Projectile projectile, EntityHitResult hit, @Nullable LivingEntity attacker, @Nullable LivingEntity target) {
boolean ret = false;
for (ProjectileHitModifierHook module : modules) {
if (module.onProjectileHitEntity(modifiers, persistentData, modifier, projectile, hit, attacker, target)) {
return true;
}
ret |= module.onProjectileHitEntity(modifiers, persistentData, modifier, projectile, hit, attacker, target);
}
return false;
return ret;
}

@Override
public boolean onProjectileHitBlock(ModifierNBT modifiers, NamespacedNBT persistentData, ModifierEntry modifier, Projectile projectile, BlockHitResult hit, @Nullable LivingEntity attacker) {
boolean ret = false;
for (ProjectileHitModifierHook module : modules) {
if (module.onProjectileHitBlock(modifiers, persistentData, modifier, projectile, hit, attacker)) {
return true;
}
ret |= module.onProjectileHitBlock(modifiers, persistentData, modifier, projectile, hit, attacker);
}
return false;
return ret;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -414,7 +414,6 @@ static void projectileHit(ProjectileImpactEvent event) {
for (ModifierEntry entry : modifiers.getModifiers()) {
if (entry.getHook(TinkerHooks.PROJECTILE_HIT).onProjectileHitEntity(modifiers, nbt, entry, projectile, entityHit, attacker, target)) {
event.setCanceled(true);
return;
}
}
}
Expand All @@ -424,7 +423,6 @@ static void projectileHit(ProjectileImpactEvent event) {
for (ModifierEntry entry : modifiers.getModifiers()) {
if (entry.getHook(TinkerHooks.PROJECTILE_HIT).onProjectileHitBlock(modifiers, nbt, entry, projectile, blockHit, attacker)) {
event.setCanceled(true);
return;
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ public class EnderportingModifier extends NoLevelsModifier implements PlantHarve

@Override
public int getPriority() {
return 75;
return 45;
}

/** Attempts to teleport to the given location */
Expand Down Expand Up @@ -97,9 +97,11 @@ public int afterEntityHit(IToolStackView tool, int level, ToolAttackContext cont
if (!context.isExtraAttack()) {
LivingEntity target = context.getLivingTarget();
// if the entity is dead now
if (target != null && target.getHealth() == 0) {
Vec3 pos = target.position();
if (tryTeleport(context.getAttacker(), pos.x(), pos.y(), pos.z())) {
if (target != null) {
LivingEntity attacker = context.getAttacker();
Vec3 oldPosition = attacker.position();
if (tryTeleport(attacker, target.getX(), target.getY(), target.getZ())) {
tryTeleport(target, oldPosition.x, oldPosition.y, oldPosition.z);
return 2;
}
}
Expand Down Expand Up @@ -131,9 +133,12 @@ public void afterHarvest(IToolStackView tool, ModifierEntry modifier, UseOnConte

@Override
public boolean onProjectileHitEntity(ModifierNBT modifiers, NamespacedNBT persistentData, ModifierEntry modifier, Projectile projectile, EntityHitResult hit, @Nullable LivingEntity attacker, @Nullable LivingEntity target) {
if (attacker != null) {
if (attacker != null && attacker != target) {
Entity hitEntity = hit.getEntity();
tryTeleport(attacker, hitEntity.getX(), hitEntity.getY(), hitEntity.getZ());
Vec3 oldPosition = attacker.position();
if (tryTeleport(attacker, hitEntity.getX(), hitEntity.getY(), hitEntity.getZ()) && target != null) {
tryTeleport(target, oldPosition.x, oldPosition.y, oldPosition.z);
}
}
return false;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -183,7 +183,6 @@ public boolean onProjectileHitEntity(ModifierNBT modifiers, NamespacedNBT persis
}
}


return true;
}
}
Expand Down

0 comments on commit a296f0e

Please sign in to comment.