-
-
Notifications
You must be signed in to change notification settings - Fork 294
/
EntityMoveControl.java
126 lines (110 loc) · 3.51 KB
/
EntityMoveControl.java
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
package net.citizensnpcs.nms.v1_20_R1.util;
import java.util.Random;
import net.citizensnpcs.util.NMS;
import net.minecraft.util.Mth;
import net.minecraft.world.entity.EntityType;
import net.minecraft.world.entity.LivingEntity;
import net.minecraft.world.entity.Mob;
import net.minecraft.world.entity.ai.attributes.Attributes;
import net.minecraft.world.entity.ai.control.MoveControl;
import net.minecraft.world.entity.monster.Slime;
public class EntityMoveControl extends MoveControl {
protected LivingEntity entity;
private int jumpTicks;
protected boolean moving;
protected double speedMod;
protected double tx;
protected double ty;
protected double tz;
public EntityMoveControl(LivingEntity entityinsentient) {
super(entityinsentient instanceof Mob ? (Mob) entityinsentient
: new Slime(EntityType.SLIME, entityinsentient.level()));
this.entity = entityinsentient;
this.tx = entityinsentient.getX();
this.ty = entityinsentient.getY();
this.tz = entityinsentient.getZ();
}
@Override
public double getSpeedModifier() {
return this.speedMod;
}
@Override
public double getWantedX() {
return this.tx;
}
@Override
public double getWantedY() {
return this.ty;
}
@Override
public double getWantedZ() {
return this.tz;
}
@Override
public boolean hasWanted() {
return this.moving;
}
protected int jumpTicks() {
return new Random().nextInt(20) + 10;
}
@Override
protected float rotlerp(float f, float f1, float f2) {
float f3 = Mth.wrapDegrees(f1 - f);
if (f3 > f2) {
f3 = f2;
}
if (f3 < -f2) {
f3 = -f2;
}
float f4 = f + f3;
if (f4 < 0.0F)
f4 += 360.0F;
else if (f4 > 360.0F) {
f4 -= 360.0F;
}
return f4;
}
@Override
public void setWantedPosition(double d0, double d1, double d2, double d3) {
this.tx = d0;
this.ty = d1;
this.tz = d2;
this.speedMod = d3;
this.moving = true;
}
private boolean shouldJump() {
if (!(this.entity instanceof Slime))
return false;
if (this.jumpTicks-- <= 0)
return true;
return false;
}
@Override
public void tick() {
this.entity.zza = 0;
if (this.moving) {
this.moving = false;
double dX = this.tx - this.entity.getX();
double dZ = this.tz - this.entity.getZ();
double dY = this.ty - this.entity.getY();
double dXZ = Math.sqrt(dX * dX + dZ * dZ);
if (Math.abs(dY) < 1.0 && dXZ < 0.01) {
// this.entity.zza = 0.0F;
return;
}
if (dXZ > 0.4) {
float f = (float) Math.toDegrees(Mth.atan2(dZ, dX)) - 90.0F;
this.entity.setYRot(rotlerp(this.entity.getYRot(), f, 90.0F));
NMS.setHeadYaw(entity.getBukkitEntity(), this.entity.getYRot());
}
float movement = (float) (this.speedMod * this.entity.getAttribute(Attributes.MOVEMENT_SPEED).getValue());
this.entity.setSpeed(movement);
this.entity.zza = movement;
if (shouldJump() || (dY >= NMS.getStepHeight(entity.getBukkitEntity()) && dXZ < 0.4D)) {
this.jumpTicks = jumpTicks();
this.jumpTicks /= 3;
entity.setJumping(true);
}
}
}
}