重构榴弹实体类
This commit is contained in:
parent
1333c877bc
commit
67cdecd6c4
3 changed files with 40 additions and 67 deletions
|
@ -2,7 +2,9 @@ package net.mcreator.target.entity;
|
||||||
|
|
||||||
import net.mcreator.target.headshot.BoundingBoxManager;
|
import net.mcreator.target.headshot.BoundingBoxManager;
|
||||||
import net.mcreator.target.headshot.IHeadshotBox;
|
import net.mcreator.target.headshot.IHeadshotBox;
|
||||||
|
import net.mcreator.target.init.TargetModDamageTypes;
|
||||||
import net.mcreator.target.init.TargetModEntities;
|
import net.mcreator.target.init.TargetModEntities;
|
||||||
|
import net.mcreator.target.init.TargetModItems;
|
||||||
import net.mcreator.target.init.TargetModSounds;
|
import net.mcreator.target.init.TargetModSounds;
|
||||||
import net.mcreator.target.network.TargetModVariables;
|
import net.mcreator.target.network.TargetModVariables;
|
||||||
import net.mcreator.target.tools.ParticleTool;
|
import net.mcreator.target.tools.ParticleTool;
|
||||||
|
@ -14,63 +16,41 @@ import net.minecraft.sounds.SoundSource;
|
||||||
import net.minecraft.world.entity.Entity;
|
import net.minecraft.world.entity.Entity;
|
||||||
import net.minecraft.world.entity.EntityType;
|
import net.minecraft.world.entity.EntityType;
|
||||||
import net.minecraft.world.entity.LivingEntity;
|
import net.minecraft.world.entity.LivingEntity;
|
||||||
import net.minecraft.world.entity.projectile.AbstractArrow;
|
import net.minecraft.world.entity.projectile.ThrowableItemProjectile;
|
||||||
import net.minecraft.world.entity.projectile.ItemSupplier;
|
import net.minecraft.world.item.Item;
|
||||||
import net.minecraft.world.item.ItemStack;
|
|
||||||
import net.minecraft.world.level.Level;
|
import net.minecraft.world.level.Level;
|
||||||
import net.minecraft.world.level.block.Blocks;
|
|
||||||
import net.minecraft.world.phys.AABB;
|
import net.minecraft.world.phys.AABB;
|
||||||
import net.minecraft.world.phys.BlockHitResult;
|
import net.minecraft.world.phys.BlockHitResult;
|
||||||
import net.minecraft.world.phys.EntityHitResult;
|
import net.minecraft.world.phys.EntityHitResult;
|
||||||
import net.minecraft.world.phys.Vec3;
|
import net.minecraft.world.phys.Vec3;
|
||||||
import net.minecraftforge.api.distmarker.Dist;
|
|
||||||
import net.minecraftforge.api.distmarker.OnlyIn;
|
|
||||||
import net.minecraftforge.network.NetworkHooks;
|
import net.minecraftforge.network.NetworkHooks;
|
||||||
import net.minecraftforge.network.PlayMessages;
|
|
||||||
|
|
||||||
import java.util.Optional;
|
import java.util.Optional;
|
||||||
|
|
||||||
// TODO 父类改为Projectile
|
public class GunGrenadeEntity extends ThrowableItemProjectile {
|
||||||
@OnlyIn(value = Dist.CLIENT, _interface = ItemSupplier.class)
|
private float damage = 5f;
|
||||||
public class GunGrenadeEntity extends AbstractArrow implements ItemSupplier {
|
|
||||||
public static final ItemStack PROJECTILE_ITEM = new ItemStack(Blocks.AIR);
|
|
||||||
|
|
||||||
public GunGrenadeEntity(PlayMessages.SpawnEntity packet, Level world) {
|
|
||||||
super(TargetModEntities.GUN_GRENADE.get(), world);
|
|
||||||
}
|
|
||||||
|
|
||||||
public GunGrenadeEntity(EntityType<? extends GunGrenadeEntity> type, Level world) {
|
public GunGrenadeEntity(EntityType<? extends GunGrenadeEntity> type, Level world) {
|
||||||
super(type, world);
|
super(type, world);
|
||||||
}
|
}
|
||||||
|
|
||||||
public GunGrenadeEntity(EntityType<? extends GunGrenadeEntity> type, double x, double y, double z, Level world) {
|
|
||||||
super(type, x, y, z, world);
|
|
||||||
}
|
|
||||||
|
|
||||||
public GunGrenadeEntity(EntityType<? extends GunGrenadeEntity> type, LivingEntity entity, Level world) {
|
public GunGrenadeEntity(EntityType<? extends GunGrenadeEntity> type, LivingEntity entity, Level world) {
|
||||||
super(type, entity, world);
|
super(type, entity, world);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public GunGrenadeEntity(LivingEntity entity, Level level, float damage) {
|
||||||
|
super(TargetModEntities.GUN_GRENADE.get(), entity, level);
|
||||||
|
this.damage = damage;
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public Packet<ClientGamePacketListener> getAddEntityPacket() {
|
public Packet<ClientGamePacketListener> getAddEntityPacket() {
|
||||||
return NetworkHooks.getEntitySpawningPacket(this);
|
return NetworkHooks.getEntitySpawningPacket(this);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@OnlyIn(Dist.CLIENT)
|
protected Item getDefaultItem() {
|
||||||
public ItemStack getItem() {
|
return TargetModItems.GRENADE_40MM.get();
|
||||||
return PROJECTILE_ITEM;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
protected ItemStack getPickupItem() {
|
|
||||||
return PROJECTILE_ITEM;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
protected void doPostHurtEffects(LivingEntity entity) {
|
|
||||||
super.doPostHurtEffects(entity);
|
|
||||||
entity.setArrowCount(entity.getArrowCount() - 1);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
@ -87,7 +67,6 @@ public class GunGrenadeEntity extends AbstractArrow implements ItemSupplier {
|
||||||
}
|
}
|
||||||
|
|
||||||
if (this.getPersistentData().getInt("fuse") > 0) {
|
if (this.getPersistentData().getInt("fuse") > 0) {
|
||||||
|
|
||||||
if (this.level() instanceof ServerLevel level) {
|
if (this.level() instanceof ServerLevel level) {
|
||||||
level.explode(this, (this.getX()), (this.getY()), (this.getZ()), 5.5f, Level.ExplosionInteraction.NONE);
|
level.explode(this, (this.getX()), (this.getY()), (this.getZ()), 5.5f, Level.ExplosionInteraction.NONE);
|
||||||
if (!entity.level().isClientSide()) {
|
if (!entity.level().isClientSide()) {
|
||||||
|
@ -100,6 +79,7 @@ public class GunGrenadeEntity extends AbstractArrow implements ItemSupplier {
|
||||||
if (entity instanceof LivingEntity) {
|
if (entity instanceof LivingEntity) {
|
||||||
entity.invulnerableTime = 0;
|
entity.invulnerableTime = 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
AABB boundingBox = entity.getBoundingBox();
|
AABB boundingBox = entity.getBoundingBox();
|
||||||
Vec3 startVec = this.position();
|
Vec3 startVec = this.position();
|
||||||
Vec3 endVec = startVec.add(this.getDeltaMovement());
|
Vec3 endVec = startVec.add(this.getDeltaMovement());
|
||||||
|
@ -122,7 +102,6 @@ public class GunGrenadeEntity extends AbstractArrow implements ItemSupplier {
|
||||||
}
|
}
|
||||||
if (headshot) {
|
if (headshot) {
|
||||||
if (this.getOwner() instanceof LivingEntity living) {
|
if (this.getOwner() instanceof LivingEntity living) {
|
||||||
setBaseDamage(getBaseDamage() * 2);
|
|
||||||
living.getCapability(TargetModVariables.PLAYER_VARIABLES_CAPABILITY, null).ifPresent(capability -> {
|
living.getCapability(TargetModVariables.PLAYER_VARIABLES_CAPABILITY, null).ifPresent(capability -> {
|
||||||
capability.headIndicator = 25;
|
capability.headIndicator = 25;
|
||||||
capability.syncPlayerVariables(living);
|
capability.syncPlayerVariables(living);
|
||||||
|
@ -135,7 +114,13 @@ public class GunGrenadeEntity extends AbstractArrow implements ItemSupplier {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
super.onHitEntity(result);
|
|
||||||
|
if (headshot) {
|
||||||
|
entity.hurt(TargetModDamageTypes.causeGunFireHeadshotDamage(this.level().registryAccess(), this.getOwner()), this.damage * 2f);
|
||||||
|
} else {
|
||||||
|
entity.hurt(TargetModDamageTypes.causeGunFireDamage(this.level().registryAccess(), this.getOwner()), this.damage);
|
||||||
|
}
|
||||||
|
|
||||||
this.discard();
|
this.discard();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -145,8 +130,11 @@ public class GunGrenadeEntity extends AbstractArrow implements ItemSupplier {
|
||||||
if (this.getPersistentData().getInt("fuse") > 0) {
|
if (this.getPersistentData().getInt("fuse") > 0) {
|
||||||
if (this.level() instanceof ServerLevel) {
|
if (this.level() instanceof ServerLevel) {
|
||||||
this.level().explode(this, this.getX(), this.getY(), this.getZ(), 5.5f, Level.ExplosionInteraction.NONE);
|
this.level().explode(this, this.getX(), this.getY(), this.getZ(), 5.5f, Level.ExplosionInteraction.NONE);
|
||||||
|
ParticleTool.spawnMediumExplosionParticles(this.level(), this.position());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
this.discard();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
@ -159,12 +147,7 @@ public class GunGrenadeEntity extends AbstractArrow implements ItemSupplier {
|
||||||
ParticleTool.sendParticle(serverLevel, ParticleTypes.CAMPFIRE_COSY_SMOKE, this.getX(), this.getY(), this.getZ(),
|
ParticleTool.sendParticle(serverLevel, ParticleTypes.CAMPFIRE_COSY_SMOKE, this.getX(), this.getY(), this.getZ(),
|
||||||
1, 0, 0, 0, 0.02, true);
|
1, 0, 0, 0, 0.02, true);
|
||||||
}
|
}
|
||||||
if (this.inGround) {
|
|
||||||
if (!this.level().isClientSide()) {
|
|
||||||
ParticleTool.spawnMediumExplosionParticles(this.level(), this.position());
|
|
||||||
}
|
|
||||||
this.discard();
|
|
||||||
}
|
|
||||||
if (this.tickCount > 200) {
|
if (this.tickCount > 200) {
|
||||||
this.discard();
|
this.discard();
|
||||||
}
|
}
|
||||||
|
|
|
@ -30,7 +30,7 @@ public class TargetModEntities {
|
||||||
EntityType.Builder.<TaserBulletProjectileEntity>of(TaserBulletProjectileEntity::new, MobCategory.MISC).setShouldReceiveVelocityUpdates(true).setTrackingRange(64)
|
EntityType.Builder.<TaserBulletProjectileEntity>of(TaserBulletProjectileEntity::new, MobCategory.MISC).setShouldReceiveVelocityUpdates(true).setTrackingRange(64)
|
||||||
.setUpdateInterval(1).sized(0.5f, 0.5f));
|
.setUpdateInterval(1).sized(0.5f, 0.5f));
|
||||||
public static final RegistryObject<EntityType<GunGrenadeEntity>> GUN_GRENADE = register("projectile_gun_grenade",
|
public static final RegistryObject<EntityType<GunGrenadeEntity>> GUN_GRENADE = register("projectile_gun_grenade",
|
||||||
EntityType.Builder.<GunGrenadeEntity>of(GunGrenadeEntity::new, MobCategory.MISC).setCustomClientFactory(GunGrenadeEntity::new).setShouldReceiveVelocityUpdates(true).setTrackingRange(64).setUpdateInterval(1).sized(0.5f, 0.5f));
|
EntityType.Builder.<GunGrenadeEntity>of(GunGrenadeEntity::new, MobCategory.MISC).setShouldReceiveVelocityUpdates(true).setTrackingRange(64).setUpdateInterval(1).sized(0.5f, 0.5f));
|
||||||
public static final RegistryObject<EntityType<TargetEntity>> TARGET = register("target",
|
public static final RegistryObject<EntityType<TargetEntity>> TARGET = register("target",
|
||||||
EntityType.Builder.<TargetEntity>of(TargetEntity::new, MobCategory.CREATURE).setShouldReceiveVelocityUpdates(true).setTrackingRange(64).setUpdateInterval(3).setCustomClientFactory(TargetEntity::new).fireImmune().sized(0.875f, 2f));
|
EntityType.Builder.<TargetEntity>of(TargetEntity::new, MobCategory.CREATURE).setShouldReceiveVelocityUpdates(true).setTrackingRange(64).setUpdateInterval(3).setCustomClientFactory(TargetEntity::new).fireImmune().sized(0.875f, 2f));
|
||||||
public static final RegistryObject<EntityType<RpgRocketEntity>> RPG_ROCKET = register("projectile_rpg_rocket",
|
public static final RegistryObject<EntityType<RpgRocketEntity>> RPG_ROCKET = register("projectile_rpg_rocket",
|
||||||
|
|
|
@ -2,7 +2,6 @@ package net.mcreator.target.procedures;
|
||||||
|
|
||||||
import net.mcreator.target.entity.GunGrenadeEntity;
|
import net.mcreator.target.entity.GunGrenadeEntity;
|
||||||
import net.mcreator.target.init.TargetModAttributes;
|
import net.mcreator.target.init.TargetModAttributes;
|
||||||
import net.mcreator.target.init.TargetModEntities;
|
|
||||||
import net.mcreator.target.init.TargetModItems;
|
import net.mcreator.target.init.TargetModItems;
|
||||||
import net.mcreator.target.init.TargetModSounds;
|
import net.mcreator.target.init.TargetModSounds;
|
||||||
import net.mcreator.target.network.TargetModVariables;
|
import net.mcreator.target.network.TargetModVariables;
|
||||||
|
@ -14,18 +13,17 @@ import net.minecraft.server.level.ServerPlayer;
|
||||||
import net.minecraft.world.entity.Entity;
|
import net.minecraft.world.entity.Entity;
|
||||||
import net.minecraft.world.entity.LivingEntity;
|
import net.minecraft.world.entity.LivingEntity;
|
||||||
import net.minecraft.world.entity.player.Player;
|
import net.minecraft.world.entity.player.Player;
|
||||||
import net.minecraft.world.entity.projectile.AbstractArrow;
|
|
||||||
import net.minecraft.world.entity.projectile.Projectile;
|
|
||||||
import net.minecraft.world.item.ItemStack;
|
import net.minecraft.world.item.ItemStack;
|
||||||
import net.minecraft.world.level.Level;
|
import net.minecraft.world.level.Level;
|
||||||
|
|
||||||
|
// TODO 内联这个类
|
||||||
public class M79fireProcedure {
|
public class M79fireProcedure {
|
||||||
public static void execute(Entity entity) {
|
public static void execute(Entity entity) {
|
||||||
if (entity == null) return;
|
if (entity == null) return;
|
||||||
if (entity instanceof Player player && !player.isSpectator()) {
|
if (entity instanceof Player player && !player.isSpectator()) {
|
||||||
ItemStack usehand = player.getMainHandItem();
|
ItemStack stack = player.getMainHandItem();
|
||||||
if (usehand.getItem() == TargetModItems.M_79.get() && !usehand.getOrCreateTag().getBoolean("reloading") && !(entity instanceof Player _plrCldCheck4 && _plrCldCheck4.getCooldowns().isOnCooldown(usehand.getItem()))
|
if (stack.getItem() == TargetModItems.M_79.get() && !stack.getOrCreateTag().getBoolean("reloading") && !(entity instanceof Player _plrCldCheck4 && _plrCldCheck4.getCooldowns().isOnCooldown(stack.getItem()))
|
||||||
&& usehand.getOrCreateTag().getInt("ammo") > 0) {
|
&& stack.getOrCreateTag().getInt("ammo") > 0) {
|
||||||
entity.getCapability(TargetModVariables.PLAYER_VARIABLES_CAPABILITY, null).ifPresent(capability -> {
|
entity.getCapability(TargetModVariables.PLAYER_VARIABLES_CAPABILITY, null).ifPresent(capability -> {
|
||||||
capability.recoilHorizon = Math.random() < 0.5 ? -1 : 1;
|
capability.recoilHorizon = Math.random() < 0.5 ? -1 : 1;
|
||||||
capability.recoil = 0.1;
|
capability.recoil = 0.1;
|
||||||
|
@ -33,22 +31,14 @@ public class M79fireProcedure {
|
||||||
capability.syncPlayerVariables(entity);
|
capability.syncPlayerVariables(entity);
|
||||||
});
|
});
|
||||||
|
|
||||||
Level projectileLevel = entity.level();
|
Level level = entity.level();
|
||||||
if (!projectileLevel.isClientSide()) {
|
if (!level.isClientSide()) {
|
||||||
Projectile _entityToSpawn = new Object() {
|
GunGrenadeEntity gunGrenadeEntity = new GunGrenadeEntity(player, level, (float) stack.getOrCreateTag().getDouble("damage"));
|
||||||
public Projectile getArrow(Level level, Entity shooter, float damage, int knockback) {
|
|
||||||
AbstractArrow entityToSpawn = new GunGrenadeEntity(TargetModEntities.GUN_GRENADE.get(), level);
|
gunGrenadeEntity.setPos(entity.getX(), entity.getEyeY() - 0.1, entity.getZ());
|
||||||
entityToSpawn.setOwner(shooter);
|
gunGrenadeEntity.shoot(entity.getLookAngle().x, entity.getLookAngle().y, entity.getLookAngle().z, (float) stack.getOrCreateTag().getDouble("velocity"),
|
||||||
entityToSpawn.setBaseDamage(damage);
|
|
||||||
entityToSpawn.setKnockback(knockback);
|
|
||||||
entityToSpawn.setSilent(true);
|
|
||||||
return entityToSpawn;
|
|
||||||
}
|
|
||||||
}.getArrow(projectileLevel, entity, (float) ((usehand.getOrCreateTag().getDouble("damage") / usehand.getOrCreateTag().getDouble("velocity")) * (1 + 0.05 * usehand.getOrCreateTag().getInt("level"))), 0);
|
|
||||||
_entityToSpawn.setPos(entity.getX(), entity.getEyeY() - 0.1, entity.getZ());
|
|
||||||
_entityToSpawn.shoot(entity.getLookAngle().x, entity.getLookAngle().y, entity.getLookAngle().z, (float) usehand.getOrCreateTag().getDouble("velocity"),
|
|
||||||
(float) ((LivingEntity) entity).getAttribute(TargetModAttributes.SPREAD.get()).getBaseValue());
|
(float) ((LivingEntity) entity).getAttribute(TargetModAttributes.SPREAD.get()).getBaseValue());
|
||||||
projectileLevel.addFreshEntity(_entityToSpawn);
|
level.addFreshEntity(gunGrenadeEntity);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!entity.level().isClientSide() && entity.getServer() != null) {
|
if (!entity.level().isClientSide() && entity.getServer() != null) {
|
||||||
|
@ -58,7 +48,7 @@ public class M79fireProcedure {
|
||||||
("particle minecraft:cloud" + (" " + (entity.getX() + 1.8 * entity.getLookAngle().x)) + (" " + (entity.getY() + entity.getBbHeight() - 0.1 + 1.8 * entity.getLookAngle().y))
|
("particle minecraft:cloud" + (" " + (entity.getX() + 1.8 * entity.getLookAngle().x)) + (" " + (entity.getY() + entity.getBbHeight() - 0.1 + 1.8 * entity.getLookAngle().y))
|
||||||
+ (" " + (entity.getZ() + 1.8 * entity.getLookAngle().z)) + " 0.1 0.1 0.1 0.002 4 force @s"));
|
+ (" " + (entity.getZ() + 1.8 * entity.getLookAngle().z)) + " 0.1 0.1 0.1 0.002 4 force @s"));
|
||||||
}
|
}
|
||||||
player.getCooldowns().addCooldown(usehand.getItem(), 15);
|
player.getCooldowns().addCooldown(stack.getItem(), 15);
|
||||||
|
|
||||||
if (entity instanceof ServerPlayer) {
|
if (entity instanceof ServerPlayer) {
|
||||||
SoundTool.playLocalSound(player, TargetModSounds.M_79_FIRE_1P.get(), 2, 1);
|
SoundTool.playLocalSound(player, TargetModSounds.M_79_FIRE_1P.get(), 2, 1);
|
||||||
|
@ -66,8 +56,8 @@ public class M79fireProcedure {
|
||||||
SoundTool.playLocalSound(player, TargetModSounds.M_79_FAR.get(), 6, 1);
|
SoundTool.playLocalSound(player, TargetModSounds.M_79_FAR.get(), 6, 1);
|
||||||
SoundTool.playLocalSound(player, TargetModSounds.M_79_VERYFAR.get(), 12, 1);
|
SoundTool.playLocalSound(player, TargetModSounds.M_79_VERYFAR.get(), 12, 1);
|
||||||
}
|
}
|
||||||
usehand.getOrCreateTag().putInt("fire_animation", 2);
|
stack.getOrCreateTag().putInt("fire_animation", 2);
|
||||||
usehand.getOrCreateTag().putInt("ammo", (usehand.getOrCreateTag().getInt("ammo") - 1));
|
stack.getOrCreateTag().putInt("ammo", (stack.getOrCreateTag().getInt("ammo") - 1));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Reference in a new issue