修改子弹伤害算法

This commit is contained in:
17146 2024-05-06 13:48:29 +08:00
parent c87d5c5a69
commit 437dc57f44
5 changed files with 111 additions and 106 deletions

View file

@ -3,18 +3,22 @@ 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.TargetCustomModEntities; import net.mcreator.target.init.TargetCustomModEntities;
import net.mcreator.target.procedures.ProjectileHeadshotEntity; import net.mcreator.target.init.TargetModDamageTypes;
import net.mcreator.target.procedures.ProjectileHitEntity; import net.mcreator.target.init.TargetModSounds;
import net.mcreator.target.network.TargetModVariables;
import net.mcreator.target.util.math.ExtendedEntityRayTraceResult; import net.mcreator.target.util.math.ExtendedEntityRayTraceResult;
import net.minecraft.commands.CommandSource; import net.minecraft.commands.CommandSource;
import net.minecraft.commands.CommandSourceStack; import net.minecraft.commands.CommandSourceStack;
import net.minecraft.core.BlockPos; import net.minecraft.core.BlockPos;
import net.minecraft.core.Direction; import net.minecraft.core.Direction;
import net.minecraft.core.Holder;
import net.minecraft.nbt.CompoundTag; import net.minecraft.nbt.CompoundTag;
import net.minecraft.network.FriendlyByteBuf; import net.minecraft.network.FriendlyByteBuf;
import net.minecraft.network.chat.Component; import net.minecraft.network.chat.Component;
import net.minecraft.network.protocol.game.ClientboundSoundPacket;
import net.minecraft.server.level.ServerLevel; import net.minecraft.server.level.ServerLevel;
import net.minecraft.server.level.ServerPlayer; import net.minecraft.server.level.ServerPlayer;
import net.minecraft.sounds.SoundSource;
import net.minecraft.util.Mth; import net.minecraft.util.Mth;
import net.minecraft.world.entity.Entity; import net.minecraft.world.entity.Entity;
import net.minecraft.world.entity.EntityType; import net.minecraft.world.entity.EntityType;
@ -43,6 +47,7 @@ public class ProjectileEntity extends Entity implements IEntityAdditionalSpawnDa
protected LivingEntity shooter; protected LivingEntity shooter;
protected int shooterId; protected int shooterId;
private float damage = 1f; private float damage = 1f;
private float headShot = 1f;
public ProjectileEntity(EntityType<? extends ProjectileEntity> p_i50159_1_, Level p_i50159_2_) { public ProjectileEntity(EntityType<? extends ProjectileEntity> p_i50159_1_, Level p_i50159_2_) {
super(p_i50159_1_, p_i50159_2_); super(p_i50159_1_, p_i50159_2_);
@ -59,6 +64,13 @@ public class ProjectileEntity extends Entity implements IEntityAdditionalSpawnDa
this.damage = damage; this.damage = damage;
} }
public ProjectileEntity(Level world, LivingEntity entity, float damage, float headShot) {
super(TargetCustomModEntities.PROJECTILE.get(), world);
this.shooter = entity;
this.damage = damage;
this.headShot = headShot;
}
@Nullable @Nullable
protected EntityResult findEntityOnPath(Vec3 startVec, Vec3 endVec) { protected EntityResult findEntityOnPath(Vec3 startVec, Vec3 endVec) {
Vec3 hitVec = null; Vec3 hitVec = null;
@ -182,10 +194,10 @@ public class ProjectileEntity extends Entity implements IEntityAdditionalSpawnDa
} }
} }
if (result != null) { if (result != null) {
this.onHit(result, startVec, endVec); this.onHit(result);
} }
} else { } else {
this.onHit(result, startVec, endVec); this.onHit(result);
} }
} }
@ -222,7 +234,7 @@ public class ProjectileEntity extends Entity implements IEntityAdditionalSpawnDa
this.discard(); this.discard();
} }
private void onHit(HitResult result, Vec3 startVec, Vec3 endVec) { private void onHit(HitResult result) {
if (result instanceof BlockHitResult blockHitResult) { if (result instanceof BlockHitResult blockHitResult) {
if (blockHitResult.getType() == HitResult.Type.MISS) { if (blockHitResult.getType() == HitResult.Type.MISS) {
return; return;
@ -244,16 +256,47 @@ public class ProjectileEntity extends Entity implements IEntityAdditionalSpawnDa
} }
} }
this.onHitEntity(entity, result.getLocation(), startVec, endVec, entityHitResult.isHeadshot()); this.onHitEntity(entity, entityHitResult.isHeadshot());
entity.invulnerableTime = 0; entity.invulnerableTime = 0;
} }
} }
protected void onHitEntity(Entity entity, Vec3 hitVec, Vec3 startVec, Vec3 endVec, boolean headshot) { protected void onHitEntity(Entity entity, boolean headshot) {
if (headshot) { if (headshot) {
ProjectileHeadshotEntity.execute(this.level(), entity, this, this.shooter); if (entity == null)
return;
if (!this.shooter.level().isClientSide() && this.shooter instanceof ServerPlayer player) {
var holder = Holder.direct(TargetModSounds.HEADSHOT.get());
player.connection.send(new ClientboundSoundPacket(holder, SoundSource.PLAYERS, player.getX(), player.getY(), player.getZ(), 1f, 1f, player.level().random.nextLong()));
}
double i = 25;
shooter.getCapability(TargetModVariables.PLAYER_VARIABLES_CAPABILITY, null).ifPresent(capability -> {
capability.headind = i;
capability.syncPlayerVariables(shooter);
});
entity.hurt(TargetModDamageTypes.causeGunFireDamage(this.level().registryAccess(), this.shooter), this.damage * this.headShot);
this.discard();
} else {
if (entity == null)
return;
if (!this.shooter.level().isClientSide() && this.shooter instanceof ServerPlayer player) {
var holder = Holder.direct(TargetModSounds.INDICATION.get());
player.connection.send(new ClientboundSoundPacket(holder, SoundSource.PLAYERS, player.getX(), player.getY(), player.getZ(), 1f, 1f, player.level().random.nextLong()));
}
double i = 25;
shooter.getCapability(TargetModVariables.PLAYER_VARIABLES_CAPABILITY, null).ifPresent(capability -> {
capability.hitind = i;
capability.syncPlayerVariables(shooter);
});
entity.hurt(TargetModDamageTypes.causeGunFireDamage(this.level().registryAccess(), this.shooter), this.damage);
this.discard();
} }
ProjectileHitEntity.execute(this.level(), entity, this, this.shooter);
} }
public void setDamage(float damage) { public void setDamage(float damage) {
@ -265,7 +308,7 @@ public class ProjectileEntity extends Entity implements IEntityAdditionalSpawnDa
} }
public void shoot(double p_37266_, double p_37267_, double p_37268_, float p_37269_, float p_37270_) { public void shoot(double p_37266_, double p_37267_, double p_37268_, float p_37269_, float p_37270_) {
Vec3 vec3 = (new Vec3(p_37266_, p_37267_, p_37268_)).normalize().add(this.random.triangle(0.0D, 0.0172275D * (double)p_37270_), this.random.triangle(0.0D, 0.0172275D * (double)p_37270_), this.random.triangle(0.0D, 0.0172275D * (double)p_37270_)).scale((double)p_37269_); Vec3 vec3 = (new Vec3(p_37266_, p_37267_, p_37268_)).normalize().add(this.random.triangle(0.0D, 0.0172275D * (double) p_37270_), this.random.triangle(0.0D, 0.0172275D * (double) p_37270_), this.random.triangle(0.0D, 0.0172275D * (double) p_37270_)).scale(p_37269_);
this.setDeltaMovement(vec3); this.setDeltaMovement(vec3);
double d0 = vec3.horizontalDistance(); double d0 = vec3.horizontalDistance();
this.setYRot((float) (Mth.atan2(vec3.x, vec3.z) * (double) (180F / (float) Math.PI))); this.setYRot((float) (Mth.atan2(vec3.x, vec3.z) * (double) (180F / (float) Math.PI)));

View file

@ -0,0 +1,44 @@
package net.mcreator.target.init;
import net.mcreator.target.TargetMod;
import net.minecraft.core.Holder;
import net.minecraft.core.RegistryAccess;
import net.minecraft.core.registries.Registries;
import net.minecraft.network.chat.Component;
import net.minecraft.resources.ResourceKey;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.world.damagesource.DamageSource;
import net.minecraft.world.damagesource.DamageType;
import net.minecraft.world.entity.Entity;
import net.minecraft.world.entity.LivingEntity;
import javax.annotation.Nullable;
@SuppressWarnings("OptionalGetWithoutIsPresent")
public class TargetModDamageTypes {
public static final ResourceKey<DamageType> GUNFIRE = ResourceKey.create(Registries.DAMAGE_TYPE, new ResourceLocation(TargetMod.MODID, "gunfire"));
public static DamageSource causeGunFireDamage(RegistryAccess registryAccess, @Nullable Entity entity) {
return new DamageMessages(registryAccess.registry(Registries.DAMAGE_TYPE).get().getHolderOrThrow(GUNFIRE), entity);
}
private static class DamageMessages extends DamageSource {
public DamageMessages(Holder.Reference<DamageType> typeReference) {
super(typeReference);
}
public DamageMessages(Holder.Reference<DamageType> typeReference, Entity entity) {
super(typeReference, entity);
}
@Override
public Component getLocalizedDeathMessage(LivingEntity pLivingEntity) {
Entity entity = this.getDirectEntity() == null ? this.getEntity() : this.getDirectEntity();
if (entity == null) {
return Component.translatable("death.attack." + this.getMsgId(), pLivingEntity.getDisplayName());
} else {
return Component.translatable("death.attack." + this.getMsgId() + ".entity", pLivingEntity.getDisplayName(), entity.getDisplayName());
}
}
}
}

View file

@ -2,6 +2,7 @@ package net.mcreator.target.procedures;
import net.mcreator.target.entity.ProjectileEntity; import net.mcreator.target.entity.ProjectileEntity;
import net.mcreator.target.init.TargetModAttributes; import net.mcreator.target.init.TargetModAttributes;
import net.mcreator.target.init.TargetModItems;
import net.mcreator.target.network.TargetModVariables; import net.mcreator.target.network.TargetModVariables;
import net.minecraft.world.entity.Entity; import net.minecraft.world.entity.Entity;
import net.minecraft.world.entity.LivingEntity; import net.minecraft.world.entity.LivingEntity;
@ -34,7 +35,17 @@ public class BulletFireNormalProcedure {
}); });
if (!entity.level().isClientSide() && entity instanceof LivingEntity living) { if (!entity.level().isClientSide() && entity instanceof LivingEntity living) {
ProjectileEntity projectile = new ProjectileEntity(entity.level(), living, (float) heldItem.getOrCreateTag().getDouble("dmg")); float damage;
if (heldItem.getItem() == TargetModItems.BOCEK.get()) {
damage = (float) heldItem.getOrCreateTag().getDouble("speed") * (float) heldItem.getOrCreateTag().getDouble("damageadd");
} else {
damage = (float) (heldItem.getOrCreateTag().getDouble("damage") + heldItem.getOrCreateTag().getDouble("adddamage"))
* (float) heldItem.getOrCreateTag().getDouble("damageadd");
}
float headshot = (float) heldItem.getOrCreateTag().getDouble("headshot");
ProjectileEntity projectile = new ProjectileEntity(entity.level(), living, damage, headshot);
projectile.setPos((living.getX() + (-0.5) * living.getLookAngle().x), (living.getEyeY() - 0.1 + (-0.5) * living.getLookAngle().y), (living.getZ() + (-0.5) * living.getLookAngle().z)); projectile.setPos((living.getX() + (-0.5) * living.getLookAngle().x), (living.getEyeY() - 0.1 + (-0.5) * living.getLookAngle().y), (living.getZ() + (-0.5) * living.getLookAngle().z));
projectile.shoot(living.getLookAngle().x, living.getLookAngle().y, living.getLookAngle().z, (float) heldItem.getOrCreateTag().getDouble("velocity"), projectile.shoot(living.getLookAngle().x, living.getLookAngle().y, living.getLookAngle().z, (float) heldItem.getOrCreateTag().getDouble("velocity"),

View file

@ -1,48 +0,0 @@
package net.mcreator.target.procedures;
import net.mcreator.target.init.TargetModItems;
import net.mcreator.target.network.TargetModVariables;
import net.minecraft.commands.CommandSource;
import net.minecraft.commands.CommandSourceStack;
import net.minecraft.core.registries.Registries;
import net.minecraft.resources.ResourceKey;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.server.level.ServerLevel;
import net.minecraft.world.damagesource.DamageSource;
import net.minecraft.world.entity.Entity;
import net.minecraft.world.entity.LivingEntity;
import net.minecraft.world.item.ItemStack;
import net.minecraft.world.level.LevelAccessor;
public class ProjectileHeadshotEntity {
public static void execute(LevelAccessor world, Entity entity, Entity immediatesourceentity, Entity sourceentity) {
if (entity == null || sourceentity == null)
return;
ItemStack usehand = ItemStack.EMPTY;
double dam = 0;
usehand = (entity instanceof LivingEntity _livEnt ? _livEnt.getMainHandItem() : ItemStack.EMPTY);
{
if (!sourceentity.level().isClientSide() && sourceentity.getServer() != null) {
sourceentity.getServer().getCommands().performPrefixedCommand(new CommandSourceStack(CommandSource.NULL, sourceentity.position(), sourceentity.getRotationVector(), sourceentity.level() instanceof ServerLevel ? (ServerLevel) sourceentity.level() : null, 4,
sourceentity.getName().getString(), sourceentity.getDisplayName(), sourceentity.level().getServer(), sourceentity), "playsound target:headshot voice @s ~ ~ ~ 1 1");
}
}
double _setval = 25;
sourceentity.getCapability(TargetModVariables.PLAYER_VARIABLES_CAPABILITY, null).ifPresent(capability -> {
capability.headind = _setval;
capability.syncPlayerVariables(sourceentity);
});
if ((sourceentity instanceof LivingEntity _livEnt ? _livEnt.getMainHandItem() : ItemStack.EMPTY).getItem() == TargetModItems.BOCEK.get()) {
entity.hurt(new DamageSource(world.registryAccess().registryOrThrow(Registries.DAMAGE_TYPE).getHolderOrThrow(ResourceKey.create(Registries.DAMAGE_TYPE, new ResourceLocation("target:gunfire"))), sourceentity),
(0.2f * (float) ((sourceentity instanceof LivingEntity _livEnt ? _livEnt.getMainHandItem() : ItemStack.EMPTY).getOrCreateTag().getDouble("speed"))) * (float) ((sourceentity instanceof LivingEntity _livEnt ? _livEnt.getMainHandItem() : ItemStack.EMPTY).getOrCreateTag().getDouble("damageadd")) * (float) ((sourceentity instanceof LivingEntity _livEnt ? _livEnt.getMainHandItem() : ItemStack.EMPTY).getOrCreateTag().getDouble("headshot")));
} else {
entity.hurt(new DamageSource(world.registryAccess().registryOrThrow(Registries.DAMAGE_TYPE).getHolderOrThrow(ResourceKey.create(Registries.DAMAGE_TYPE, new ResourceLocation("target:gunfire"))), sourceentity),
((float) ((sourceentity instanceof LivingEntity _livEnt ? _livEnt.getMainHandItem() : ItemStack.EMPTY).getOrCreateTag().getDouble("adddamage")) + (float) ((sourceentity instanceof LivingEntity _livEnt ? _livEnt.getMainHandItem() : ItemStack.EMPTY).getOrCreateTag().getDouble("damage"))) * (float) ((sourceentity instanceof LivingEntity _livEnt ? _livEnt.getMainHandItem() : ItemStack.EMPTY).getOrCreateTag().getDouble("headshot")) * (float) ((sourceentity instanceof LivingEntity _livEnt ? _livEnt.getMainHandItem() : ItemStack.EMPTY).getOrCreateTag().getDouble("damageadd")));
}
immediatesourceentity.discard();
}
}

View file

@ -1,45 +0,0 @@
package net.mcreator.target.procedures;
import net.mcreator.target.init.TargetModItems;
import net.mcreator.target.network.TargetModVariables;
import net.minecraft.commands.CommandSource;
import net.minecraft.commands.CommandSourceStack;
import net.minecraft.core.registries.Registries;
import net.minecraft.resources.ResourceKey;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.server.level.ServerLevel;
import net.minecraft.world.damagesource.DamageSource;
import net.minecraft.world.entity.Entity;
import net.minecraft.world.entity.LivingEntity;
import net.minecraft.world.item.ItemStack;
import net.minecraft.world.level.LevelAccessor;
public class ProjectileHitEntity {
public static void execute(LevelAccessor world, Entity entity, Entity immediatesourceentity, Entity sourceentity) {
if (entity == null || sourceentity == null)
return;
{
Entity _ent = sourceentity;
if (!_ent.level().isClientSide() && _ent.getServer() != null) {
_ent.getServer().getCommands().performPrefixedCommand(new CommandSourceStack(CommandSource.NULL, _ent.position(), _ent.getRotationVector(), _ent.level() instanceof ServerLevel ? (ServerLevel) _ent.level() : null, 4,
_ent.getName().getString(), _ent.getDisplayName(), _ent.level().getServer(), _ent), "playsound target:indication voice @s ~ ~ ~ 1 1");
}
}
double _setval = 25;
sourceentity.getCapability(TargetModVariables.PLAYER_VARIABLES_CAPABILITY, null).ifPresent(capability -> {
capability.hitind = _setval;
capability.syncPlayerVariables(sourceentity);
});
if ((sourceentity instanceof LivingEntity _livEnt ? _livEnt.getMainHandItem() : ItemStack.EMPTY).getItem() == TargetModItems.BOCEK.get()) {
entity.hurt(new DamageSource(world.registryAccess().registryOrThrow(Registries.DAMAGE_TYPE).getHolderOrThrow(ResourceKey.create(Registries.DAMAGE_TYPE, new ResourceLocation("target:gunfire"))), sourceentity),
(0.2f * (float) ((sourceentity instanceof LivingEntity _livEnt ? _livEnt.getMainHandItem() : ItemStack.EMPTY).getOrCreateTag().getDouble("speed"))) * (float) ((sourceentity instanceof LivingEntity _livEnt ? _livEnt.getMainHandItem() : ItemStack.EMPTY).getOrCreateTag().getDouble("damageadd")));
} else {
entity.hurt(new DamageSource(world.registryAccess().registryOrThrow(Registries.DAMAGE_TYPE).getHolderOrThrow(ResourceKey.create(Registries.DAMAGE_TYPE, new ResourceLocation("target:gunfire"))), sourceentity),
((float) ((sourceentity instanceof LivingEntity _livEnt ? _livEnt.getMainHandItem() : ItemStack.EMPTY).getOrCreateTag().getDouble("adddamage")) + (float) ((sourceentity instanceof LivingEntity _livEnt ? _livEnt.getMainHandItem() : ItemStack.EMPTY).getOrCreateTag().getDouble("damage"))) * (float) ((sourceentity instanceof LivingEntity _livEnt ? _livEnt.getMainHandItem() : ItemStack.EMPTY).getOrCreateTag().getDouble("damageadd")));
}
immediatesourceentity.discard();
}
}