添加集装箱开启机制

This commit is contained in:
17146 2024-11-28 20:58:14 +08:00
parent 8539d74e42
commit f8fe42a253
2 changed files with 59 additions and 3 deletions

View file

@ -1,6 +1,8 @@
package com.atsuishio.superbwarfare.block; package com.atsuishio.superbwarfare.block;
import com.atsuishio.superbwarfare.block.entity.ContainerBlockEntity; 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.BlockPos;
import net.minecraft.core.Direction; import net.minecraft.core.Direction;
import net.minecraft.nbt.CompoundTag; 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.Level;
import net.minecraft.world.level.block.*; import net.minecraft.world.level.block.*;
import net.minecraft.world.level.block.entity.BlockEntity; 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.BlockBehaviour;
import net.minecraft.world.level.block.state.BlockState; import net.minecraft.world.level.block.state.BlockState;
import net.minecraft.world.level.block.state.StateDefinition; import net.minecraft.world.level.block.state.StateDefinition;
@ -42,17 +46,21 @@ public class ContainerBlock extends BaseEntityBlock {
@Override @Override
public InteractionResult use(BlockState pState, Level pLevel, BlockPos pPos, Player pPlayer, InteractionHand pHand, BlockHitResult pHit) { public InteractionResult use(BlockState pState, Level pLevel, BlockPos pPos, Player pPlayer, InteractionHand pHand, BlockHitResult pHit) {
if (!pLevel.isClientSide) { 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)); pLevel.setBlockAndUpdate(pPos, pState.setValue(OPENED, true));
return InteractionResult.SUCCESS; return InteractionResult.SUCCESS;
} }
pPlayer.displayClientMessage(Component.literal("打不开哼哼啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊"), true); pPlayer.displayClientMessage(Component.literal("打不开哼哼啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊"), true);
} }
return InteractionResult.PASS; return InteractionResult.PASS;
} }
public boolean canOpen(Level pLevel, BlockPos pPos) { 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; boolean flag = true;
for (int i = -4; i < 5; i++) { for (int i = -4; i < 5; i++) {
@ -71,6 +79,15 @@ public class ContainerBlock extends BaseEntityBlock {
return flag; return flag;
} }
@Nullable
@Override
public <T extends BlockEntity> BlockEntityTicker<T> getTicker(Level pLevel, BlockState pState, BlockEntityType<T> pBlockEntityType) {
if (!pLevel.isClientSide) {
return createTickerHelper(pBlockEntityType, ModBlockEntities.CONTAINER.get(), ContainerBlockEntity::serverTick);
}
return null;
}
@Override @Override
public void appendHoverText(ItemStack pStack, @Nullable BlockGetter pLevel, List<Component> pTooltip, TooltipFlag pFlag) { public void appendHoverText(ItemStack pStack, @Nullable BlockGetter pLevel, List<Component> pTooltip, TooltipFlag pFlag) {
super.appendHoverText(pStack, pLevel, pTooltip, pFlag); super.appendHoverText(pStack, pLevel, pTooltip, pFlag);

View file

@ -2,11 +2,18 @@ package com.atsuishio.superbwarfare.block.entity;
import com.atsuishio.superbwarfare.block.ContainerBlock; import com.atsuishio.superbwarfare.block.ContainerBlock;
import com.atsuishio.superbwarfare.init.ModBlockEntities; import com.atsuishio.superbwarfare.init.ModBlockEntities;
import com.atsuishio.superbwarfare.tools.ParticleTool;
import net.minecraft.core.BlockPos; import net.minecraft.core.BlockPos;
import net.minecraft.core.particles.ParticleTypes;
import net.minecraft.nbt.CompoundTag; import net.minecraft.nbt.CompoundTag;
import net.minecraft.network.protocol.game.ClientboundBlockEntityDataPacket; 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.Entity;
import net.minecraft.world.entity.EntityType; 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.entity.BlockEntity;
import net.minecraft.world.level.block.state.BlockState; import net.minecraft.world.level.block.state.BlockState;
import software.bernie.geckolib.animatable.GeoBlockEntity; import software.bernie.geckolib.animatable.GeoBlockEntity;
@ -22,6 +29,7 @@ public class ContainerBlockEntity extends BlockEntity implements GeoBlockEntity
public EntityType<?> entityType; public EntityType<?> entityType;
public Entity entity = null; public Entity entity = null;
public int tick = 0;
private final AnimatableInstanceCache cache = GeckoLibUtil.createInstanceCache(this); private final AnimatableInstanceCache cache = GeckoLibUtil.createInstanceCache(this);
@ -29,6 +37,35 @@ public class ContainerBlockEntity extends BlockEntity implements GeoBlockEntity
super(ModBlockEntities.CONTAINER.get(), pos, state); 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<ContainerBlockEntity> event) { private PlayState predicate(AnimationState<ContainerBlockEntity> event) {
if (this.getBlockState().getValue(ContainerBlock.OPENED)) { if (this.getBlockState().getValue(ContainerBlock.OPENED)) {
return event.setAndContinue(RawAnimation.begin().thenPlay("animation.container.open")); return event.setAndContinue(RawAnimation.begin().thenPlay("animation.container.open"));
@ -55,6 +92,7 @@ public class ContainerBlockEntity extends BlockEntity implements GeoBlockEntity
if (compound.contains("EntityType")) { if (compound.contains("EntityType")) {
this.entityType = EntityType.byString(compound.getString("EntityType")).orElse(null); this.entityType = EntityType.byString(compound.getString("EntityType")).orElse(null);
} }
this.tick = compound.getInt("Tick");
} }
@Override @Override
@ -66,6 +104,7 @@ public class ContainerBlockEntity extends BlockEntity implements GeoBlockEntity
if (this.entityType != null) { if (this.entityType != null) {
compound.putString("EntityType", EntityType.getKey(this.entityType).toString()); compound.putString("EntityType", EntityType.getKey(this.entityType).toString());
} }
compound.putInt("Tick", this.tick);
} }
@Override @Override