-
Notifications
You must be signed in to change notification settings - Fork 16
/
BulletEntity.java
256 lines (221 loc) · 7.65 KB
/
BulletEntity.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
package mod.azure.doom.entity.projectiles;
import mod.azure.doom.entity.tierboss.IconofsinEntity;
import mod.azure.doom.network.DoomEntityPacket;
import mod.azure.doom.util.registry.DoomItems;
import mod.azure.doom.util.registry.DoomParticles;
import mod.azure.doom.util.registry.ProjectilesEntityRegister;
import net.fabricmc.api.EnvType;
import net.fabricmc.api.Environment;
import net.minecraft.enchantment.EnchantmentHelper;
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.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.world.World;
import software.bernie.geckolib3.core.IAnimatable;
import software.bernie.geckolib3.core.PlayState;
import software.bernie.geckolib3.core.controller.AnimationController;
import software.bernie.geckolib3.core.event.predicate.AnimationEvent;
import software.bernie.geckolib3.core.manager.AnimationData;
import software.bernie.geckolib3.core.manager.AnimationFactory;
public class BulletEntity extends PersistentProjectileEntity implements IAnimatable {
protected int timeInAir;
protected boolean inAir;
private int ticksInAir;
private static final TrackedData<Integer> PARTICLE = DataTracker.registerData(BulletEntity.class,
TrackedDataHandlerRegistry.INTEGER);
private float projectiledamage;
public BulletEntity(EntityType<? extends BulletEntity> entityType, World world) {
super(entityType, world);
this.pickupType = PersistentProjectileEntity.PickupPermission.DISALLOWED;
}
public BulletEntity(World world, LivingEntity owner, float damage) {
super(ProjectilesEntityRegister.BULLETS, owner, world);
this.projectiledamage = damage;
}
private AnimationFactory factory = new AnimationFactory(this);
private <E extends IAnimatable> PlayState predicate(AnimationEvent<E> event) {
return PlayState.CONTINUE;
}
@Override
public void registerControllers(AnimationData data) {
data.addAnimationController(new AnimationController<BulletEntity>(this, "controller", 0, this::predicate));
}
@Override
public AnimationFactory getFactory() {
return this.factory;
}
public BulletEntity(World world, LivingEntity owner) {
super(ProjectilesEntityRegister.BULLETS, owner, world);
}
protected BulletEntity(EntityType<? extends BulletEntity> type, double x, double y, double z, World world) {
this(type, world);
}
protected BulletEntity(EntityType<? extends BulletEntity> type, LivingEntity owner, World world) {
this(type, owner.getX(), owner.getEyeY() - 0.10000000149011612D, owner.getZ(), world);
this.setOwner(owner);
if (owner instanceof PlayerEntity) {
this.pickupType = PersistentProjectileEntity.PickupPermission.ALLOWED;
}
}
protected void initDataTracker() {
super.initDataTracker();
this.dataTracker.startTracking(PARTICLE, 0);
}
public Integer useParticle() {
return this.dataTracker.get(PARTICLE);
}
public void setParticle(Integer spin) {
this.dataTracker.set(PARTICLE, spin);
}
@Override
public Packet<?> createSpawnPacket() {
return DoomEntityPacket.createPacket(this);
}
@Override
protected void age() {
++this.ticksInAir;
if (this.ticksInAir >= 40) {
this.remove(Entity.RemovalReason.DISCARDED);
}
}
@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 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() {
super.tick();
++this.ticksInAir;
if (this.ticksInAir >= 80) {
this.remove(Entity.RemovalReason.DISCARDED);
}
if (this.world.isClient) {
double d2 = this.getX() + (this.random.nextDouble()) * (double) this.getWidth() * 0.5D;
double f2 = this.getZ() + (this.random.nextDouble()) * (double) this.getWidth() * 0.5D;
if (this.useParticle() == 1) {
this.world.addParticle(DoomParticles.PISTOL, true, d2, this.getY(), f2, 0, 0, 0);
}
if (this.useParticle() == 2) {
this.world.addParticle(ParticleTypes.SMOKE, true, d2, this.getY(), f2, 0, 0, 0);
}
}
}
public void initFromStack(ItemStack stack) {
if (stack.getItem() == DoomItems.BULLETS) {
}
}
@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) {
this.remove(Entity.RemovalReason.DISCARDED);
}
this.setSound(SoundEvents.ITEM_ARMOR_EQUIP_IRON);
}
@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, projectiledamage)) {
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.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.BULLETS);
}
@Override
@Environment(EnvType.CLIENT)
public boolean shouldRender(double distance) {
return true;
}
@Override
public boolean doesRenderOnFire() {
return false;
}
}