minecraft-source/src/net/minecraft/world/level/block/StructureBlock.java

114 lines
4.1 KiB
Java

package net.minecraft.world.level.block;
import net.minecraft.world.level.block.state.AbstractStateHolder;
import net.minecraft.world.level.block.state.properties.BlockStateProperties;
import net.minecraft.world.level.block.state.StateDefinition;
import net.minecraft.world.level.block.state.properties.Property;
import net.minecraft.world.item.BlockPlaceContext;
import net.minecraft.world.item.ItemStack;
import javax.annotation.Nullable;
import net.minecraft.world.entity.LivingEntity;
import net.minecraft.world.InteractionResult;
import net.minecraft.world.phys.BlockHitResult;
import net.minecraft.world.InteractionHand;
import net.minecraft.world.entity.player.Player;
import net.minecraft.core.BlockPos;
import net.minecraft.world.level.Level;
import net.minecraft.world.level.block.state.BlockState;
import net.minecraft.world.level.block.entity.StructureBlockEntity;
import net.minecraft.world.level.block.entity.BlockEntity;
import net.minecraft.world.level.BlockGetter;
import net.minecraft.world.level.block.state.properties.StructureMode;
import net.minecraft.world.level.block.state.properties.EnumProperty;
public class StructureBlock extends BaseEntityBlock {
public static final EnumProperty<StructureMode> MODE;
protected StructureBlock(final Properties c) {
super(c);
}
@Override
public BlockEntity newBlockEntity(final BlockGetter bjd) {
return new StructureBlockEntity();
}
@Override
public InteractionResult use(final BlockState byg, final Level bjt, final BlockPos fk, final Player ayg, final InteractionHand ajh, final BlockHitResult cvd) {
final BlockEntity bwi8 = bjt.getBlockEntity(fk);
if (bwi8 instanceof StructureBlockEntity) {
return ((StructureBlockEntity)bwi8).usedBy(ayg) ? InteractionResult.SUCCESS : InteractionResult.PASS;
}
return InteractionResult.PASS;
}
@Override
public void setPlacedBy(final Level bjt, final BlockPos fk, final BlockState byg, @Nullable final LivingEntity akw, final ItemStack bek) {
if (bjt.isClientSide) {
return;
}
if (akw != null) {
final BlockEntity bwi7 = bjt.getBlockEntity(fk);
if (bwi7 instanceof StructureBlockEntity) {
((StructureBlockEntity)bwi7).createdBy(akw);
}
}
}
@Override
public RenderShape getRenderShape(final BlockState byg) {
return RenderShape.MODEL;
}
@Override
public BlockState getStateForPlacement(final BlockPlaceContext bcn) {
return ((AbstractStateHolder<O, BlockState>)this.defaultBlockState()).<StructureMode, StructureMode>setValue(StructureBlock.MODE, StructureMode.DATA);
}
@Override
protected void createBlockStateDefinition(final StateDefinition.Builder<Block, BlockState> a) {
a.add(StructureBlock.MODE);
}
@Override
public void neighborChanged(final BlockState byg, final Level bjt, final BlockPos fk3, final Block bpe, final BlockPos fk5, final boolean boolean6) {
if (bjt.isClientSide) {
return;
}
final BlockEntity bwi8 = bjt.getBlockEntity(fk3);
if (!(bwi8 instanceof StructureBlockEntity)) {
return;
}
final StructureBlockEntity bxi9 = (StructureBlockEntity)bwi8;
final boolean boolean7 = bjt.hasNeighborSignal(fk3);
final boolean boolean8 = bxi9.isPowered();
if (boolean7 && !boolean8) {
bxi9.setPowered(true);
this.trigger(bxi9);
}
else if (!boolean7 && boolean8) {
bxi9.setPowered(false);
}
}
private void trigger(final StructureBlockEntity bxi) {
switch (bxi.getMode()) {
case SAVE: {
bxi.saveStructure(false);
break;
}
case LOAD: {
bxi.loadStructure(false);
break;
}
case CORNER: {
bxi.unloadStructure();
}
}
}
static {
MODE = BlockStateProperties.STRUCTUREBLOCK_MODE;
}
}