-
-
Notifications
You must be signed in to change notification settings - Fork 307
/
MobAI.java
176 lines (142 loc) · 5.36 KB
/
MobAI.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
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
package net.citizensnpcs.nms.v1_16_R3.util;
import java.util.Map;
import com.google.common.collect.Maps;
import net.citizensnpcs.Settings.Setting;
import net.minecraft.server.v1_16_R3.ControllerJump;
import net.minecraft.server.v1_16_R3.ControllerMove;
import net.minecraft.server.v1_16_R3.Entity;
import net.minecraft.server.v1_16_R3.EntityInsentient;
import net.minecraft.server.v1_16_R3.EntityLiving;
import net.minecraft.server.v1_16_R3.GenericAttributes;
import net.minecraft.server.v1_16_R3.NavigationAbstract;
import net.minecraft.server.v1_16_R3.PathType;
public interface MobAI {
org.bukkit.entity.Entity getBukkitEntity();
ControllerJump getJumpControl();
Map<PathType, Float> getMalus();
ControllerMove getMoveControl();
NavigationAbstract getNavigation();
default float getPathfindingMalus(PathType var1) {
Map<PathType, Float> malus = getMalus();
return malus.containsKey(var1) ? malus.get(var1) : var1.a();
}
default void setPathfindingMalus(PathType water, float oldWaterCost) {
getMalus().put(water, oldWaterCost);
}
default void tickAI() {
getJumpControl().b();
getMoveControl().a();
NavigationAbstract navigation = getNavigation();
if (!NMSImpl.isNavigationFinished(navigation)) {
NMSImpl.updateNavigation(navigation);
}
}
default void updatePathfindingRange(float range) {
((EntityLiving) NMSImpl.getHandle(getBukkitEntity())).getAttributeInstance(GenericAttributes.FOLLOW_RANGE)
.setValue(range);
}
public static class BasicMobAI implements MobAI {
private final EntityJumpControl controllerJump;
private final EntityMoveControl controllerMove;
private final EntityLiving entity;
private final Map<PathType, Float> malus;
private final EntityNavigation navigation;
public BasicMobAI(EntityLiving entity) {
this.entity = entity;
NMSImpl.setAttribute(entity, GenericAttributes.FOLLOW_RANGE, Setting.DEFAULT_PATHFINDING_RANGE.asDouble());
entity.getAttributeInstance(GenericAttributes.MOVEMENT_SPEED).setValue(0.3D);
controllerJump = new EntityJumpControl(entity);
controllerMove = new EntityMoveControl(entity);
navigation = new EntityNavigation(entity, entity.world);
malus = Maps.newEnumMap(PathType.class);
}
@Override
public org.bukkit.entity.Entity getBukkitEntity() {
return entity.getBukkitEntity();
}
@Override
public ControllerJump getJumpControl() {
return controllerJump;
}
@Override
public Map<PathType, Float> getMalus() {
return malus;
}
@Override
public ControllerMove getMoveControl() {
return controllerMove;
}
@Override
public NavigationAbstract getNavigation() {
return navigation;
}
}
public static interface ForwardingMobAI extends MobAI {
MobAI getAI();
@Override
default org.bukkit.entity.Entity getBukkitEntity() {
return getAI().getBukkitEntity();
}
@Override
default ControllerJump getJumpControl() {
return getAI().getJumpControl();
}
@Override
default Map<PathType, Float> getMalus() {
return getAI().getMalus();
}
@Override
default ControllerMove getMoveControl() {
return getAI().getMoveControl();
}
@Override
default NavigationAbstract getNavigation() {
return getAI().getNavigation();
}
}
public static MobAI from(Entity handle) {
if (handle instanceof EntityInsentient) {
EntityInsentient mob = (EntityInsentient) handle;
return new MobAI() {
@Override
public org.bukkit.entity.Entity getBukkitEntity() {
return mob.getBukkitEntity();
}
@Override
public ControllerJump getJumpControl() {
return mob.getControllerJump();
}
@Override
public Map<PathType, Float> getMalus() {
return null;
}
@Override
public ControllerMove getMoveControl() {
return mob.getControllerMove();
}
@Override
public NavigationAbstract getNavigation() {
return mob.getNavigation();
}
@Override
public float getPathfindingMalus(PathType var1) {
return mob.a(var1);
}
@Override
public void setPathfindingMalus(PathType water, float oldWaterCost) {
mob.a(water, oldWaterCost);
}
@Override
public void tickAI() {
mob.getEntitySenses().a();
NMSImpl.updateNavigation(mob.getNavigation());
mob.getControllerMove().a();
mob.getControllerLook().a();
mob.getControllerJump().b();
}
};
} else if (handle instanceof MobAI)
return (MobAI) handle;
return null;
}
}