获取近战攻击载具的精确位置

This commit is contained in:
Atsuishio 2025-06-25 13:23:36 +08:00 committed by Light_Quanta
parent a0a3cf4ac7
commit 3f47b72d40
No known key found for this signature in database
GPG key ID: 11A39A1B8C890959
2 changed files with 33 additions and 6 deletions

View file

@ -10,6 +10,8 @@ import com.atsuishio.superbwarfare.item.gun.GunItem;
import com.atsuishio.superbwarfare.network.message.receive.SimulationDistanceMessage; import com.atsuishio.superbwarfare.network.message.receive.SimulationDistanceMessage;
import com.atsuishio.superbwarfare.tools.InventoryTool; import com.atsuishio.superbwarfare.tools.InventoryTool;
import com.atsuishio.superbwarfare.tools.NBTTool; import com.atsuishio.superbwarfare.tools.NBTTool;
import com.atsuishio.superbwarfare.tools.TraceTool;
import net.minecraft.core.BlockPos;
import net.minecraft.resources.ResourceLocation; import net.minecraft.resources.ResourceLocation;
import net.minecraft.server.level.ServerLevel; import net.minecraft.server.level.ServerLevel;
import net.minecraft.server.level.ServerPlayer; import net.minecraft.server.level.ServerPlayer;
@ -20,6 +22,7 @@ import net.minecraft.world.entity.ai.attributes.AttributeModifier;
import net.minecraft.world.entity.ai.attributes.Attributes; import net.minecraft.world.entity.ai.attributes.Attributes;
import net.minecraft.world.entity.player.Player; import net.minecraft.world.entity.player.Player;
import net.minecraft.world.item.ItemStack; import net.minecraft.world.item.ItemStack;
import net.minecraft.world.phys.Vec3;
import net.neoforged.bus.api.SubscribeEvent; import net.neoforged.bus.api.SubscribeEvent;
import net.neoforged.fml.common.EventBusSubscriber; import net.neoforged.fml.common.EventBusSubscriber;
import net.neoforged.neoforge.event.AnvilUpdateEvent; import net.neoforged.neoforge.event.AnvilUpdateEvent;
@ -196,13 +199,19 @@ public class PlayerEventHandler {
public static void onAttackEntity(AttackEntityEvent event) { public static void onAttackEntity(AttackEntityEvent event) {
var target = event.getTarget(); var target = event.getTarget();
if (target instanceof VehicleEntity vehicle) { if (target instanceof VehicleEntity vehicle) {
if (vehicle.shouldSendHitSounds()) { Vec3 position;
vehicle.level().playSound(null, vehicle.getOnPos(), ModSounds.HIT.get(), SoundSource.PLAYERS, 1, 1);
}
if (vehicle.shouldSendHitParticles() && vehicle.level() instanceof ServerLevel serverLevel) { position = TraceTool.playerFindLookingPos(event.getEntity(), vehicle, event.getEntity().entityInteractionRange());
sendParticle(serverLevel, ModParticleTypes.FIRE_STAR.get(), vehicle.getX(), vehicle.getY() + 0.5 * vehicle.getBbHeight(), vehicle.getZ(),
2, 0.4, 0.4, 0.4, 0.2, false); if (position != null) {
if (vehicle.shouldSendHitSounds()) {
vehicle.level().playSound(null, BlockPos.containing(position), ModSounds.HIT.get(), SoundSource.PLAYERS, 1, 1);
}
if (vehicle.shouldSendHitParticles() && vehicle.level() instanceof ServerLevel serverLevel) {
sendParticle(serverLevel, ModParticleTypes.FIRE_STAR.get(), position.x, position.y, position.z,
2, 0, 0, 0, 0.2, false);
}
} }
} }
} }

View file

@ -123,6 +123,24 @@ public class TraceTool {
return null; return null;
} }
public static Vec3 playerFindLookingPos(Player player, VehicleEntity vehicle, double entityReach) {
double distance = entityReach * entityReach;
HitResult hitResult = player.pick(entityReach, 1.0f, false);
Vec3 viewVec = player.getViewVector(1);
Vec3 toVec = player.getEyePosition().add(viewVec.x * entityReach, viewVec.y * entityReach, viewVec.z * entityReach);
AABB aabb = vehicle.getBoundingBox().expandTowards(viewVec.scale(entityReach)).inflate(1.0D, 1.0D, 1.0D);
EntityHitResult entityhitresult = ProjectileUtil.getEntityHitResult(player, player.getEyePosition(), toVec, aabb, p -> true, distance);
if (entityhitresult != null) {
hitResult = entityhitresult;
}
if (hitResult.getType() == HitResult.Type.ENTITY) {
return hitResult.getLocation();
}
return null;
}
public static Entity droneFindLookingEntity(Entity entity, Vec3 pos, double entityReach, float ticks) { public static Entity droneFindLookingEntity(Entity entity, Vec3 pos, double entityReach, float ticks) {
double distance = entityReach * entityReach; double distance = entityReach * entityReach;
HitResult hitResult = entity.pick(entityReach, 1.0f, false); HitResult hitResult = entity.pick(entityReach, 1.0f, false);