From c2b36fb562a85545a5d0b4999523ca682d9e452b Mon Sep 17 00:00:00 2001 From: itsmeow Date: Mon, 24 May 2021 01:53:33 -0400 Subject: [PATCH] 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) --- gradle.properties | 2 +- .../whisperwoods/entity/EntityHidebehind.java | 30 ++++++++++++++++++- .../whisperwoods/init/ModEntities.java | 1 + 3 files changed, 31 insertions(+), 2 deletions(-) diff --git a/gradle.properties b/gradle.properties index 8e18731..3437d0e 100644 --- a/gradle.properties +++ b/gradle.properties @@ -6,7 +6,7 @@ # Forge mc_version = 1.15.2 forge_version = 31.2.50 - mappings_version = 20210424 + mappings_version = 20210523 mappings_channel = snapshot mappings_mc_version = 1.15.1 diff --git a/src/main/java/dev/itsmeow/whisperwoods/entity/EntityHidebehind.java b/src/main/java/dev/itsmeow/whisperwoods/entity/EntityHidebehind.java index c5a6e4b..8fe45e4 100644 --- a/src/main/java/dev/itsmeow/whisperwoods/entity/EntityHidebehind.java +++ b/src/main/java/dev/itsmeow/whisperwoods/entity/EntityHidebehind.java @@ -12,6 +12,7 @@ import net.minecraft.entity.*; import net.minecraft.entity.ai.RandomPositionGenerator; import net.minecraft.entity.ai.goal.Goal; +import net.minecraft.entity.ai.goal.HurtByTargetGoal; import net.minecraft.entity.ai.goal.SwimGoal; import net.minecraft.entity.player.PlayerEntity; import net.minecraft.item.BlockItem; @@ -21,6 +22,8 @@ import net.minecraft.network.datasync.DataSerializers; import net.minecraft.network.datasync.EntityDataManager; import net.minecraft.pathfinding.*; +import net.minecraft.potion.EffectInstance; +import net.minecraft.potion.Effects; import net.minecraft.tags.BlockTags; import net.minecraft.util.*; import net.minecraft.util.math.BlockPos; @@ -79,6 +82,22 @@ public boolean attackEntityFrom(DamageSource source, float amount) { if(source.getTrueSource() == this.getAttackTarget() && this.attackSequenceTicks() > 0) { this.setAttackSequenceTicks(0); } + if (!world.isRemote()) { + boolean isImmediate = source.getImmediateSource() instanceof PlayerEntity; + PlayerEntity player = isImmediate ? (PlayerEntity) source.getImmediateSource() : (source.getTrueSource() instanceof PlayerEntity ? (PlayerEntity) source.getTrueSource() : null); + if (player != null) { + if (!this.isEntityAttackable(player)) { + // retaliate attacks if you can't chase due to light + if (!player.isCreative()) { + player.addPotionEffect(new EffectInstance(Effects.BLINDNESS, 15 * 20, 1)); + if (player.getDistance(this) < 3) + player.attackEntityFrom(HIDEBEHIND, 1F); + } + HideFromTargetGoal.doTreeTick(this); + return false; + } + } + } return super.attackEntityFrom(source, amount); } @@ -337,7 +356,11 @@ public void startExecuting() { @Override public void tick() { - this.hidebehind.getNavigator().clearPath(); + doTreeTick(hidebehind); + } + + public static void doTreeTick(EntityHidebehind hidebehind) { + hidebehind.getNavigator().clearPath(); boolean nearTree = false; for(Direction dir : Direction.values()) { if(!nearTree) { @@ -392,6 +415,11 @@ protected void registerData() { this.dataManager.register(ATTACK_SEQUENCE_TICKS, 0); } + @Override + public boolean canDespawn(double range) { + return world.isDaytime() && super.canDespawn(range); + } + @Override protected PathNavigator createNavigator(World world) { return new HidebehindGroundNavigator(this, world); diff --git a/src/main/java/dev/itsmeow/whisperwoods/init/ModEntities.java b/src/main/java/dev/itsmeow/whisperwoods/init/ModEntities.java index 91f9f6c..3ec4bf7 100644 --- a/src/main/java/dev/itsmeow/whisperwoods/init/ModEntities.java +++ b/src/main/java/dev/itsmeow/whisperwoods/init/ModEntities.java @@ -52,6 +52,7 @@ public static void subscribe(IEventBus modBus) { .defaultPlacement((t, w, e, p, r) -> w.getDifficulty() != Difficulty.PEACEFUL && MobEntity.canSpawnOn(t, w, e, p, r)) .egg(0x473123, 0xfff494) .size(1F, 5.2F) + .despawn() .variants( new HidebehindVariant("black"), new HidebehindVariant("coniferous"),