-
Notifications
You must be signed in to change notification settings - Fork 207
Expand file tree
/
Copy pathEntityRattlesnake.java
More file actions
311 lines (271 loc) · 11.7 KB
/
EntityRattlesnake.java
File metadata and controls
311 lines (271 loc) · 11.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
package com.github.alexthe666.alexsmobs.entity;
import com.github.alexthe666.alexsmobs.config.AMConfig;
import com.github.alexthe666.alexsmobs.entity.ai.AnimalAIWanderRanged;
import com.github.alexthe666.alexsmobs.misc.AMSoundRegistry;
import com.github.alexthe666.alexsmobs.misc.AMTagRegistry;
import com.github.alexthe666.citadel.animation.Animation;
import com.github.alexthe666.citadel.animation.AnimationHandler;
import com.github.alexthe666.citadel.animation.IAnimatedEntity;
import net.minecraft.core.BlockPos;
import net.minecraft.network.syncher.EntityDataAccessor;
import net.minecraft.network.syncher.EntityDataSerializers;
import net.minecraft.network.syncher.SynchedEntityData;
import net.minecraft.server.level.ServerLevel;
import net.minecraft.sounds.SoundEvent;
import net.minecraft.util.RandomSource;
import net.minecraft.world.damagesource.DamageSource;
import net.minecraft.world.effect.MobEffectInstance;
import net.minecraft.world.effect.MobEffects;
import net.minecraft.world.entity.*;
import net.minecraft.world.entity.ai.attributes.AttributeSupplier;
import net.minecraft.world.entity.ai.attributes.Attributes;
import net.minecraft.world.entity.ai.goal.*;
import net.minecraft.world.entity.ai.goal.target.HurtByTargetGoal;
import net.minecraft.world.entity.ai.goal.target.NearestAttackableTargetGoal;
import net.minecraft.world.entity.animal.Animal;
import net.minecraft.world.entity.animal.Rabbit;
import net.minecraft.world.entity.monster.Monster;
import net.minecraft.world.entity.player.Player;
import net.minecraft.world.item.ItemStack;
import net.minecraft.world.level.Level;
import net.minecraft.world.level.LevelAccessor;
import net.minecraft.world.level.gameevent.GameEvent;
import net.minecraft.world.phys.Vec3;
import javax.annotation.Nullable;
import java.util.List;
import java.util.function.Predicate;
public class EntityRattlesnake extends Animal implements IAnimatedEntity {
public float prevCurlProgress;
public float curlProgress;
public int randomToungeTick = 0;
public int maxCurlTime = 75;
private int curlTime = 0;
private static final EntityDataAccessor<Boolean> RATTLING = SynchedEntityData.defineId(EntityRattlesnake.class, EntityDataSerializers.BOOLEAN);
private static final EntityDataAccessor<Boolean> CURLED = SynchedEntityData.defineId(EntityRattlesnake.class, EntityDataSerializers.BOOLEAN);
private static final Predicate<LivingEntity> WARNABLE_PREDICATE = (mob) -> {
return mob instanceof Player && !((Player) mob).isCreative() && !mob.isSpectator() || mob instanceof EntityRoadrunner;
};
private static final Predicate<LivingEntity> TARGETABLE_PREDICATE = (mob) -> {
return mob instanceof Player && !((Player) mob).isCreative() && !mob.isSpectator() || mob instanceof EntityRoadrunner;
};
private int animationTick;
private Animation currentAnimation;
public static final Animation ANIMATION_BITE = Animation.create(20);
private int loopSoundTick = 0;
protected EntityRattlesnake(EntityType type, Level worldIn) {
super(type, worldIn);
}
protected void registerGoals() {
this.goalSelector.addGoal(0, new FloatGoal(this));
this.goalSelector.addGoal(1, new MeleeAttackGoal(this, 1.2D, true));
this.goalSelector.addGoal(2, new WarnPredatorsGoal());
this.goalSelector.addGoal(2, new BreedGoal(this, 1.0D));
this.goalSelector.addGoal(4, new FollowParentGoal(this, 1.1D));
this.goalSelector.addGoal(5, new AnimalAIWanderRanged(this, 60, 1.0D, 7, 7));
this.goalSelector.addGoal(6, new LookAtPlayerGoal(this, Player.class, 15.0F));
this.goalSelector.addGoal(7, new RandomLookAroundGoal(this));
this.targetSelector.addGoal(1, new NearestAttackableTargetGoal<>(this, Rabbit.class, 15, true, true, null));
this.targetSelector.addGoal(2, new NearestAttackableTargetGoal<>(this, EntityJerboa.class, 15, true, true, null));
this.targetSelector.addGoal(3, (new HurtByTargetGoal(this)));
this.targetSelector.addGoal(4, new ShortDistanceTarget());
}
protected SoundEvent getHurtSound(DamageSource damageSourceIn) {
return AMSoundRegistry.RATTLESNAKE_HURT.get();
}
protected SoundEvent getDeathSound() {
return AMSoundRegistry.RATTLESNAKE_HURT.get();
}
public boolean checkSpawnRules(LevelAccessor worldIn, MobSpawnType spawnReasonIn) {
return AMEntityRegistry.rollSpawn(AMConfig.rattlesnakeSpawnRolls, this.getRandom(), spawnReasonIn);
}
public boolean doHurtTarget(Entity entityIn) {
this.setAnimation(ANIMATION_BITE);
return true;
}
public boolean canBeAffected(MobEffectInstance potioneffectIn) {
if (potioneffectIn.getEffect() == MobEffects.POISON) {
return false;
}
return super.canBeAffected(potioneffectIn);
}
@Override
protected void defineSynchedData() {
super.defineSynchedData();
this.entityData.define(CURLED, false);
this.entityData.define(RATTLING, false);
}
public boolean isCurled() {
return this.entityData.get(CURLED);
}
public void setCurled(boolean curled) {
this.entityData.set(CURLED, curled);
}
public boolean isRattling() {
return this.entityData.get(RATTLING);
}
public void setRattling(boolean rattling) {
this.entityData.set(RATTLING, rattling);
}
public void tick(){
super.tick();
prevCurlProgress = curlProgress;
if (this.isCurled()) {
if (curlProgress < 5F)
curlProgress += 0.5F;
} else {
if (curlProgress > 0F)
curlProgress--;
}
if (randomToungeTick == 0 && random.nextInt(15) == 0) {
randomToungeTick = 10 + random.nextInt(20);
}
if (randomToungeTick > 0) {
randomToungeTick--;
}
if (isCurled() && !isRattling() && ++curlTime > maxCurlTime) {
this.setCurled(false);
curlTime = 0;
maxCurlTime = 75 + random.nextInt(50);
}
LivingEntity target = this.getTarget();
if (!this.level().isClientSide) {
if (this.isCurled() && (target != null && target.isAlive())) {
this.setCurled(false);
}
if (this.isRattling() && target == null) {
this.setCurled(true);
}
if (!this.isCurled() && this.getTarget() == null && random.nextInt(500) == 0) {
maxCurlTime = 300 + random.nextInt(250);
this.setCurled(true);
}
}
if (this.getAnimation() == ANIMATION_BITE) {
if (this.getAnimationTick() == 4) {
this.playSound(AMSoundRegistry.RATTLESNAKE_ATTACK.get(), getSoundVolume(), getVoicePitch());
}
if (this.getAnimationTick() == 8 && target != null && this.distanceTo(target) < 2D) {
final boolean meepMeep = target instanceof EntityRoadrunner;
final int f = isBaby() ? 2 : 1;
target.hurt(this.damageSources().mobAttack(this), meepMeep ? 1.0F : f * (float) getAttribute(Attributes.ATTACK_DAMAGE).getBaseValue());
if (!meepMeep) {
target.addEffect(new MobEffectInstance(MobEffects.POISON, 300, f * 2));
}
}
}
if(isRattling()){
if(loopSoundTick == 0){
this.gameEvent(GameEvent.ENTITY_ROAR);
this.playSound(AMSoundRegistry.RATTLESNAKE_LOOP.get(), this.getSoundVolume() * 0.5F, this.getVoicePitch());
}
loopSoundTick++;
if(loopSoundTick > 50){
loopSoundTick = 0;
}
}
AnimationHandler.INSTANCE.updateAnimations(this);
}
public void travel(Vec3 vec3d) {
if (this.onGround() && this.isCurled()) {
if (this.getNavigation().getPath() != null) {
this.getNavigation().stop();
}
vec3d = Vec3.ZERO;
}
super.travel(vec3d);
}
public static AttributeSupplier.Builder bakeAttributes() {
return Monster.createMonsterAttributes().add(Attributes.MAX_HEALTH, 8D).add(Attributes.ARMOR, 0.0D).add(Attributes.ATTACK_DAMAGE, 2.0D).add(Attributes.MOVEMENT_SPEED, 0.28F);
}
public boolean isFood(ItemStack stack) {
return stack.getItem().isEdible() && stack.getItem().getFoodProperties() != null && stack.getItem().getFoodProperties().isMeat();
}
@Nullable
@Override
public AgeableMob getBreedOffspring(ServerLevel p_241840_1_, AgeableMob p_241840_2_) {
return AMEntityRegistry.RATTLESNAKE.get().create(p_241840_1_);
}
@Override
public int getAnimationTick() {
return animationTick;
}
@Override
public void setAnimationTick(int tick) {
animationTick = tick;
}
@Override
public Animation getAnimation() {
return currentAnimation;
}
@Override
public void setAnimation(Animation animation) {
currentAnimation = animation;
}
@Override
public Animation[] getAnimations() {
return new Animation[]{ANIMATION_BITE};
}
public static boolean canRattlesnakeSpawn(EntityType<? extends Animal> animal, LevelAccessor worldIn, MobSpawnType reason, BlockPos pos, RandomSource random) {
boolean spawnBlock = worldIn.getBlockState(pos.below()).is(AMTagRegistry.RATTLESNAKE_SPAWNS);
return spawnBlock && worldIn.getRawBrightness(pos, 0) > 8;
}
class WarnPredatorsGoal extends Goal {
int executionChance = 20;
Entity target = null;
@Override
public boolean canUse() {
if(EntityRattlesnake.this.getRandom().nextInt(executionChance) == 0){
final double dist = 5D;
List<LivingEntity> list = EntityRattlesnake.this.level().getEntitiesOfClass(LivingEntity.class, EntityRattlesnake.this.getBoundingBox().inflate(dist, dist, dist), WARNABLE_PREDICATE);
double d0 = Double.MAX_VALUE;
Entity possibleTarget = null;
for(Entity entity : list) {
double d1 = EntityRattlesnake.this.distanceToSqr(entity);
if (!(d1 > d0)) {
d0 = d1;
possibleTarget = entity;
}
}
target = possibleTarget;
return !list.isEmpty();
}
return false;
}
@Override
public boolean canContinueToUse(){
return target != null && EntityRattlesnake.this.distanceTo(target) < 5D && EntityRattlesnake.this.getTarget() == null;
}
@Override
public void stop() {
target = null;
EntityRattlesnake.this.setRattling(false);
}
@Override
public void tick(){
EntityRattlesnake.this.setRattling(true);
EntityRattlesnake.this.setCurled(true);
EntityRattlesnake.this.curlTime = 0;
EntityRattlesnake.this.getLookControl().setLookAt(target, 30, 30);
}
}
class ShortDistanceTarget extends NearestAttackableTargetGoal<Player> {
public ShortDistanceTarget() {
super(EntityRattlesnake.this, Player.class, 3, true, true, TARGETABLE_PREDICATE);
}
public boolean canUse() {
if (EntityRattlesnake.this.isBaby()) {
return false;
} else {
return super.canUse();
}
}
public void start(){
super.start();
EntityRattlesnake.this.setCurled(false);
EntityRattlesnake.this.setRattling(true);
}
protected double getFollowDistance() {
return 2D;
}
}
}