-
Notifications
You must be signed in to change notification settings - Fork 16
/
ArgentBoltEntity.java
340 lines (302 loc) · 10.7 KB
/
ArgentBoltEntity.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
336
337
338
339
340
package mod.azure.doom.entity.projectiles;
import java.util.List;
import mod.azure.doom.config.DoomConfig;
import mod.azure.doom.entity.tierboss.IconofsinEntity;
import mod.azure.doom.entity.tileentity.TickingLightEntity;
import mod.azure.doom.network.DoomEntityPacket;
import mod.azure.doom.util.registry.DoomBlocks;
import mod.azure.doom.util.registry.DoomItems;
import mod.azure.doom.util.registry.ProjectilesEntityRegister;
import net.fabricmc.api.EnvType;
import net.fabricmc.api.Environment;
import net.minecraft.block.BlockState;
import net.minecraft.block.entity.BlockEntity;
import net.minecraft.enchantment.EnchantmentHelper;
import net.minecraft.entity.AreaEffectCloudEntity;
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.data.DataTracker;
import net.minecraft.entity.data.TrackedData;
import net.minecraft.entity.data.TrackedDataHandlerRegistry;
import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.entity.projectile.PersistentProjectileEntity;
import net.minecraft.item.ItemStack;
import net.minecraft.nbt.NbtCompound;
import net.minecraft.network.Packet;
import net.minecraft.network.packet.s2c.play.GameStateChangeS2CPacket;
import net.minecraft.particle.ParticleTypes;
import net.minecraft.server.network.ServerPlayerEntity;
import net.minecraft.sound.SoundCategory;
import net.minecraft.sound.SoundEvent;
import net.minecraft.sound.SoundEvents;
import net.minecraft.util.hit.BlockHitResult;
import net.minecraft.util.hit.EntityHitResult;
import net.minecraft.util.hit.HitResult;
import net.minecraft.util.math.BlockPos;
import net.minecraft.util.math.Box;
import net.minecraft.util.math.MathHelper;
import net.minecraft.util.math.Vec3d;
import net.minecraft.world.World;
import net.minecraft.world.explosion.Explosion;
public class ArgentBoltEntity extends PersistentProjectileEntity {
protected int timeInAir;
protected boolean inAir;
private int ticksInAir;
private static final TrackedData<Boolean> PARTICLE = DataTracker.registerData(ArgentBoltEntity.class,
TrackedDataHandlerRegistry.BOOLEAN);
private LivingEntity shooter;
private BlockPos lightBlockPos = null;
private int idleTicks = 0;
public ArgentBoltEntity(EntityType<? extends ArgentBoltEntity> entityType, World world) {
super(entityType, world);
this.pickupType = PersistentProjectileEntity.PickupPermission.DISALLOWED;
}
public ArgentBoltEntity(World world, LivingEntity owner) {
super(ProjectilesEntityRegister.ARGENT_BOLT, owner, world);
this.shooter = owner;
}
protected ArgentBoltEntity(EntityType<? extends ArgentBoltEntity> type, double x, double y, double z, World world) {
this(type, world);
}
protected ArgentBoltEntity(EntityType<? extends ArgentBoltEntity> type, LivingEntity owner, World world) {
this(type, owner.getX(), owner.getEyeY() - 0.10000000149011612D, owner.getZ(), world);
this.setOwner(owner);
this.shooter = owner;
if (owner instanceof PlayerEntity) {
this.pickupType = PersistentProjectileEntity.PickupPermission.ALLOWED;
}
}
protected void initDataTracker() {
super.initDataTracker();
this.dataTracker.startTracking(PARTICLE, false);
}
public boolean useParticle() {
return (Boolean) this.dataTracker.get(PARTICLE);
}
public void setParticle(boolean spin) {
this.dataTracker.set(PARTICLE, spin);
}
@Override
protected void onHit(LivingEntity living) {
super.onHit(living);
if (!(living instanceof PlayerEntity) && !(living instanceof IconofsinEntity)) {
living.setVelocity(0, 0, 0);
living.timeUntilRegen = 0;
}
}
@Override
public Packet<?> createSpawnPacket() {
return DoomEntityPacket.createPacket(this);
}
@Override
protected void age() {
++this.ticksInAir;
if (this.ticksInAir >= 40) {
this.remove(Entity.RemovalReason.DISCARDED);
}
}
@Override
public void setVelocity(double x, double y, double z, float speed, float divergence) {
super.setVelocity(x, y, z, speed, divergence);
this.ticksInAir = 0;
}
@Override
public void writeCustomDataToNbt(NbtCompound tag) {
super.writeCustomDataToNbt(tag);
tag.putShort("life", (short) this.ticksInAir);
}
@Override
public void readCustomDataFromNbt(NbtCompound tag) {
super.readCustomDataFromNbt(tag);
this.ticksInAir = tag.getShort("life");
}
@Override
public void tick() {
int idleOpt = 100;
if (getVelocity().lengthSquared() < 0.01)
idleTicks++;
else
idleTicks = 0;
if (idleOpt <= 0 || idleTicks < idleOpt)
super.tick();
++this.ticksInAir;
if (this.ticksInAir >= 80) {
this.remove(Entity.RemovalReason.DISCARDED);
}
boolean isInsideWaterBlock = world.isWater(getBlockPos());
spawnLightSource(isInsideWaterBlock);
if (this.world.isClient) {
this.world.addParticle(this.useParticle() ? ParticleTypes.ANGRY_VILLAGER : ParticleTypes.FLASH, true,
this.getX(), this.getY(), this.getZ(), 0, 0, 0);
}
}
private void spawnLightSource(boolean isInWaterBlock) {
if (lightBlockPos == null) {
lightBlockPos = findFreeSpace(world, getBlockPos(), 2);
if (lightBlockPos == null)
return;
world.setBlockState(lightBlockPos, DoomBlocks.TICKING_LIGHT_BLOCK.getDefaultState());
} else if (checkDistance(lightBlockPos, getBlockPos(), 2)) {
BlockEntity blockEntity = world.getBlockEntity(lightBlockPos);
if (blockEntity instanceof TickingLightEntity) {
((TickingLightEntity) blockEntity).refresh(isInWaterBlock ? 20 : 0);
} else
lightBlockPos = null;
} else
lightBlockPos = null;
}
private boolean checkDistance(BlockPos blockPosA, BlockPos blockPosB, int distance) {
return Math.abs(blockPosA.getX() - blockPosB.getX()) <= distance
&& Math.abs(blockPosA.getY() - blockPosB.getY()) <= distance
&& Math.abs(blockPosA.getZ() - blockPosB.getZ()) <= distance;
}
private BlockPos findFreeSpace(World world, BlockPos blockPos, int maxDistance) {
if (blockPos == null)
return null;
int[] offsets = new int[maxDistance * 2 + 1];
offsets[0] = 0;
for (int i = 2; i <= maxDistance * 2; i += 2) {
offsets[i - 1] = i / 2;
offsets[i] = -i / 2;
}
for (int x : offsets)
for (int y : offsets)
for (int z : offsets) {
BlockPos offsetPos = blockPos.add(x, y, z);
BlockState state = world.getBlockState(offsetPos);
if (state.isAir() || state.getBlock().equals(DoomBlocks.TICKING_LIGHT_BLOCK))
return offsetPos;
}
return null;
}
public void initFromStack(ItemStack stack) {
if (stack.getItem() == DoomItems.ARGENT_BOLT) {
}
}
@Override
public boolean hasNoGravity() {
if (this.isSubmergedInWater()) {
return false;
} else {
return true;
}
}
public SoundEvent hitSound = this.getHitSound();
@Override
public void setSound(SoundEvent soundIn) {
this.hitSound = soundIn;
}
@Override
protected SoundEvent getHitSound() {
return SoundEvents.ITEM_ARMOR_EQUIP_IRON;
}
@Override
protected void onBlockHit(BlockHitResult blockHitResult) {
super.onBlockHit(blockHitResult);
if (!this.world.isClient) {
if (this.useParticle()) {
if (this.age >= 46) {
this.explode();
this.remove(Entity.RemovalReason.DISCARDED);
}
} else {
this.world.createExplosion(this, this.getX(), this.getBodyY(0.0625D), this.getZ(), 1.5F,
Explosion.DestructionType.NONE);
this.remove(Entity.RemovalReason.DISCARDED);
}
}
this.setSound(SoundEvents.ITEM_ARMOR_EQUIP_IRON);
}
@Override
public void remove(RemovalReason reason) {
if (this.useParticle()) {
AreaEffectCloudEntity areaeffectcloudentity = new AreaEffectCloudEntity(this.world, this.getX(),
this.getY(), this.getZ());
areaeffectcloudentity.setParticleType(ParticleTypes.LAVA);
areaeffectcloudentity.setRadius(6);
areaeffectcloudentity.setDuration(1);
areaeffectcloudentity.updatePosition(this.getX(), this.getY(), this.getZ());
this.world.spawnEntity(areaeffectcloudentity);
world.playSound((PlayerEntity) null, this.getX(), this.getY(), this.getZ(),
SoundEvents.ENTITY_GENERIC_EXPLODE, SoundCategory.PLAYERS, 1.0F, 1.5F);
}
super.remove(reason);
}
protected void explode() {
double xn = MathHelper.floor(this.getX() - 5.0D);
double xp = MathHelper.floor(this.getX() + 7.0D);
double yn = MathHelper.floor(this.getY() - 5.0D);
double yp = MathHelper.floor(this.getY() + 7.0D);
double zn = MathHelper.floor(this.getZ() - 5.0D);
double zp = MathHelper.floor(this.getZ() + 7.0D);
List<Entity> list = this.world.getOtherEntities(this, new Box(xn, yn, zn, xp, yp, zp));
Vec3d vec3d = new Vec3d(this.getX(), this.getY(), this.getZ());
for (int x = 0; x < list.size(); ++x) {
Entity entity = (Entity) list.get(x);
double y = (double) (MathHelper.sqrt((float) entity.squaredDistanceTo(vec3d)) / 6);
if (entity instanceof LivingEntity) {
if (y <= 1.0D) {
entity.damage(DamageSource.player((PlayerEntity) this.shooter),
DoomConfig.argent_bolt_damage);
}
}
}
}
@Override
protected void onEntityHit(EntityHitResult entityHitResult) {
Entity entity = entityHitResult.getEntity();
if (entityHitResult.getType() != HitResult.Type.ENTITY
|| !((EntityHitResult) entityHitResult).getEntity().isPartOf(entity)) {
if (!this.world.isClient) {
this.remove(Entity.RemovalReason.DISCARDED);
}
}
Entity entity2 = this.getOwner();
DamageSource damageSource2;
if (entity2 == null) {
damageSource2 = DamageSource.arrow(this, this);
} else {
damageSource2 = DamageSource.arrow(this, entity2);
if (entity2 instanceof LivingEntity) {
((LivingEntity) entity2).onAttacking(entity);
}
}
if (entity.damage(damageSource2, DoomConfig.argent_bolt_damage)) {
if (entity instanceof LivingEntity) {
LivingEntity livingEntity = (LivingEntity) entity;
if (!this.world.isClient && entity2 instanceof LivingEntity) {
EnchantmentHelper.onUserDamaged(livingEntity, entity2);
EnchantmentHelper.onTargetDamaged((LivingEntity) entity2, livingEntity);
this.world.createExplosion(this, this.getX(), this.getBodyY(0.0625D), this.getZ(), 1.5F,
Explosion.DestructionType.NONE);
}
this.remove(Entity.RemovalReason.DISCARDED);
this.onHit(livingEntity);
if (entity2 != null && livingEntity != entity2 && livingEntity instanceof PlayerEntity
&& entity2 instanceof ServerPlayerEntity && !this.isSilent()) {
((ServerPlayerEntity) entity2).networkHandler.sendPacket(
new GameStateChangeS2CPacket(GameStateChangeS2CPacket.PROJECTILE_HIT_PLAYER, 0.0F));
}
}
} else {
if (!this.world.isClient) {
this.remove(Entity.RemovalReason.DISCARDED);
}
}
}
@Override
public ItemStack asItemStack() {
return new ItemStack(DoomItems.ARGENT_BOLT);
}
@Override
@Environment(EnvType.CLIENT)
public boolean shouldRender(double distance) {
return true;
}
@Override
public boolean doesRenderOnFire() {
return false;
}
}