Skip to content

Commit

Permalink
Added tame check for target selector test.
Browse files Browse the repository at this point in the history
  • Loading branch information
LtxProgrammer committed Oct 2, 2024
1 parent d99fbc9 commit 8be5132
Show file tree
Hide file tree
Showing 4 changed files with 147 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -1,8 +1,35 @@
package net.ltxprogrammer.changed.entity;

import net.minecraft.world.entity.LivingEntity;
import net.minecraft.world.entity.OwnableEntity;
import net.minecraft.world.entity.TamableAnimal;
import net.minecraft.world.entity.animal.Wolf;
import net.minecraft.world.entity.animal.horse.AbstractHorse;
import net.minecraft.world.entity.monster.Creeper;
import net.minecraft.world.entity.monster.Ghast;
import net.minecraft.world.entity.player.Player;

public interface TamableLatexEntity extends OwnableEntity {
boolean isFollowingOwner();
void setFollowOwner(boolean value);

default boolean isTame() {
return this.getOwner() != null;
}

default boolean wantsToAttack(LivingEntity target, LivingEntity owner) {
if (!(target instanceof Creeper) && !(target instanceof Ghast)) {
if (target instanceof TamableLatexEntity tamable) {
return !tamable.isTame() || tamable.getOwner() != owner;
} else if (target instanceof Player && owner instanceof Player && !((Player)owner).canHarmPlayer((Player)target)) {
return false;
} else if (target instanceof AbstractHorse && ((AbstractHorse)target).isTamed()) {
return false;
} else {
return !(target instanceof TamableAnimal) || !((TamableAnimal)target).isTame();
}
} else {
return false;
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package net.ltxprogrammer.changed.entity.ai;

import net.ltxprogrammer.changed.entity.ChangedEntity;
import net.ltxprogrammer.changed.entity.TamableLatexEntity;
import net.minecraft.world.entity.LivingEntity;
import net.minecraft.world.entity.TamableAnimal;
import net.minecraft.world.entity.ai.goal.target.TargetGoal;
import net.minecraft.world.entity.ai.targeting.TargetingConditions;

import java.util.EnumSet;

public class LatexOwnerHurtByTargetGoal<T extends ChangedEntity & TamableLatexEntity> extends TargetGoal {
private final T tameAnimal;
private LivingEntity ownerLastHurtBy;
private int timestamp;

public LatexOwnerHurtByTargetGoal(T p_26107_) {
super(p_26107_, false);
this.tameAnimal = p_26107_;
this.setFlags(EnumSet.of(Flag.TARGET));
}

public boolean canUse() {
if (this.tameAnimal.isTame()) {
if (!(this.tameAnimal.getOwner() instanceof LivingEntity livingentity)) {
return false;
} else {
this.ownerLastHurtBy = livingentity.getLastHurtByMob();
int i = livingentity.getLastHurtByMobTimestamp();
return i != this.timestamp && this.canAttack(this.ownerLastHurtBy, TargetingConditions.DEFAULT) && this.tameAnimal.wantsToAttack(this.ownerLastHurtBy, livingentity);
}
} else {
return false;
}
}

public void start() {
this.mob.setTarget(this.ownerLastHurtBy);
if (this.tameAnimal.getOwner() instanceof LivingEntity livingentity) {
this.timestamp = livingentity.getLastHurtByMobTimestamp();
}

super.start();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
package net.ltxprogrammer.changed.entity.ai;

import net.ltxprogrammer.changed.entity.ChangedEntity;
import net.ltxprogrammer.changed.entity.TamableLatexEntity;
import net.minecraft.core.BlockPos;
import net.minecraft.world.entity.Entity;
import net.minecraft.world.entity.LivingEntity;
import net.minecraft.world.entity.TamableAnimal;
import net.minecraft.world.entity.ai.goal.Goal;
import net.minecraft.world.entity.ai.goal.target.TargetGoal;
import net.minecraft.world.entity.ai.navigation.FlyingPathNavigation;
import net.minecraft.world.entity.ai.navigation.GroundPathNavigation;
import net.minecraft.world.entity.ai.navigation.PathNavigation;
import net.minecraft.world.entity.ai.targeting.TargetingConditions;
import net.minecraft.world.level.LevelReader;
import net.minecraft.world.level.block.LeavesBlock;
import net.minecraft.world.level.block.state.BlockState;
import net.minecraft.world.level.pathfinder.BlockPathTypes;
import net.minecraft.world.level.pathfinder.WalkNodeEvaluator;

import java.util.EnumSet;

public class LatexOwnerHurtTargetGoal<T extends ChangedEntity & TamableLatexEntity> extends TargetGoal {
private final T tameAnimal;
private LivingEntity ownerLastHurt;
private int timestamp;

public LatexOwnerHurtTargetGoal(T p_26114_) {
super(p_26114_, false);
this.tameAnimal = p_26114_;
this.setFlags(EnumSet.of(Flag.TARGET));
}

public boolean canUse() {
if (this.tameAnimal.isTame()) {
if (!(this.tameAnimal.getOwner() instanceof LivingEntity livingentity)) {
return false;
} else {
this.ownerLastHurt = livingentity.getLastHurtMob();
int i = livingentity.getLastHurtMobTimestamp();
return i != this.timestamp && this.canAttack(this.ownerLastHurt, TargetingConditions.DEFAULT) && this.tameAnimal.wantsToAttack(this.ownerLastHurt, livingentity);
}
} else {
return false;
}
}

public void start() {
this.mob.setTarget(this.ownerLastHurt);
if (this.tameAnimal.getOwner() instanceof LivingEntity livingentity) {
this.timestamp = livingentity.getLastHurtMobTimestamp();
}

super.start();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
import net.ltxprogrammer.changed.entity.LatexType;
import net.ltxprogrammer.changed.entity.TamableLatexEntity;
import net.ltxprogrammer.changed.entity.ai.LatexFollowOwnerGoal;
import net.ltxprogrammer.changed.entity.ai.LatexOwnerHurtByTargetGoal;
import net.ltxprogrammer.changed.entity.ai.LatexOwnerHurtTargetGoal;
import net.ltxprogrammer.changed.init.ChangedCriteriaTriggers;
import net.ltxprogrammer.changed.init.ChangedItems;
import net.ltxprogrammer.changed.util.Color3;
Expand All @@ -26,6 +28,8 @@
import net.minecraft.world.entity.LivingEntity;
import net.minecraft.world.entity.ai.attributes.AttributeMap;
import net.minecraft.world.entity.ai.attributes.Attributes;
import net.minecraft.world.entity.ai.goal.target.OwnerHurtByTargetGoal;
import net.minecraft.world.entity.ai.goal.target.OwnerHurtTargetGoal;
import net.minecraft.world.entity.player.Player;
import net.minecraft.world.item.Item;
import net.minecraft.world.item.ItemStack;
Expand All @@ -51,6 +55,8 @@ public AbstractDarkLatexEntity(EntityType<? extends AbstractLatexWolf> p_19870_,
protected void registerGoals() {
super.registerGoals();
this.goalSelector.addGoal(6, new LatexFollowOwnerGoal<>(this, 0.35D, 10.0F, 2.0F, false));
this.targetSelector.addGoal(1, new LatexOwnerHurtByTargetGoal<>(this));
this.targetSelector.addGoal(2, new LatexOwnerHurtTargetGoal<>(this));
}

@Override
Expand Down Expand Up @@ -112,8 +118,11 @@ public Color3 getHairColor(int layer) {

@Override
protected boolean targetSelectorTest(LivingEntity livingEntity) {
if (livingEntity == this.getOwner())
final var owner = this.getOwner();
if (livingEntity == owner)
return false;
if (owner != null)
return livingEntity == owner.getLastHurtByMob();

if (!this.isMaskless()) {// Check if masked DL can see entity
if (livingEntity.distanceToSqr(this) <= 1.0)
Expand Down Expand Up @@ -250,6 +259,15 @@ public void setFollowOwner(boolean value) {

}

@Override
public boolean wantsToAttack(LivingEntity target, LivingEntity owner) {
if (target instanceof AbstractDarkLatexEntity) {
return false;
}

return TamableLatexEntity.super.wantsToAttack(target, owner);
}

@Override
public void checkDespawn() {
if (isTame())
Expand Down

0 comments on commit 8be5132

Please sign in to comment.