调整迫击炮初始朝向

This commit is contained in:
17146 2025-02-23 21:47:40 +08:00
parent c0f931f4bc
commit c3c46f26d4
2 changed files with 60 additions and 31 deletions

View file

@ -55,12 +55,18 @@ public class MortarEntity extends VehicleEntity implements GeoEntity, AnimatedEn
private final AnimatableInstanceCache cache = GeckoLibUtil.createInstanceCache(this);
public MortarEntity(PlayMessages.SpawnEntity packet, Level world) {
this(ModEntities.MORTAR.get(), world);
public MortarEntity(PlayMessages.SpawnEntity packet, Level level) {
this(ModEntities.MORTAR.get(), level);
}
public MortarEntity(EntityType<MortarEntity> type, Level world) {
super(type, world);
public MortarEntity(EntityType<MortarEntity> type, Level level) {
super(type, level);
}
public MortarEntity(Level level, float yRot) {
super(ModEntities.MORTAR.get(), level);
this.setYRot(yRot);
this.entityData.set(YAW, yRot);
}
@Override
@ -68,7 +74,7 @@ public class MortarEntity extends VehicleEntity implements GeoEntity, AnimatedEn
super.defineSynchedData();
this.entityData.define(FIRE_TIME, 0);
this.entityData.define(PITCH, -70f);
this.entityData.define(YAW, 0f);
this.entityData.define(YAW, this.getYRot());
}
@Override
@ -95,7 +101,7 @@ public class MortarEntity extends VehicleEntity implements GeoEntity, AnimatedEn
public boolean hurt(DamageSource source, float amount) {
this.level().playSound(null, this.getOnPos(), ModSounds.HIT.get(), SoundSource.PLAYERS, 1, 1);
amount = damageModifier.compute(source, amount);
amount = this.damageModifier.compute(source, amount);
super.hurt(source, amount);
this.hurt(amount, source.getEntity(), true);
@ -115,7 +121,7 @@ public class MortarEntity extends VehicleEntity implements GeoEntity, AnimatedEn
if (compound.contains("Pitch")) {
this.entityData.set(PITCH, compound.getFloat("Pitch"));
}
if (compound.contains("YRot")) {
if (compound.contains("Yaw")) {
this.entityData.set(YAW, compound.getFloat("Yaw"));
}
}

View file

@ -1,6 +1,6 @@
package com.atsuishio.superbwarfare.item;
import com.atsuishio.superbwarfare.init.ModEntities;
import com.atsuishio.superbwarfare.entity.MortarEntity;
import net.minecraft.core.BlockPos;
import net.minecraft.core.Direction;
import net.minecraft.server.level.ServerLevel;
@ -8,8 +8,6 @@ import net.minecraft.stats.Stats;
import net.minecraft.world.InteractionHand;
import net.minecraft.world.InteractionResult;
import net.minecraft.world.InteractionResultHolder;
import net.minecraft.world.entity.Entity;
import net.minecraft.world.entity.MobSpawnType;
import net.minecraft.world.entity.player.Player;
import net.minecraft.world.item.Item;
import net.minecraft.world.item.ItemStack;
@ -17,15 +15,20 @@ import net.minecraft.world.item.Rarity;
import net.minecraft.world.item.context.UseOnContext;
import net.minecraft.world.level.ClipContext;
import net.minecraft.world.level.Level;
import net.minecraft.world.level.LevelReader;
import net.minecraft.world.level.block.LiquidBlock;
import net.minecraft.world.level.block.state.BlockState;
import net.minecraft.world.level.gameevent.GameEvent;
import net.minecraft.world.phys.AABB;
import net.minecraft.world.phys.BlockHitResult;
import net.minecraft.world.phys.HitResult;
import net.minecraft.world.phys.shapes.Shapes;
import net.minecraft.world.phys.shapes.VoxelShape;
import java.util.Objects;
public class MortarDeployer extends Item {
public MortarDeployer() {
super(new Item.Properties().rarity(Rarity.RARE));
}
@ -36,26 +39,47 @@ public class MortarDeployer extends Item {
if (!(level instanceof ServerLevel)) {
return InteractionResult.SUCCESS;
} else {
ItemStack itemstack = pContext.getItemInHand();
BlockPos blockpos = pContext.getClickedPos();
ItemStack stack = pContext.getItemInHand();
BlockPos clickedPos = pContext.getClickedPos();
Direction direction = pContext.getClickedFace();
BlockState blockstate = level.getBlockState(blockpos);
BlockPos blockpos1;
if (blockstate.getCollisionShape(level, blockpos).isEmpty()) {
blockpos1 = blockpos;
} else {
blockpos1 = blockpos.relative(direction);
Player player = pContext.getPlayer();
if (player == null) {
return InteractionResult.PASS;
}
if (ModEntities.MORTAR.get().spawn((ServerLevel)level, itemstack, pContext.getPlayer(), blockpos1, MobSpawnType.SPAWN_EGG, true, !Objects.equals(blockpos, blockpos1) && direction == Direction.UP) != null) {
itemstack.shrink(1);
level.gameEvent(pContext.getPlayer(), GameEvent.ENTITY_PLACE, blockpos);
BlockState blockstate = level.getBlockState(clickedPos);
BlockPos pos;
if (blockstate.getCollisionShape(level, clickedPos).isEmpty()) {
pos = clickedPos;
} else {
pos = clickedPos.relative(direction);
}
MortarEntity mortarEntity = new MortarEntity(level, player.getYRot());
mortarEntity.setPos((double) pos.getX() + 0.5D, pos.getY() + 1, (double) pos.getZ() + 0.5D);
double yOffset = this.getYOffset(level, pos, !Objects.equals(clickedPos, pos) && direction == Direction.UP, mortarEntity.getBoundingBox());
mortarEntity.moveTo((double) pos.getX() + 0.5D, pos.getY() + yOffset, (double) pos.getZ() + 0.5D);
level.addFreshEntity(mortarEntity);
if (!player.getAbilities().instabuild) {
stack.shrink(1);
}
level.gameEvent(pContext.getPlayer(), GameEvent.ENTITY_PLACE, clickedPos);
return InteractionResult.CONSUME;
}
}
public double getYOffset(LevelReader pLevel, BlockPos pPos, boolean pShouldOffsetYMore, AABB pBox) {
AABB aabb = new AABB(pPos);
if (pShouldOffsetYMore) {
aabb = aabb.expandTowards(0.0D, -1.0D, 0.0D);
}
Iterable<VoxelShape> iterable = pLevel.getCollisions(null, aabb);
return 1.0D + Shapes.collide(Direction.Axis.Y, pBox, iterable, pShouldOffsetYMore ? -2.0D : -1.0D);
}
@Override
public InteractionResultHolder<ItemStack> use(Level pLevel, Player pPlayer, InteractionHand pHand) {
ItemStack itemstack = pPlayer.getItemInHand(pHand);
@ -69,18 +93,17 @@ public class MortarDeployer extends Item {
if (!(pLevel.getBlockState(blockpos).getBlock() instanceof LiquidBlock)) {
return InteractionResultHolder.pass(itemstack);
} else if (pLevel.mayInteract(pPlayer, blockpos) && pPlayer.mayUseItemAt(blockpos, blockhitresult.getDirection(), itemstack)) {
Entity entity = ModEntities.MORTAR.get().spawn((ServerLevel)pLevel, itemstack, pPlayer, blockpos, MobSpawnType.SPAWN_EGG, false, false);
if (entity == null) {
return InteractionResultHolder.pass(itemstack);
} else {
if (!pPlayer.getAbilities().instabuild) {
itemstack.shrink(1);
}
MortarEntity mortarEntity = new MortarEntity(pLevel, pPlayer.getYRot());
mortarEntity.setPos((double) blockpos.getX() + 0.5D, blockpos.getY(), (double) blockpos.getZ() + 0.5D);
pLevel.addFreshEntity(mortarEntity);
pPlayer.awardStat(Stats.ITEM_USED.get(this));
pLevel.gameEvent(pPlayer, GameEvent.ENTITY_PLACE, entity.position());
return InteractionResultHolder.consume(itemstack);
if (!pPlayer.getAbilities().instabuild) {
itemstack.shrink(1);
}
pPlayer.awardStat(Stats.ITEM_USED.get(this));
pLevel.gameEvent(pPlayer, GameEvent.ENTITY_PLACE, mortarEntity.position());
return InteractionResultHolder.consume(itemstack);
} else {
return InteractionResultHolder.fail(itemstack);
}