use of com.bluepowermod.api.multipart.IBPPartBlock in project BluePower by Qmunity.
the class ItemBPPart method place.
@Override
public ActionResultType place(BlockItemUseContext context) {
BlockState state = context.getLevel().getBlockState(context.getClickedPos());
BlockState thisState = getBlock().getStateForPlacement(context);
if (state.getBlock() instanceof IBPPartBlock && thisState != null && !AABBUtils.testOcclusion(((IBPPartBlock) thisState.getBlock()).getOcclusionShape(thisState), state.getShape(context.getLevel(), context.getClickedPos()))) {
// Save the Tile Entity Data
CompoundNBT nbt = new CompoundNBT();
TileEntity tileEntity = context.getLevel().getBlockEntity(context.getClickedPos());
if (tileEntity != null) {
nbt = tileEntity.save(nbt);
}
// Replace with Multipart
context.getLevel().setBlockAndUpdate(context.getClickedPos(), BPBlocks.multipart.defaultBlockState());
tileEntity = context.getLevel().getBlockEntity(context.getClickedPos());
if (tileEntity instanceof TileBPMultipart) {
// Add the original State to the Multipart
((TileBPMultipart) tileEntity).addState(state);
// Restore the Tile Entity Data
TileEntity tile = ((TileBPMultipart) tileEntity).getTileForState(state);
if (tile != null)
tile.load(state, nbt);
// Add the new State
((TileBPMultipart) tileEntity).addState(thisState);
thisState.getBlock().setPlacedBy(context.getLevel(), context.getClickedPos(), thisState, context.getPlayer(), context.getItemInHand());
}
// Update Self
state.neighborChanged(context.getLevel(), context.getClickedPos(), state.getBlock(), context.getClickedPos(), false);
context.getItemInHand().shrink(1);
// Place Sound
context.getLevel().playSound(null, context.getClickedPos(), SoundEvents.STONE_PLACE, SoundCategory.BLOCKS, 1.0F, 1.0F);
return ActionResultType.SUCCESS;
} else if (state.getBlock() instanceof BlockBPMultipart && thisState != null && !AABBUtils.testOcclusion(((IBPPartBlock) thisState.getBlock()).getOcclusionShape(thisState), state.getShape(context.getLevel(), context.getClickedPos()))) {
// Add to the Existing Multipart
TileEntity tileEntity = context.getLevel().getBlockEntity(context.getClickedPos());
if (tileEntity instanceof TileBPMultipart) {
((TileBPMultipart) tileEntity).addState(thisState);
thisState.getBlock().setPlacedBy(context.getLevel(), context.getClickedPos(), thisState, context.getPlayer(), context.getItemInHand());
// Update Neighbors
for (Direction dir : Direction.values()) {
context.getLevel().getBlockState(context.getClickedPos().relative(dir)).neighborChanged(context.getLevel(), context.getClickedPos().relative(dir), state.getBlock(), context.getClickedPos(), false);
}
// Update Self
state.neighborChanged(context.getLevel(), context.getClickedPos(), state.getBlock(), context.getClickedPos(), false);
context.getItemInHand().shrink(1);
// Place Sound
context.getLevel().playSound(null, context.getClickedPos(), SoundEvents.STONE_PLACE, SoundCategory.BLOCKS, 1.0F, 1.0F);
return ActionResultType.SUCCESS;
}
}
return super.place(context);
}
use of com.bluepowermod.api.multipart.IBPPartBlock in project BluePower by Qmunity.
the class BlockBPMultipart method removedByPlayer.
@Override
public boolean removedByPlayer(BlockState state, World world, BlockPos pos, PlayerEntity player, boolean willHarvest, FluidState fluid) {
BlockState partState = MultipartUtils.getClosestState(player, pos);
TileEntity te = world.getBlockEntity(pos);
if (partState != null && partState.getBlock() instanceof IBPPartBlock && te instanceof TileBPMultipart) {
// Remove Selected Part
((TileBPMultipart) te).removeState(partState);
// Call onMultipartReplaced
((IBPPartBlock) partState.getBlock()).onMultipartReplaced(partState, world, pos, state, false);
// Play Break Sound
world.playSound(null, pos, SoundEvents.STONE_BREAK, SoundCategory.BLOCKS, 1.0F, 1.0F);
return false;
}
return false;
}
Aggregations