11package dev.sterner.witchery.content.entity
22
33import dev.sterner.witchery.core.registry.WitcheryEntityTypes
4+ import dev.sterner.witchery.mixin.EntityAccessor
45import net.minecraft.core.particles.ParticleTypes
56import net.minecraft.nbt.CompoundTag
67import net.minecraft.network.syncher.EntityDataAccessor
@@ -28,11 +29,12 @@ class InsanityEntity(level: Level) : PathfinderMob(WitcheryEntityTypes.INSANITY.
2829 private var oldSwell = 0
2930 private var swell = 0
3031 private var maxSwell = 30
32+ private var hasPlayedAggroSound = false
3133
3234 override fun registerGoals () {
3335 this .goalSelector.addGoal(1 , MeleeAttackGoal (this , 1.0 , false ))
3436 this .goalSelector.addGoal(2 , WaterAvoidingRandomStrollGoal (this , 1.0 ))
35- this .goalSelector.addGoal(3 , LookAtPlayerGoal (this , Player ::class .java, 8 .0f ))
37+ this .goalSelector.addGoal(3 , LookAtPlayerGoal (this , Player ::class .java, 16 .0f ))
3638 this .goalSelector.addGoal(4 , RandomLookAroundGoal (this ))
3739 this .goalSelector.addGoal(2 , SwellGoal (this ))
3840
@@ -43,6 +45,7 @@ class InsanityEntity(level: Level) : PathfinderMob(WitcheryEntityTypes.INSANITY.
4345 super .defineSynchedData(builder)
4446 builder.define(DATA_SWELL_DIR , - 1 )
4547 builder.define(DATA_MIMIC , " " )
48+ builder.define(DATA_IS_AGGRESSIVE , false )
4649 }
4750
4851 fun getSwelling (partialTicks : Float ): Float {
@@ -57,14 +60,19 @@ class InsanityEntity(level: Level) : PathfinderMob(WitcheryEntityTypes.INSANITY.
5760 ): SpawnGroupData ? {
5861 val mimicType: InsanityMobType = InsanityMobType .entries[Random .nextInt(InsanityMobType .entries.size)]
5962 entityData.set(DATA_MIMIC , mimicType.name.lowercase())
63+ if (mimicType.name.lowercase() == " enderman" ) {
64+ val accessor: EntityAccessor = this as EntityAccessor
65+ accessor.`witchery$setEyeHeight`(2.55f )
66+
67+ }
6068 return super .finalizeSpawn(level, difficulty, spawnType, spawnGroupData)
6169 }
6270
6371 companion object {
6472 fun createAttributes (): AttributeSupplier .Builder {
6573 return createMobAttributes()
6674 .add(Attributes .MAX_HEALTH , 1.0 )
67- .add(Attributes .MOVEMENT_SPEED , 0.25 )
75+ .add(Attributes .MOVEMENT_SPEED , 0.32 )
6876 .add(Attributes .ATTACK_DAMAGE , 0.0 )
6977 }
7078
@@ -74,9 +82,28 @@ class InsanityEntity(level: Level) : PathfinderMob(WitcheryEntityTypes.INSANITY.
7482 val DATA_MIMIC : EntityDataAccessor <String > = SynchedEntityData .defineId(
7583 InsanityEntity ::class .java, EntityDataSerializers .STRING
7684 )
85+ val DATA_IS_AGGRESSIVE : EntityDataAccessor <Boolean > = SynchedEntityData .defineId(
86+ InsanityEntity ::class .java, EntityDataSerializers .BOOLEAN
87+ )
7788 }
7889
7990 override fun tick () {
91+
92+
93+ if (entityData.get(DATA_MIMIC ) == InsanityMobType .ENDERMAN .name.lowercase()) {
94+ val hasTarget = target != null
95+ if (hasTarget && ! entityData.get(DATA_IS_AGGRESSIVE )) {
96+ entityData.set(DATA_IS_AGGRESSIVE , true )
97+ if (! hasPlayedAggroSound) {
98+ playSound(SoundEvents .ENDERMAN_SCREAM , 0.7f , 1.0f )
99+ hasPlayedAggroSound = true
100+ }
101+ } else if (! hasTarget && entityData.get(DATA_IS_AGGRESSIVE )) {
102+ entityData.set(DATA_IS_AGGRESSIVE , false )
103+ hasPlayedAggroSound = false
104+ }
105+ }
106+
80107 if (entityData.get(DATA_MIMIC ) == InsanityMobType .CREEPER .name.lowercase()) {
81108 if (this .isAlive) {
82109 this .oldSwell = this .swell
@@ -103,34 +130,33 @@ class InsanityEntity(level: Level) : PathfinderMob(WitcheryEntityTypes.INSANITY.
103130 if (Random .nextInt(200 ) == 0 ) {
104131 playMobSound()
105132 }
133+ this .target?.let {
134+ if (this .distanceToSqr(this .target!! ) < 2f ) {
135+ hurt(level().damageSources().generic(), 1f )
136+ }
137+ }
106138
107139 super .tick()
108140 }
109141
110142 private fun playMobSound () {
111143 if (entityData.get(DATA_MIMIC ) == " zombie" ) {
112- this .playSound (SoundEvents .ZOMBIE_AMBIENT , 1.0f , 1.0f )
144+ this .makeSound (SoundEvents .ZOMBIE_AMBIENT )
113145 }
114146 if (entityData.get(DATA_MIMIC ) == " skeleton" ) {
115- this .playSound (SoundEvents .SKELETON_AMBIENT , 1.0f , 1.0f )
147+ this .makeSound (SoundEvents .SKELETON_AMBIENT )
116148 }
117149 if (entityData.get(DATA_MIMIC ) == " enderman" ) {
118- this .playSound (SoundEvents .ENDERMAN_AMBIENT , 1.0f , 1.0f )
150+ this .makeSound (SoundEvents .ENDERMAN_AMBIENT )
119151 }
120152 }
121153
122154 private fun playerDeathSound () {
123- if (entityData.get(DATA_MIMIC ) == " zombie" ) {
124- this .playSound(SoundEvents .ZOMBIE_HURT , 1.0f , 1.0f )
125- }
126- if (entityData.get(DATA_MIMIC ) == " skeleton" ) {
127- this .playSound(SoundEvents .SKELETON_HURT , 1.0f , 1.0f )
128- }
129- if (entityData.get(DATA_MIMIC ) == " enderman" ) {
130- this .playSound(SoundEvents .ENDERMAN_HURT , 1.0f , 1.0f )
131- }
132- if (entityData.get(DATA_MIMIC ) == " creeper" ) {
133- this .playSound(SoundEvents .CREEPER_HURT , 1.0f , 1.0f )
155+ when (entityData.get(DATA_MIMIC )) {
156+ " zombie" -> makeSound(SoundEvents .ZOMBIE_DEATH )
157+ " skeleton" -> makeSound(SoundEvents .SKELETON_DEATH )
158+ " enderman" -> makeSound(SoundEvents .ENDERMAN_DEATH )
159+ " creeper" -> makeSound(SoundEvents .CREEPER_DEATH )
134160 }
135161 }
136162
@@ -142,6 +168,7 @@ class InsanityEntity(level: Level) : PathfinderMob(WitcheryEntityTypes.INSANITY.
142168 super .addAdditionalSaveData(compound)
143169 compound.putShort(" Fuse" , maxSwell.toShort())
144170 compound.putString(" Mimic" , entityData.get(DATA_MIMIC ))
171+ compound.putBoolean(" IsAggressive" , entityData.get(DATA_IS_AGGRESSIVE ))
145172 }
146173
147174 override fun readAdditionalSaveData (compound : CompoundTag ) {
@@ -152,32 +179,35 @@ class InsanityEntity(level: Level) : PathfinderMob(WitcheryEntityTypes.INSANITY.
152179 if (compound.contains(" Mimic" , 99 )) {
153180 entityData.set(DATA_MIMIC , compound.getString(" Mimic" ))
154181 }
182+ if (compound.contains(" IsAggressive" )) {
183+ entityData.set(DATA_IS_AGGRESSIVE , compound.getBoolean(" IsAggressive" ))
184+ }
155185 }
156186
157187 override fun hurt (source : DamageSource , amount : Float ): Boolean {
158- if (source.entity is Player ) {
159- (0 until 20 ).forEach { i ->
160- this .level().addParticle(
161- ParticleTypes .POOF ,
162- this .x + (Random .nextDouble() - 0.5 ) * this .bbWidth,
163- this .y + Random .nextDouble() * this .bbHeight,
164- this .z + (Random .nextDouble() - 0.5 ) * this .bbWidth,
165- 0.0 , 0.0 , 0.0
166- )
167- playerDeathSound()
168- }
169-
170- this .remove(RemovalReason .KILLED )
171- return true
188+ (0 until 20 ).forEach { i ->
189+ this .level().addParticle(
190+ ParticleTypes .POOF ,
191+ this .x + (Random .nextDouble() - 0.5 ) * this .bbWidth,
192+ this .y + Random .nextDouble() * this .bbHeight,
193+ this .z + (Random .nextDouble() - 0.5 ) * this .bbWidth,
194+ 0.0 , 0.0 , 0.0
195+ )
172196 }
197+ playerDeathSound()
173198
174- return false
199+ this .remove(RemovalReason .KILLED )
200+ return true
175201 }
176202
177203 override fun isInvulnerableTo (source : DamageSource ): Boolean {
178204 return source.entity !is Player || super .isInvulnerableTo(source)
179205 }
180206
207+ override fun isAggressive (): Boolean {
208+ return entityData.get(DATA_IS_AGGRESSIVE )
209+ }
210+
181211 enum class InsanityMobType {
182212 CREEPER ,
183213 ZOMBIE ,
0 commit comments