From f8fe42a2536fce16924f78c6e684bf0bc558aa5a Mon Sep 17 00:00:00 2001 From: 17146 <1714673995@qq.com> Date: Thu, 28 Nov 2024 20:58:14 +0800 Subject: [PATCH] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=E9=9B=86=E8=A3=85=E7=AE=B1?= =?UTF-8?q?=E5=BC=80=E5=90=AF=E6=9C=BA=E5=88=B6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../superbwarfare/block/ContainerBlock.java | 23 +++++++++-- .../block/entity/ContainerBlockEntity.java | 39 +++++++++++++++++++ 2 files changed, 59 insertions(+), 3 deletions(-) diff --git a/src/main/java/com/atsuishio/superbwarfare/block/ContainerBlock.java b/src/main/java/com/atsuishio/superbwarfare/block/ContainerBlock.java index 290af542f..43e39bb4e 100644 --- a/src/main/java/com/atsuishio/superbwarfare/block/ContainerBlock.java +++ b/src/main/java/com/atsuishio/superbwarfare/block/ContainerBlock.java @@ -1,6 +1,8 @@ package com.atsuishio.superbwarfare.block; import com.atsuishio.superbwarfare.block.entity.ContainerBlockEntity; +import com.atsuishio.superbwarfare.init.ModBlockEntities; +import com.atsuishio.superbwarfare.init.ModItems; import net.minecraft.core.BlockPos; import net.minecraft.core.Direction; import net.minecraft.nbt.CompoundTag; @@ -16,6 +18,8 @@ import net.minecraft.world.level.BlockGetter; import net.minecraft.world.level.Level; import net.minecraft.world.level.block.*; import net.minecraft.world.level.block.entity.BlockEntity; +import net.minecraft.world.level.block.entity.BlockEntityTicker; +import net.minecraft.world.level.block.entity.BlockEntityType; import net.minecraft.world.level.block.state.BlockBehaviour; import net.minecraft.world.level.block.state.BlockState; import net.minecraft.world.level.block.state.StateDefinition; @@ -42,17 +46,21 @@ public class ContainerBlock extends BaseEntityBlock { @Override public InteractionResult use(BlockState pState, Level pLevel, BlockPos pPos, Player pPlayer, InteractionHand pHand, BlockHitResult pHit) { if (!pLevel.isClientSide) { - if (canOpen(pLevel, pPos)) { + ItemStack stack = pPlayer.getItemInHand(pHand); + if (stack.is(ModItems.CROWBAR.get()) && canOpen(pLevel, pPos)) { pLevel.setBlockAndUpdate(pPos, pState.setValue(OPENED, true)); - return InteractionResult.SUCCESS; } - pPlayer.displayClientMessage(Component.literal("打不开哼哼啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊"), true); + pPlayer.displayClientMessage(Component.literal("打不开哼哼啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊"), true); } return InteractionResult.PASS; } public boolean canOpen(Level pLevel, BlockPos pPos) { + BlockEntity blockEntity = pLevel.getBlockEntity(pPos); + if (!(blockEntity instanceof ContainerBlockEntity containerBlockEntity)) return false; + if (containerBlockEntity.entity == null && containerBlockEntity.entityType == null) return false; + boolean flag = true; for (int i = -4; i < 5; i++) { @@ -71,6 +79,15 @@ public class ContainerBlock extends BaseEntityBlock { return flag; } + @Nullable + @Override + public BlockEntityTicker getTicker(Level pLevel, BlockState pState, BlockEntityType pBlockEntityType) { + if (!pLevel.isClientSide) { + return createTickerHelper(pBlockEntityType, ModBlockEntities.CONTAINER.get(), ContainerBlockEntity::serverTick); + } + return null; + } + @Override public void appendHoverText(ItemStack pStack, @Nullable BlockGetter pLevel, List pTooltip, TooltipFlag pFlag) { super.appendHoverText(pStack, pLevel, pTooltip, pFlag); diff --git a/src/main/java/com/atsuishio/superbwarfare/block/entity/ContainerBlockEntity.java b/src/main/java/com/atsuishio/superbwarfare/block/entity/ContainerBlockEntity.java index fe430accb..0efdf0fd9 100644 --- a/src/main/java/com/atsuishio/superbwarfare/block/entity/ContainerBlockEntity.java +++ b/src/main/java/com/atsuishio/superbwarfare/block/entity/ContainerBlockEntity.java @@ -2,11 +2,18 @@ package com.atsuishio.superbwarfare.block.entity; import com.atsuishio.superbwarfare.block.ContainerBlock; import com.atsuishio.superbwarfare.init.ModBlockEntities; +import com.atsuishio.superbwarfare.tools.ParticleTool; import net.minecraft.core.BlockPos; +import net.minecraft.core.particles.ParticleTypes; import net.minecraft.nbt.CompoundTag; import net.minecraft.network.protocol.game.ClientboundBlockEntityDataPacket; +import net.minecraft.server.level.ServerLevel; +import net.minecraft.sounds.SoundEvents; +import net.minecraft.sounds.SoundSource; import net.minecraft.world.entity.Entity; import net.minecraft.world.entity.EntityType; +import net.minecraft.world.level.Level; +import net.minecraft.world.level.block.Blocks; import net.minecraft.world.level.block.entity.BlockEntity; import net.minecraft.world.level.block.state.BlockState; import software.bernie.geckolib.animatable.GeoBlockEntity; @@ -22,6 +29,7 @@ public class ContainerBlockEntity extends BlockEntity implements GeoBlockEntity public EntityType entityType; public Entity entity = null; + public int tick = 0; private final AnimatableInstanceCache cache = GeckoLibUtil.createInstanceCache(this); @@ -29,6 +37,35 @@ public class ContainerBlockEntity extends BlockEntity implements GeoBlockEntity super(ModBlockEntities.CONTAINER.get(), pos, state); } + public static void serverTick(Level pLevel, BlockPos pPos, BlockState pState, ContainerBlockEntity blockEntity) { + if (!pState.getValue(ContainerBlock.OPENED)) { + return; + } + + if (blockEntity.tick < 20) { + blockEntity.tick++; + blockEntity.setChanged(); + + if (blockEntity.tick == 18) { + ParticleTool.sendParticle((ServerLevel) pLevel, ParticleTypes.EXPLOSION, pPos.getX(), pPos.getY() + 1, pPos.getZ(), 40, 1.5, 1.5, 1.5, 1, false); + pLevel.playSound(null, pPos, SoundEvents.GENERIC_EXPLODE, SoundSource.BLOCKS, 4.0F, (1.0F + (pLevel.random.nextFloat() - pLevel.random.nextFloat()) * 0.2F) * 0.7F); + } + } else { + if (blockEntity.entity != null) { + blockEntity.entity.setPos(pPos.getX() + 0.5, pPos.getY() + 0.5, pPos.getZ() + 0.5); + pLevel.addFreshEntity(blockEntity.entity); + } else if (blockEntity.entityType != null) { + var entity = blockEntity.entityType.create(pLevel); + if (entity != null) { + entity.setPos(pPos.getX() + 0.5, pPos.getY() + 0.5, pPos.getZ() + 0.5); + pLevel.addFreshEntity(entity); + } + } + + pLevel.setBlockAndUpdate(pPos, Blocks.AIR.defaultBlockState()); + } + } + private PlayState predicate(AnimationState event) { if (this.getBlockState().getValue(ContainerBlock.OPENED)) { return event.setAndContinue(RawAnimation.begin().thenPlay("animation.container.open")); @@ -55,6 +92,7 @@ public class ContainerBlockEntity extends BlockEntity implements GeoBlockEntity if (compound.contains("EntityType")) { this.entityType = EntityType.byString(compound.getString("EntityType")).orElse(null); } + this.tick = compound.getInt("Tick"); } @Override @@ -66,6 +104,7 @@ public class ContainerBlockEntity extends BlockEntity implements GeoBlockEntity if (this.entityType != null) { compound.putString("EntityType", EntityType.getKey(this.entityType).toString()); } + compound.putInt("Tick", this.tick); } @Override