调整迫击炮初始朝向
This commit is contained in:
parent
c0f931f4bc
commit
c3c46f26d4
2 changed files with 60 additions and 31 deletions
|
@ -55,12 +55,18 @@ public class MortarEntity extends VehicleEntity implements GeoEntity, AnimatedEn
|
||||||
|
|
||||||
private final AnimatableInstanceCache cache = GeckoLibUtil.createInstanceCache(this);
|
private final AnimatableInstanceCache cache = GeckoLibUtil.createInstanceCache(this);
|
||||||
|
|
||||||
public MortarEntity(PlayMessages.SpawnEntity packet, Level world) {
|
public MortarEntity(PlayMessages.SpawnEntity packet, Level level) {
|
||||||
this(ModEntities.MORTAR.get(), world);
|
this(ModEntities.MORTAR.get(), level);
|
||||||
}
|
}
|
||||||
|
|
||||||
public MortarEntity(EntityType<MortarEntity> type, Level world) {
|
public MortarEntity(EntityType<MortarEntity> type, Level level) {
|
||||||
super(type, world);
|
super(type, level);
|
||||||
|
}
|
||||||
|
|
||||||
|
public MortarEntity(Level level, float yRot) {
|
||||||
|
super(ModEntities.MORTAR.get(), level);
|
||||||
|
this.setYRot(yRot);
|
||||||
|
this.entityData.set(YAW, yRot);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
@ -68,7 +74,7 @@ public class MortarEntity extends VehicleEntity implements GeoEntity, AnimatedEn
|
||||||
super.defineSynchedData();
|
super.defineSynchedData();
|
||||||
this.entityData.define(FIRE_TIME, 0);
|
this.entityData.define(FIRE_TIME, 0);
|
||||||
this.entityData.define(PITCH, -70f);
|
this.entityData.define(PITCH, -70f);
|
||||||
this.entityData.define(YAW, 0f);
|
this.entityData.define(YAW, this.getYRot());
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
@ -95,7 +101,7 @@ public class MortarEntity extends VehicleEntity implements GeoEntity, AnimatedEn
|
||||||
public boolean hurt(DamageSource source, float amount) {
|
public boolean hurt(DamageSource source, float amount) {
|
||||||
this.level().playSound(null, this.getOnPos(), ModSounds.HIT.get(), SoundSource.PLAYERS, 1, 1);
|
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);
|
super.hurt(source, amount);
|
||||||
this.hurt(amount, source.getEntity(), true);
|
this.hurt(amount, source.getEntity(), true);
|
||||||
|
|
||||||
|
@ -115,7 +121,7 @@ public class MortarEntity extends VehicleEntity implements GeoEntity, AnimatedEn
|
||||||
if (compound.contains("Pitch")) {
|
if (compound.contains("Pitch")) {
|
||||||
this.entityData.set(PITCH, compound.getFloat("Pitch"));
|
this.entityData.set(PITCH, compound.getFloat("Pitch"));
|
||||||
}
|
}
|
||||||
if (compound.contains("YRot")) {
|
if (compound.contains("Yaw")) {
|
||||||
this.entityData.set(YAW, compound.getFloat("Yaw"));
|
this.entityData.set(YAW, compound.getFloat("Yaw"));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,6 +1,6 @@
|
||||||
package com.atsuishio.superbwarfare.item;
|
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.BlockPos;
|
||||||
import net.minecraft.core.Direction;
|
import net.minecraft.core.Direction;
|
||||||
import net.minecraft.server.level.ServerLevel;
|
import net.minecraft.server.level.ServerLevel;
|
||||||
|
@ -8,8 +8,6 @@ import net.minecraft.stats.Stats;
|
||||||
import net.minecraft.world.InteractionHand;
|
import net.minecraft.world.InteractionHand;
|
||||||
import net.minecraft.world.InteractionResult;
|
import net.minecraft.world.InteractionResult;
|
||||||
import net.minecraft.world.InteractionResultHolder;
|
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.entity.player.Player;
|
||||||
import net.minecraft.world.item.Item;
|
import net.minecraft.world.item.Item;
|
||||||
import net.minecraft.world.item.ItemStack;
|
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.item.context.UseOnContext;
|
||||||
import net.minecraft.world.level.ClipContext;
|
import net.minecraft.world.level.ClipContext;
|
||||||
import net.minecraft.world.level.Level;
|
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.LiquidBlock;
|
||||||
import net.minecraft.world.level.block.state.BlockState;
|
import net.minecraft.world.level.block.state.BlockState;
|
||||||
import net.minecraft.world.level.gameevent.GameEvent;
|
import net.minecraft.world.level.gameevent.GameEvent;
|
||||||
|
import net.minecraft.world.phys.AABB;
|
||||||
import net.minecraft.world.phys.BlockHitResult;
|
import net.minecraft.world.phys.BlockHitResult;
|
||||||
import net.minecraft.world.phys.HitResult;
|
import net.minecraft.world.phys.HitResult;
|
||||||
|
import net.minecraft.world.phys.shapes.Shapes;
|
||||||
|
import net.minecraft.world.phys.shapes.VoxelShape;
|
||||||
|
|
||||||
import java.util.Objects;
|
import java.util.Objects;
|
||||||
|
|
||||||
public class MortarDeployer extends Item {
|
public class MortarDeployer extends Item {
|
||||||
|
|
||||||
public MortarDeployer() {
|
public MortarDeployer() {
|
||||||
super(new Item.Properties().rarity(Rarity.RARE));
|
super(new Item.Properties().rarity(Rarity.RARE));
|
||||||
}
|
}
|
||||||
|
@ -36,26 +39,47 @@ public class MortarDeployer extends Item {
|
||||||
if (!(level instanceof ServerLevel)) {
|
if (!(level instanceof ServerLevel)) {
|
||||||
return InteractionResult.SUCCESS;
|
return InteractionResult.SUCCESS;
|
||||||
} else {
|
} else {
|
||||||
ItemStack itemstack = pContext.getItemInHand();
|
ItemStack stack = pContext.getItemInHand();
|
||||||
BlockPos blockpos = pContext.getClickedPos();
|
BlockPos clickedPos = pContext.getClickedPos();
|
||||||
Direction direction = pContext.getClickedFace();
|
Direction direction = pContext.getClickedFace();
|
||||||
BlockState blockstate = level.getBlockState(blockpos);
|
Player player = pContext.getPlayer();
|
||||||
BlockPos blockpos1;
|
if (player == null) {
|
||||||
if (blockstate.getCollisionShape(level, blockpos).isEmpty()) {
|
return InteractionResult.PASS;
|
||||||
blockpos1 = blockpos;
|
|
||||||
} else {
|
|
||||||
blockpos1 = blockpos.relative(direction);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if (ModEntities.MORTAR.get().spawn((ServerLevel)level, itemstack, pContext.getPlayer(), blockpos1, MobSpawnType.SPAWN_EGG, true, !Objects.equals(blockpos, blockpos1) && direction == Direction.UP) != null) {
|
BlockState blockstate = level.getBlockState(clickedPos);
|
||||||
itemstack.shrink(1);
|
BlockPos pos;
|
||||||
level.gameEvent(pContext.getPlayer(), GameEvent.ENTITY_PLACE, blockpos);
|
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;
|
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
|
@Override
|
||||||
public InteractionResultHolder<ItemStack> use(Level pLevel, Player pPlayer, InteractionHand pHand) {
|
public InteractionResultHolder<ItemStack> use(Level pLevel, Player pPlayer, InteractionHand pHand) {
|
||||||
ItemStack itemstack = pPlayer.getItemInHand(pHand);
|
ItemStack itemstack = pPlayer.getItemInHand(pHand);
|
||||||
|
@ -69,18 +93,17 @@ public class MortarDeployer extends Item {
|
||||||
if (!(pLevel.getBlockState(blockpos).getBlock() instanceof LiquidBlock)) {
|
if (!(pLevel.getBlockState(blockpos).getBlock() instanceof LiquidBlock)) {
|
||||||
return InteractionResultHolder.pass(itemstack);
|
return InteractionResultHolder.pass(itemstack);
|
||||||
} else if (pLevel.mayInteract(pPlayer, blockpos) && pPlayer.mayUseItemAt(blockpos, blockhitresult.getDirection(), 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);
|
MortarEntity mortarEntity = new MortarEntity(pLevel, pPlayer.getYRot());
|
||||||
if (entity == null) {
|
mortarEntity.setPos((double) blockpos.getX() + 0.5D, blockpos.getY(), (double) blockpos.getZ() + 0.5D);
|
||||||
return InteractionResultHolder.pass(itemstack);
|
pLevel.addFreshEntity(mortarEntity);
|
||||||
} else {
|
|
||||||
if (!pPlayer.getAbilities().instabuild) {
|
|
||||||
itemstack.shrink(1);
|
|
||||||
}
|
|
||||||
|
|
||||||
pPlayer.awardStat(Stats.ITEM_USED.get(this));
|
if (!pPlayer.getAbilities().instabuild) {
|
||||||
pLevel.gameEvent(pPlayer, GameEvent.ENTITY_PLACE, entity.position());
|
itemstack.shrink(1);
|
||||||
return InteractionResultHolder.consume(itemstack);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pPlayer.awardStat(Stats.ITEM_USED.get(this));
|
||||||
|
pLevel.gameEvent(pPlayer, GameEvent.ENTITY_PLACE, mortarEntity.position());
|
||||||
|
return InteractionResultHolder.consume(itemstack);
|
||||||
} else {
|
} else {
|
||||||
return InteractionResultHolder.fail(itemstack);
|
return InteractionResultHolder.fail(itemstack);
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Reference in a new issue