Skip to content

Commit f3e5d3b

Browse files
committed
Make hidebehinds retaliate when attacked while unable to attack you due to light, and make them immortal to attacks during this time. Also make hidebehinds despawn during the daytime (Fixes #13)
1 parent 8e1ec5b commit f3e5d3b

File tree

2 files changed

+30
-1
lines changed

2 files changed

+30
-1
lines changed

src/main/java/dev/itsmeow/whisperwoods/entity/EntityHidebehind.java

+29-1
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
import net.minecraft.entity.ai.RandomPositionGenerator;
1414
import net.minecraft.entity.ai.attributes.Attributes;
1515
import net.minecraft.entity.ai.goal.Goal;
16+
import net.minecraft.entity.ai.goal.HurtByTargetGoal;
1617
import net.minecraft.entity.ai.goal.SwimGoal;
1718
import net.minecraft.entity.player.PlayerEntity;
1819
import net.minecraft.item.BlockItem;
@@ -22,6 +23,8 @@
2223
import net.minecraft.network.datasync.DataSerializers;
2324
import net.minecraft.network.datasync.EntityDataManager;
2425
import net.minecraft.pathfinding.*;
26+
import net.minecraft.potion.EffectInstance;
27+
import net.minecraft.potion.Effects;
2528
import net.minecraft.tags.BlockTags;
2629
import net.minecraft.util.*;
2730
import net.minecraft.util.math.BlockPos;
@@ -72,6 +75,22 @@ public boolean attackEntityFrom(DamageSource source, float amount) {
7275
if(source.getTrueSource() == this.getAttackTarget() && this.attackSequenceTicks() > 0) {
7376
this.setAttackSequenceTicks(0);
7477
}
78+
if (!world.isRemote()) {
79+
boolean isImmediate = source.getImmediateSource() instanceof PlayerEntity;
80+
PlayerEntity player = isImmediate ? (PlayerEntity) source.getImmediateSource() : (source.getTrueSource() instanceof PlayerEntity ? (PlayerEntity) source.getTrueSource() : null);
81+
if (player != null) {
82+
if (!this.isEntityAttackable(player)) {
83+
// retaliate attacks if you can't chase due to light
84+
if (!player.isCreative()) {
85+
player.addPotionEffect(new EffectInstance(Effects.BLINDNESS, 15 * 20, 1));
86+
if (player.getDistance(this) < 3)
87+
player.attackEntityFrom(HIDEBEHIND, 1F);
88+
}
89+
HideFromTargetGoal.doTreeTick(this);
90+
return false;
91+
}
92+
}
93+
}
7594
return super.attackEntityFrom(source, amount);
7695
}
7796

@@ -330,7 +349,11 @@ public void startExecuting() {
330349

331350
@Override
332351
public void tick() {
333-
this.hidebehind.getNavigator().clearPath();
352+
doTreeTick(hidebehind);
353+
}
354+
355+
public static void doTreeTick(EntityHidebehind hidebehind) {
356+
hidebehind.getNavigator().clearPath();
334357
boolean nearTree = false;
335358
for(Direction dir : Direction.values()) {
336359
if(!nearTree) {
@@ -385,6 +408,11 @@ protected void registerData() {
385408
this.dataManager.register(ATTACK_SEQUENCE_TICKS, 0);
386409
}
387410

411+
@Override
412+
public boolean canDespawn(double range) {
413+
return world.isDaytime() && super.canDespawn(range);
414+
}
415+
388416
@Override
389417
protected PathNavigator createNavigator(World world) {
390418
return new HidebehindGroundNavigator(this, world);

src/main/java/dev/itsmeow/whisperwoods/init/ModEntities.java

+1
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@ public static void subscribe(IEventBus modBus) {
5353
.defaultPlacement((t, w, e, p, r) -> w.getDifficulty() != Difficulty.PEACEFUL && MobEntity.canSpawnOn(t, w, e, p, r))
5454
.egg(0x473123, 0xfff494)
5555
.size(1F, 5.2F)
56+
.despawn()
5657
.variants(
5758
new HidebehindVariant("black"),
5859
new HidebehindVariant("coniferous"),

0 commit comments

Comments
 (0)