From c3c46f26d4bdb9f26d881ce2af671674ce17ed98 Mon Sep 17 00:00:00 2001 From: 17146 <1714673995@qq.com> Date: Sun, 23 Feb 2025 21:47:40 +0800 Subject: [PATCH] =?UTF-8?q?=E8=B0=83=E6=95=B4=E8=BF=AB=E5=87=BB=E7=82=AE?= =?UTF-8?q?=E5=88=9D=E5=A7=8B=E6=9C=9D=E5=90=91?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../superbwarfare/entity/MortarEntity.java | 20 ++++-- .../superbwarfare/item/MortarDeployer.java | 71 ++++++++++++------- 2 files changed, 60 insertions(+), 31 deletions(-) diff --git a/src/main/java/com/atsuishio/superbwarfare/entity/MortarEntity.java b/src/main/java/com/atsuishio/superbwarfare/entity/MortarEntity.java index ff108d8c5..a9300fcd5 100644 --- a/src/main/java/com/atsuishio/superbwarfare/entity/MortarEntity.java +++ b/src/main/java/com/atsuishio/superbwarfare/entity/MortarEntity.java @@ -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 type, Level world) { - super(type, world); + public MortarEntity(EntityType 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")); } } diff --git a/src/main/java/com/atsuishio/superbwarfare/item/MortarDeployer.java b/src/main/java/com/atsuishio/superbwarfare/item/MortarDeployer.java index 54be35dd6..702bd04db 100644 --- a/src/main/java/com/atsuishio/superbwarfare/item/MortarDeployer.java +++ b/src/main/java/com/atsuishio/superbwarfare/item/MortarDeployer.java @@ -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 iterable = pLevel.getCollisions(null, aabb); + return 1.0D + Shapes.collide(Direction.Axis.Y, pBox, iterable, pShouldOffsetYMore ? -2.0D : -1.0D); + } + @Override public InteractionResultHolder 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); }