-
Notifications
You must be signed in to change notification settings - Fork 0
/
MissileEntity.java
335 lines (285 loc) · 12.1 KB
/
MissileEntity.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
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
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
package com.slimeist.server_mobs.entities;
import com.slimeist.server_mobs.ServerMobsMod;
import com.slimeist.server_mobs.api.server_rendering.entity.IServerRenderedEntity;
import com.slimeist.server_mobs.api.server_rendering.model.BakedServerEntityModel;
import com.slimeist.server_mobs.api.util.VectorUtil;
import eu.pb4.polymer.api.entity.PolymerEntity;
import net.minecraft.entity.Entity;
import net.minecraft.entity.EntityType;
import net.minecraft.entity.LivingEntity;
import net.minecraft.entity.damage.DamageSource;
import net.minecraft.entity.projectile.FireworkRocketEntity;
import net.minecraft.entity.projectile.thrown.ThrownEntity;
import net.minecraft.item.ItemStack;
import net.minecraft.item.Items;
import net.minecraft.nbt.NbtCompound;
import net.minecraft.nbt.NbtList;
import net.minecraft.particle.DustColorTransitionParticleEffect;
import net.minecraft.particle.ParticleTypes;
import net.minecraft.server.world.ServerWorld;
import net.minecraft.sound.SoundCategory;
import net.minecraft.util.hit.EntityHitResult;
import net.minecraft.util.hit.HitResult;
import net.minecraft.util.math.EulerAngle;
import net.minecraft.util.math.MathHelper;
import net.minecraft.util.math.Vec3d;
import net.minecraft.util.math.Vec3f;
import net.minecraft.world.World;
import net.minecraft.world.explosion.Explosion;
import org.jetbrains.annotations.Nullable;
import java.util.UUID;
import java.util.function.Supplier;
//Sort of a port of Pneumaticraft's Micromissile
@SuppressWarnings("FieldCanBeLocal")
public class MissileEntity extends ThrownEntity implements PolymerEntity, IServerRenderedEntity {
private static Supplier<BakedServerEntityModel> bakedModelSupplier;
private BakedServerEntityModel.Instance modelInstance;
@Nullable
private Entity target;
@Nullable
private UUID targetUuid;
private double targetX;
private double targetY;
private double targetZ;
//Values from Pneumaticraft
@SuppressWarnings("FieldCanBeLocal")
private final float maxVelocitySq = 0.5f;
private final float accel = 1.05f; // straight line acceleration
private final float turnSpeed = 0.1f;
private final float explosionPower;
private boolean outOfFuel = false;
//End values from Pneumaticraft
private MissileColor colorData = MissileColor.EMPTY;
private ItemStack launchStack = ItemStack.EMPTY;
//Constructor
public MissileEntity(EntityType<? extends MissileEntity> entityType, World world) {
super(entityType, world);
explosionPower = ServerMobsMod.getConfig().missileExplosionPower;
}
protected MissileEntity(EntityType<? extends MissileEntity> entityType, LivingEntity owner, World world) {
super(entityType, owner, world);
explosionPower = ServerMobsMod.getConfig().missileExplosionPower;
}
public static MissileEntity targeting(World world, LivingEntity owner, Entity target) {
MissileEntity missile = new MissileEntity(ServerMobsMod.MISSILE, owner, world);
missile.target = target;
return missile;
}
public void setColorData(int[] colors, int[] fadeColors) {
this.setColorData(new MissileColor(colors, fadeColors));
}
public void setColorData(MissileColor colorData) {
this.colorData = colorData;
}
public void setLaunchStack(ItemStack launchStack) {
this.launchStack = launchStack;
}
//IServerRenderedEntity
@Override
public BakedServerEntityModel.Instance createModelInstance() {
return getBakedModel().createInstance(this);
}
@Override
public BakedServerEntityModel.Instance getModelInstance() {
if (modelInstance == null) {
modelInstance = createModelInstance();
}
return modelInstance;
}
@Override
public BakedServerEntityModel getBakedModel() {
return bakedModelSupplier.get();
}
@Override
public void updateAngles() {
this.getModelInstance().setPartPivot("base", Vec3d.ZERO);
this.getModelInstance().setPartRotation("base", new EulerAngle(-this.getPitch(), -this.getYaw(), 0));
if (!this.isAlive()) {
this.getModelInstance().setDamageFlash(true);
}
}
public static void setBakedModelSupplier(Supplier<BakedServerEntityModel> bakedModel) {
bakedModelSupplier = bakedModel;
}
//PolymerEntity
@Override
public EntityType<?> getPolymerEntityType() {
return EntityType.MARKER;
}
@Override
protected void initDataTracker() {
}
//MissileEntity
@Override
public SoundCategory getSoundCategory() {
return SoundCategory.HOSTILE;
}
@Override
protected void writeCustomDataToNbt(NbtCompound nbt) {
super.writeCustomDataToNbt(nbt);
if (this.target != null) {
nbt.putUuid("Target", this.target.getUuid());
}
}
@Override
protected void readCustomDataFromNbt(NbtCompound nbt) {
super.readCustomDataFromNbt(nbt);
if (nbt.containsUuid("Target")) {
this.targetUuid = nbt.getUuid("Target");
}
}
private Vec3d normalize(Vec3d in) {
Vec3d tmp = new Vec3d(in.getX(), in.getY(), in.getZ());
double biggest = Math.max(Math.abs(tmp.getX()), Math.max(Math.abs(tmp.getY()), Math.abs(tmp.getZ())));
tmp.multiply(1 / biggest);
return tmp;
}
@Override
public void tick() {
super.tick();
Vec3d vec3d;
if (!this.world.isClient) {
if (this.target == null && this.targetUuid != null) {
this.target = ((ServerWorld) this.world).getEntity(this.targetUuid);
if (this.target == null) {
this.targetUuid = null;
}
}
if (age > 300) {
this.outOfFuel = true;
}
//Following block from Pneumaticraft
if (!outOfFuel) {
// negate default slowdown of projectiles applied in superclass
if (this.isTouchingWater()) {
setVelocity(getVelocity().multiply(1.25));
} else {
setVelocity(getVelocity().multiply(1 / 0.99));
}
if (target != null && target.isAlive() && !target.isSpectator()) {
// turn toward the target
Vec3d diff = target.getPos().add(0, target.getStandingEyeHeight(), 0).subtract(getPos()).normalize().multiply(turnSpeed);
setVelocity(getVelocity().add(diff));
} else {
this.outOfFuel = true;
}
// accelerate up to max velocity but cap there
double velSq = getVelocity().lengthSquared();//motionX * motionX + motionY * motionY + motionZ * motionZ;
double mul = velSq > maxVelocitySq ? maxVelocitySq / velSq : accel;
setVelocity(getVelocity().multiply(mul));
if (getEntityWorld() instanceof ServerWorld serverWorld && !colorData.equals(MissileColor.EMPTY)) {
Vec3d m = getVelocity();
Vec3f fromCol = VectorUtil.fireworkColor(colorData.colors()[random.nextInt(colorData.colors().length)]);
Vec3f toCol = fromCol;
if (colorData.fadeColors().length > 0) {
toCol = VectorUtil.fireworkColor(colorData.fadeColors()[random.nextInt(colorData.fadeColors().length)]);
}
serverWorld.spawnParticles(new DustColorTransitionParticleEffect(fromCol, toCol, 4.0f), getX(), getY(), getZ(), 0, -m.x / 2, -m.y / 2, -m.z / 2, 1);
} else if (getEntityWorld() instanceof ServerWorld serverWorld && getEntityWorld().random.nextBoolean()) {
Vec3d m = getVelocity();
serverWorld.spawnParticles(ParticleTypes.LARGE_SMOKE, getX(), getY(), getZ(), 0, -m.x / 2, -m.y / 2, -m.z / 2, 1);
}
}//End Pneumaticraft block
if (this.target != null) {
if (this.target.getPos().isInRange(this.getPos(), 0.75)) {
this.onCollision(new EntityHitResult(this.target));
}
}
}
this.getModelInstance().updateHologram();
}
@Override
protected boolean canHit(Entity entity) {
return super.canHit(entity) && !entity.noClip && !entity.getType().equals(this.getType());
}
@Override
public boolean isOnFire() {
return false;
}
@Override
public boolean shouldRender(double distance) {
return distance < 16384.0;
}
@Override
public float getBrightnessAtEyes() {
return 1.0f;
}
//Following method copied from Pneumaticraft
@Override
protected void onCollision(HitResult hitResult) {
if (age > 5 && isAlive()) {
goBoom(hitResult instanceof EntityHitResult ? ((EntityHitResult) hitResult).getEntity() : null);
}
}
//Folowing method inspired by Pneumaticraft
private void goBoom(Entity e) {
discard();
DamageSource source = null;
if (this.getOwner() instanceof LivingEntity) {
source = DamageSource.explosion((LivingEntity) this.getOwner());
}
double x = this.getX();
double y = this.getY();
double z = this.getZ();
//Following if block is from Pneumaticraft
if (e != null) {
x = MathHelper.lerp(0.25f, e.getX(), getX());
y = MathHelper.lerp(0.25f, e.getY(), getY());
z = MathHelper.lerp(0.25f, e.getZ(), getZ());
}
this.world.createExplosion(this, source, null, x, y, z, this.explosionPower, false, Explosion.DestructionType.NONE);
if (!this.launchStack.equals(ItemStack.EMPTY)) {
if (this.launchStack.hasNbt()) {
NbtCompound explosionData = this.launchStack.getSubNbt("Explosion");
if (explosionData != null) {
ItemStack fireworkStack = new ItemStack(Items.FIREWORK_ROCKET, 1);
NbtCompound fireworksData = new NbtCompound();
NbtList explosionList = new NbtList();
explosionList.add(explosionData);
fireworksData.put("Explosions", explosionList);
fireworksData.putInt("Flight", 1);
fireworkStack.setSubNbt("Fireworks", fireworksData);
FireworkRocketEntity rocket = new FireworkRocketEntity(this.world, x, y, z, fireworkStack);
this.world.spawnEntity(rocket);
rocket.explodeAndRemove();
//((FireworkRocketEntityAccessor) rocket).setLife(((FireworkRocketEntityAccessor) rocket).getLifeTime()-20);
}
}
}
}
@Override
public boolean canHit() {
return true;
}
//Pneumaticraft MicroMissile code for rest of class
@Override
public void setVelocity(Entity entityThrower, float pitch, float yaw, float pitchOffset, float velocity, float inaccuracy) {
float x = -MathHelper.sin(yaw * 0.017453292F) * MathHelper.cos(pitch * 0.017453292F);
float y = -MathHelper.sin(pitch * 0.017453292F);
float z = MathHelper.cos(yaw * 0.017453292F) * MathHelper.cos(pitch * 0.017453292F);
this.setVelocity(x, y, z, velocity, 0f);
this.setVelocity(this.getVelocity().add(entityThrower.getVelocity().x, 0, entityThrower.getVelocity().z));
}
@Override
public void setVelocity(double x, double y, double z, float velocity, float inaccuracy) {
double f = Math.sqrt(x * x + y * y + z * z);
x = x / f * velocity;
y = y / f * velocity;
z = z / f * velocity;
this.setVelocity(x, y, z);
float f1 = MathHelper.sqrt((float) (x * x + z * z));
//noinspection SuspiciousNameCombination
this.setYaw((float) (MathHelper.atan2(x, z) * (180D / Math.PI)));
this.setPitch((float) (MathHelper.atan2(y, f1) * (180D / Math.PI)));
this.prevYaw = this.getYaw();
this.prevPitch = this.getPitch();
}
@Override
protected float getGravity() {
return outOfFuel ? super.getGravity() : 0F;
}
@Override
public boolean hasNoGravity() {
return !outOfFuel;
}
}