修改RPG实体类型

This commit is contained in:
17146 2024-05-20 14:25:07 +08:00
parent 10d91c16b7
commit 70a9195361
3 changed files with 45 additions and 96 deletions

View file

@ -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;
@ -11,78 +13,44 @@ import net.minecraft.network.protocol.Packet;
import net.minecraft.network.protocol.game.ClientGamePacketListener; import net.minecraft.network.protocol.game.ClientGamePacketListener;
import net.minecraft.server.level.ServerLevel; import net.minecraft.server.level.ServerLevel;
import net.minecraft.sounds.SoundSource; import net.minecraft.sounds.SoundSource;
import net.minecraft.util.RandomSource;
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 RpgRocketEntity extends ThrowableItemProjectile {
@OnlyIn(value = Dist.CLIENT, _interface = ItemSupplier.class) private float damage = 150f;
public class RpgRocketEntity extends AbstractArrow implements ItemSupplier {
public static final ItemStack PROJECTILE_ITEM = new ItemStack(Blocks.AIR);
public RpgRocketEntity(PlayMessages.SpawnEntity packet, Level world) {
super(TargetModEntities.RPG_ROCKET.get(), world);
}
public RpgRocketEntity(EntityType<? extends RpgRocketEntity> type, Level world) { public RpgRocketEntity(EntityType<? extends RpgRocketEntity> type, Level world) {
super(type, world); super(type, world);
} }
public RpgRocketEntity(EntityType<? extends RpgRocketEntity> type, double x, double y, double z, Level world) {
super(type, x, y, z, world);
}
public RpgRocketEntity(EntityType<? extends RpgRocketEntity> type, LivingEntity entity, Level world) { public RpgRocketEntity(EntityType<? extends RpgRocketEntity> type, LivingEntity entity, Level world) {
super(type, entity, world); super(type, entity, world);
} }
public RpgRocketEntity(LivingEntity entity, Level level, float damage) {
super(TargetModEntities.RPG_ROCKET.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.ROCKET.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);
}
public static RpgRocketEntity shoot(Level world, LivingEntity entity, RandomSource random, float power, double damage, int knockback) {
RpgRocketEntity entityArrow = new RpgRocketEntity(TargetModEntities.RPG_ROCKET.get(), entity, world);
entityArrow.shoot(entity.getViewVector(1).x, entity.getViewVector(1).y, entity.getViewVector(1).z, power * 2, 0);
entityArrow.setSilent(true);
entityArrow.setCritArrow(false);
entityArrow.setBaseDamage(damage);
entityArrow.setKnockback(knockback);
world.addFreshEntity(entityArrow);
return entityArrow;
} }
@Override @Override
@ -104,7 +72,6 @@ public class RpgRocketEntity extends AbstractArrow implements ItemSupplier {
if (!entity.level().isClientSide()) { if (!entity.level().isClientSide()) {
ParticleTool.spawnMediumExplosionParticles(this.level(), this.position()); ParticleTool.spawnMediumExplosionParticles(this.level(), this.position());
} }
this.discard(); this.discard();
} }
} }
@ -112,6 +79,7 @@ public class RpgRocketEntity 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());
@ -134,7 +102,6 @@ public class RpgRocketEntity extends AbstractArrow implements ItemSupplier {
} }
if (headshot) { if (headshot) {
if (this.getOwner() instanceof LivingEntity living) { if (this.getOwner() instanceof LivingEntity living) {
setBaseDamage(getBaseDamage() * 5);
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);
@ -147,7 +114,13 @@ public class RpgRocketEntity extends AbstractArrow implements ItemSupplier {
} }
} }
} }
super.onHitEntity(result);
if (headshot) {
entity.hurt(TargetModDamageTypes.causeGunFireHeadshotDamage(this.level().registryAccess(), this.getOwner()), this.damage * 5f);
} else {
entity.hurt(TargetModDamageTypes.causeGunFireDamage(this.level().registryAccess(), this.getOwner()), this.damage);
}
this.discard(); this.discard();
} }
@ -163,22 +136,23 @@ public class RpgRocketEntity extends AbstractArrow implements ItemSupplier {
if (this.getPersistentData().getInt("time") > 0) { if (this.getPersistentData().getInt("time") > 0) {
if (this.level() instanceof ServerLevel level) { if (this.level() instanceof ServerLevel level) {
level.explode(this, this.getX(), this.getY(), this.getZ(), 6, Level.ExplosionInteraction.NONE); level.explode(this, this.getX(), this.getY(), this.getZ(), 6, Level.ExplosionInteraction.NONE);
ParticleTool.spawnMediumExplosionParticles(this.level(), this.position());
} }
} }
this.discard();
} }
@Override @Override
public void tick() { public void tick() {
super.tick(); super.tick();
this.getPersistentData().putInt("time", (1 + this.getPersistentData().getInt("time"))); if (this.tickCount == 4) {
double life = this.getPersistentData().getInt("time");
if (life == 4) {
if (!this.level().isClientSide() && this.level() instanceof ServerLevel serverLevel) { if (!this.level().isClientSide() && this.level() instanceof ServerLevel serverLevel) {
ParticleTool.sendParticle(serverLevel, ParticleTypes.CAMPFIRE_COSY_SMOKE, this.getX(), this.getY(), this.getZ(), 50, 0.8, 0.8, 0.8, 0.01, true); ParticleTool.sendParticle(serverLevel, ParticleTypes.CAMPFIRE_COSY_SMOKE, this.getX(), this.getY(), this.getZ(), 50, 0.8, 0.8, 0.8, 0.01, true);
} }
} }
if (life >= 4) { if (this.tickCount > 4) {
this.setDeltaMovement(new Vec3((1.04 * this.getDeltaMovement().x()), (1.04 * this.getDeltaMovement().y() - 0.02), (1.04 * this.getDeltaMovement().z()))); this.setDeltaMovement(new Vec3((1.04 * this.getDeltaMovement().x()), (1.04 * this.getDeltaMovement().y() - 0.02), (1.04 * this.getDeltaMovement().z())));
if (!this.level().isClientSide() && this.level() instanceof ServerLevel serverLevel) { if (!this.level().isClientSide() && this.level() instanceof ServerLevel serverLevel) {
@ -186,20 +160,11 @@ public class RpgRocketEntity extends AbstractArrow implements ItemSupplier {
ParticleTool.sendParticle(serverLevel, ParticleTypes.CAMPFIRE_COSY_SMOKE, this.getX(), this.getY(), this.getZ(), 2, 0, 0, 0, 0, true); ParticleTool.sendParticle(serverLevel, ParticleTypes.CAMPFIRE_COSY_SMOKE, this.getX(), this.getY(), this.getZ(), 2, 0, 0, 0, 0, true);
} }
} }
if (life >= 90) {
if (this.tickCount >= 90) {
if (!this.level().isClientSide()) { if (!this.level().isClientSide()) {
ParticleTool.spawnMediumExplosionParticles(this.level(), this.position()); ParticleTool.spawnMediumExplosionParticles(this.level(), this.position());
} }
if (!this.level().isClientSide())
this.discard();
}
if (this.inGround) {
if (!this.level().isClientSide()) {
ParticleTool.spawnMediumExplosionParticles(this.level(), this.position());
}
this.discard();
}
if (this.tickCount > 100) {
this.discard(); this.discard();
} }
} }

View file

@ -34,7 +34,7 @@ public class TargetModEntities {
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",
EntityType.Builder.<RpgRocketEntity>of(RpgRocketEntity::new, MobCategory.MISC).setCustomClientFactory(RpgRocketEntity::new).setShouldReceiveVelocityUpdates(true).setTrackingRange(64).setUpdateInterval(1).sized(0.5f, 0.5f)); EntityType.Builder.<RpgRocketEntity>of(RpgRocketEntity::new, MobCategory.MISC).setShouldReceiveVelocityUpdates(true).setTrackingRange(64).setUpdateInterval(1).sized(0.5f, 0.5f));
public static final RegistryObject<EntityType<MortarShellEntity>> MORTAR_SHELL = register("projectile_mortar_shell", public static final RegistryObject<EntityType<MortarShellEntity>> MORTAR_SHELL = register("projectile_mortar_shell",
EntityType.Builder.<MortarShellEntity>of(MortarShellEntity::new, MobCategory.MISC).setShouldReceiveVelocityUpdates(true).setTrackingRange(64).setUpdateInterval(1).sized(0.5f, 0.5f)); EntityType.Builder.<MortarShellEntity>of(MortarShellEntity::new, MobCategory.MISC).setShouldReceiveVelocityUpdates(true).setTrackingRange(64).setUpdateInterval(1).sized(0.5f, 0.5f));
public static final RegistryObject<EntityType<BocekArrowEntity>> BOCEK_ARROW = register("projectile_bocekarrow", public static final RegistryObject<EntityType<BocekArrowEntity>> BOCEK_ARROW = register("projectile_bocekarrow",
@ -49,13 +49,7 @@ public class TargetModEntities {
@SubscribeEvent @SubscribeEvent
public static void init(FMLCommonSetupEvent event) { public static void init(FMLCommonSetupEvent event) {
event.enqueueWork(() -> { event.enqueueWork(SenpaiEntity::init);
Target1Entity.init();
MortarEntity.init();
SenpaiEntity.init();
ClaymoreEntity.init();
TargetEntity.init();
});
} }
@SubscribeEvent @SubscribeEvent

View file

@ -2,7 +2,6 @@ package net.mcreator.target.procedures;
import net.mcreator.target.entity.RpgRocketEntity; import net.mcreator.target.entity.RpgRocketEntity;
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;
@ -11,17 +10,18 @@ import net.minecraft.commands.CommandSourceStack;
import net.minecraft.nbt.CompoundTag; import net.minecraft.nbt.CompoundTag;
import net.minecraft.server.level.ServerLevel; import net.minecraft.server.level.ServerLevel;
import net.minecraft.sounds.SoundSource; import net.minecraft.sounds.SoundSource;
import net.minecraft.world.entity.Entity;
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 RpgFireProcedure { public class RpgFireProcedure {
public static void execute(Player player) { public static void execute(Player player) {
if (player.isSpectator()) return; if (player.isSpectator()) {
return;
}
Level level = player.level();
ItemStack mainHandItem = player.getMainHandItem(); ItemStack mainHandItem = player.getMainHandItem();
CompoundTag tag = mainHandItem.getOrCreateTag(); CompoundTag tag = mainHandItem.getOrCreateTag();
@ -34,13 +34,13 @@ public class RpgFireProcedure {
capability.syncPlayerVariables(player); capability.syncPlayerVariables(player);
}); });
Level projectileLevel = player.level();
if (!projectileLevel.isClientSide()) { if (!level.isClientSide()) {
Projectile projectile = getArrow(player, projectileLevel, tag); RpgRocketEntity rocketEntity = new RpgRocketEntity(player, level, (float) tag.getDouble("damage"));
projectile.setPos(player.getX(), player.getEyeY() - 0.1, player.getZ()); rocketEntity.setPos(player.getX(), player.getEyeY() - 0.1, player.getZ());
projectile.shoot(player.getLookAngle().x, player.getLookAngle().y, player.getLookAngle().z, (float) tag.getDouble("velocity"), rocketEntity.shoot(player.getLookAngle().x, player.getLookAngle().y, player.getLookAngle().z, (float) tag.getDouble("velocity"),
(float) player.getAttribute(TargetModAttributes.SPREAD.get()).getBaseValue()); (float) player.getAttribute(TargetModAttributes.SPREAD.get()).getBaseValue());
projectileLevel.addFreshEntity(projectile); level.addFreshEntity(rocketEntity);
} }
if (player.getServer() != null) { if (player.getServer() != null) {
@ -50,32 +50,22 @@ public class RpgFireProcedure {
("particle minecraft:cloud" + (" " + (player.getX() + 1.8 * player.getLookAngle().x)) + (" " + (player.getY() + player.getBbHeight() - 0.1 + 1.8 * player.getLookAngle().y)) ("particle minecraft:cloud" + (" " + (player.getX() + 1.8 * player.getLookAngle().x)) + (" " + (player.getY() + player.getBbHeight() - 0.1 + 1.8 * player.getLookAngle().y))
+ (" " + (player.getZ() + 1.8 * player.getLookAngle().z)) + " 0.4 0.4 0.4 0.005 30 force @s")); + (" " + (player.getZ() + 1.8 * player.getLookAngle().z)) + " 0.4 0.4 0.4 0.005 30 force @s"));
} }
if (tag.getInt("ammo") == 1) { if (tag.getInt("ammo") == 1) {
tag.putDouble("empty", 1); tag.putDouble("empty", 1);
} }
player.getCooldowns().addCooldown(mainHandItem.getItem(), 10); player.getCooldowns().addCooldown(mainHandItem.getItem(), 10);
if (!player.level().isClientSide() && player.getServer() != null) { if (!player.level().isClientSide() && player.getServer() != null) {
player.level().playSound(null, player.blockPosition(), TargetModSounds.RPG_FIRE_1P.get(), SoundSource.PLAYERS, 2, 1); player.level().playSound(null, player.blockPosition(), TargetModSounds.RPG_FIRE_1P.get(), SoundSource.PLAYERS, 2, 1);
player.level().playSound(null, player.blockPosition(), TargetModSounds.RPG_FIRE_3P.get(), SoundSource.PLAYERS, 4, 1); player.level().playSound(null, player.blockPosition(), TargetModSounds.RPG_FIRE_3P.get(), SoundSource.PLAYERS, 4, 1);
player.level().playSound(null, player.blockPosition(), TargetModSounds.RPG_FAR.get(), SoundSource.PLAYERS, 8, 1); player.level().playSound(null, player.blockPosition(), TargetModSounds.RPG_FAR.get(), SoundSource.PLAYERS, 8, 1);
player.level().playSound(null, player.blockPosition(), TargetModSounds.RPG_VERYFAR.get(), SoundSource.PLAYERS, 16, 1); player.level().playSound(null, player.blockPosition(), TargetModSounds.RPG_VERYFAR.get(), SoundSource.PLAYERS, 16, 1);
} }
tag.putInt("fire_animation", 2); tag.putInt("fire_animation", 2);
tag.putInt("ammo", tag.getInt("ammo") - 1); tag.putInt("ammo", tag.getInt("ammo") - 1);
} }
} }
private static Projectile getArrow(Player player, Level projectileLevel, CompoundTag tag) {
return new Object() {
public Projectile getArrow(Level level, Entity shooter, float damage, int knockback) {
AbstractArrow entityToSpawn = new RpgRocketEntity(TargetModEntities.RPG_ROCKET.get(), level);
entityToSpawn.setOwner(shooter);
entityToSpawn.setBaseDamage(damage);
entityToSpawn.setKnockback(knockback);
entityToSpawn.setSilent(true);
return entityToSpawn;
}
}.getArrow(projectileLevel, player, (float) ((tag.getDouble("damage") / tag.getDouble("velocity")) * (1 + 0.05 * tag.getInt("level"))), 0);
}
} }