-
-
Notifications
You must be signed in to change notification settings - Fork 299
/
MobAI.java
175 lines (141 loc) · 5.27 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
package net.citizensnpcs.nms.v1_17_R1.util;
import java.util.Map;
import com.google.common.collect.Maps;
import net.citizensnpcs.Settings.Setting;
import net.minecraft.world.entity.Entity;
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.JumpControl;
import net.minecraft.world.entity.ai.control.MoveControl;
import net.minecraft.world.entity.ai.navigation.PathNavigation;
import net.minecraft.world.level.pathfinder.BlockPathTypes;
public interface MobAI {
org.bukkit.entity.Entity getBukkitEntity();
JumpControl getJumpControl();
Map<BlockPathTypes, Float> getMalus();
MoveControl getMoveControl();
PathNavigation getNavigation();
default float getPathfindingMalus(BlockPathTypes var1) {
Map<BlockPathTypes, Float> malus = getMalus();
return malus.containsKey(var1) ? malus.get(var1) : var1.getMalus();
}
default void setPathfindingMalus(BlockPathTypes water, float oldWaterCost) {
getMalus().put(water, oldWaterCost);
}
default void tickAI() {
getJumpControl().tick();
getMoveControl().tick();
PathNavigation nav = getNavigation();
if (!nav.isDone()) {
nav.tick();
}
}
default void updatePathfindingRange(float range) {
((LivingEntity) NMSImpl.getHandle(getBukkitEntity())).getAttribute(Attributes.FOLLOW_RANGE).setBaseValue(range);
}
public static class BasicMobAI implements MobAI {
private final EntityJumpControl controllerJump;
private final EntityMoveControl controllerMove;
private final LivingEntity entity;
private final Map<BlockPathTypes, Float> malus;
private final EntityNavigation navigation;
public BasicMobAI(LivingEntity entity) {
this.entity = entity;
NMSImpl.setAttribute(entity, Attributes.FOLLOW_RANGE, Setting.DEFAULT_PATHFINDING_RANGE.asDouble());
entity.getAttribute(Attributes.MOVEMENT_SPEED).setBaseValue(0.3D);
controllerJump = new EntityJumpControl(entity);
controllerMove = new EntityMoveControl(entity);
navigation = new EntityNavigation(entity, entity.level);
malus = Maps.newEnumMap(BlockPathTypes.class);
}
@Override
public org.bukkit.entity.Entity getBukkitEntity() {
return entity.getBukkitEntity();
}
@Override
public JumpControl getJumpControl() {
return controllerJump;
}
@Override
public Map<BlockPathTypes, Float> getMalus() {
return malus;
}
@Override
public MoveControl getMoveControl() {
return controllerMove;
}
@Override
public PathNavigation getNavigation() {
return navigation;
}
}
public static interface ForwardingMobAI extends MobAI {
MobAI getAI();
@Override
default org.bukkit.entity.Entity getBukkitEntity() {
return getAI().getBukkitEntity();
}
@Override
default JumpControl getJumpControl() {
return getAI().getJumpControl();
}
@Override
default Map<BlockPathTypes, Float> getMalus() {
return getAI().getMalus();
}
@Override
default MoveControl getMoveControl() {
return getAI().getMoveControl();
}
@Override
default PathNavigation getNavigation() {
return getAI().getNavigation();
}
}
public static MobAI from(Entity handle) {
if (handle instanceof Mob) {
Mob mob = (Mob) handle;
return new MobAI() {
@Override
public org.bukkit.entity.Entity getBukkitEntity() {
return mob.getBukkitEntity();
}
@Override
public JumpControl getJumpControl() {
return mob.getJumpControl();
}
@Override
public Map<BlockPathTypes, Float> getMalus() {
return null;
}
@Override
public MoveControl getMoveControl() {
return mob.getMoveControl();
}
@Override
public PathNavigation getNavigation() {
return mob.getNavigation();
}
@Override
public float getPathfindingMalus(BlockPathTypes var1) {
return mob.getPathfindingMalus(var1);
}
@Override
public void setPathfindingMalus(BlockPathTypes water, float oldWaterCost) {
mob.setPathfindingMalus(water, oldWaterCost);
}
@Override
public void tickAI() {
mob.getSensing().tick();
mob.getNavigation().tick();
mob.getMoveControl().tick();
mob.getLookControl().tick();
mob.getJumpControl().tick();
}
};
} else if (handle instanceof MobAI)
return (MobAI) handle;
return null;
}
}